0
votes

I am fairly new to active_admin, I was wondering if there is a way to achieve the following

I have two models

User
  belongs_to :group

Group
 has_many :users

I have successfully created pages in activeadmin for groups and users, now what I want is show users that belong to a certain group. I have button manage_members on groups index page which should show members of only that group. Where I can remove members from the group or add more members.

This is what I have been able to do so far

member_action :manage_members do
    @group = Group.find(params[:id])
    @page_title = "Manage Groups > @#{@group.name}, Edit Members"
end

and a the view app/vies/admin/groups/manage_users.html.arb

table_for assigns[:group].users do
  column "Name" do |u|
    u.user_id
  end
  column "email" do |u|
    u.user.email
  end
  column "Created Date" do |u|
    u.user.created_at
  end

  column "OfficePhone" do |u|
    u.user.office_no
  end
end

This shows the member of the groups, but I have to do all the work on this page to add edit delete a member, I cannot have active_admin filters and other cool stuff here, this is like a custom page,

Is there a way to have an index page (with all goodness of filters batch actions etc) ( like that of users ) but only showing users of a group. Something like a scoped index page which shows on users from a group and I have the same control over that page as any active admin index page ? More like the image below

This is What I mean by index page

rather than having to do all that work my self which currently looks like

I dont want this

Very new to active_admin so apologies if there something really straight forward that I am missing.

Thanks

1

1 Answers

1
votes

Maybe a filter will do. That would look like (put it in the same file where you put the member_action)

filter :group, :as => :select, :collection => proc { Group.for_select }

The proc is there to make sure changes to groups (adding/removing/..) are immediately reflected to the select-list in the filter. That has to do with class caching in production. Dont forget to put this scope in your Group model.

scope :for_select, :select => [:id, :name], :order => ['name asc']

Another way is to use scopes. If you have a field in your Group model, like a slug/label that could serve as a method header then you could do something like this in your activeadmin user register block:

Group.all.each do |group|
  # note the sanitization of the Group name in the gsub
  scope "#{group.name.gsub(/-/,'_')}".to_sym
end

And this in your User model:

Group.all.each do |group|
  # note the sanitization of the Group name in the gsub
  scope "#{group.name.gsub(/-/,'_')}".to_sym, joins(:group).where("group.name = ?",role.name)  
  # using joins(:group) or joins(:groups) makes a difference,
  # so not sure as I have not tested, but maybe the where should be
  # ....where("groups.name = ....
end

It should give you nice buttons above your index views like here: http://demo.activeadmin.info/admin/orders

If you want this for a has_and_belongs_to_many relation, I suggest you take a look at this Rails3 Active Admin - How to filter only records that meet all checked items in collection

Good luck!