活动管理员范围为相关模型的每个实例 [英] Active Admin scopes for each instance of a related model

查看:160
本文介绍了活动管理员范围为相关模型的每个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态活动管理范围的问题。我试图在我的应用程序中为每个项目的经理创建一个范围。但是,当新管理器创建(或分配给项目)时,范围似乎不会更新,但如果我重新启动服务器,它们会更新。所以代码本身起作用,但显然不是我希望的方式。我是一个ruby / rails noob,所以我不确定是否需要采取某种方式来刷新范围。



作为供参考,我在Heroku Cedar上使用Rails 3.2和ActiveAdmin



以下是有问题的代码(可以工作,但仅在重新启动服务器后才引入新的管理器):






  Manager.find_each do | m | 
scope m.first_name do | projects |
projects.where(:manager_id => m.id)
结束
结束

以及整个Active Admin Project模型:

  ActiveAdmin.register Project do 
menu:priority => 1
索引做
列:名称
列:company_name
列:status
列:投影do |项目|
number_to_currency project.projection
结束
列:updated_at
default_actions
结束

范围:全部
范围:工作,: default => true do | projects |
projects.where(:status =>'working')
end

Manager.find_each do | m |
scope m.first_name do | projects |
projects.where(:manager_id => m.id)
end
end
end


解决方案

以下是这个问题的实际解决方案...虽然使用过滤器取而代之的是更理想的稳定性和维护方面,这在ActiveAdmin中看起来更好,是更方便用户的,因为范围变成了漂亮的标签。



这是一个黑客攻击,但是在适当的情况下它是一个可行的解决方案:

诀窍在于更新控制器索引操作的before_filter中的作用域



这可能会变得很糟糕如果您在资源上创建了多个范围(即使您可以轻松设置一些限制)

  ActiveAdmin.register Project do 
菜单:优先级=> 1
索引做
列:名称
列:company_name
列:status
列:投影do |项目|
number_to_currency project.projection
结束
列:updated_at
default_actions
结束

范围:全部
范围:工作,: default => true do | projects |
projects.where(:status =>'working')
end

controller do
before_filter:update_scopes,:only => :index

def update_scopes
resource = active_admin_config

Manager.all.each do | m |
next如果resource.scopes.any? {| scope | scope.name == m.first_name}
resource.scopes<< (ActiveAdmin :: Scope.new m.first_name do | projects |
projects.where(:manager_id => m.id)
end)
end

#尝试像这样删除(未经测试)
resource.scopes.delete_if do | scope |
!(Manager.all.any?{| m | scope.name == m.first_name} || ['all','working']。include?(scope.name))#不要删除您定义的其他范围
结束

结束
结束
结束


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 update when a new manager is created (or assigned to a project) but they DO update if I restart the server. So the code "works" per se but obviously not in the way I would like it to. I'm a ruby/rails noob so I am not sure if I need to do something to "refresh" the scope in some way.

As an FYI, I am using Rails 3.2 on Heroku Cedar with ActiveAdmin

Here is the code in question (that works but only brings in new managers after the server is restarted):


Manager.find_each do |m|
  scope m.first_name do |projects|
    projects.where(:manager_id => m.id)
  end
end

And the entire Active Admin Project model:

ActiveAdmin.register Project do
 menu :priority => 1
 index do
  column :name
  column :company_name
  column :status
  column :projection do |project|
   number_to_currency project.projection
  end
  column :updated_at
  default_actions
 end

 scope :all
 scope :working, :default => true do |projects|
  projects.where(:status => 'working')
 end

 Manager.find_each do |m|
  scope m.first_name do |projects|
    projects.where(:manager_id => m.id)
  end
 end
end

解决方案

Here is an actual solution to this problem ... Altho using filters instead is more desirable stability and maintenance wise, this looks nicer in ActiveAdmin and is more user friendly since scopes become nice looking tabs.

It is a bit of a hack, but it is a viable solution where appropriate:

The trick is to update the scopes in a before_filter on the controllers index action.

This could get bad if you have many scopes created on a resource (altho you can easily set some limits)

ActiveAdmin.register Project do
  menu :priority => 1
  index do
    column :name
    column :company_name
    column :status
    column :projection do |project|
      number_to_currency project.projection
    end
    column :updated_at
    default_actions
  end

  scope :all
  scope :working, :default => true do |projects|
    projects.where(:status => 'working')
  end

  controller do
    before_filter :update_scopes, :only => :index

    def update_scopes
      resource = active_admin_config

      Manager.all.each do |m|
        next if resource.scopes.any? { |scope| scope.name == m.first_name }
        resource.scopes << (ActiveAdmin::Scope.new m.first_name do |projects|
          projects.where(:manager_id => m.id)
        end)
      end

      # try something like this for deletions (untested)
      resource.scopes.delete_if do |scope|
        !(Manager.all.any? { |m| scope.name == m.first_name } || ['all', 'working'].include?(scope.name)) # don't delete other scopes you have defined
      end

    end
  end
end

这篇关于活动管理员范围为相关模型的每个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆