0

I have an ActiveAdmin instance where I have User that has_many Things. I also want to allow users to act as managers for other User's Things using a self referential has_many through association to Other User using an Employments model. The models look something like this...

class User < ActiveRecord::Base
    ...

    has_many :things, dependent: :destroy


    has_many :employments, foreign_key: 'employer_id', dependent: :destroy
    has_many :employees, through: :employments
    has_many :inverse_employments, class_name: 'Employment', foreign_key: 
                  'employee_id', dependent: :destroy
   has_many :employers, through: :inverse_employments

   accepts_nested_attributes_for :employments, allow_destroy: true

   ...
end

class Employments < ActiveRecord::Base
    belongs_to :employer, :class_name => 'User'
    belongs_to :employee, :class_name => 'User'

    # Also has other metadata attributes
end

class Thing < ActiveRecord::Base
    belongs_to :user
end

In ActiveAdmin, I'd like a user to be able to manage their own Things and also other Users' things. I have set it up with scopes and filter as below

ActiveAdmin.register Thing do
    ...

    scope_to :current_user

    scope 'Owned', :all, default: true do
      Rezzable::Pinger.where(user_id: current_user.id)
    end

    scope :managed do
      Rezzable::Pinger.where(user_id: Employment.where(employee_id: 
          current_user.id).collect{ |e| e.employer_id })
    end
end

The scoping works great, but then the filters don't work. If I remove the scopes the filters work fine. I've also worked at creating a filter that shows only managed or owned items, but the ransacking the association is proving difficult as well since the association for owned Things is direct, but the other must be done through Employments and getting the employer's Things.

Is there a way to get scopes and filters to work nicely together? Failing that, are there alternative, perhaps better ways to accomplish this? Thanks in advance.

PS - I do see this as the same problem as ActiveAdmin - Using scopes with filters, but I don't think this solution works for me because of our very different associations.

Community
  • 1
  • 1
Rob Olendorf
  • 561
  • 5
  • 9

1 Answers1

0

emI found a workable solution, although it doesn't really resolved the issue of mixing scopes and filters.

Rather than creating a managed scope I made a new namespace

config/initializers/active_admin.rb

config.namespace :my do |my|
  my.authentication_method = :authenticate_user!
  my.current_user_method = :current_user
  my.build_menu :utility_navigation do |menu|
    menu.add label: proc{ "#{display_name current_user.name} - My Stuff" },
           url: proc { my_dashboard_path },
           id: 'current_user',
           priority: 1,
           if: proc { current_user.can_be_user? }
    menu.add label: 'Admin Dashboard',
           url: proc { admin_dashboard_path },
           if: proc { current_user.can_be_admin? }
    menu.add label: 'Manager Dashboard',
           url: proc { manager_dashboard_path },
           if: proc { current_user.employers.size > 0 }
    my.add_logout_button_to_menu menu
  end
end

config.namespace :manager do |manager|
  manager.authentication_method = :authenticate_user!
  manager.current_user_method = :current_user
  manager.build_menu :utility_navigation do |menu|
    menu.add label: proc{ "#{display_name current_user.name} -My Stuff" },
           url: proc { my_dashboard_path },
           id: 'current_user',
           priority: 1,
           if: proc { current_user.can_be_user? }
    menu.add label: 'Admin Dashboard',
           url: proc { admin_dashboard_path },
           if: proc { current_user.can_be_admin? }
    manager.add_logout_button_to_menu menu
  end
end

And added menu links to the other namespaces as needed so that Manager pages were accessible (Admin Links are for the site administrator)...

Then for Things in the manager namespace I overrode the controller to get only the things managed.

ActiveAdmin.register Thing, namespace: :manager do

controller do
  def scoped_collection
    Thing.where( user_id: current_user.employers.collect { |e| e.user_id } )
  end
end

So this got the affect I was looking for anyway.

Rob Olendorf
  • 561
  • 5
  • 9