Forum: Ruby on Rails Setting a callback whose action depends on the previous stat

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8749270955fc7c361eac3d6073a8ffcf?d=identicon&s=25 Maurice Gladwell (maurice-gladwell)
on 2007-07-07 23:50
(Received via mailing list)
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-07-08 18:56
(Received via mailing list)
On Jul 7, 2007, at 5:49 PM, Maurice Gladwell wrote:
> 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

Thinking 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@AgileConsultingLLC.com
E60b2dc57668b5662ce3f07781e41710?d=identicon&s=25 Matthew Rudy Jacobs (matthewrudy)
on 2007-07-08 22:59
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.
B09a3f6cdc4797532647d2d264b5df49?d=identicon&s=25 Jodi Showers (jshow)
on 2007-07-08 23:12
(Received via mailing list)
On 8-Jul-07, at 4:59 PM, Matthew Rudy wrote:

>>
>>
>> --
>> 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/ac...

Jodi
D61a2db96e73acf66b0b18688a39ecfb?d=identicon&s=25 Obie Fernandez (Guest)
on 2007-07-21 18:42
(Received via mailing list)
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@gmail.com> wrote:
> 1. That the user has at first been in "active" status.
> --
> Maurice B. Gladwell
>
> >
>


--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Pre-order my book The Rails Way
http://www.amazon.com/dp/0321445619

blog: http://jroller.com/obie/
This topic is locked and can not be replied to.