Skip to content

Instantly share code, notes, and snippets.

@realmyst
Created December 25, 2015 17:38

Revisions

  1. realmyst created this gist Dec 25, 2015.
    47 changes: 47 additions & 0 deletions state_machine.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #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