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.



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aasm/base.rb', line 8

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

  # raise if the model is invalid (in ActiveRecord)
  configure :whiny_persistence, false

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

  # use pessimistic locking (in ActiveRecord)
  # true for FOR UPDATE lock
  # string for a specific lock type i.e. FOR UPDATE NOWAIT
  configure :requires_lock, false

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

  # allow a AASM::Base sub-class to be used for state machine
  configure :with_klass, AASM::Base

  configure :enum, nil

  # Set to true to namespace reader methods and constants
  configure :namespace, false

  # Configure a logger, with default being a Logger to STDERR
  configure :logger, Logger.new(STDERR)

  # make sure to raise an error if no_direct_assignment is enabled
  # and attribute is directly assigned though
  aasm_name = @name
  klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do
    if self.class.aasm(:"#{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(state_name)
    end
  end
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/aasm/base.rb', line 6

def klass
  @klass
end

#state_machineObject (readonly)

Returns the value of attribute state_machine.



6
7
8
# File 'lib/aasm/base.rb', line 6

def state_machine
  @state_machine
end

Instance Method Details

#after_all_events(*callbacks, &block) ⇒ Object



151
152
153
# File 'lib/aasm/base.rb', line 151

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

#after_all_transactions(*callbacks, &block) ⇒ Object



139
140
141
# File 'lib/aasm/base.rb', line 139

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

#after_all_transitions(*callbacks, &block) ⇒ Object



135
136
137
# File 'lib/aasm/base.rb', line 135

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



66
67
68
69
70
71
72
73
# File 'lib/aasm/base.rb', line 66

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

#before_all_events(*callbacks, &block) ⇒ Object



147
148
149
# File 'lib/aasm/base.rb', line 147

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

#before_all_transactions(*callbacks, &block) ⇒ Object



143
144
145
# File 'lib/aasm/base.rb', line 143

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

#ensure_on_all_events(*callbacks, &block) ⇒ Object



159
160
161
# File 'lib/aasm/base.rb', line 159

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

#error_on_all_events(*callbacks, &block) ⇒ Object



155
156
157
# File 'lib/aasm/base.rb', line 155

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

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

define an event



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/aasm/base.rb', line 111

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

  aasm_name = @name.to_sym
  event = name.to_sym

  # 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.
  safely_define_method klass, "may_#{name}?", ->(*args) do
    aasm(aasm_name).may_fire_event?(event, *args)
  end

  safely_define_method klass, "#{name}!", ->(*args, &block) do
    aasm(aasm_name).current_event = :"#{name}!"
    aasm_fire_event(aasm_name, event, {:persist => true}, *args, &block)
  end

  safely_define_method klass, name, ->(*args, &block) do
    aasm(aasm_name).current_event = event
    aasm_fire_event(aasm_name, event, {:persist => false}, *args, &block)
  end
end

#eventsObject



167
168
169
# File 'lib/aasm/base.rb', line 167

def events
  @state_machine.events.values
end

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



180
181
182
183
184
185
186
187
# File 'lib/aasm/base.rb', line 180

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?



172
173
174
# File 'lib/aasm/base.rb', line 172

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

#initial_state(new_initial_state = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/aasm/base.rb', line 75

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



163
164
165
# File 'lib/aasm/base.rb', line 163

def states
  @state_machine.states
end

#states_for_selectObject



176
177
178
# File 'lib/aasm/base.rb', line 176

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