From 08ee3fcba611e37f2a56e16f9c935d8ba8bb6b59 Mon Sep 17 00:00:00 2001 From: Alex Tkachev Date: Mon, 26 Aug 2013 15:12:49 +0300 Subject: [PATCH 1/2] fix: use values returned by attribute setter methods --- lib/enumerated_attribute/integrations/active_record.rb | 2 +- spec/active_record/active_record_spec.rb | 7 ++++++- spec/active_record/race_car.rb | 5 +++++ spec/active_record/test_in_memory.rb | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/enumerated_attribute/integrations/active_record.rb b/lib/enumerated_attribute/integrations/active_record.rb index f4daa6d..5b814dc 100644 --- a/lib/enumerated_attribute/integrations/active_record.rb +++ b/lib/enumerated_attribute/integrations/active_record.rb @@ -97,7 +97,7 @@ class << self def new(*args, &block) result = new_without_enumerated_attribute(*args, &block) params = (!args.empty? && args.first.instance_of?(Hash)) ? args.first : {} - params.each { |k, v| result.write_enumerated_attribute(k, v) } + params.each { |k, v| result.write_enumerated_attribute(k, result[k.to_s]) } result.initialize_enumerated_attributes(true) yield result if block_given? result diff --git a/spec/active_record/active_record_spec.rb b/spec/active_record/active_record_spec.rb index 1092948..39b31ad 100644 --- a/spec/active_record/active_record_spec.rb +++ b/spec/active_record/active_record_spec.rb @@ -409,6 +409,11 @@ s = RaceCar.find r.id s.lights.should == "--- :off\n" s[:lights].should == "--- :off\n" - end + end + + it "should use values returned by attribute setter methods" do + r =RaceCar.new(:license_plate_number => ' asdf ') + r.license_plate_number.should == 'asdf' + end end diff --git a/spec/active_record/race_car.rb b/spec/active_record/race_car.rb index 95f9d9b..e5aacba 100644 --- a/spec/active_record/race_car.rb +++ b/spec/active_record/race_car.rb @@ -2,6 +2,11 @@ class RaceCar < ActiveRecord::Base enum_attr :gear, %w(reverse ^neutral first second over_drive) enum_attr :choke, %w(^none medium full) + + def license_plate_number=(value) + value = value.strip # remove whitespace from start and end of value + write_attribute(:license_plate_number, value) + end end #gear = enumerated column attribute diff --git a/spec/active_record/test_in_memory.rb b/spec/active_record/test_in_memory.rb index 69a9e2e..7f284b1 100644 --- a/spec/active_record/test_in_memory.rb +++ b/spec/active_record/test_in_memory.rb @@ -12,6 +12,7 @@ connection = ActiveRecord::Base.connection connection.create_table(:race_cars, :force=>true) do |t| t.string :name + t.string :license_plate_number t.enum :gear t.enum :lights t.timestamps From 053740ab314275af5953e48c406b9ae3a61425ce Mon Sep 17 00:00:00 2001 From: Alex Tkachev Date: Tue, 27 Aug 2013 12:15:31 +0300 Subject: [PATCH 2/2] fix: don't break activerecord initialize functionality by creating attributes for keys that aren't really attributes --- lib/enumerated_attribute/integrations/active_record.rb | 2 +- spec/active_record/active_record_spec.rb | 7 ++++++- spec/active_record/race_car.rb | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/enumerated_attribute/integrations/active_record.rb b/lib/enumerated_attribute/integrations/active_record.rb index 5b814dc..3f87a99 100644 --- a/lib/enumerated_attribute/integrations/active_record.rb +++ b/lib/enumerated_attribute/integrations/active_record.rb @@ -97,7 +97,7 @@ class << self def new(*args, &block) result = new_without_enumerated_attribute(*args, &block) params = (!args.empty? && args.first.instance_of?(Hash)) ? args.first : {} - params.each { |k, v| result.write_enumerated_attribute(k, result[k.to_s]) } + params.each { |k, v| result.write_enumerated_attribute(k, result[k.to_s]) if self.has_enumerated_attribute?(k.to_s) } result.initialize_enumerated_attributes(true) yield result if block_given? result diff --git a/spec/active_record/active_record_spec.rb b/spec/active_record/active_record_spec.rb index 39b31ad..b5d26b9 100644 --- a/spec/active_record/active_record_spec.rb +++ b/spec/active_record/active_record_spec.rb @@ -415,5 +415,10 @@ r =RaceCar.new(:license_plate_number => ' asdf ') r.license_plate_number.should == 'asdf' end - + + it "should not generate Active Record attributes on #new for keys that are not really active record attributes" do + r =RaceCar.new(:non_active_record_attribute => 'some value') + r.should_not have_attribute(:non_active_record_attribute) + end + end diff --git a/spec/active_record/race_car.rb b/spec/active_record/race_car.rb index e5aacba..f152262 100644 --- a/spec/active_record/race_car.rb +++ b/spec/active_record/race_car.rb @@ -3,6 +3,8 @@ class RaceCar < ActiveRecord::Base enum_attr :gear, %w(reverse ^neutral first second over_drive) enum_attr :choke, %w(^none medium full) + attr_accessor :non_active_record_attribute + def license_plate_number=(value) value = value.strip # remove whitespace from start and end of value write_attribute(:license_plate_number, value)