Ruby on Rails Friday, January 8, 2016

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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a548d392-853d-48e6-b778-9414e6df95b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment