Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

ruby on rails - Two pages for the same resource - ActiveAdmin

Currently I have User model, which is registered in user.rb as a new resource for ActiveAdmin. Generated page displays all users with scopes (all/journalists/startup_employees). Now I want to create another page for the same resource, and the same scopes, but there should be only records with waiting field set to true (and the previous page should displays only this with :waiting => false). How could I do that? I know I could do that with filters, but I need two separate pages, with two links in menu.

// SOLUTION

It was even easier than advices (thanks guys!):

ActiveAdmin.register User, :as => 'Waitlist User' do
  menu :label => "Waitlist"

  controller do
    def scoped_collection
      User.where(:waitlist => true)
    end
  end

  # code

  scope :all
  scope :journalists
  scope :startup_employees
end

ActiveAdmin.register User do
  controller do
    def scoped_collection
      User.where(:waitlist => false)
    end
  end

  # code

  scope :all
  scope :journalists
  scope :startup_employees
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

STI (Single table inheritance) can be used to create multiple "sub-resources" of the same table/parent model in Active admin

  1. Add a "type" column in user table as a string

  2. Add this to User model to mirror waiting field with type field

    after_commit {|i| update_attribute(:type, waiting ? "UserWaiting" : "UserNotWaiting" )}
    
  3. Create the new models UserWaiting and UserNotWaiting

    class UserWaiting < User
    end
    class UserNotWaiting < User
    end
    
  4. Create Active Admin resources

    ActiveAdmin.register UserWaiting do
    # ....
    end
    ActiveAdmin.register UserNotWaiting do
    # ....
    end
    
  5. You can run a first-time sync in console

    User.all.each {|user| user.save}
    

..............

Another way could be to skip the type column (steps 1,2 and 5) and solve the rest with scopes.

  1. Step 3 and 4 above

  2. Then create the scopes

    #model/user.rb
    scope :waiting, where(:waiting => true)
    scope :not_waiting, where(:waiting => false)
    
  3. Scopes in Active Admin

    #admin/user.rb
    scope :waiting, :default => true
    
    #admin/user_not_waitings.rb
    scope :not_waiting, :default => true
    

Just make sure the other scopes in these two pages are also filtered on waiting/not_waiting


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...