Super Admin
By default Rhino includes an admin dashboard that allows you to manage your application. This dashboard is built using the ActiveAdmin gem and themed with arctic_admin and installs activeadmin_addons gem to add some extra functionality.
The dashboard is accessible at /admin
and requires an admin user to log in.
Configuration
The admin dashboard configuration is in config/initializers/active_admin.rb
and can be altered to the project needs.
Generating an admin resource
To generate an admin resource with Rhino defaults, use the following command:
rails g rhino:resource ModelName
This generator is included by default if you run rails g rhino:model
.
This will generate a model in the app/admin
folder with the default Rhino configuration for the model, like so:
ActiveAdmin.register ModelName do
rhino_filters
rhino_permit_params
end
That provides the default filters and permit params for the model and index
and other configuration options can be added as needed.
Generating an admin resource with ActiveAdmin defaults
If more control is needed over the resource, generate or replace an ActiveAdmin resource with the following command:
rails g admin:resource ModelName
Security
ActiveAdmin 3+ requires ransack 4 which has a system for whitelisting attributes and scopes for searching. Rhino by default allows the same attributes and associations for viewing and for filtering as for reading.
To customize the attributes and associations that can be searched, you can override the ransackable_attributes
, ransackable_associations
, and ransackable_filters
methods in your model.
def ransackable_attributes(_auth_object = nil)
super + ['my_special_attribute']
end
def ransackable_associations(_auth_object = nil)
super + ['my_special_scope']
end
def ransackable_filters(auth_object = nil)
super + ['my_special_filter']
end
end
Optimizing select filters
ActiveAdmin’s default select filter loads all records from the database which can cause significant performance issues. To avoid this, a lazy typeahead select filter can be created using activeadmin_addons, and the Ransack gem used by ActiveAdmin.
Imagine we have an InvoiceLineItem table, that belongs to a UsersRole, that in turn belongs to a User. If we want a “User“ filter in the InvoiceLineItem Index page, we would do something like this:
ActiveAdmin.register InvoiceLineItem do
preserve_default_filters!
remove_filter :users_role # remove original filter
filter :users_role_user_id, # this name must contain the relationships path from the model from the index page until the user table, so invoice_line_item -> users_role -> user. the engine will do a join.
label: "User",
as: :search_select_filter,
url: proc { admin_users_path }, # AJAX endpoint
fields: %w[name nickname email], # columns in the looked up table to be compared against
minimum_input_length: 2,
method_model: User, # original table
width: "190px", # necessary for fixing a UI bug
display_name: "full_nickname_and_email", # custom display name that gives you more flexibility to use whatever you want
order_by: "name"
end
Create a json view for the admin_users_path to return a custom attribute that will be shown in the results select box.
json.array!(@users) do |user|
json.extract! user, :id, :full_nickname_and_email
end
Finish up by implementing the custom display field in the User model:
def full_nickname_and_email
"#{nickname || name} - #{email}"
end
jbuilder is no longer included in Rhino by default. You will need to add it to your Gemfile.