Skip to content
Browse files

Merge pull request #48 from zmillman/master

README info on write_state_without_persistence
  • Loading branch information...
2 parents 932bdcf + 370488c commit fc06e56c2b2b9cc624c1008bf96f3e9556e9813b @troessner troessner committed May 1, 2012
Showing with 16 additions and 5 deletions.
  1. +8 −0 README.rdoc
  2. +8 −5 lib/active_model/transitions.rb
View
8 README.rdoc
@@ -55,6 +55,14 @@ you: <tt>discontinue</tt> and <tt>discontinue!</tt>. Both events will call
<tt>write_state_without_persistence</tt> on successful transition, but only the
bang(!)-version will call <tt>write_state</tt>.
+Note that <tt>write_state_without_persistence</tt> is not defined for you in Rails or in the transitions gem, so if you want the state attribute to be updated, you must define <tt>write_state_without_persistence</tt> yourself.
+
+The following is an example implementation:
+
+ def write_state_without_persistence(sm, new_state)
+ self.state = new_state.to_s
+ end
+
== Automatic scope generation
`transitions` will automatically generate scopes for you if you are using ActiveRecord and tell it to do so via the `auto_scopes` option:
View
13 lib/active_model/transitions.rb
@@ -45,16 +45,19 @@ def reload(options = nil)
protected
def write_state(state_machine, state)
- ivar = state_machine.current_state_variable
prev_state = current_state(state_machine.name)
- instance_variable_set(ivar, state)
- self.state = state.to_s
+ write_state_without_persistence(state_machine, state)
save!
rescue ActiveRecord::RecordInvalid
- self.state = prev_state.to_s
- instance_variable_set(ivar, prev_state)
+ write_state_without_persistence(state_machine, prev_state)
raise
end
+
+ def write_state_without_persistence(state_machine, state)
+ ivar = state_machine.current_state_variable
+ instance_variable_set(ivar, state)
+ self.state = state.to_s
+ end
def read_state(state_machine)
self.state && self.state.to_sym

0 comments on commit fc06e56

Please sign in to comment.
Something went wrong with that request. Please try again.