Skip to content

Commit

Permalink
Merge pull request troessner#48 from zmillman/master
Browse files Browse the repository at this point in the history
README info on write_state_without_persistence
  • Loading branch information
troessner committed May 1, 2012
2 parents 932bdcf + 370488c commit fc06e56
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.rdoc
Expand Up @@ -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:
Expand Down
13 changes: 8 additions & 5 deletions lib/active_model/transitions.rb
Expand Up @@ -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
Expand Down

0 comments on commit fc06e56

Please sign in to comment.