From aa38e79c3b46f04f6ee6ee0aca7d92d7babb3bbc Mon Sep 17 00:00:00 2001 From: Sean Walbran Date: Thu, 2 May 2013 10:26:43 -0500 Subject: [PATCH 1/2] use lambda to meet rails4 requirement that scopes respond to call --- lib/attr_enumerator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/attr_enumerator.rb b/lib/attr_enumerator.rb index 4068401..6faf4c1 100644 --- a/lib/attr_enumerator.rb +++ b/lib/attr_enumerator.rb @@ -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 From 45ec99c915f356bf933d01219594468d1749ddbc Mon Sep 17 00:00:00 2001 From: Sean Walbran Date: Thu, 2 May 2013 10:28:10 -0500 Subject: [PATCH 2/2] fix spec issue with AR errors class --- spec/attr_enumerator_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/attr_enumerator_spec.rb b/spec/attr_enumerator_spec.rb index 685e4de..96d552c 100644 --- a/spec/attr_enumerator_spec.rb +++ b/spec/attr_enumerator_spec.rb @@ -11,7 +11,7 @@ class Car attr_accessor :color end end - + after(:each) { Object.send(:remove_const, :Car) } context "validation" do @@ -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 @@ -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