Sha256: a1a131f107c275b29bc18e4bcdc845d8a70ff6eef7eb753bb1ad67fc0add70a9

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

module StateShifter
  module Definition
    module ActiveRecordIntegrationMethods

      class ::StateShifter::Definition::StatePersistenceAttributeNotPresent < RuntimeError; end

      def self.include_state_scopes(base)
        base.state_machine_definition.states.each do |name, definition|
          base.class_eval do
            scope name, -> { where(persist_attr_name => name) } unless respond_to?(name)
          end
        end

        base.state_machine_definition.state_tags.each do |name, states|
          base.class_eval do
            scope name, -> { where(persist_attr_name => states) } unless respond_to?(name)
          end
        end
      end

      def check_attr_presence
        raise StatePersistenceAttributeNotPresent unless self.attribute_names.include? self.class.persist_attr_name.to_s
      end

      def get_current_state
        check_attr_presence
        read_attribute self.class.persist_attr_name
      end

      def set_current_state value
        check_attr_presence
        update_attribute self.class.persist_attr_name, value
      end

      def write_initial_state
        raise StatePersistenceAttributeNotPresent unless self.attribute_names.include?(self.class.persist_attr_name.to_s)
        write_attribute self.class.persist_attr_name, self.class.state_machine_definition.initial_state.name.to_sym
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
state_shifter-1.1.3 lib/state_shifter/definition/active_record_integration_methods.rb
state_shifter-1.1.2 lib/state_shifter/definition/active_record_integration_methods.rb
state_shifter-1.0.7 lib/state_shifter/definition/active_record_integration_methods.rb
state_shifter-1.0.5 lib/state_shifter/definition/active_record_integration_methods.rb
state_shifter-1.0.3 lib/state_shifter/definition/active_record_integration_methods.rb