Skip to content

Commit

Permalink
Add update method for ActiveModel compatible classes (#37)
Browse files Browse the repository at this point in the history
* Added update method

* Added tests for update method
  • Loading branch information
Miodrag Seslija authored and calmyournerves committed Mar 9, 2017
1 parent 67d83f0 commit 0492feb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rest_in_peace/active_model_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def create_with_dirty_tracking
valid?
end

def update(attributes)
update_attributes(attributes)
save
end

def valid?
!errors.any?
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rest_in_peace/active_model_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ def extended_class.model_name
end
end

describe '#update' do
let(:new_attributes) { { name: 'new_name', description: 'yoloswag' } }

subject { instance }

it 'saves record after update' do
expect(subject).to receive(:save)

subject.update(new_attributes)
end

specify do
expect { subject.update(new_attributes) }.
to change(instance, :description).from(attributes[:description]).to(new_attributes[:description])
end

specify do
expect { subject.update(new_attributes) }.to_not change(instance, :name).from(attributes[:name])
end
end

describe 'validation handling' do
before do
def extended_class.model_name
Expand Down

0 comments on commit 0492feb

Please sign in to comment.