Hi everybody!
Is it possible to append a method to the before action callbacks but
call
it always as the last one before the action is called?
This is probably a bit badly expressed. It’s maybe clearer with an
example.
I have the default ApplicationController:
class ApplicationController
before_action :authorize!
end
And I have an UsersController with a callback:
class UsersController
before_action :set_user
private
def set_user
@user = User.find(params[:id])
end
def authorized?
return true if @user == current_user
end
end
Now I’ve a small Gem called ActionControl
https://github.com/tobiasfeistmantl/action_control. It makes it
possible
to authorize a user in a before action callback.
Now the problem is that the authorize callback is at the beginning of
the
callback queue. But the user is set after the authorization is done. As
a
consequence I have no access to the @user instance variable in my
authorized? method and the authorized? will always be negative.
As workaround for now I add the authorization callback after the
specific
setter but I think this isn’t really pretty.
I’ve looked into the source code of Rails but haven’t found a way to
manipulate the callback queue (or maybe I’ve overlooked something?).
I hope that the question above is a bit more clear :).
Happy Coding
Best
Tobias