到目前为止,这个组合设置中的大多数东西都工作得很好。但是,当我尝试有条件地禁用筛选器时,它们只是始终处于启用状态我的场景是(或多或少地)我想给Restaurant所有者(具有AdminUser角色的:restaurateur所有者)部分访问权:他们只能编辑自己的餐厅,我还想对他们隐藏一些字段。这很管用。但是禁用过滤器没有。让我详细说明一下:

# app/admin/restaurants.rb
batch_action :activate, :if => proc { can? :activate, Restaurant } do |list|
  #...
end
controller do
  def current_ability
    @current_ability ||= Ability.new(current_admin_user)
  end
end
index do
  ...
  column :city if can? :manage, Restaurant             # This works well.
end
filter :city, :if => proc { can? :manage, Restaurant } # This is always there.

Ability
# app/models/ability.rb
if user.has_role? :admin
  can :manage, :all
elsif user.has_role? :restaurateur
  cannot :manage, Restaurant

以下是我在Rails控制台中看到的:
 admin = AdminUser.find(1)                           # roles => [:admin]
 restorateur = AdminUser.find(2)                     # roles => [:restaurateur]
 Ability.new(admin).can?(:manage, Restaurant)        # true
 Ability.new(restorateur).can?(:manage, Restaurant)  # false

我知道我并没有尽可能地使用它,比如使用:manage动词,在一般情况下,它并不打算提供部分访问但它可以工作,除了禁用过滤器。
以及
有什么特别的我应该这样做,这些确实会工作?
Rolify位于3.2.0坎坎在1.6.8。activeadmin的git版本是:b0dd8fdcfbd68984a8c2ec7f2279a121eeb66c3d。如果我把它更新到最新的GIT版本(或者官方的0.5.0版本),batch_actions总是被禁用(因此它们也会禁用selectable_column。)
关于我的问题:
有没有可靠的方法来测试ActiveAdmin文件中的能力也许这些能力被实例化的方式给了错误的用户(我的意思是在检查过滤器:ifproc之前)?在这种情况下,can?助手如何获得一个实例化的能力,我几乎没有什么损失。
以及
如果我的方法不正确,有条件地禁用过滤器的推荐方法是什么?
以及
有人知道为什么activeadmin的最新版本似乎完全忽略了批处理操作吗?也许我应该把controller do块放在batch_action块之前?
谢谢你的时间。

最佳答案

我也遇到了同样的问题,并在这个issue中找到了解决方案。
使用此蒙克贴后:

# config/initializers/activeadmin_filter_conditions.rb
module ActiveAdmin
  module Filters
    class FormBuilder < ::ActiveAdmin::FormBuilder
      def filter(method, options = {})
        return "" if method.blank?
        if options[:if].is_a?(Proc)
          return "" if !template.instance_eval(&options[:if])
        end
        options[:as] ||= default_input_type(method)
        return "" unless options[:as]
        content = input(method, options)
        form_buffers.last << content.html_safe if content
      end
    end
  end
end

您应该能够使用前面提到的方式在筛选器中使用条件:
filter :city, :if => proc { can? :manage, Restaurant }

关于ruby - ActiveAdmin,CanCan,Rolify-无法有条件地禁用过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12387838/

10-16 19:21