Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account for aliased attributes when attribute methods are mixed in #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the condition for old AM version ? We will support only 6.0+


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