Skip to content

Commit e9471f4

Browse files
authored
Merge pull request #326 from senid231/ensure-correct-changes-on-resource-init
correct changes after initialize resource
2 parents 10edc85 + 4c1b9fa commit e9471f4

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
- [#328](https://github.com/JsonApiClient/json_api_client/pull/328) - allow custom type for models
1212

13+
- [#326](https://github.com/JsonApiClient/json_api_client/pull/326) - correct changes after initialize resource
14+
* remove type from changes on initialize
15+
* ensure that query builder doesn't propagate query values to resource attributes via #build method
16+
1317
## 1.7.0
1418

1519
- [#320](https://github.com/JsonApiClient/json_api_client/pull/320) - fix passing relationships on create

lib/json_api_client/helpers/dirty.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def clear_changes_information
1818
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
1919
end
2020

21+
def forget_change!(attr)
22+
@changed_attributes.delete(attr.to_s)
23+
end
24+
2125
def set_all_attributes_dirty
2226
attributes.each do |k, v|
2327
set_attribute_was(k, v)
@@ -68,4 +72,4 @@ def set_attribute(name, value)
6872

6973
end
7074
end
71-
end
75+
end

lib/json_api_client/resource.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ def initialize(params = {})
329329
self.links = self.class.linker.new(params.delete(:links) || {})
330330
self.relationships = self.class.relationship_linker.new(self.class, params.delete(:relationships) || {})
331331
self.attributes = self.class.default_attributes.merge params.except(*self.class.prefix_params)
332+
self.forget_change!(:type)
332333
self.__belongs_to_params = params.slice(*self.class.prefix_params)
333334

334335
setup_default_properties

test/test_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class Article < TestResource
2222
has_one :author
2323
end
2424

25+
class ArticleNested < TestResource
26+
belongs_to :author, shallow_path: true
27+
has_many :comments
28+
has_one :author
29+
end
30+
2531
class Person < TestResource
2632
end
2733

test/unit/query_builder_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,27 @@ def test_find_with_args
256256
assert_requested all_stub, times: 1
257257
assert_requested find_stub, times: 1
258258
end
259+
260+
def test_build_does_not_propagate_values
261+
query = Article.where(name: 'John').
262+
includes(:author).
263+
order(id: :desc).
264+
select(:id, :name).
265+
page(1).
266+
per(20).
267+
with_params(sort: "foo")
268+
269+
record = query.build
270+
assert_equal [], record.changed
271+
assert_equal [], record.relationships.changed
272+
end
273+
274+
def test_build_propagate_only_path_params
275+
query = ArticleNested.where(author_id: '123', name: 'John')
276+
record = query.build
277+
assert_equal [], record.changed
278+
assert_equal({author_id: '123'}, record.__belongs_to_params)
279+
assert_equal '123', record.author_id
280+
assert_equal [], record.relationships.changed
281+
end
259282
end

0 commit comments

Comments
 (0)