diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index 10f334c2..d00fdd8e 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -167,7 +167,7 @@ def requestor end # Default attributes that every instance of this resource should be - # intialized with. Optionally, override this method in a subclass. + # initialized with. Optionally, override this method in a subclass. # # @return [Hash] Default attributes def default_attributes @@ -272,7 +272,7 @@ def _prefix_path def _set_prefix_path(attrs) paths = _belongs_to_associations.map do |a| - a.set_prefix_path(attrs, route_formatter) + a.set_prefix_path(attrs, route_formatter) end paths.join("/") @@ -305,7 +305,7 @@ def initialize(params = {}) @persisted = nil self.links = self.class.linker.new(params.delete("links") || {}) self.relationships = self.class.relationship_linker.new(self.class, params.delete("relationships") || {}) - self.attributes = params.merge(self.class.default_attributes) + self.attributes = self.class.default_attributes.merge(params) self.class.schema.each_property do |property| attributes[property.name] = property.default unless attributes.has_key?(property.name) || property.default.nil? diff --git a/test/unit/resource_test.rb b/test/unit/resource_test.rb index 9a192312..7c003dcc 100644 --- a/test/unit/resource_test.rb +++ b/test/unit/resource_test.rb @@ -89,4 +89,10 @@ def test_associations_as_params assert_equal(article.attributes['author']['type'], 'authors') assert_equal(article.attributes['author']['id'], 1) end + + def test_default_params_overrideable + article = Article.new(type: 'Story') + assert_equal(article.type, 'Story') + end + end