Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mutator helper methods call 'save!' if object responds to it #91

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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