holden
holden

Reputation: 13581

current_user and active_admin

I have tried every way possible and can't seem to limit the scope_to method in active_admin using cancan.

I can get the menu to play nice as well as most other methods but not scope_to

For example this works fine:

menu :if => proc{ can?(:manage, Booking) }

But not

scope_to :current_user, :association_method => :space_bookings unless proc{ can?(:manage, Booking) }

or

scope_to :current_user, :association_method => :space_bookings unless proc{ current_user.admin? }

or

scope_to :current_user, :association_method => :space_bookings unless controller.current_ability.can?( :manage, config.resource )

If I try using the current_user method without proc I get an error undefined. The current_user method is defined by devise and also in the active_admin.rb

  # == Current User
  #
  # Active Admin will associate actions with the current
  # user performing them.
  #
  # This setting changes the method which Active Admin calls
  # to return the currently logged in user.
  config.current_user_method = :current_user

Any ideas how I can gain access to the current_user method? It works everywhere else I've needed it.

Upvotes: 4

Views: 3926

Answers (1)

ere
ere

Reputation: 1779

Accessing methods, even those in the application_controller within active_admin can often be painful.

But you can skip it entirely by rewriting the controller explicitly, it's not ideal, but it works.

 controller do 
  #  authorize_resource
    def index
      if can? :manage, :all
        @bookings = Booking.page params[:page] #you must call .page for the index to work.
      else
        @bookings = current_user.bookings.page params[:page]
      end
      index! #this is needed unless you are using custom views
    end 
  end

Upvotes: 3

Related Questions