Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_state_without_persistence confusion #16

Closed
robolson opened this issue Jan 18, 2011 · 4 comments
Closed

write_state_without_persistence confusion #16

robolson opened this issue Jan 18, 2011 · 4 comments
Assignees

Comments

@robolson
Copy link

While attempting to use this gem I kept running into the issue where the persistence method with a bang(!) was not working.

In the README it talks about how "both events will call write_state_without_persistence on successful transition". What it does not say is that write_state_without_persistence is not defined for you in Rails or in the transitions gem. Which leaves the gem author to implement this method if the non bang-version is going to do anything other than set the @default_current_state instance variable.

I added the following to my ActiveRecord user class:

def write_state_without_persistence(sm, new_state)
  self.state = new_state.to_s
end

which does the trick, but it also means self.state= is called once inside write_state and again inside write_state_without_persistence.

I feel like I am missing something here. What is the proper way to use this gem with the non bang(!)-versions of the events?

@venetanji
Copy link

Totally agree! I'm using mongoid. Consider something like:

  before_save :expire_free_trial

  def expire_free_trial
    begin
      trial_expired if Time.now  - created_at > FREE_TRIAL_PERIOD
      self.state = self.current_state
    rescue Transitions::InvalidTransition
    end
  end

It took me quite a while to figure out that line that sets the state attribute. I'd expect that to be set when the non-bang event fires. Perhaps that works only for active record?
Anyways I'm ok with it, but it should be more clear. Specially if you want to call a transition in a before_save...

@troessner
Copy link
Owner

@robolson, @venetanji could you submit a pull request which updates our documentation?
If not, I'll update when I find the time.

@ghost ghost assigned troessner Aug 12, 2011
@zmillman
Copy link
Contributor

As someone who stumbled on the project and tried to use it with ActiveRecord, this issue caused me a lot of confusion.

I expected the behavior of calling an event like article.publish to update the article.state attribute and was surprised when only current_state changed. Perhaps it would be best to define write_state_without_persistence as @robolson wrote it in the ActiveRecord::Transitions plugin?

@troessner
Copy link
Owner

Closed thanks to @zmillman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants