Skip to content

Commit

Permalink
Merge branch 'rails4-compat' of https://github.com/seanwalbran/attr_e…
Browse files Browse the repository at this point in the history
…numerator into seanwalbran-rails4-compat
  • Loading branch information
Chris Baker committed May 2, 2013
2 parents ebb3726 + 45ec99c commit 66e5a87
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/attr_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ def attr_enumerator(attribute, choices, options = {})
constant = options.delete(:constant) || attribute.to_s.pluralize.upcase
prefix = options[:prefix] ? options.delete(:prefix).to_s + '_' : ''
options[:message] ||= :invalid

const_set(constant, choices).freeze
validates_inclusion_of attribute, options.merge(:in => choices)

choices.each do |choice|
choice_string = prefix + choice.to_s.underscore.parameterize('_')
define_method(choice_string + '?') { send(attribute) == choice }
scope choice_string, where(attribute => choice) if respond_to? :scope
scope choice_string, lambda { where(attribute => choice) } if respond_to? :scope
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/attr_enumerator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Car
attr_accessor :color
end
end

after(:each) { Object.send(:remove_const, :Car) }

context "validation" do
Expand All @@ -30,14 +30,14 @@ class Car
it "should have a default message" do
Car.attr_enumerator :color, ['red', 'blue']
car.valid?
car.errors.should == {:color => ['is invalid']}
car.errors[:color].should == ['is invalid']
end

it "should allow for a custom message" do
Car.attr_enumerator :color, ['red', 'blue'], :message => '%{value} is not a valid color'
car.color = 'green'
car.valid?
car.errors.should == {:color => ['green is not a valid color']}
car.errors[:color].should == ['green is not a valid color']
end

it "should handle allow_blank" do
Expand Down Expand Up @@ -144,7 +144,7 @@ class Car < ActiveRecord::Base
attr_accessor :color
end
end

after(:each) { Object.send(:remove_const, :Car) }

it "should automatically be included in ActiveRecord::Base" do
Expand Down

0 comments on commit 66e5a87

Please sign in to comment.