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

fix issue initial state doesn't work in active_record 4.2 #335

Open
wants to merge 1 commit 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
8 changes: 8 additions & 0 deletions lib/state_machine/integrations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ def column_defaults(*) #:nodoc:
result
end
end_eval

define_helper :class, <<-end_eval, __FILE__, __LINE__ + 1
def _default_attributes #:nodoc:
result = super
self.state_machines.initialize_states(nil, :static => :force, :dynamic => false, :to => result)
result
end
end_eval
end

# Initializes dynamic states
Expand Down
8 changes: 6 additions & 2 deletions lib/state_machine/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,13 @@ def initialize_state(object, options = {})
state = initial_state(object)
if state && (options[:force] || initialize_state?(object))
value = state.value

if hash = options[:to]
hash[attribute.to_s] = value
if hash.is_a?(Hash)
hash[attribute.to_s] = value
else # for ActiveRecord 4.2. hash.is_a?(Activerecord::AttributeSet)
hash.write_cast_value(attribute.to_s, value)
end
else
write(object, :state, value)
end
Expand Down