@@ -189,6 +189,64 @@ module Integrations #:nodoc:
189189 # Note, also, that the transition can be accessed by simply defining
190190 # additional arguments in the callback block.
191191 #
192+ # == Observers
193+ #
194+ # In order to hook in observer support for your application, the
195+ # ActiveModel::Observing feature must be included. This can be added by including the
196+ # https://github.com/state-machines/state_machines-activemodel-observers gem in your
197+ # Gemfile. Because of the way
198+ # ActiveModel observers are designed, there is less flexibility around the
199+ # specific transitions that can be hooked in. However, a large number of
200+ # hooks *are* supported. For example, if a transition for a object's
201+ # +state+ attribute changes the state from +parked+ to +idling+ via the
202+ # +ignite+ event, the following observer methods are supported:
203+ # * before/after/after_failure_to-_ignite_from_parked_to_idling
204+ # * before/after/after_failure_to-_ignite_from_parked
205+ # * before/after/after_failure_to-_ignite_to_idling
206+ # * before/after/after_failure_to-_ignite
207+ # * before/after/after_failure_to-_transition_state_from_parked_to_idling
208+ # * before/after/after_failure_to-_transition_state_from_parked
209+ # * before/after/after_failure_to-_transition_state_to_idling
210+ # * before/after/after_failure_to-_transition_state
211+ # * before/after/after_failure_to-_transition
212+ #
213+ # The following class shows an example of some of these hooks:
214+ #
215+ # class VehicleObserver < ActiveModel::Observer
216+ # # Callback for :ignite event *before* the transition is performed
217+ # def before_ignite(vehicle, transition)
218+ # # log message
219+ # end
220+ #
221+ # # Callback for :ignite event *after* the transition has been performed
222+ # def after_ignite(vehicle, transition)
223+ # # put on seatbelt
224+ # end
225+ #
226+ # # Generic transition callback *before* the transition is performed
227+ # def after_transition(vehicle, transition)
228+ # Audit.log(vehicle, transition)
229+ # end
230+ #
231+ # def after_failure_to_transition(vehicle, transition)
232+ # Audit.error(vehicle, transition)
233+ # end
234+ # end
235+ #
236+ # More flexible transition callbacks can be defined directly within the
237+ # model as described in StateMachine::Machine#before_transition
238+ # and StateMachine::Machine#after_transition.
239+ #
240+ # To define a single observer for multiple state machines:
241+ #
242+ # class StateMachineObserver < ActiveModel::Observer
243+ # observe Vehicle, Switch, Project
244+ #
245+ # def after_transition(object, transition)
246+ # Audit.log(object, transition)
247+ # end
248+ # end
249+ #
192250 # == Internationalization
193251 #
194252 # Any error message that is generated from performing invalid transitions
@@ -461,7 +519,7 @@ def add_callback(type, options, &block)
461519 # Configures new states with the built-in humanize scheme
462520 def add_states ( *)
463521 super . each do |new_state |
464- new_state . human_name = -> ( state , klass ) { translate ( klass , :state , state . name ) }
522+ new_state . human_name || = -> ( state , klass ) { translate ( klass , :state , state . name ) }
465523 end
466524 end
467525
0 commit comments