Active Admin scopes for each instance of a related model

前端 未结 6 1785
灰色年华
灰色年华 2020-12-30 11:26

I have an issue with a dynamic active admin scope. I am attempting to create a scope for each \"manager\" of a \"project\" in my app. However, the scopes don\'t seem to up

6条回答
  •  误落风尘
    2020-12-30 12:14

    Here is my optimized solution based on Brant Sterling Wedel's answer.

    This solution contains the following additional enhancements:

    • Removes any unused scopes
    • Allows to set default scope
    ### Default scope must be set in advance
    scope "Jeff", default: true do |x| 
      x.joins(:manager).where(manager: {name: "Jeff"})
    end
    
    controller do
    
      before_action :reload_scopes, only: :index
    
      def reload_scopes
        resource_config = active_admin_config
    
        resource_config.instance_variable_set(:@scopes, [
          resource_config.scopes.first ### Default scope
        ])
    
        Manager.all.each do |m|
          next if resource_config.scopes.first.name ==  m.name
    
          resource_config.scopes << ActiveAdmin::Scope.new(m.name){|scope| scope.where(manager_id: m.id) }
        end
      end
    
    end
    

提交回复
热议问题