current_user и active_admin - PullRequest
       35

current_user и active_admin

3 голосов
/ 07 октября 2011

Я пытался всеми возможными способами и, похоже, не могу ограничить метод scope_to в active_admin с помощью cancan.

Я могу заставить меню играть хорошо, как и большинство других методов, но не scope_to

Например, это прекрасно работает:

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

Но не

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

или

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

или

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

ЕслиЯ пытаюсь использовать метод current_user без proc, я получаю ошибку undefined.Метод current_user определен devise, а также в 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

Есть идеи, как получить доступ к методу current_user?Работает везде, где мне это нужно.

1 Ответ

3 голосов
/ 09 ноября 2011

Доступ к методам, даже методам application_controller в active_admin, часто может быть болезненным.

Но вы можете полностью пропустить его, явно переписав контроллер, он не идеален, но он работает.

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