Class: AASM::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/aasm/persistence/base.rb,
lib/aasm/base.rb

Overview

Persistence

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, state_machine, options = {}, &block) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aasm/base.rb', line 6

def initialize(klass, name, state_machine, options={}, &block)
  @klass = klass
  @name = name
  # @state_machine = @klass.aasm(@name).state_machine
  @state_machine = state_machine
  @state_machine.config.column ||= (options[:column] || default_column).to_sym
  # @state_machine.config.column = options[:column].to_sym if options[:column] # master
  @options = options

  # let's cry if the transition is invalid
  configure :whiny_transitions, true

  # create named scopes for each state
  configure :create_scopes, true

  # don't store any new state if the model is invalid (in ActiveRecord)
  configure :skip_validation_on_save, false

  # use requires_new for nested transactions (in ActiveRecord)
  configure :requires_new_transaction, true

  # set to true to forbid direct assignment of aasm_state column (in ActiveRecord)
  configure :no_direct_assignment, false

  configure :enum, nil

  # make sure to raise an error if no_direct_assignment is enabled
  # and attribute is directly assigned though
  @klass.class_eval %Q(
    def #{@state_machine.config.column}=(state_name)
      if self.class.aasm(:#{@name}).state_machine.config.no_direct_assignment
        raise AASM::NoDirectAssignmentError.new(
          'direct assignment of AASM column has been disabled (see AASM configuration for this class)'
        )
      else
        super
      end
    end
  )
end

Instance Attribute Details

#state_machineObject (readonly)

Returns the value of attribute state_machine.



4
5
6
# File 'lib/aasm/base.rb', line 4

def state_machine
  @state_machine
end

Instance Method Details

#after_all_transitions(*callbacks, &block) ⇒ Object



112
113
114
# File 'lib/aasm/base.rb', line 112

def after_all_transitions(*callbacks, &block)
  @state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
end

#attribute_name(column_name = nil) ⇒ Object

This method is both a getter and a setter



48
49
50
51
52
53
54
55
# File 'lib/aasm/base.rb', line 48

def attribute_name(column_name=nil)
  if column_name
    @state_machine.config.column = column_name.to_sym
  else
    @state_machine.config.column ||= :aasm_state
  end
  @state_machine.config.column
end

#event(name, options = {}, &block) ⇒ Object

define an event



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aasm/base.rb', line 85

def event(name, options={}, &block)
  @state_machine.add_event(name, options, &block)

  if @klass.instance_methods.include?("may_#{name}?".to_sym)
    warn "#{@klass.name}: The aasm event name #{name} is already used!"
  end

  # an addition over standard aasm so that, before firing an event, you can ask
  # may_event? and get back a boolean that tells you whether the guard method
  # on the transition will let this happen.
  @klass.class_eval "    def may_\#{name}?(*args)\n      aasm(:\#{@name}).may_fire_event?(:\#{name}, *args)\n    end\n\n    def \#{name}!(*args, &block)\n      aasm(:\#{@name}).current_event = :\#{name}!\n      aasm_fire_event(:\#{@name}, :\#{name}, {:persist => true}, *args, &block)\n    end\n\n    def \#{name}(*args, &block)\n      aasm(:\#{@name}).current_event = :\#{name}\n      aasm_fire_event(:\#{@name}, :\#{name}, {:persist => false}, *args, &block)\n    end\n  EORUBY\nend\n", __FILE__, __LINE__ + 1

#eventsObject



120
121
122
# File 'lib/aasm/base.rb', line 120

def events
  @state_machine.events.values
end

#from_states_for_state(state, options = {}) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/aasm/base.rb', line 133

def from_states_for_state(state, options={})
  if options[:transition]
    @state_machine.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
  else
    events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten
  end
end

#human_event_name(event) ⇒ Object

aasm.event(:event_name).human?



125
126
127
# File 'lib/aasm/base.rb', line 125

def human_event_name(event) # event_name?
  AASM::Localizer.new.human_event_name(@klass, event)
end

#initial_state(new_initial_state = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/aasm/base.rb', line 57

def initial_state(new_initial_state=nil)
  if new_initial_state
    @state_machine.initial_state = new_initial_state
  else
    @state_machine.initial_state
  end
end

#state_with_scope(name, *args) ⇒ Object Also known as: state

make sure to create a (named) scope for each state



56
57
58
59
# File 'lib/aasm/persistence/base.rb', line 56

def state_with_scope(name, *args)
  state_without_scope(name, *args)
  create_scope(name) if create_scope?(name)
end

#statesObject



116
117
118
# File 'lib/aasm/base.rb', line 116

def states
  @state_machine.states
end

#states_for_selectObject



129
130
131
# File 'lib/aasm/base.rb', line 129

def states_for_select
  states.map { |state| state.for_select }
end