Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #26 from lerrua/add-prefix-for-helpers

Adding the ability to pass a hash to the :create_helpers option, so the created helper methods can have an optional prefix.
  • Loading branch information...
commit 1061f751995f59493605f67e86d77a502f797cf6 2 parents e9b1f29 + 3b18d82
@cassiomarques cassiomarques authored
Showing with 35 additions and 6 deletions.
  1. +10 −6 lib/enumerate_it.rb
  2. +25 −0 spec/enumerate_it_spec.rb
View
16 lib/enumerate_it.rb
@@ -305,8 +305,8 @@ def has_enumeration_for(attribute, options = {})
create_enumeration_humanize_method options[:with], attribute
store_enumeration options[:with], attribute
if options[:create_helpers]
- create_helper_methods options[:with], attribute
- create_mutator_methods options[:with], attribute
+ create_helper_methods options[:with], attribute, options[:create_helpers]
+ create_mutator_methods options[:with], attribute, options[:create_helpers]
end
if options[:create_scopes]
@@ -329,10 +329,12 @@ def create_enumeration_humanize_method(klass, attribute_name)
end
end
- def create_helper_methods(klass, attribute_name)
+ def create_helper_methods(klass, attribute_name, helpers)
+ prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
+
class_eval do
klass.enumeration.keys.each do |option|
- define_method "#{option}?" do
+ define_method "#{prefix_name}#{option}?" do
self.send(attribute_name) == klass.enumeration[option].first
end
end
@@ -347,10 +349,12 @@ def create_scopes(klass, attribute_name)
end
end
- def create_mutator_methods(klass, attribute_name)
+ def create_mutator_methods(klass, attribute_name, helpers)
+ prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]
+
class_eval do
klass.enumeration.each_pair do |key, values|
- define_method "#{key}!" do
+ define_method "#{prefix_name}#{key}!" do
self.send "#{attribute_name}=", values.first
end
end
View
25 spec/enumerate_it_spec.rb
@@ -170,6 +170,31 @@ class TestClass
target.value_3!
target.foobar.should == TestEnumeration::VALUE_3
end
+
+ context "with :prefix option" do
+ before :each do
+ class TestClass
+ has_enumeration_for :foobar, :with => TestEnumeration, :create_helpers => { :prefix => true }
+ end
+ end
+
+ it "creates helpers methods with question marks and prefixes for each enumeration option" do
+ target = TestClass.new(TestEnumeration::VALUE_2)
+ target.should be_foobar_value_2
+ end
+
+ it "creates a mutator method for each enumeration value" do
+ [:value_1, :value_2, :value_3].each do |value|
+ TestClass.new(TestEnumeration::VALUE_1).should respond_to(:"foobar_#{value}!")
+ end
+ end
+
+ it "changes the attribute's value through mutator methods" do
+ target = TestClass.new(TestEnumeration::VALUE_2)
+ target.foobar_value_3!
+ target.foobar.should == TestEnumeration::VALUE_3
+ end
+ end
end
describe "using the :create_scopes option" do
Please sign in to comment.
Something went wrong with that request. Please try again.