简体   繁体   English

ActiveAdmin-合并范围和筛选器

[英]ActiveAdmin - Combining Scope and Filter

I have an ActiveAdmin instance where I have User that has_many Thing s. 我有一个ActiveAdmin实例,其中有具有has_many ThingUser I also want to allow users to act as managers for other User 's Thing s using a self referential has_many through association to Other User using an Employments model. 我也希望让用户来充当管理者对其它UserThing S使用自我指涉的has_many通过关联其他User使用Employments模式。 The models look something like this... 这些模型看起来像这样...

class User < ActiveRecord::Base
    ...

    has_many :things, dependent: :destroy


    has_many :employments, foreign_key: 'employer_id', dependent: :destroy
    has_many :employees, through: :employments
    has_many :inverse_employments, class_name: 'Employment', foreign_key: 
                  'employee_id', dependent: :destroy
   has_many :employers, through: :inverse_employments

   accepts_nested_attributes_for :employments, allow_destroy: true

   ...
end

class Employments < ActiveRecord::Base
    belongs_to :employer, :class_name => 'User'
    belongs_to :employee, :class_name => 'User'

    # Also has other metadata attributes
end

class Thing < ActiveRecord::Base
    belongs_to :user
end

In ActiveAdmin, I'd like a user to be able to manage their own Things and also other Users' things. 在ActiveAdmin中,我希望用户能够管理自己的事物以及其他用户的事物。 I have set it up with scopes and filter as below 我已经设置了范围和过滤器如下

ActiveAdmin.register Thing do
    ...

    scope_to :current_user

    scope 'Owned', :all, default: true do
      Rezzable::Pinger.where(user_id: current_user.id)
    end

    scope :managed do
      Rezzable::Pinger.where(user_id: Employment.where(employee_id: 
          current_user.id).collect{ |e| e.employer_id })
    end
end

The scoping works great, but then the filters don't work. 范围确定效果很好,但是过滤器不起作用。 If I remove the scopes the filters work fine. 如果删除示波器,则过滤器工作正常。 I've also worked at creating a filter that shows only managed or owned items, but the ransacking the association is proving difficult as well since the association for owned Things is direct, but the other must be done through Employments and getting the employer's Things. 我还曾创建过一个仅显示托管或自有项目的过滤器,但是事实证明,对关联的洗劫也很困难,因为自有物的关联是直接的,但另一个必须通过雇佣并获取雇主的物来完成。

Is there a way to get scopes and filters to work nicely together? 有没有办法使范围和过滤器很好地协同工作? Failing that, are there alternative, perhaps better ways to accomplish this? 如果不这样做,是否有替代方法,也许是更好的方法来完成此任务? Thanks in advance. 提前致谢。

PS - I do see this as the same problem as ActiveAdmin - Using scopes with filters , but I don't think this solution works for me because of our very different associations. PS-我确实将其视为与ActiveAdmin相同的问题-将范围与过滤器一起使用 ,但由于我们的关联非常不同,因此我认为此解决方案不适用于我。

emI found a workable solution, although it doesn't really resolved the issue of mixing scopes and filters. emI找到了一个可行的解决方案,尽管它并没有真正解决混合示波器和过滤器的问题。

Rather than creating a managed scope I made a new namespace 我没有创建托管范围,而是创建了新的名称空间

config/initializers/active_admin.rb config / initializers / active_admin.rb

config.namespace :my do |my|
  my.authentication_method = :authenticate_user!
  my.current_user_method = :current_user
  my.build_menu :utility_navigation do |menu|
    menu.add label: proc{ "#{display_name current_user.name} - My Stuff" },
           url: proc { my_dashboard_path },
           id: 'current_user',
           priority: 1,
           if: proc { current_user.can_be_user? }
    menu.add label: 'Admin Dashboard',
           url: proc { admin_dashboard_path },
           if: proc { current_user.can_be_admin? }
    menu.add label: 'Manager Dashboard',
           url: proc { manager_dashboard_path },
           if: proc { current_user.employers.size > 0 }
    my.add_logout_button_to_menu menu
  end
end

config.namespace :manager do |manager|
  manager.authentication_method = :authenticate_user!
  manager.current_user_method = :current_user
  manager.build_menu :utility_navigation do |menu|
    menu.add label: proc{ "#{display_name current_user.name} -My Stuff" },
           url: proc { my_dashboard_path },
           id: 'current_user',
           priority: 1,
           if: proc { current_user.can_be_user? }
    menu.add label: 'Admin Dashboard',
           url: proc { admin_dashboard_path },
           if: proc { current_user.can_be_admin? }
    manager.add_logout_button_to_menu menu
  end
end

And added menu links to the other namespaces as needed so that Manager pages were accessible (Admin Links are for the site administrator)... 并根据需要添加了指向其他命名空间的菜单链接,以便可以访问Manager页面(管理员链接适用于站点管理员)...

Then for Things in the manager namespace I overrode the controller to get only the things managed. 然后,对于管理器名称空间中的事物 ,我覆盖了控制器以仅管理事物。

ActiveAdmin.register Thing, namespace: :manager do

controller do
  def scoped_collection
    Thing.where( user_id: current_user.employers.collect { |e| e.user_id } )
  end
end

So this got the affect I was looking for anyway. 因此,无论如何,这还是我一直在寻找的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM