gvaughn-aasm 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,150 @@
1
+ require File.join(File.dirname(__FILE__), 'event')
2
+ require File.join(File.dirname(__FILE__), 'state')
3
+ require File.join(File.dirname(__FILE__), 'state_machine')
4
+ require File.join(File.dirname(__FILE__), 'persistence')
5
+
6
+ module AASM
7
+ class InvalidTransition < RuntimeError
8
+ end
9
+
10
+ def self.included(base) #:nodoc:
11
+ # TODO - need to ensure that a machine is being created because
12
+ # AASM was either included or arrived at via inheritance. It
13
+ # cannot be both.
14
+ base.extend AASM::ClassMethods
15
+ AASM::Persistence.set_persistence(base)
16
+ AASM::StateMachine[base] = AASM::StateMachine.new('')
17
+
18
+ base.class_eval do
19
+ def base.inherited(klass)
20
+ AASM::StateMachine[klass] = AASM::StateMachine[self].dup
21
+ super
22
+ end
23
+ end
24
+ end
25
+
26
+ module ClassMethods
27
+ def aasm_initial_state(set_state=nil)
28
+ if set_state
29
+ aasm_state_machine.initial_state = set_state
30
+ else
31
+ aasm_state_machine.initial_state
32
+ end
33
+ end
34
+
35
+ def aasm_initial_state=(state)
36
+ aasm_state_machine.initial_state = state
37
+ end
38
+
39
+ def aasm_state(name, options={})
40
+ sm = aasm_state_machine
41
+ sm.create_state(name, options)
42
+ sm.initial_state = name unless sm.initial_state
43
+
44
+ define_method("#{name.to_s}?") do
45
+ aasm_current_state == name
46
+ end
47
+ end
48
+
49
+ def aasm_event(name, options = {}, &block)
50
+ sm = aasm_state_machine
51
+
52
+ unless sm.events.has_key?(name)
53
+ sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
54
+ end
55
+
56
+ define_method("#{name.to_s}!") do |*args|
57
+ aasm_fire_event(name, true, *args)
58
+ end
59
+
60
+ define_method("#{name.to_s}") do |*args|
61
+ aasm_fire_event(name, false, *args)
62
+ end
63
+ end
64
+
65
+ def aasm_states
66
+ aasm_state_machine.states
67
+ end
68
+
69
+ def aasm_events
70
+ aasm_state_machine.events
71
+ end
72
+
73
+ def aasm_states_for_select
74
+ aasm_state_machine.states.map { |state| state.for_select }
75
+ end
76
+
77
+ def aasm_state_machine
78
+ AASM::StateMachine[self]
79
+ end
80
+
81
+ end
82
+
83
+ # Instance methods
84
+ def aasm_current_state
85
+ return @aasm_current_state if @aasm_current_state
86
+
87
+ if self.respond_to?(:aasm_read_state) || self.private_methods.include?('aasm_read_state')
88
+ @aasm_current_state = aasm_read_state
89
+ end
90
+ return @aasm_current_state if @aasm_current_state
91
+ self.class.aasm_initial_state
92
+ end
93
+
94
+ def aasm_events_for_current_state
95
+ aasm_events_for_state(aasm_current_state)
96
+ end
97
+
98
+ def aasm_events_for_state(state)
99
+ events = self.class.aasm_events.values.select {|event| event.transitions_from_state?(state) }
100
+ events.map {|event| event.name}
101
+ end
102
+
103
+ private
104
+ def aasm_current_state_with_persistence=(state)
105
+ if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
106
+ aasm_write_state(state)
107
+ end
108
+ self.aasm_current_state = state
109
+ end
110
+
111
+ def aasm_current_state=(state)
112
+ if self.respond_to?(:aasm_write_state_without_persistence) || self.private_methods.include?('aasm_write_state_without_persistence')
113
+ aasm_write_state_without_persistence(state)
114
+ end
115
+ @aasm_current_state = state
116
+ end
117
+
118
+ def aasm_state_object_for_state(name)
119
+ self.class.aasm_states.find {|s| s == name}
120
+ end
121
+
122
+ def aasm_fire_event(name, persist, *args)
123
+ aasm_state_object_for_state(aasm_current_state).call_action(:exit, self)
124
+
125
+ new_state = self.class.aasm_events[name].fire(self, *args)
126
+
127
+ unless new_state.nil?
128
+ aasm_state_object_for_state(new_state).call_action(:enter, self)
129
+
130
+ if self.respond_to?(:aasm_event_fired)
131
+ self.aasm_event_fired(self.aasm_current_state, new_state)
132
+ end
133
+
134
+ if persist
135
+ self.aasm_current_state_with_persistence = new_state
136
+ self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
137
+ else
138
+ self.aasm_current_state = new_state
139
+ end
140
+
141
+ true
142
+ else
143
+ if self.respond_to?(:aasm_event_failed)
144
+ self.aasm_event_failed(name)
145
+ end
146
+
147
+ false
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,45 @@
1
+ require File.join(File.dirname(__FILE__), 'state_transition')
2
+
3
+ module AASM
4
+ module SupportingClasses
5
+ class Event
6
+ attr_reader :name, :success
7
+
8
+ def initialize(name, options = {}, &block)
9
+ @name = name
10
+ @success = options[:success]
11
+ @transitions = []
12
+ instance_eval(&block) if block
13
+ end
14
+
15
+ def fire(obj, *args)
16
+ transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
17
+ raise AASM::InvalidTransition, "Event '#{name}' cannot transition from '#{obj.aasm_current_state}'" if transitions.size == 0
18
+ to_state = args.shift if args[0].nil? || #nil check is for backwards compatability
19
+ (args[0].is_a?(Symbol) && obj.class.aasm_state_machine.states.include?(args[0]))
20
+
21
+ next_state = nil
22
+ transitions.each do |transition|
23
+ next if to_state and !Array(transition.to).include?(to_state)
24
+ if transition.perform(obj)
25
+ next_state = to_state || Array(transition.to).first
26
+ transition.execute(obj, *args)
27
+ break
28
+ end
29
+ end
30
+ next_state
31
+ end
32
+
33
+ def transitions_from_state?(state)
34
+ @transitions.any? { |t| t.from == state }
35
+ end
36
+
37
+ private
38
+ def transitions(trans_opts)
39
+ Array(trans_opts[:from]).each do |s|
40
+ @transitions << SupportingClasses::StateTransition.new(trans_opts.merge({:from => s.to_sym}))
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ module AASM
2
+ module Persistence
3
+
4
+ # Checks to see this class or any of it's superclasses inherit from
5
+ # ActiveRecord::Base and if so includes ActiveRecordPersistence
6
+ def self.set_persistence(base)
7
+ # Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
8
+ hierarchy = base.ancestors.map {|klass| klass.to_s}
9
+
10
+ if hierarchy.include?("ActiveRecord::Base")
11
+ require File.join(File.dirname(__FILE__), 'persistence', 'active_record_persistence')
12
+ base.send(:include, AASM::Persistence::ActiveRecordPersistence)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,238 @@
1
+ module AASM
2
+ module Persistence
3
+ module ActiveRecordPersistence
4
+ # This method:
5
+ #
6
+ # * extends the model with ClassMethods
7
+ # * includes InstanceMethods
8
+ #
9
+ # Unless the corresponding methods are already defined, it includes
10
+ # * ReadState
11
+ # * WriteState
12
+ # * WriteStateWithoutPersistence
13
+ #
14
+ # Adds
15
+ #
16
+ # before_validation_on_create :aasm_ensure_initial_state
17
+ #
18
+ # As a result, it doesn't matter when you define your methods - the following 2 are equivalent
19
+ #
20
+ # class Foo < ActiveRecord::Base
21
+ # def aasm_write_state(state)
22
+ # "bar"
23
+ # end
24
+ # include AASM
25
+ # end
26
+ #
27
+ # class Foo < ActiveRecord::Base
28
+ # include AASM
29
+ # def aasm_write_state(state)
30
+ # "bar"
31
+ # end
32
+ # end
33
+ #
34
+ def self.included(base)
35
+ base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
36
+ base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
37
+ base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
38
+ base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
39
+ base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
40
+
41
+ if base.respond_to?(:named_scope)
42
+ base.extend(AASM::Persistence::ActiveRecordPersistence::NamedScopeMethods)
43
+
44
+ base.class_eval do
45
+ class << self
46
+ alias_method :aasm_state_without_named_scope, :aasm_state
47
+ alias_method :aasm_state, :aasm_state_with_named_scope
48
+ end
49
+ end
50
+ end
51
+
52
+ base.before_validation_on_create :aasm_ensure_initial_state
53
+ end
54
+
55
+ module ClassMethods
56
+ # Maps to the aasm_column in the database. Deafults to "aasm_state". You can write:
57
+ #
58
+ # create_table :foos do |t|
59
+ # t.string :name
60
+ # t.string :aasm_state
61
+ # end
62
+ #
63
+ # class Foo < ActiveRecord::Base
64
+ # include AASM
65
+ # end
66
+ #
67
+ # OR:
68
+ #
69
+ # create_table :foos do |t|
70
+ # t.string :name
71
+ # t.string :status
72
+ # end
73
+ #
74
+ # class Foo < ActiveRecord::Base
75
+ # include AASM
76
+ # aasm_column :status
77
+ # end
78
+ #
79
+ # This method is both a getter and a setter
80
+ def aasm_column(column_name=nil)
81
+ if column_name
82
+ AASM::StateMachine[self].config.column = column_name.to_sym
83
+ # @aasm_column = column_name.to_sym
84
+ else
85
+ AASM::StateMachine[self].config.column ||= :aasm_state
86
+ # @aasm_column ||= :aasm_state
87
+ end
88
+ # @aasm_column
89
+ AASM::StateMachine[self].config.column
90
+ end
91
+
92
+ def find_in_state(number, state, *args)
93
+ with_state_scope state do
94
+ find(number, *args)
95
+ end
96
+ end
97
+
98
+ def count_in_state(state, *args)
99
+ with_state_scope state do
100
+ count(*args)
101
+ end
102
+ end
103
+
104
+ def calculate_in_state(state, *args)
105
+ with_state_scope state do
106
+ calculate(*args)
107
+ end
108
+ end
109
+
110
+ protected
111
+ def with_state_scope(state)
112
+ with_scope :find => {:conditions => ["#{table_name}.#{aasm_column} = ?", state.to_s]} do
113
+ yield if block_given?
114
+ end
115
+ end
116
+ end
117
+
118
+ module InstanceMethods
119
+
120
+ # Returns the current aasm_state of the object. Respects reload and
121
+ # any changes made to the aasm_state field directly
122
+ #
123
+ # Internally just calls <tt>aasm_read_state</tt>
124
+ #
125
+ # foo = Foo.find(1)
126
+ # foo.aasm_current_state # => :pending
127
+ # foo.aasm_state = "opened"
128
+ # foo.aasm_current_state # => :opened
129
+ # foo.close # => calls aasm_write_state_without_persistence
130
+ # foo.aasm_current_state # => :closed
131
+ # foo.reload
132
+ # foo.aasm_current_state # => :pending
133
+ #
134
+ def aasm_current_state
135
+ @current_state = aasm_read_state
136
+ end
137
+
138
+ private
139
+
140
+ # Ensures that if the aasm_state column is nil and the record is new
141
+ # that the initial state gets populated before validation on create
142
+ #
143
+ # foo = Foo.new
144
+ # foo.aasm_state # => nil
145
+ # foo.valid?
146
+ # foo.aasm_state # => "open" (where :open is the initial state)
147
+ #
148
+ #
149
+ # foo = Foo.find(:first)
150
+ # foo.aasm_state # => 1
151
+ # foo.aasm_state = nil
152
+ # foo.valid?
153
+ # foo.aasm_state # => nil
154
+ #
155
+ def aasm_ensure_initial_state
156
+ send("#{self.class.aasm_column}=", self.aasm_current_state.to_s)
157
+ end
158
+
159
+ end
160
+
161
+ module WriteStateWithoutPersistence
162
+ # Writes <tt>state</tt> to the state column, but does not persist it to the database
163
+ #
164
+ # foo = Foo.find(1)
165
+ # foo.aasm_current_state # => :opened
166
+ # foo.close
167
+ # foo.aasm_current_state # => :closed
168
+ # Foo.find(1).aasm_current_state # => :opened
169
+ # foo.save
170
+ # foo.aasm_current_state # => :closed
171
+ # Foo.find(1).aasm_current_state # => :closed
172
+ #
173
+ # NOTE: intended to be called from an event
174
+ def aasm_write_state_without_persistence(state)
175
+ write_attribute(self.class.aasm_column, state.to_s)
176
+ end
177
+ end
178
+
179
+ module WriteState
180
+ # Writes <tt>state</tt> to the state column and persists it to the database
181
+ # using update_attribute (which bypasses validation)
182
+ #
183
+ # foo = Foo.find(1)
184
+ # foo.aasm_current_state # => :opened
185
+ # foo.close!
186
+ # foo.aasm_current_state # => :closed
187
+ # Foo.find(1).aasm_current_state # => :closed
188
+ #
189
+ # NOTE: intended to be called from an event
190
+ def aasm_write_state(state)
191
+ update_attribute(self.class.aasm_column, state.to_s)
192
+ end
193
+ end
194
+
195
+ module ReadState
196
+
197
+ # Returns the value of the aasm_column - called from <tt>aasm_current_state</tt>
198
+ #
199
+ # If it's a new record, and the aasm state column is blank it returns the initial state:
200
+ #
201
+ # class Foo < ActiveRecord::Base
202
+ # include AASM
203
+ # aasm_column :status
204
+ # aasm_state :opened
205
+ # aasm_state :closed
206
+ # end
207
+ #
208
+ # foo = Foo.new
209
+ # foo.current_state # => :opened
210
+ # foo.close
211
+ # foo.current_state # => :closed
212
+ #
213
+ # foo = Foo.find(1)
214
+ # foo.current_state # => :opened
215
+ # foo.aasm_state = nil
216
+ # foo.current_state # => nil
217
+ #
218
+ # NOTE: intended to be called from an event
219
+ #
220
+ # This allows for nil aasm states - be sure to add validation to your model
221
+ def aasm_read_state
222
+ if new_record?
223
+ send(self.class.aasm_column).blank? ? self.class.aasm_initial_state : send(self.class.aasm_column).to_sym
224
+ else
225
+ send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
226
+ end
227
+ end
228
+ end
229
+
230
+ module NamedScopeMethods
231
+ def aasm_state_with_named_scope name, options = {}
232
+ aasm_state_without_named_scope name, options
233
+ self.named_scope name, :conditions => {self.aasm_column => name.to_s} unless self.scopes.include?(name)
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end