Synopsis
In Ruby on Rails, does the state machine gem support the use of a model instance that doesn't directly relate to the host model? If they do, how do I do it?
The conclusion I'm leaning toward is that authorization should be left to other parts of the framework, and the state machine should just be an interface defining the transition of states. That being said, I see some support for transition conditions and I was wondering if the data inside those conditions could be something NOT set on the host model, but instead passed in like a parameter.
Background
Say we have a Task
that has the states in_progress
and completed
, and in order to transition from them respectively, the current_user
(assigned in the session, access in the controller) needs to pass a check.
I understand through the documentation that in order to add a check to the transition I have to program it like this:
transition :in_progress => :completed, :if => :user_is_owner?
and define the function like:
def user_is_owner()
true
end
but let's try to implement the restriction so that the task can only be edited if the user_id
is the same as the id
of the user
that requested the task
USING dynamic data.
def user_is_owner?(user)
user.id == self.requester_id
end
Notice I don't have that user
object, how would one pass the user
object they need in?
Ruby Version: 1.9.3
Rails Version: 3.2.9
Thanks!