RailsAdmin

repository·master·Indexed 27 days ago

https://github.com/railsadminteam/rails_admin

A Rails engine providing a user-friendly interface for managing application data. It supports CRUD operations, custom actions, and various ORMs including ActiveRecord and Mongoid. Version 3.3.0 includes support for multiple asset delivery methods (Webpack, Importmaps, Vite, Sprockets) and integrates with authorization libraries like CanCanCan and Pundit, as well as PaperTrail for auditing.

Tokens
2.7K
Snippets
3
Records
17
Agent score
93%

What's inside rails_admin

  1. Upgrade RailsAdmin from 2.x to 3.x

    master

    Upgrading from version 2.x to 3.x requires attention to Webpack/Webpacker support, which introduces new dependencies and configuration requirements.

    To facilitate the upgrade, run the installation generator:

    rails g rails_admin:install

    The generator will suggest the required changes based on your application's current setup.

  2. Install RailsAdmin

    master

    To install RailsAdmin in your Rails application, follow these steps:

    1. Add the gem to your Gemfile:
      gem 'rails_admin', '~> 3.0'
    2. Run bundle install.
    3. Run the installation generator:
      rails g rails_admin:install
    4. When prompted, provide a namespace for your routes (e.g., admin).
    5. Start your server (rails s) and access the interface at /[your_namespace] (default is /admin).
    gem 'rails_admin', '~> 3.0'
  3. Install RailsAdmin via the Rails generator

    master

    To install RailsAdmin in a Rails application, use the provided Rails generator. The generator handles mounting the engine in your routes, creating the initial configuration file, and setting up assets based on your chosen delivery method.

    Arguments

    • _namespace (optional): The URL namespace where you want to mount RailsAdmin (defaults to admin).

    Options

    • --asset: Specifies the asset delivery method. Supported values are:
      • webpacker (default if Webpacker is defined)
      • webpack (default if webpack.config.js exists)
      • importmap (default if config/importmap.rb exists)
      • vite (default if ViteRuby is defined)
      • sprockets (fallback)

    If you want to explicitly specify a method, use the --asset flag.

  4. Use Mongoid with RailsAdmin

    master

    RailsAdmin supports Mongoid as a database adapter. When using Mongoid, certain column types are disabled in the RailsAdmin interface to prevent errors.

    Disabled Column Types:

    • Range
    • Moped::BSON::Binary
    • BSON::Binary
    • Mongoid::Geospatial::Point

    Pagination Requirement: To use pagination features with the Mongoid adapter, you must have the kaminari-mongoid gem installed in your Gemfile.

  5. Integrate Pundit authorization with RailsAdmin

    master

    RailsAdmin supports the Pundit authorization library via the RailsAdmin::Extensions::Pundit::AuthorizationAdapter. This adapter allows you to use Pundit policies to control access to RailsAdmin controller actions (like :create, :bulk_delete, etc.) and to scope database queries in list views.

    To use this, you must ensure the authorize_with configuration method in RailsAdmin is set up to initialize this adapter. The adapter interacts with your Pundit policies using the following logic:

    • Action Authorization: It maps RailsAdmin actions to Pundit policy methods by appending a ? (e.g., :create becomes create?).
    • Query Scoping: In list or bulk actions, it calls policy_scope on the model's relation. If a policy is not defined, it defaults to returning all records (model.all).
    • Attribute Filtering: It calls attributes_for(action) on the Pundit policy to determine which attributes are allowed for new/create actions. If the policy does not implement this method, it returns an empty hash {}.
    • Error Handling: If authorization fails during a controller action, it raises ::Pundit::NotAuthorizedError.
  6. Integrate CanCanCan with RailsAdmin

    master

    RailsAdmin provides an authorization adapter for the CanCanCan library. This adapter allows RailsAdmin to respect your existing CanCanCan abilities. It uses _current_user (the RailsAdmin internal user method) to instantiate the Ability class.

    To use this, you must configure the authorize_with method in your RailsAdmin configuration to point to your CanCanCan adapter/logic.

  7. Configure RailsAdmin per model

    master

    You can customize the RailsAdmin interface for specific models by using the rails_admin block within your model class. This allows you to configure field labels, visibility, and other model-specific behaviors.

    Example of configuring a field label:

    class Ball < ActiveRecord::Base
      validates :name, presence: true
      belongs_to :player
    
      rails_admin do
        configure :player do
          label 'Owner of this ball: '
        end
      end
    end
  8. Configure the Index action in RailsAdmin

    master

    The Index action is used to list entries for a model. You can customize its behavior using the following instance options within your RailsAdmin configuration:

    • :collection: Boolean. Determines if the action is a collection action. Defaults to true.
    • :http_methods: Array of symbols. The allowed HTTP methods for this action. Defaults to [:get, :post].
    • :route_fragment: String. A fragment to be added to the route. Defaults to ''.
    • :breadcrumb_parent: Array. Defines the parent for the breadcrumb. It defaults to the index of the parent model if a parent is configured, otherwise it defaults to [:dashboard].
    • :link_icon: String. The FontAwesome icon class used for the link. Defaults to 'fas fa-th-list'.
    • :controller: Proc. A custom controller implementation. If overridden, you are responsible for handling the logic for fetching objects and responding to different formats (HTML, JSON, XML, CSV).
  9. Configure PaperTrail auditing options

    master

    When using the PaperTrail auditing adapter, you can configure the following instance options within the audit_with :paper_trail block:

    • user_class: The class used to resolve the whodunnit field to a username (defaults to User).
    • version_class: The PaperTrail version model class (defaults to PaperTrail::Version).
    • sort_by: A hash defining the default sort order for versions (defaults to {id: :desc}).
  10. Configure PaperTrail auditing in RailsAdmin

    master

    To integrate PaperTrail with RailsAdmin, use the audit_with :paper_trail configuration block. You must explicitly define the user_class and version_class if they deviate from the defaults (User and PaperTrail::Version respectively).

    If you use custom version classes via PaperTrail's has_paper_trail versions: { class_name: 'MyVersion' } configuration, those model-specific settings will take precedence over the global configuration provided here.

    config.audit_with :paper_trail do
      user_class { User }
      version_class { PaperTrail::Version }
    end