Skip to content

Loading…

Allow prepending to the filter chain #290

Closed
jarijokinen opened this Issue · 2 comments

2 participants

@jarijokinen

Background

I have some situations where I would like to prepend the load_and_authorize_resource to the filter chain. Here's one (but probably not the best) example:

def ApplicationController
  load_and_authorize_resource
  before_filter :do_something_for_the_resource

  def do_something_for_the_resource
    if controller_name == "tasks"
      @project.do_something
      # ERROR: @project is not loaded or authorized yet
    end
  end
end

def ProjectsController < ApplicationController
  def index
  end
end

def TasksController < ApplicationController
  load_and_authorize_resource :project
  load_and_authorize_resource :task, :through => :project

  def index
  end
end

The problem

When calling TasksController#index, the before_filters execution order is:

load_and_authorize_resource
before_filter :do_something_for_the_resource
load_and_authorize_resource :project
load_and_authorize_resource :task, :through => :project

The solution?

I was thinking that an option like :prepend => true would be nice:

def TasksController < ApplicationController
  skip_before_filter load_and_authorize_resource
  load_and_authorize_resource :task, :through => :project, :prepend => true
  load_and_authorize_resource :project, :prepend => true

  def index
  end
end

Then the execution order would be:

load_and_authorize_resource :project
load_and_authorize_resource :task, :through => :project
before_filter :do_something_for_the_resource

Or am I missing something here? AFAIK this is not possible at the moment?

@ryanb
Owner

This isn't available at the moment, but I could see the use case and should be fairly easy to add. I'll mark this for being added.

@ryanb
Owner

adding :prepend option to load_and_authorize_resource - closed by 951d70e

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.