Skip to content

Commit

Permalink
coerce new attributes to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharywelch committed Feb 13, 2019
1 parent ab77a42 commit 8a0d873
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/her/model/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ def respond_to_missing?(method, include_private = false)
# user.assign_attributes(name: "Lindsay")
# user.changes # => { :name => ["Tobias", "Lindsay"] }
def assign_attributes(new_attributes)
if !new_attributes.respond_to?(:to_hash)
raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
end

# Coerce new_attributes to hash in case of strong parameters
new_attributes = new_attributes.to_hash

@_her_attributes ||= attributes

# Use setter methods first
unset_attributes = self.class.use_setter_methods(self, new_attributes)

Expand Down
4 changes: 4 additions & 0 deletions spec/model/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
@user.assign_attributes active: true
expect(@user).to be_active
end

it "expects to receive hash" do
expect { @user.assign_attributes(1) }.to raise_error(ArgumentError, 'When assigning attributes, you must pass a hash as an argument.')
end
end

context "checking resource equality" do
Expand Down

0 comments on commit 8a0d873

Please sign in to comment.