From 626d5758a0ddf8a737e94b8e1ff3774cc8a4e213 Mon Sep 17 00:00:00 2001 From: Joel Van Horn Date: Fri, 16 Apr 2021 19:39:47 -0400 Subject: [PATCH 1/2] Map attribute aliases to column names when initializing --- lib/state_machines/integrations/active_record.rb | 3 ++- test/machine_with_aliased_attribute_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/state_machines/integrations/active_record.rb b/lib/state_machines/integrations/active_record.rb index de3031a..a8d1ea2 100644 --- a/lib/state_machines/integrations/active_record.rb +++ b/lib/state_machines/integrations/active_record.rb @@ -445,7 +445,8 @@ def define_state_initializer define_helper :instance, <<-end_eval, __FILE__, __LINE__ + 1 def initialize(attributes = nil, *) super(attributes) do |*args| - scoped_attributes = (attributes || {}).merge(self.class.scope_attributes) + attributes = (attributes || {}).transform_keys { |key| self.class.attribute_aliases[key.to_s] || key } + scoped_attributes = attributes.merge(self.class.scope_attributes) self.class.state_machines.initialize_states(self, {}, scoped_attributes) yield(*args) if block_given? diff --git a/test/machine_with_aliased_attribute_test.rb b/test/machine_with_aliased_attribute_test.rb index 0837384..de7fb45 100644 --- a/test/machine_with_aliased_attribute_test.rb +++ b/test/machine_with_aliased_attribute_test.rb @@ -19,5 +19,11 @@ def test_should_check_custom_attribute_for_predicate @record.vehicle_status = 'parked' assert @record.status?(:parked) end + + def test_should_initialize_with_original_attribute_names + record = @model.new(:vehicle_status => 'bogus') + refute record.status?(:parked) + assert record.status?(:bogus) + end end From 0e32ee46730c221096185a82cf749da850785456 Mon Sep 17 00:00:00 2001 From: Joel Van Horn Date: Fri, 16 Apr 2021 20:42:52 -0400 Subject: [PATCH 2/2] Fix implementation to map aliased to original attribute names --- test/machine_with_aliased_attribute_test.rb | 6 ------ ...with_initialized_aliased_attribute_test.rb | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 test/machine_with_initialized_aliased_attribute_test.rb diff --git a/test/machine_with_aliased_attribute_test.rb b/test/machine_with_aliased_attribute_test.rb index de7fb45..0837384 100644 --- a/test/machine_with_aliased_attribute_test.rb +++ b/test/machine_with_aliased_attribute_test.rb @@ -19,11 +19,5 @@ def test_should_check_custom_attribute_for_predicate @record.vehicle_status = 'parked' assert @record.status?(:parked) end - - def test_should_initialize_with_original_attribute_names - record = @model.new(:vehicle_status => 'bogus') - refute record.status?(:parked) - assert record.status?(:bogus) - end end diff --git a/test/machine_with_initialized_aliased_attribute_test.rb b/test/machine_with_initialized_aliased_attribute_test.rb new file mode 100644 index 0000000..4c057fa --- /dev/null +++ b/test/machine_with_initialized_aliased_attribute_test.rb @@ -0,0 +1,20 @@ +require_relative 'test_helper' + +class MachineWithInitializedAliasedAttributeTest < BaseTestCase + def setup + @model = new_model do + alias_attribute :custom_status, :state + end + + @machine = StateMachines::Machine.new(@model, :initial => :parked, :attribute => :state) + @machine.state :started + + @record = @model.new(:custom_status => :started) + end + + def test_should_match_original_attribute_value + refute @record.state?(:parked) + assert @record.state?(:started) + end +end +