Skip to content

Commit

Permalink
add update_attributes to model
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharywelch committed Feb 13, 2019
1 parent 417fbc1 commit 1849339
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ user.fullname = "Lindsay Fünke" # OR user.assign_attributes(fullname: "Lindsay
user.save # returns false if it fails, errors in user.response_errors array
# PUT "/users/1" with `fullname=Lindsay+Fünke`

user.update_attributes(fullname: "Maeby Fünke")
# PUT "/users/1" with `fullname=Maeby+Fünke`

# => PUT /users/1 { "id": 1, "name": "new new name" }
# Update a resource without fetching it
User.save_existing(1, fullname: "Lindsay Fünke")
# PUT "/users/1" with `fullname=Lindsay+Fünke`
Expand Down
10 changes: 10 additions & 0 deletions lib/her/model/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def save!
self
end

# Update a resource and return it
#
# @example
# @user = User.find(1)
# @user.update_attributes(:name => "Tobias Fünke")
# # Called via PUT "/users/1"
def update_attributes(attributes)
assign_attributes(attributes) && save
end

# Destroy a resource
#
# @example
Expand Down
7 changes: 7 additions & 0 deletions spec/model/orm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ def friends
expect(@user.fullname).to eq("Lindsay Fünke")
end

it "handle resource update through #update_attributes" do
@user = Foo::User.find(1)
expect(@user).to receive(:save).and_return(true)
@user.update_attributes(fullname: "Lindsay Fünke")
expect(@user.fullname).to eq("Lindsay Fünke")
end

it "handles resource update through #toggle without saving it" do
@user = Foo::User.find(1)
expect(@user.admin).to be_falsey
Expand Down

0 comments on commit 1849339

Please sign in to comment.