Skip to content

Commit

Permalink
Merge pull request #91 from lucascaton/change-mutator-methods-behavior
Browse files Browse the repository at this point in the history
Make mutator helper methods call 'save!' if object responds to it
  • Loading branch information
lucascaton authored Sep 19, 2019
2 parents 761a9eb + f45c84b commit ea4d458
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/enumerate_it/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def create_mutator_methods(klass, attribute_name, helpers)
klass.enumeration.each_pair do |key, values|
define_method "#{prefix_name}#{key}!" do
send "#{attribute_name}=", values.first
save! if respond_to?(:save!)
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/enumerate_it_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ def initialize(foobar)
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
end

context 'when class responds to save! method' do
it 'calls save!' do
target = test_class_with_helper.new(TestEnumeration::VALUE_2)
allow(target).to receive(:save!)
target.value_3!
expect(target).to have_received(:save!)
end
end

context 'with :prefix option' do
let :test_class_with_prefixed_helper do
Class.new do
Expand Down Expand Up @@ -193,6 +202,15 @@ def initialize(foobar)
target.foobar_value_3!
expect(target.foobar).to eq(TestEnumeration::VALUE_3)
end

context 'when class responds to save! method' do
it 'calls save!' do
target = test_class_with_prefixed_helper.new(TestEnumeration::VALUE_2)
allow(target).to receive(:save!)
target.foobar_value_3!
expect(target).to have_received(:save!)
end
end
end

context 'with :polymorphic option' do
Expand Down

0 comments on commit ea4d458

Please sign in to comment.