Skip to content

Commit a42c6fd

Browse files
kdonovanchingor13
authored andcommitted
Fixed: Resource default attributes act as defaults and can be overridden (#266)
1 parent 1b4d7da commit a42c6fd

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/json_api_client/resource.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def requestor
167167
end
168168

169169
# Default attributes that every instance of this resource should be
170-
# intialized with. Optionally, override this method in a subclass.
170+
# initialized with. Optionally, override this method in a subclass.
171171
#
172172
# @return [Hash] Default attributes
173173
def default_attributes
@@ -272,7 +272,7 @@ def _prefix_path
272272

273273
def _set_prefix_path(attrs)
274274
paths = _belongs_to_associations.map do |a|
275-
a.set_prefix_path(attrs, route_formatter)
275+
a.set_prefix_path(attrs, route_formatter)
276276
end
277277

278278
paths.join("/")
@@ -305,7 +305,7 @@ def initialize(params = {})
305305
@persisted = nil
306306
self.links = self.class.linker.new(params.delete("links") || {})
307307
self.relationships = self.class.relationship_linker.new(self.class, params.delete("relationships") || {})
308-
self.attributes = params.merge(self.class.default_attributes)
308+
self.attributes = self.class.default_attributes.merge(params)
309309

310310
self.class.schema.each_property do |property|
311311
attributes[property.name] = property.default unless attributes.has_key?(property.name) || property.default.nil?

test/unit/resource_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,10 @@ def test_associations_as_params
8989
assert_equal(article.attributes['author']['type'], 'authors')
9090
assert_equal(article.attributes['author']['id'], 1)
9191
end
92+
93+
def test_default_params_overrideable
94+
article = Article.new(type: 'Story')
95+
assert_equal(article.type, 'Story')
96+
end
97+
9298
end

0 commit comments

Comments
 (0)