activeadmin_favorites
Saved filter favorites, per-resource view lens defaults, and a cogwheel personalization UI for ActiveAdmin 4 index and show pages.
Requirements
- Ruby 3.2+ (CI matrix: Ruby 3.4, 4.0, and TruffleRuby)
- Rails 7.2+ (Rails 7.2 and 8.1 tested in CI)
- ActiveAdmin 4.0.0.beta13+
- PostgreSQL for production migrations (
jsonbcolumns and concurrent index helpers)
Install
Add the gem to your Gemfile:
gem "activeadmin_favorites"Run the install generator:
bin/rails generate activeadmin_favorites:install
bin/rails db:migrateOr copy migrations manually:
bin/rails activeadmin_favorites:install:migrations
bin/rails db:migrateGreenfield apps pick up engine migrations automatically when no matching version exists in db/migrate/.
Configure the host app (the generator writes config/initializers/activeadmin_favorites.rb with neutral defaults):
ActiveAdmin::Favorites.configure do |config|
config.user_class = "User"
config.user_foreign_key = :user_id
config.user_association_name = :user
config.user_label_method = :email
config.current_user_method = :current_user
endAdd ActiveAdmin::Favorites::FavoritePolicy in the host app (the generator copies a template). Point has_many :favorites on your user model at ActiveAdmin::Favorites::Favorite.
Configure the table name when needed:
ActiveAdmin::Favorites.configure do |config|
config.table_name = "admin_favorites"
endWhen using ActionPolicy, add save_lens_favorite? and reset? to ActiveAdmin::PagePolicy (or equivalent) so the view lens preference page actions authorize correctly.
Stylesheet
Link activeadmin_favorites/personalization_menu from the host Active Admin HTML head so the cogwheel dropdown can scroll the catalog while keeping action buttons in view. The file is plain CSS served through Propshaft; it does not need Tailwind processing. The gem does not ship active_admin/_html_head, so customized host heads keep their stylesheet names and extra tags.
<%= stylesheet_link_tag "activeadmin_favorites/personalization_menu" %>Do not bare-@import that Propshaft logical path into an Active Admin CSS entry that Tailwind CLI compiles from a generated tmp file. Tailwind resolves imports from the input file directory, not through Propshaft. Hosts that must fold the rules into one built stylesheet can concatenate the absolute file instead:
File.read(ActiveAdmin::Favorites.personalization_menu_stylesheet_path)Locales
English ships with the gem. Copy config/locales/activeadmin_favorites.host.yml.example into your locale files and translate active_admin.favorites keys.
Favorite kinds
| Kind | Stores |
|---|---|
filters |
scope, sort, ransack query, macros |
lens |
hidden columns, filters, panels, rows, action items |
combined |
filters and layout together |
View lens layout schema
{
"version": 1,
"hidden": {
"columns": ["created_at"],
"filters": ["status"],
"action_items": ["new"],
"panels": ["details"],
"show_rows": ["author"]
}
}Version 1 supports show/hide only. Column reorder is deferred.
Catalog entries are discovered at render time from ActiveAdmin columns, filters, panels, and show rows. Use lens_id: on custom columns or panels when auto ids are unstable. Block columns without a title receive stable block_column_N ids automatically. Personalization catalog data is injected on every index layout, not only table indexes.
Personalization UI
The gem installs:
- Add to favorites star icon on index pages (outline when unsaved, filled when an owned filter favorite matches the current query; label is the accessible name only). With JavaScript, the star saves a private filter favorite under a suggested name and stays on the filtered index. The full form remains for edit and as a no-JavaScript fallback.
- Personalize page cogwheel on index and show pages (hidden when the page lens catalog is empty)
-
Favorites hub at
/admin/favoriteswith type tabs (all, filters, lens, combined)
Defaults are stored in active_admin_view_lens_defaults. Named lens favorites and combined favorites use admin_favorites.layout. The favorites hub stays reachable from /admin/favorites and from personalization menus on pages that still have catalog entries.
GraphQL
When GraphQL is enabled on the admin namespace, the Favorites resource is exposed as type Favorite (favorites / favorite, plus create/update/delete). Create assigns the current admin user. Opt out:
ActiveAdmin::Favorites.configure do |config|
config.graphql = false
endDevelopment
bundle install
bundle exec appraisal install
make lint
make testSee spec/README.md for focused runs and coverage notes.
If native extensions fail to load after switching Ruby installs (for example mise vs rbenv with the same version), wipe and reinstall both bundle trees with one active Ruby:
rm -rf vendor/bundle
bundle install
rm -rf gemfiles/vendor/bundle
bundle exec appraisal installLinks
License
The gem is available as open source under the terms of the MIT License.
Sponsors
Sponsored by Kisko Labs.