Maurice Gladwell
2007-Jul-07 21:49 UTC
Setting a callback whose action depends on the previous state of the object
I''ve been reading about ActiveRecord::Callbacks and they''re really cool. But I have a complex need I''m not sure they answer. Let''s say we have a User model. User has a #status attribute, which can be either "active", "closed" or "blocked". I want to set a callback to perform an action if an "active" User has been blocked. So I need to know: 1. That the user has at first been in "active" status. 2. That his status has been set to "blocked". I need to set the callback when someone tries to save him in this state, so to sum up: I need to run some logic when someone updates a user.status from ''active'' to ''blocked''. Any idea how? -- Maurice B. Gladwell --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Jul-08 16:55 UTC
Re: Setting a callback whose action depends on the previous state of the object
On Jul 7, 2007, at 5:49 PM, Maurice Gladwell wrote:> Let''s say we have a User model. User has a #status attribute, which > can be either "active", "closed" or "blocked". > > I want to set a callback to perform an action if an "active" User has > been blocked. So I need to know: > > 1. That the user has at first been in "active" status. > 2. That his status has been set to "blocked". > > I need to set the callback when someone tries to save him in this > state, so to sum up: > > I need to run some logic when someone updates a user.status from > ''active'' to ''blocked''. > > Any idea how? > > -- > Maurice B. GladwellThinking completely off the top of my head (e.g., none of this has actually been run), I''d say you could hook into the status writer and set a flag for later use in a before_save callback. before_save :handle_newly_blocked def status=(new_status) if self.status == ''active'' && new_status == ''blocked'' @newly_blocked = true end write_attribute(:status, new_status) end def handle_newly_blocked if @newly_blocked # .. handle it .. end end -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Jul-08 20:59 UTC
Re: Setting a callback whose action depends on the previous
Maurice Gladwell wrote:> I''ve been reading about ActiveRecord::Callbacks and they''re really > cool. But I have a complex need I''m not sure they answer. > > Let''s say we have a User model. User has a #status attribute, which > can be either "active", "closed" or "blocked". > > I want to set a callback to perform an action if an "active" User has > been blocked. So I need to know: > > 1. That the user has at first been in "active" status. > 2. That his status has been set to "blocked". > > I need to set the callback when someone tries to save him in this > state, so to sum up: > > I need to run some logic when someone updates a user.status from > ''active'' to ''blocked''. > > Any idea how? > > -- > Maurice B. GladwellI don''t know how you are going to use this function, but maybe you''re going to use it this way. A user presses the "block" button. this calls the "block!" method on the user, execute whatever code you need. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jodi Showers
2007-Jul-08 21:11 UTC
Re: Setting a callback whose action depends on the previous
On 8-Jul-07, at 4:59 PM, Matthew Rudy wrote:> > Maurice Gladwell wrote: >> I''ve been reading about ActiveRecord::Callbacks and they''re really >> cool. But I have a complex need I''m not sure they answer. >> >> Let''s say we have a User model. User has a #status attribute, which >> can be either "active", "closed" or "blocked". >> >> I want to set a callback to perform an action if an "active" User has >> been blocked. So I need to know: >> >> 1. That the user has at first been in "active" status. >> 2. That his status has been set to "blocked". >> >> I need to set the callback when someone tries to save him in this >> state, so to sum up: >> >> I need to run some logic when someone updates a user.status from >> ''active'' to ''blocked''. >> >> Any idea how? >> >> -- >> Maurice B. Gladwell > > I don''t know how you are going to use this function, > but maybe you''re going to use it this way. > > A user presses the "block" button. > this calls the "block!" method on the user, > execute whatever code you need. >Although your example is a simple state machine, I am a big fan of acts_as_state_machine. http://lunchroom.lunchboxsoftware.com/2006/1/21/acts-as-state-machine Jodi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Obie Fernandez
2007-Jul-21 16:41 UTC
Re: Setting a callback whose action depends on the previous state of the object
Sorry for the late response, but I couldn''t resist telling you to look at the acts_as_state_machine plugin. It lets you program transition logic like you''re describing declaratively. On 7/7/07, Maurice Gladwell <maurice.gladwell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''ve been reading about ActiveRecord::Callbacks and they''re really > cool. But I have a complex need I''m not sure they answer. > > Let''s say we have a User model. User has a #status attribute, which > can be either "active", "closed" or "blocked". > > I want to set a callback to perform an action if an "active" User has > been blocked. So I need to know: > > 1. That the user has at first been in "active" status. > 2. That his status has been set to "blocked". > > I need to set the callback when someone tries to save him in this > state, so to sum up: > > I need to run some logic when someone updates a user.status from > ''active'' to ''blocked''. > > Any idea how? > > -- > Maurice B. Gladwell > > > >-- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Pre-order my book The Rails Way http://www.amazon.com/dp/0321445619 blog: http://jroller.com/obie/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Seemingly Similar Threads
- acts_as_state_machine override :initial state
- aasm callback order?
- acts_as_state_machine doesn't error on invalid state transition?
- random exception driving me crazy: SecurityError (Insecure: can't modify array)
- having a problem with acts_as_state_machine