Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/json_api_client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("/")
Expand Down Expand Up @@ -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?
Expand Down
6 changes: 6 additions & 0 deletions test/unit/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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