-
Notifications
You must be signed in to change notification settings - Fork 7
/
state.rb
57 lines (48 loc) · 1.18 KB
/
state.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
module AASM
module SupportingClasses
class State
attr_reader :name, :options
def initialize(name, options={})
@name = name
update(options)
end
def ==(state)
if state.is_a? Symbol
name == state
else
name == state.name
end
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 ||= name.to_s.gsub(/_/, ' ').capitalize
end
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 # SupportingClasses
end # AASM