Skip to content

Commit

Permalink
account for aliased attributes when attribute methods are mixed in (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvh authored Aug 7, 2022
1 parent 59157c1 commit 33e2a5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/state_machines/integrations/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ def around_validation(object)
def define_state_initializer
define_helper :instance, <<-end_eval, __FILE__, __LINE__ + 1
def initialize(params = {})
params.transform_keys! do |key|
self.class.attribute_aliases[key.to_s] || key.to_s
end if self.class.respond_to?(:attribute_aliases)
self.class.state_machines.initialize_states(self, {}, params) { super }
end
end_eval
Expand Down
33 changes: 33 additions & 0 deletions test/machine_with_initialized_aliased_attribute_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative 'test_helper'

class MachineWithInitializedAliasedAttributeTest < BaseTestCase
def test_should_match_original_attribute_value_with_attribute_methods
model = new_model do
include ActiveModel::AttributeMethods
alias_attribute :custom_status, :state
end

machine = StateMachines::Machine.new(model, initial: :parked, integration: :active_model)
machine.other_states(:started)

record = model.new(custom_status: 'started')

refute record.state?(:parked)
assert record.state?(:started)
end

def test_should_not_match_original_attribute_value_without_attribute_methods
model = new_model do
alias_attribute :custom_status, :state
end

machine = StateMachines::Machine.new(model, initial: :parked, integration: :active_model)
machine.other_states(:started)

record = model.new(custom_status: 'started')

assert record.state?(:parked)
refute record.state?(:started)
end
end

0 comments on commit 33e2a5f

Please sign in to comment.