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
Mongoid: Find Through Array of Ids
How to Reference a Lambda from Within Itself Using Ruby
Singleton Method VS. Class Method
What Is a Worker in Ruby/Rails
How to Authorize a Google Service Account Without the Default Credentials File
Can a Ruby Script Tell What Directory It's In
Ruby: How to Generate CSV Files That Has Excel-Friendly Encoding
Add API Key to Every Request in Activeresource
Ruby: How to Store and Display a Day of the Week
Printing to Screen in a Rake Task
How to Ignore Multiline Comments in SASS
How to Bypass Mass Assignment Protection
Ruby on Rails: Yielding Specific Views in a Specific Places in the Layout
How to Render a Plain HTML File with Sinatra
How to Output Names of Ruby Unit Tests
Ruby: Creating a Hash Key and Value from a Variable in Ruby