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

Map attribute aliases to column names when initializing #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/state_machines/integrations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
20 changes: 20 additions & 0 deletions test/machine_with_initialized_aliased_attribute_test.rb
Original file line number Diff line number Diff line change
@@ -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