Active Admin Scopes for Each Instance of a Related Model

Rails ActiveAdmin how to use model scope inside admin after_create block

As acquired is a scope, you would have to write:

if book.in?(Book.acquired)

Or

if book.status == 'acquired'

Creating and Using Scopes In Active Admin

where works with scopes the same way it works everywhere else in Rails:

where(:expiry_date => Date.today)

You give it a hash with a key/value. Your way, using ==, basically invokes where(false), as the == isn't "passed in", it's resolved instantly (to false as a symbol and date will always be unequal) and the resulting value is passed to where.

ActiveAdmin: Modifying Index Table Based on Active Scope

you can try something like that

index do
column :title
column :published_year
column :author unless params['scope'] == 'smith'
end

ActiveAdmin - Combining Scope and Filter

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.

How to display scopes based on the role of the user in active admin?

I think this should work:

ActiveAdmin.register Job do

scope :all
scope :draft, :if => proc { current_admin_user.super_admin? }
scope :scheduling
scope :rejected

end

The DSL for setting up scopes is evaluated at server startup time and does not have access to request cycle based data (like the logged in user). To get around this ActiveAdmin has the concept of wrapping code code with proc so that the enclosed code evaluation is deferred to request-cycle-time.

Good luck!



Related Topics



Leave a reply



Submit