From 0492febf61d776401e3512c2748199b16cdf3af3 Mon Sep 17 00:00:00 2001 From: Miodrag Seslija Date: Thu, 9 Mar 2017 09:10:33 +0100 Subject: [PATCH] Add update method for ActiveModel compatible classes (#37) * Added update method * Added tests for update method --- lib/rest_in_peace/active_model_api.rb | 5 +++++ spec/rest_in_peace/active_model_api_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/rest_in_peace/active_model_api.rb b/lib/rest_in_peace/active_model_api.rb index a2ed2c4..44d3544 100644 --- a/lib/rest_in_peace/active_model_api.rb +++ b/lib/rest_in_peace/active_model_api.rb @@ -46,6 +46,11 @@ def create_with_dirty_tracking valid? end + def update(attributes) + update_attributes(attributes) + save + end + def valid? !errors.any? end diff --git a/spec/rest_in_peace/active_model_api_spec.rb b/spec/rest_in_peace/active_model_api_spec.rb index 6fc317a..052c5a1 100644 --- a/spec/rest_in_peace/active_model_api_spec.rb +++ b/spec/rest_in_peace/active_model_api_spec.rb @@ -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