branston 0.3.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/branston/app/controllers/scenarios_controller.rb +6 -5
- data/lib/branston/app/controllers/stories_controller.rb +101 -89
- data/lib/branston/app/models/story.rb +30 -1
- data/lib/branston/app/models/user.rb +4 -0
- data/lib/branston/app/views/iterations/index.html.erb +1 -1
- data/lib/branston/app/views/layouts/_header.html.erb +1 -2
- data/lib/branston/app/views/scenarios/_scenario.html.erb +6 -3
- data/lib/branston/app/views/scenarios/_scenarios.html.erb +4 -2
- data/lib/branston/app/views/stories/_form.html.erb +15 -4
- data/lib/branston/app/views/stories/_story.html.erb +26 -6
- data/lib/branston/app/views/stories/edit.html.erb +3 -3
- data/lib/branston/app/views/stories/index.html.erb +22 -3
- data/lib/branston/app/views/stories/new.html.erb +2 -2
- data/lib/branston/app/views/stories/show.html.erb +3 -3
- data/lib/branston/config/routes.rb +7 -4
- data/lib/branston/coverage/app-controllers-application_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-iterations_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-outcomes_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-preconditions_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-releases_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-scenarios_controller_rb.html +18 -12
- data/lib/branston/coverage/app-controllers-sessions_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-stories_controller_rb.html +193 -121
- data/lib/branston/coverage/app-controllers-user_roles_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-users_controller_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-application_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-iterations_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-outcomes_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-preconditions_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-releases_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-sessions_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-stories_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-user_roles_helper_rb.html +1 -1
- data/lib/branston/coverage/app-models-iteration_rb.html +1 -1
- data/lib/branston/coverage/app-models-outcome_rb.html +1 -1
- data/lib/branston/coverage/app-models-participation_rb.html +1 -1
- data/lib/branston/coverage/app-models-precondition_rb.html +1 -1
- data/lib/branston/coverage/app-models-release_rb.html +1 -1
- data/lib/branston/coverage/app-models-scenario_rb.html +1 -1
- data/lib/branston/coverage/app-models-story_rb.html +192 -18
- data/lib/branston/coverage/app-models-user_rb.html +33 -9
- data/lib/branston/coverage/app-models-user_role_rb.html +1 -1
- data/lib/branston/coverage/index.html +13 -13
- data/lib/branston/coverage/lib-client_rb.html +1 -1
- data/lib/branston/coverage/lib-faker_extras_rb.html +1 -1
- data/lib/branston/coverage/lib-story_generator_rb.html +1 -1
- data/lib/branston/db/development.sqlite3 +0 -0
- data/lib/branston/db/migrate/20091223100903_add_status_to_story.rb +11 -0
- data/lib/branston/db/pristine.sqlite3 +0 -0
- data/lib/branston/db/schema.rb +5 -3
- data/lib/branston/db/test.sqlite3 +0 -0
- data/lib/branston/lib/branston.rb +4 -2
- data/lib/branston/log/development.log +4970 -0
- data/lib/branston/log/test.log +88225 -0
- data/lib/branston/test/blueprints.rb +10 -7
- data/lib/branston/test/functional/scenarios_controller_test.rb +22 -15
- data/lib/branston/test/functional/stories_controller_test.rb +51 -30
- data/lib/branston/test/unit/story_test.rb +47 -7
- data/lib/branston/test/unit/user_test.rb +4 -0
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_flat.txt +3 -2
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_graph.html +2041 -1307
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_tree.txt +7922 -7922
- data/lib/branston/vendor/plugins/state_machine/CHANGELOG.rdoc +298 -0
- data/lib/branston/vendor/plugins/state_machine/LICENSE +20 -0
- data/lib/branston/vendor/plugins/state_machine/README.rdoc +466 -0
- data/lib/branston/vendor/plugins/state_machine/Rakefile +98 -0
- data/lib/branston/vendor/plugins/state_machine/examples/AutoShop_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/Car_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/TrafficLight_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/Vehicle_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/auto_shop.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/examples/car.rb +19 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/controller.rb +51 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/model.rb +28 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_edit.html.erb +24 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_index.html.erb +23 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_new.html.erb +13 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_show.html.erb +17 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/controller.rb +43 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/migration.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/model.rb +23 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_edit.html.erb +25 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_index.html.erb +23 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_new.html.erb +14 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_show.html.erb +17 -0
- data/lib/branston/vendor/plugins/state_machine/examples/traffic_light.rb +7 -0
- data/lib/branston/vendor/plugins/state_machine/examples/vehicle.rb +31 -0
- data/lib/branston/vendor/plugins/state_machine/init.rb +1 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/assertions.rb +36 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/callback.rb +189 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/condition_proxy.rb +94 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/eval_helpers.rb +67 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event.rb +252 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event_collection.rb +122 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/extensions.rb +149 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/guard.rb +230 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/locale.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb +41 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record.rb +492 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper/observer.rb +139 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper.rb +351 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/sequel.rb +322 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations.rb +68 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine.rb +1467 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine_collection.rb +155 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher.rb +123 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher_helpers.rb +54 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/node_collection.rb +152 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state.rb +249 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state_collection.rb +112 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/transition.rb +394 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine.rb +388 -0
- data/lib/branston/vendor/plugins/state_machine/state_machine.gemspec +30 -0
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rake +1 -0
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rb +30 -0
- data/lib/branston/vendor/plugins/state_machine/test/classes/switch.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/test/functional/state_machine_test.rb +941 -0
- data/lib/branston/vendor/plugins/state_machine/test/test_helper.rb +4 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/assertions_test.rb +40 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/callback_test.rb +455 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/condition_proxy_test.rb +328 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/eval_helpers_test.rb +120 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_collection_test.rb +326 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_test.rb +743 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/guard_test.rb +908 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/active_record_test.rb +1367 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/data_mapper_test.rb +962 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/sequel_test.rb +859 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations_test.rb +42 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_event_test.rb +7 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_transition_test.rb +7 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb +938 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_test.rb +2004 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_helpers_test.rb +37 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_test.rb +155 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/node_collection_test.rb +207 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_collection_test.rb +280 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_machine_test.rb +31 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_test.rb +795 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/transition_test.rb +1212 -0
- metadata +81 -2
data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record.rb
ADDED
@@ -0,0 +1,492 @@
|
|
1
|
+
module StateMachine
|
2
|
+
module Integrations #:nodoc:
|
3
|
+
# Adds support for integrating state machines with ActiveRecord models.
|
4
|
+
#
|
5
|
+
# == Examples
|
6
|
+
#
|
7
|
+
# Below is an example of a simple state machine defined within an
|
8
|
+
# ActiveRecord model:
|
9
|
+
#
|
10
|
+
# class Vehicle < ActiveRecord::Base
|
11
|
+
# state_machine :initial => :parked do
|
12
|
+
# event :ignite do
|
13
|
+
# transition :parked => :idling
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# The examples in the sections below will use the above class as a
|
19
|
+
# reference.
|
20
|
+
#
|
21
|
+
# == Actions
|
22
|
+
#
|
23
|
+
# By default, the action that will be invoked when a state is transitioned
|
24
|
+
# is the +save+ action. This will cause the record to save the changes
|
25
|
+
# made to the state machine's attribute. *Note* that if any other changes
|
26
|
+
# were made to the record prior to transition, then those changes will
|
27
|
+
# be saved as well.
|
28
|
+
#
|
29
|
+
# For example,
|
30
|
+
#
|
31
|
+
# vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
32
|
+
# vehicle.name = 'Ford Explorer'
|
33
|
+
# vehicle.ignite # => true
|
34
|
+
# vehicle.reload # => #<Vehicle id: 1, name: "Ford Explorer", state: "idling">
|
35
|
+
#
|
36
|
+
# == Events
|
37
|
+
#
|
38
|
+
# As described in StateMachine::InstanceMethods#state_machine, event
|
39
|
+
# attributes are created for every machine that allow transitions to be
|
40
|
+
# performed automatically when the object's action (in this case, :save)
|
41
|
+
# is called.
|
42
|
+
#
|
43
|
+
# In ActiveRecord, these automated events are run in the following order:
|
44
|
+
# * before validation - Run before callbacks and persist new states, then validate
|
45
|
+
# * before save - If validation was skipped, run before callbacks and persist new states, then save
|
46
|
+
# * after save - Run after callbacks
|
47
|
+
#
|
48
|
+
# For example,
|
49
|
+
#
|
50
|
+
# vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
51
|
+
# vehicle.state_event # => nil
|
52
|
+
# vehicle.state_event = 'invalid'
|
53
|
+
# vehicle.valid? # => false
|
54
|
+
# vehicle.errors.full_messages # => ["State event is invalid"]
|
55
|
+
#
|
56
|
+
# vehicle.state_event = 'ignite'
|
57
|
+
# vehicle.valid? # => true
|
58
|
+
# vehicle.save # => true
|
59
|
+
# vehicle.state # => "idling"
|
60
|
+
# vehicle.state_event # => nil
|
61
|
+
#
|
62
|
+
# Note that this can also be done on a mass-assignment basis:
|
63
|
+
#
|
64
|
+
# vehicle = Vehicle.create(:state_event => 'ignite') # => #<Vehicle id: 1, name: nil, state: "idling">
|
65
|
+
# vehicle.state # => "idling"
|
66
|
+
#
|
67
|
+
# === Security implications
|
68
|
+
#
|
69
|
+
# Beware that public event attributes mean that events can be fired
|
70
|
+
# whenever mass-assignment is being used. If you want to prevent malicious
|
71
|
+
# users from tampering with events through URLs / forms, the attribute
|
72
|
+
# should be protected like so:
|
73
|
+
#
|
74
|
+
# class Vehicle < ActiveRecord::Base
|
75
|
+
# attr_protected :state_event
|
76
|
+
# # attr_accessible ... # Alternative technique
|
77
|
+
#
|
78
|
+
# state_machine do
|
79
|
+
# ...
|
80
|
+
# end
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# If you want to only have *some* events be able to fire via mass-assignment,
|
84
|
+
# you can build two state machines (one public and one protected) like so:
|
85
|
+
#
|
86
|
+
# class Vehicle < ActiveRecord::Base
|
87
|
+
# attr_protected :state_event # Prevent access to events in the first machine
|
88
|
+
#
|
89
|
+
# state_machine do
|
90
|
+
# # Define private events here
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# # Public machine targets the same state as the private machine
|
94
|
+
# state_machine :public_state, :attribute => :state do
|
95
|
+
# # Define public events here
|
96
|
+
# end
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# == Transactions
|
100
|
+
#
|
101
|
+
# In order to ensure that any changes made during transition callbacks
|
102
|
+
# are rolled back during a failed attempt, every transition is wrapped
|
103
|
+
# within a transaction.
|
104
|
+
#
|
105
|
+
# For example,
|
106
|
+
#
|
107
|
+
# class Message < ActiveRecord::Base
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# Vehicle.state_machine do
|
111
|
+
# before_transition do |vehicle, transition|
|
112
|
+
# Message.create(:content => transition.inspect)
|
113
|
+
# false
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
#
|
117
|
+
# vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
118
|
+
# vehicle.ignite # => false
|
119
|
+
# Message.count # => 0
|
120
|
+
#
|
121
|
+
# *Note* that only before callbacks that halt the callback chain and
|
122
|
+
# failed attempts to save the record will result in the transaction being
|
123
|
+
# rolled back. If an after callback halts the chain, the previous result
|
124
|
+
# still applies and the transaction is *not* rolled back.
|
125
|
+
#
|
126
|
+
# To turn off transactions:
|
127
|
+
#
|
128
|
+
# class Vehicle < ActiveRecord::Base
|
129
|
+
# state_machine :initial => :parked, :use_transactions => false do
|
130
|
+
# ...
|
131
|
+
# end
|
132
|
+
# end
|
133
|
+
#
|
134
|
+
# == Validation errors
|
135
|
+
#
|
136
|
+
# If an event fails to successfully fire because there are no matching
|
137
|
+
# transitions for the current record, a validation error is added to the
|
138
|
+
# record's state attribute to help in determining why it failed and for
|
139
|
+
# reporting via the UI.
|
140
|
+
#
|
141
|
+
# For example,
|
142
|
+
#
|
143
|
+
# vehicle = Vehicle.create(:state => 'idling') # => #<Vehicle id: 1, name: nil, state: "idling">
|
144
|
+
# vehicle.ignite # => false
|
145
|
+
# vehicle.errors.full_messages # => ["State cannot transition via \"ignite\""]
|
146
|
+
#
|
147
|
+
# If an event fails to fire because of a validation error on the record and
|
148
|
+
# *not* because a matching transition was not available, no error messages
|
149
|
+
# will be added to the state attribute.
|
150
|
+
#
|
151
|
+
# == Scopes
|
152
|
+
#
|
153
|
+
# To assist in filtering models with specific states, a series of named
|
154
|
+
# scopes are defined on the model for finding records with or without a
|
155
|
+
# particular set of states.
|
156
|
+
#
|
157
|
+
# These named scopes are essentially the functional equivalent of the
|
158
|
+
# following definitions:
|
159
|
+
#
|
160
|
+
# class Vehicle < ActiveRecord::Base
|
161
|
+
# named_scope :with_states, lambda {|*states| {:conditions => {:state => states}}}
|
162
|
+
# # with_states also aliased to with_state
|
163
|
+
#
|
164
|
+
# named_scope :without_states, lambda {|*states| {:conditions => ['state NOT IN (?)', states]}}
|
165
|
+
# # without_states also aliased to without_state
|
166
|
+
# end
|
167
|
+
#
|
168
|
+
# *Note*, however, that the states are converted to their stored values
|
169
|
+
# before being passed into the query.
|
170
|
+
#
|
171
|
+
# Because of the way named scopes work in ActiveRecord, they can be
|
172
|
+
# chained like so:
|
173
|
+
#
|
174
|
+
# Vehicle.with_state(:parked).all(:order => 'id DESC')
|
175
|
+
#
|
176
|
+
# == Callbacks
|
177
|
+
#
|
178
|
+
# All before/after transition callbacks defined for ActiveRecord models
|
179
|
+
# behave in the same way that other ActiveRecord callbacks behave. The
|
180
|
+
# object involved in the transition is passed in as an argument.
|
181
|
+
#
|
182
|
+
# For example,
|
183
|
+
#
|
184
|
+
# class Vehicle < ActiveRecord::Base
|
185
|
+
# state_machine :initial => :parked do
|
186
|
+
# before_transition any => :idling do |vehicle|
|
187
|
+
# vehicle.put_on_seatbelt
|
188
|
+
# end
|
189
|
+
#
|
190
|
+
# before_transition do |vehicle, transition|
|
191
|
+
# # log message
|
192
|
+
# end
|
193
|
+
#
|
194
|
+
# event :ignite do
|
195
|
+
# transition :parked => :idling
|
196
|
+
# end
|
197
|
+
# end
|
198
|
+
#
|
199
|
+
# def put_on_seatbelt
|
200
|
+
# ...
|
201
|
+
# end
|
202
|
+
# end
|
203
|
+
#
|
204
|
+
# Note, also, that the transition can be accessed by simply defining
|
205
|
+
# additional arguments in the callback block.
|
206
|
+
#
|
207
|
+
# == Observers
|
208
|
+
#
|
209
|
+
# In addition to support for ActiveRecord-like hooks, there is additional
|
210
|
+
# support for ActiveRecord observers. Because of the way ActiveRecord
|
211
|
+
# observers are designed, there is less flexibility around the specific
|
212
|
+
# transitions that can be hooked in. However, a large number of hooks
|
213
|
+
# *are* supported. For example, if a transition for a record's +state+
|
214
|
+
# attribute changes the state from +parked+ to +idling+ via the +ignite+
|
215
|
+
# event, the following observer methods are supported:
|
216
|
+
# * before/after_ignite_from_parked_to_idling
|
217
|
+
# * before/after_ignite_from_parked
|
218
|
+
# * before/after_ignite_to_idling
|
219
|
+
# * before/after_ignite
|
220
|
+
# * before/after_transition_state_from_parked_to_idling
|
221
|
+
# * before/after_transition_state_from_parked
|
222
|
+
# * before/after_transition_state_to_idling
|
223
|
+
# * before/after_transition_state
|
224
|
+
# * before/after_transition
|
225
|
+
#
|
226
|
+
# The following class shows an example of some of these hooks:
|
227
|
+
#
|
228
|
+
# class VehicleObserver < ActiveRecord::Observer
|
229
|
+
# def before_save(vehicle)
|
230
|
+
# # log message
|
231
|
+
# end
|
232
|
+
#
|
233
|
+
# # Callback for :ignite event *before* the transition is performed
|
234
|
+
# def before_ignite(vehicle, transition)
|
235
|
+
# # log message
|
236
|
+
# end
|
237
|
+
#
|
238
|
+
# # Callback for :ignite event *after* the transition has been performed
|
239
|
+
# def after_ignite(vehicle, transition)
|
240
|
+
# # put on seatbelt
|
241
|
+
# end
|
242
|
+
#
|
243
|
+
# # Generic transition callback *before* the transition is performed
|
244
|
+
# def after_transition(vehicle, transition)
|
245
|
+
# Audit.log(vehicle, transition)
|
246
|
+
# end
|
247
|
+
# end
|
248
|
+
#
|
249
|
+
# More flexible transition callbacks can be defined directly within the
|
250
|
+
# model as described in StateMachine::Machine#before_transition
|
251
|
+
# and StateMachine::Machine#after_transition.
|
252
|
+
#
|
253
|
+
# To define a single observer for multiple state machines:
|
254
|
+
#
|
255
|
+
# class StateMachineObserver < ActiveRecord::Observer
|
256
|
+
# observe Vehicle, Switch, Project
|
257
|
+
#
|
258
|
+
# def after_transition(record, transition)
|
259
|
+
# Audit.log(record, transition)
|
260
|
+
# end
|
261
|
+
# end
|
262
|
+
module ActiveRecord
|
263
|
+
# The default options to use for state machines using this integration
|
264
|
+
class << self; attr_reader :defaults; end
|
265
|
+
@defaults = {:action => :save}
|
266
|
+
|
267
|
+
# Should this integration be used for state machines in the given class?
|
268
|
+
# Classes that inherit from ActiveRecord::Base will automatically use
|
269
|
+
# the ActiveRecord integration.
|
270
|
+
def self.matches?(klass)
|
271
|
+
defined?(::ActiveRecord::Base) && klass <= ::ActiveRecord::Base
|
272
|
+
end
|
273
|
+
|
274
|
+
# Loads additional files specific to ActiveRecord
|
275
|
+
def self.extended(base) #:nodoc:
|
276
|
+
require 'state_machine/integrations/active_record/observer'
|
277
|
+
|
278
|
+
if Object.const_defined?(:I18n)
|
279
|
+
locale = "#{File.dirname(__FILE__)}/active_record/locale.rb"
|
280
|
+
I18n.load_path << locale unless I18n.load_path.include?(locale)
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
# Forces the change in state to be recognized regardless of whether the
|
285
|
+
# state value actually changed
|
286
|
+
def write(object, attribute, value)
|
287
|
+
result = super
|
288
|
+
object.send("#{self.attribute}_will_change!") if attribute == :state && object.respond_to?("#{self.attribute}_will_change!")
|
289
|
+
result
|
290
|
+
end
|
291
|
+
|
292
|
+
# Adds a validation error to the given object
|
293
|
+
def invalidate(object, attribute, message, values = [])
|
294
|
+
attribute = self.attribute(attribute)
|
295
|
+
|
296
|
+
if Object.const_defined?(:I18n)
|
297
|
+
options = values.inject({}) {|options, (key, value)| options[key] = value; options}
|
298
|
+
object.errors.add(attribute, message, options.merge(
|
299
|
+
:default => @messages[message]
|
300
|
+
))
|
301
|
+
else
|
302
|
+
object.errors.add(attribute, generate_message(message, values))
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
# Resets any errors previously added when invalidating the given object
|
307
|
+
def reset(object)
|
308
|
+
object.errors.clear
|
309
|
+
end
|
310
|
+
|
311
|
+
protected
|
312
|
+
# Adds the default callbacks for notifying ActiveRecord observers
|
313
|
+
# before/after a transition has been performed.
|
314
|
+
def after_initialize
|
315
|
+
callbacks[:before] << Callback.new {|object, transition| notify(:before, object, transition)}
|
316
|
+
callbacks[:after] << Callback.new {|object, transition| notify(:after, object, transition)}
|
317
|
+
end
|
318
|
+
|
319
|
+
# Defines an initialization hook into the owner class for setting the
|
320
|
+
# initial state of the machine *before* any attributes are set on the
|
321
|
+
# object
|
322
|
+
def define_state_initializer
|
323
|
+
@instance_helper_module.class_eval <<-end_eval, __FILE__, __LINE__
|
324
|
+
# Ensure that the attributes setter gets used to force initialization
|
325
|
+
# of the state machines
|
326
|
+
def initialize(attributes = nil, *args)
|
327
|
+
attributes ||= {}
|
328
|
+
super
|
329
|
+
end
|
330
|
+
|
331
|
+
# Hooks in to attribute initialization to set the states *prior*
|
332
|
+
# to the attributes being set
|
333
|
+
def attributes=(new_attributes, *args)
|
334
|
+
if new_record? && !@initialized_state_machines
|
335
|
+
@initialized_state_machines = true
|
336
|
+
|
337
|
+
if new_attributes
|
338
|
+
attributes = new_attributes.dup
|
339
|
+
attributes.stringify_keys!
|
340
|
+
ignore = remove_attributes_protected_from_mass_assignment(attributes).keys
|
341
|
+
else
|
342
|
+
ignore = []
|
343
|
+
end
|
344
|
+
|
345
|
+
initialize_state_machines(:dynamic => false, :ignore => ignore)
|
346
|
+
super
|
347
|
+
initialize_state_machines(:dynamic => true, :ignore => ignore)
|
348
|
+
else
|
349
|
+
super
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end_eval
|
353
|
+
end
|
354
|
+
|
355
|
+
# Skips defining reader/writer methods since this is done automatically
|
356
|
+
def define_state_accessor
|
357
|
+
name = self.name
|
358
|
+
|
359
|
+
owner_class.validates_each(attribute) do |record, attr, value|
|
360
|
+
machine = record.class.state_machine(name)
|
361
|
+
machine.invalidate(record, :state, :invalid) unless machine.states.match(record)
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
# Adds support for defining the attribute predicate, while providing
|
366
|
+
# compatibility with the default predicate which determines whether
|
367
|
+
# *anything* is set for the attribute's value
|
368
|
+
def define_state_predicate
|
369
|
+
name = self.name
|
370
|
+
|
371
|
+
# Still use class_eval here instance of define_instance_method since
|
372
|
+
# we need to be able to call +super+
|
373
|
+
@instance_helper_module.class_eval do
|
374
|
+
define_method("#{name}?") do |*args|
|
375
|
+
args.empty? ? super(*args) : self.class.state_machine(name).states.matches?(self, *args)
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
# Adds hooks into validation for automatically firing events
|
381
|
+
def define_action_helpers
|
382
|
+
if action == :save
|
383
|
+
if super(:create_or_update)
|
384
|
+
@instance_helper_module.class_eval do
|
385
|
+
define_method(:valid?) do |*args|
|
386
|
+
self.class.state_machines.fire_event_attributes(self, :save, false) { super(*args) }
|
387
|
+
end
|
388
|
+
end
|
389
|
+
end
|
390
|
+
else
|
391
|
+
super
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
# Creates a scope for finding records *with* a particular state or
|
396
|
+
# states for the attribute
|
397
|
+
def create_with_scope(name)
|
398
|
+
attribute = self.attribute
|
399
|
+
define_scope(name, lambda {|values| {:conditions => {attribute => values}}})
|
400
|
+
end
|
401
|
+
|
402
|
+
# Creates a scope for finding records *without* a particular state or
|
403
|
+
# states for the attribute
|
404
|
+
def create_without_scope(name)
|
405
|
+
attribute = self.attribute
|
406
|
+
define_scope(name, lambda {|values| {:conditions => ["#{attribute} NOT IN (?)", values]}})
|
407
|
+
end
|
408
|
+
|
409
|
+
# Runs a new database transaction, rolling back any changes by raising
|
410
|
+
# an ActiveRecord::Rollback exception if the yielded block fails
|
411
|
+
# (i.e. returns false).
|
412
|
+
def transaction(object)
|
413
|
+
object.class.transaction {raise ::ActiveRecord::Rollback unless yield}
|
414
|
+
end
|
415
|
+
|
416
|
+
# Creates a new callback in the callback chain, always inserting it
|
417
|
+
# before the default Observer callbacks that were created after
|
418
|
+
# initialization.
|
419
|
+
def add_callback(type, options, &block)
|
420
|
+
options[:terminator] = @terminator ||= lambda {|result| result == false}
|
421
|
+
@callbacks[type].insert(-2, callback = Callback.new(options, &block))
|
422
|
+
add_states(callback.known_states)
|
423
|
+
|
424
|
+
callback
|
425
|
+
end
|
426
|
+
|
427
|
+
private
|
428
|
+
# Defines a new named scope with the given name. Since ActiveRecord
|
429
|
+
# does not allow direct access to the model being used within the
|
430
|
+
# evaluation of a dynamic named scope, the scope must be generated
|
431
|
+
# manually. It's necessary to have access to the model so that the
|
432
|
+
# state names can be translated to their associated values and so that
|
433
|
+
# inheritance is respected properly.
|
434
|
+
def define_scope(name, scope)
|
435
|
+
if owner_class.respond_to?(:named_scope)
|
436
|
+
name = name.to_sym
|
437
|
+
machine_name = self.name
|
438
|
+
|
439
|
+
# Create the scope and then override it with state translation
|
440
|
+
owner_class.named_scope(name)
|
441
|
+
owner_class.scopes[name] = lambda do |klass, *states|
|
442
|
+
machine_states = klass.state_machine(machine_name).states
|
443
|
+
values = states.flatten.map {|state| machine_states.fetch(state).value}
|
444
|
+
|
445
|
+
::ActiveRecord::NamedScope::Scope.new(klass, scope.call(values))
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
# Prevent the Machine class from wrapping the scope
|
450
|
+
false
|
451
|
+
end
|
452
|
+
|
453
|
+
# Notifies observers on the given object that a callback occurred
|
454
|
+
# involving the given transition. This will attempt to call the
|
455
|
+
# following methods on observers:
|
456
|
+
# * #{type}_#{qualified_event}_from_#{from}_to_#{to}
|
457
|
+
# * #{type}_#{qualified_event}_from_#{from}
|
458
|
+
# * #{type}_#{qualified_event}_to_#{to}
|
459
|
+
# * #{type}_#{qualified_event}
|
460
|
+
# * #{type}_transition_#{machine_name}_from_#{from}_to_#{to}
|
461
|
+
# * #{type}_transition_#{machine_name}_from_#{from}
|
462
|
+
# * #{type}_transition_#{machine_name}_to_#{to}
|
463
|
+
# * #{type}_transition_#{machine_name}
|
464
|
+
# * #{type}_transition
|
465
|
+
#
|
466
|
+
# This will always return true regardless of the results of the
|
467
|
+
# callbacks.
|
468
|
+
def notify(type, object, transition)
|
469
|
+
name = self.name
|
470
|
+
event = transition.qualified_event
|
471
|
+
from = transition.from_name
|
472
|
+
to = transition.to_name
|
473
|
+
|
474
|
+
# Machine-specific updates
|
475
|
+
["#{type}_#{event}", "#{type}_transition_#{name}"].each do |event_segment|
|
476
|
+
["_from_#{from}", nil].each do |from_segment|
|
477
|
+
["_to_#{to}", nil].each do |to_segment|
|
478
|
+
object.class.changed
|
479
|
+
object.class.notify_observers([event_segment, from_segment, to_segment].join, object, transition)
|
480
|
+
end
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
# Generic updates
|
485
|
+
object.class.changed
|
486
|
+
object.class.notify_observers("#{type}_transition", object, transition)
|
487
|
+
|
488
|
+
true
|
489
|
+
end
|
490
|
+
end
|
491
|
+
end
|
492
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
module StateMachine
|
2
|
+
module Integrations #:nodoc:
|
3
|
+
module DataMapper
|
4
|
+
# Adds support for creating before/after transition callbacks within a
|
5
|
+
# DataMapper observer. These callbacks behave very similar to
|
6
|
+
# before/after hooks during save/update/destroy/etc., but with the
|
7
|
+
# following modifications:
|
8
|
+
# * Each callback can define a set of transition conditions (i.e. guards)
|
9
|
+
# that must be met in order for the callback to get invoked.
|
10
|
+
# * An additional transition parameter is available that provides
|
11
|
+
# contextual information about the event (see StateMachine::Transition
|
12
|
+
# for more information)
|
13
|
+
#
|
14
|
+
# To define a single observer for multiple state machines:
|
15
|
+
#
|
16
|
+
# class StateMachineObserver
|
17
|
+
# include DataMapper::Observer
|
18
|
+
#
|
19
|
+
# observe Vehicle, Switch, Project
|
20
|
+
#
|
21
|
+
# after_transition do |transition|
|
22
|
+
# Audit.log(self, transition)
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# == Requirements
|
27
|
+
#
|
28
|
+
# To use this feature of the DataMapper integration, the dm-observer library
|
29
|
+
# must be available. This can be installed either directly or indirectly
|
30
|
+
# through dm-more. When loading DataMapper, be sure to load the dm-observer
|
31
|
+
# library as well like so:
|
32
|
+
#
|
33
|
+
# require 'rubygems'
|
34
|
+
# require 'dm-core'
|
35
|
+
# require 'dm-observer'
|
36
|
+
#
|
37
|
+
# If dm-observer is not available, then this feature will be skipped.
|
38
|
+
module Observer
|
39
|
+
include MatcherHelpers
|
40
|
+
|
41
|
+
# Creates a callback that will be invoked *before* a transition is
|
42
|
+
# performed, so long as the given configuration options match the
|
43
|
+
# transition. Each part of the transition (event, to state, from state)
|
44
|
+
# must match in order for the callback to get invoked.
|
45
|
+
#
|
46
|
+
# See StateMachine::Machine#before_transition for more
|
47
|
+
# information about the various configuration options available.
|
48
|
+
#
|
49
|
+
# == Examples
|
50
|
+
#
|
51
|
+
# class Vehicle
|
52
|
+
# include DataMapper::Resource
|
53
|
+
#
|
54
|
+
# property :id, Serial
|
55
|
+
# property :state, :String
|
56
|
+
#
|
57
|
+
# state_machine :initial => :parked do
|
58
|
+
# event :ignite do
|
59
|
+
# transition :parked => :idling
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# class VehicleObserver
|
65
|
+
# include DataMapper::Observer
|
66
|
+
#
|
67
|
+
# observe Vehicle
|
68
|
+
#
|
69
|
+
# before :save do
|
70
|
+
# # log message
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# # Target all state machines
|
74
|
+
# before_transition :parked => :idling, :on => :ignite do
|
75
|
+
# # put on seatbelt
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# # Target a specific state machine
|
79
|
+
# before_transition :state, any => :idling do
|
80
|
+
# # put on seatbelt
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# # Target all state machines without requirements
|
84
|
+
# before_transition do |transition|
|
85
|
+
# # log message
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# *Note* that in each of the above +before_transition+ callbacks, the
|
90
|
+
# callback is executed within the context of the object (i.e. the
|
91
|
+
# Vehicle instance being transition). This means that +self+ refers
|
92
|
+
# to the vehicle record within each callback block.
|
93
|
+
def before_transition(*args, &block)
|
94
|
+
add_transition_callback(:before, *args, &block)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Creates a callback that will be invoked *after* a transition is
|
98
|
+
# performed so long as the given configuration options match the
|
99
|
+
# transition.
|
100
|
+
#
|
101
|
+
# See +before_transition+ for a description of the possible configurations
|
102
|
+
# for defining callbacks.
|
103
|
+
def after_transition(*args, &block)
|
104
|
+
add_transition_callback(:after, *args, &block)
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
# Adds the transition callback to a specific machine or all of the
|
109
|
+
# state machines for each observed class.
|
110
|
+
def add_transition_callback(type, *args, &block)
|
111
|
+
if args.any? && !args.first.is_a?(Hash)
|
112
|
+
# Specific machine(s) being targeted
|
113
|
+
names = args
|
114
|
+
args = args.last.is_a?(Hash) ? [args.pop] : []
|
115
|
+
else
|
116
|
+
# Target all state machines
|
117
|
+
names = nil
|
118
|
+
end
|
119
|
+
|
120
|
+
# Add the transition callback to each class being observed
|
121
|
+
observing.each do |klass|
|
122
|
+
state_machines =
|
123
|
+
if names
|
124
|
+
names.map {|name| klass.state_machines.fetch(name)}
|
125
|
+
else
|
126
|
+
klass.state_machines.values
|
127
|
+
end
|
128
|
+
|
129
|
+
state_machines.each {|machine| machine.send("#{type}_transition", *args, &block)}
|
130
|
+
end if observing
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
DataMapper::Observer::ClassMethods.class_eval do
|
138
|
+
include StateMachine::Integrations::DataMapper::Observer
|
139
|
+
end
|