/
base.rb
240 lines (192 loc) · 7.26 KB
/
base.rb
1
2
3
4
5
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require 'logger'
module AASM
class Base
attr_reader :klass, :state_machine
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, true
# 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
# This method is both a getter and a setter
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
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
# define a state
# args
# [0] state
# [1] options (or nil)
# or
# [0] state
# [1..] state
def state(*args)
names, options = interpret_state_args(args)
names.each do |name|
@state_machine.add_state(name, klass, options)
aasm_name = @name.to_sym
state = name.to_sym
method_name = namespace? ? "#{namespace}_#{name}" : name
safely_define_method klass, "#{method_name}?", -> do
aasm(aasm_name).current_state == state
end
const_name = namespace? ? "STATE_#{namespace.upcase}_#{name.upcase}" : "STATE_#{name.upcase}"
unless klass.const_defined?(const_name)
klass.const_set(const_name, name)
end
end
end
# define an event
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
def after_all_transitions(*callbacks, &block)
@state_machine.add_global_callbacks(:after_all_transitions, *callbacks, &block)
end
def after_all_transactions(*callbacks, &block)
@state_machine.add_global_callbacks(:after_all_transactions, *callbacks, &block)
end
def before_all_transactions(*callbacks, &block)
@state_machine.add_global_callbacks(:before_all_transactions, *callbacks, &block)
end
def before_all_events(*callbacks, &block)
@state_machine.add_global_callbacks(:before_all_events, *callbacks, &block)
end
def after_all_events(*callbacks, &block)
@state_machine.add_global_callbacks(:after_all_events, *callbacks, &block)
end
def error_on_all_events(*callbacks, &block)
@state_machine.add_global_callbacks(:error_on_all_events, *callbacks, &block)
end
def ensure_on_all_events(*callbacks, &block)
@state_machine.add_global_callbacks(:ensure_on_all_events, *callbacks, &block)
end
def states
@state_machine.states
end
def events
@state_machine.events.values
end
# aasm.event(:event_name).human?
def human_event_name(event) # event_name?
AASM::Localizer.new.human_event_name(klass, event)
end
def states_for_select
states.map { |state| state.for_select }
end
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
private
def default_column
@name.to_sym == :default ? :aasm_state : @name.to_sym
end
def configure(key, default_value)
if @options.key?(key)
@state_machine.config.send("#{key}=", @options[key])
elsif @state_machine.config.send(key).nil?
@state_machine.config.send("#{key}=", default_value)
end
end
def safely_define_method(klass, method_name, method_definition)
# Warn if method exists and it did not originate from an enum
if klass.method_defined?(method_name) &&
! ( @state_machine.config.enum &&
klass.respond_to?(:defined_enums) &&
klass.defined_enums.values.any?{ |methods|
methods.keys{| enum | enum + '?' == method_name }
})
@state_machine.config.logger.warn "#{klass.name}: overriding method '#{method_name}'!"
end
klass.send(:define_method, method_name, method_definition)
end
def namespace?
!!@state_machine.config.namespace
end
def namespace
if @state_machine.config.namespace == true
@name
else
@state_machine.config.namespace
end
end
def interpret_state_args(args)
if args.last.is_a?(Hash) && args.size == 2
[[args.first], args.last]
elsif args.size > 0
[args, {}]
else
raise "count not parse states: #{args}"
end
end
end
end