Skip to content

Commit

Permalink
Map attribute aliases to column names when initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvh committed Apr 26, 2021
1 parent 94662c6 commit 626d575
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
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
6 changes: 6 additions & 0 deletions test/machine_with_aliased_attribute_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 626d575

Please sign in to comment.