Skip to content

Instantly share code, notes, and snippets.

@realmyst
Created December 25, 2015 17:38
Show Gist options
  • Save realmyst/6080fc05f9c85afe0e7d to your computer and use it in GitHub Desktop.
Save realmyst/6080fc05f9c85afe0e7d to your computer and use it in GitHub Desktop.
#See https://github.com/pluginaweek/state_machine/issues/251
module StateMachine
module Integrations
module ActiveModel
public :around_validation
end
module ActiveRecord
public :around_save
end
end
end
module ActiveRecord
# HACK to set state_machine initial states as default attributes with Rails 4
module ModelSchema
module ClassMethods
private
def raw_default_values
result = columns_hash.transform_values(&:default)
state_machines.initialize_states(nil, static: :force, dynamic: false, to: result) if respond_to?(:state_machines)
result
end
end
end
# HACK to store state_machine initial states in DB too with Rails 4
module AttributeMethods
private
def attributes_for_create(attribute_names)
if self.class.respond_to?(:state_machines)
state_machine_attributes = self.class.state_machines.keys.map(&:to_s)
else
state_machine_attributes = []
end
(attribute_names + state_machine_attributes).uniq.reject do |name|
pk_attribute?(name) && id.nil?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment