How to call lifecycle transition from a script

34 views
Skip to first unread message

brettnem

unread,
Sep 11, 2012, 2:48:23 PM9/11/12
to hobo...@googlegroups.com
Hello All,
I have my lifecycle working great with transition buttons, but I'd like a script to be able to call the transition.

So far, I've tried the following:
unsent_message.lifecycle.deliver  (undefined method `deliver`)

unsent_message.lifecycle.deliver!  (wrong number of arguments 0 for 1)

unsent_message.lifecycle.deliver!(user) (Hobo::Model::Lifecycles::LifecycleError: No transition deliver available)

I'm pretty sure it's seeing the lifecycle, just not sure why it's not running. Not sure why the "!" makes a difference as I see it referenced in the docs but couldn't find it's explanation. 

My transition looks like this:
transition :deliver, {:not_sent => :sent}, :available_to => :all do
..code..
end

However, I would like it to be available to "A valid user OR the script called externally" (not sure how to define that)

Thanks!
-Brett

kevinpfromnm

unread,
Sep 11, 2012, 3:12:55 PM9/11/12
to hobo...@googlegroups.com
Aside: The ! is a ruby convention for a method that modifies it's source object.

Your 3rd attempt should work as far as I can tell... as long as the unsent_message object is in the :not_sent state.

brettnem

unread,
Sep 11, 2012, 5:06:31 PM9/11/12
to hobo...@googlegroups.com
Oh, right.. I'm new to ruby. I do remember the ! convention now.

Ok, so I figured out the issue, but the more I think about it, the more I'm confused.

so the unsent_message's user_id was nil. Once I set that to 1 (which my user object was user_id 1) then it worked great.

But then I set user.administrator = false. Now nothing works

I've gone back through the model and lifecycle and removed all requirements from both the permissions and the lifecycle from requiring admin. 

The lifecycle looks like this:
  
  lifecycle do
    state :sent
    state :not_sent, :default => true

      transition :redeliver, {:sent => :sent}, :available_to => :all  do
        Audit.write_log(self.user.domain.id,"Re-Delivered message: (" + self.subject + ") " + self.description + " by " + acting_user.name)
      end
      transition :deliver, {:not_sent => :sent}, :available_to => :all do
        Audit.write_log(self.user.domain.id,"Delivered message: (" + self.subject + ") " + self.description + " by " + acting_user.name)
      end
      transition :Mark_Unsent, {:sent => :not_sent}, :available_to => :all
      
  end 

As you can see, it shouldn't care who the user is. I don't really understand what to place into :available_to for this. Especially since the script is calling it. Who is acting_user at that point? Do I set acting_user like this:
acting_user = User.first (as an example)

Or for the lifecycle is acting_user whatever user you pass into it as the first param when it's called?

What I really want to do is to enable the transition if the message.user_id is the user_id observing the record or an administrative account (or a script_user account). 

Thanks all for the help!
-Brett
Reply all
Reply to author
Forward
0 new messages