Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the same model in two active admin classes

I'm working on an ActiveAdmin app for a large production application. I'm currently trying to use the same model for two activeadmin "entities".

So, say I have

class Person < ActiveRecord::Base

  scope :special, where(:is_special => true)
  scope :ordinary, where(:is_special => false)

end

Can I do something like

ActiveAdmin.register Person, :name => "Special People" do

  # columns, filters for special people

  controller do
    def scoped_collection
      Person.special
    end
  end  

end

ActiveAdmin.register Person, :name => "Ordinary People" do

  # columns, filters for ordinary people

  controller do
    def scoped_collection
      Person.ordinary
    end
  end  

end

(I'm making up the syntax a bit here to explain what I want to do.)

The two types of people would appear as menu items and different CRUD interfaces as defined in the ActiveAdmin.register block. They would just have the same underlying model.

like image 890
mattfitzgerald Avatar asked Sep 03 '12 07:09

mattfitzgerald


1 Answers

Active Admin model Code:

   ActiveAdmin.register Person, as: "Special People" do
      scope :Special, default: true do |person|
        person = Person.special
      end

      controller do
        def scoped_collection
          Person.special
        end
      end
    end

    ActiveAdmin.register Person, as: "Ordinary People" do
      scope :Ordinary, default: true do |person|
        person = Person.ordinary
      end

      controller do
        def scoped_collection
          Person.ordinary
        end
      end
    end

Now in routes:

match '/admin/special_people/scoped_collection/:id' => 'admin/special_people#scoped_collection'

match '/admin/ordinary_people/scoped_collection/:id' => 'admin/ordinary_people#scoped_collection'

Try with above changes. Hope this would solve your issues. Thanks.

like image 160
Swati Avatar answered Sep 21 '22 12:09

Swati