Sha256: b38ffc93d296f68b00a445278f5fa2ec834e44184cf9b1c47738b778ffa96674

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

module AASM
  class State
    attr_reader :name, :options

    def initialize(name, klass, options={})
      @name = name
      @klass = klass
      update(options)
    end

    def ==(state)
      if state.is_a? Symbol
        name == state
      else
        name == state.name
      end
    end

    def <=>(state)
      if state.is_a? Symbol
        name <=> state
      else
        name <=> state.name
      end
    end

    def to_s
      name.to_s
    end

    def fire_callbacks(action, record)
      action = @options[action]
      catch :halt_aasm_chain do
        action.is_a?(Array) ?
                action.each {|a| _fire_callbacks(a, record)} :
                _fire_callbacks(action, record)
      end
    end

    def display_name
      @display_name ||= begin
        if Module.const_defined?(:I18n)
          localized_name
        else
          name.to_s.gsub(/_/, ' ').capitalize
        end
      end
    end

    def localized_name
      AASM::Localizer.new.human_state_name(@klass, self)
    end
    alias human_name localized_name

    def for_select
      [display_name, name.to_s]
    end

  private

    def update(options = {})
      if options.key?(:display) then
        @display_name = options.delete(:display)
      end
      @options = options
      self
    end

    def _fire_callbacks(action, record)
      case action
        when Symbol, String
          record.send(action)
        when Proc
          action.call(record)
      end
    end

  end
end # AASM

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
aasm-4.0.3 lib/aasm/state.rb
aasm-4.0.2 lib/aasm/state.rb
aasm-4.0.1 lib/aasm/state.rb
aasm-4.0.0 lib/aasm/state.rb
aasm-3.4.0 lib/aasm/state.rb
aasm-3.3.3 lib/aasm/state.rb
aasm-3.3.2 lib/aasm/state.rb
aasm-3.3.1 lib/aasm/state.rb
aasm-3.3.0 lib/aasm/state.rb
aasm-3.2.1 lib/aasm/state.rb