/
state.rb
92 lines (78 loc) · 1.94 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
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
# frozen_string_literal: true
module AASM::Core
class State
attr_reader :name, :state_machine, :options
def initialize(name, klass, state_machine, options={})
@name = name
@klass = klass
@state_machine = state_machine
update(options)
end
# called internally by Ruby 1.9 after clone()
def initialize_copy(orig)
super
@options = {}
orig.options.each_pair do |name, setting|
@options[name] = if setting.is_a?(Hash) || setting.is_a?(Array)
setting.dup
else
setting
end
end
end
def ==(state)
if state.is_a? Symbol
name == state
else
name == state.name
end
end
def <=>(state)
if state.is_a? Symbol
name <=> state
else
name <=> state.name
end
end
def to_s
name.to_s
end
def fire_callbacks(action, record, *args)
action = @options[action]
catch :halt_aasm_chain do
action.is_a?(Array) ?
action.each {|a| _fire_callbacks(a, record, args)} :
_fire_callbacks(action, record, args)
end
end
def display_name
@display_name = begin
if @fixed_display_name
@fixed_display_name
elsif Module.const_defined?(:I18n)
localized_name
else
name.to_s.gsub(/_/, ' ').capitalize
end
end
end
def localized_name
AASM::Localizer.new.human_state_name(@klass, self)
end
alias human_name localized_name
def for_select
[display_name, name.to_s]
end
private
def update(options = {})
if options.key?(:display)
@fixed_display_name = options.delete(:display)
end
@options = options
self
end
def _fire_callbacks(action, record, args)
Invoker.new(action, record, args).invoke
end
end
end # AASM