Method: AASM::InstanceBase#states

Defined in:
lib/aasm/instance_base.rb

#states(options = {}, *args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aasm/instance_base.rb', line 33

def states(options={}, *args)
  if options.has_key?(:permitted)
    selected_events = events({:permitted => options[:permitted]}, *args)
    # An array of arrays. Each inner array represents the transitions that
    # transition from the current state for an event
    event_transitions = selected_events.map {|e| e.transitions_from_state(current_state) }

    # An array of :to transition states
    to_state_names = event_transitions.map do |transitions|
      return nil if transitions.empty?

      # Return the :to state of the first transition that is allowed (or not) or nil
      if options[:permitted]
        transition = transitions.find { |t| t.allowed?(@instance, *args) }
      else
        transition = transitions.find { |t| !t.allowed?(@instance, *args) }
      end
      transition ? transition.to : nil
    end.flatten.compact.uniq

    # Select states that are in to_state_names
    @instance.class.aasm(@name).states.select {|s| to_state_names.include?(s.name)}
  else
    @instance.class.aasm(@name).states
  end
end