Skip to content

Commit dbdb0f6

Browse files
authored
Merge pull request #304 from senid231/303-optional-add-default-to-changes
#303 optional add default to changes
2 parents 5bc5f2c + f76c5c8 commit dbdb0f6

File tree

2 files changed

+119
-4
lines changed

2 files changed

+119
-4
lines changed

lib/json_api_client/resource.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Resource
3535
:request_params_class,
3636
:keep_request_params,
3737
instance_accessor: false
38+
class_attribute :add_defaults_to_changes,
39+
instance_writer: false
3840
self.primary_key = :id
3941
self.parser = Parsers::Parser
4042
self.paginator = Paginating::Paginator
@@ -48,6 +50,7 @@ class Resource
4850
self.associations = []
4951
self.request_params_class = RequestParams
5052
self.keep_request_params = false
53+
self.add_defaults_to_changes = false
5154

5255
#:underscored_key, :camelized_key, :dasherized_key, or custom
5356
self.json_key_format = :underscored_key
@@ -315,9 +318,7 @@ def initialize(params = {})
315318
self.relationships = self.class.relationship_linker.new(self.class, params.delete(:relationships) || {})
316319
self.attributes = self.class.default_attributes.merge(params)
317320

318-
self.class.schema.each_property do |property|
319-
attributes[property.name] = property.default unless attributes.has_key?(property.name) || property.default.nil?
320-
end
321+
setup_default_properties
321322

322323
self.class.associations.each do |association|
323324
if params.has_key?(association.attr_name.to_s)
@@ -481,6 +482,15 @@ def reset_request_select!(*resource_types)
481482

482483
protected
483484

485+
def setup_default_properties
486+
self.class.schema.each_property do |property|
487+
unless attributes.has_key?(property.name) || property.default.nil?
488+
attribute_will_change!(property.name) if add_defaults_to_changes
489+
attributes[property.name] = property.default
490+
end
491+
end
492+
end
493+
484494
def method_missing(method, *args)
485495
association = association_for(method)
486496

test/unit/association_test.rb

+106-1
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,124 @@ class MultiWordParent < Formatted
4646

4747
class MultiWordChild < Formatted
4848
belongs_to :multi_word_parent
49+
self.read_only_attributes = read_only_attributes + [:multi_word_parent_id]
50+
51+
def self.key_formatter
52+
JsonApiClient::DasherizedKeyFormatter
53+
end
54+
55+
def self.route_formatter
56+
JsonApiClient::UnderscoredKeyFormatter
57+
end
58+
end
59+
60+
class Account < TestResource
61+
property :name
62+
property :is_active, default: true
63+
property :balance
64+
end
65+
66+
class UserAccount < TestResource
67+
self.add_defaults_to_changes = true
68+
property :name
69+
property :is_active, default: true
70+
property :balance
4971
end
5072

5173
class AssociationTest < MiniTest::Test
5274

75+
def test_default_properties_no_changes
76+
stub_request(:post, 'http://example.com/accounts').
77+
with(headers: { content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json' }, body: {
78+
data: {
79+
type: 'accounts',
80+
attributes: {
81+
name: 'foo'
82+
}
83+
}
84+
}.to_json)
85+
.to_return(headers: { content_type: 'application/vnd.api+json' }, body: {
86+
data: {
87+
id: '1',
88+
type: 'accounts',
89+
attributes: {
90+
name: 'foo',
91+
is_active: false,
92+
balance: '0.0'
93+
}
94+
}
95+
}.to_json)
96+
record = Account.new(name: 'foo')
97+
assert record.save
98+
assert_equal(false, record.is_active)
99+
assert_equal('0.0', record.balance)
100+
end
101+
102+
def test_default_properties_changes
103+
stub_request(:post, 'http://example.com/user_accounts').
104+
with(headers: { content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json' }, body: {
105+
data: {
106+
type: 'user_accounts',
107+
attributes: {
108+
name: 'foo',
109+
is_active: true
110+
}
111+
}
112+
}.to_json)
113+
.to_return(headers: { content_type: 'application/vnd.api+json' }, body: {
114+
data: {
115+
id: '1',
116+
type: 'user_accounts',
117+
attributes: {
118+
name: 'foo',
119+
is_active: true,
120+
balance: '0.0'
121+
}
122+
}
123+
}.to_json)
124+
record = UserAccount.new(name: 'foo')
125+
assert record.save
126+
assert_equal(true, record.is_active)
127+
assert_equal('0.0', record.balance)
128+
end
129+
53130
def test_belongs_to_urls_are_formatted
54-
request = stub_request(:get, "http://example.com/multi-word-parents/1/multi-word-children")
131+
request = stub_request(:get, "http://example.com/multi_word_parents/1/multi_word_children")
55132
.to_return(headers: {content_type: "application/vnd.api+json"}, body: { data: [] }.to_json)
56133

57134
MultiWordChild.where(multi_word_parent_id: 1).to_a
58135

59136
assert_requested(request)
60137
end
61138

139+
def test_belongs_to_urls_create_record
140+
stub_request(:post, 'http://example.com/multi_word_parents/1/multi_word_children').
141+
with(headers: { content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json' }, body: {
142+
data: {
143+
type: 'multi_word_children',
144+
attributes: {
145+
foo: 'bar',
146+
'multi-word-field': true
147+
}
148+
}
149+
}.to_json)
150+
.to_return(headers: { content_type: 'application/vnd.api+json' }, body: {
151+
data: {
152+
id: '2',
153+
type: 'multi_word_children',
154+
attributes: {
155+
foo: 'bar',
156+
'multi-word-field': true
157+
}
158+
}
159+
}.to_json)
160+
161+
record = MultiWordChild.new(multi_word_parent_id: 1, foo: 'bar', multi_word_field: true)
162+
result = record.save
163+
assert result
164+
assert_equal('2', record.id)
165+
end
166+
62167
def test_load_has_one
63168
stub_request(:get, "http://example.com/properties/1")
64169
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {

0 commit comments

Comments
 (0)