Madmin Documentation

repository·main·Indexed 20 days ago

https://github.com/excid3/madmin

A customizable admin interface for Ruby on Rails applications designed to feel like Rails scaffolds. Madmin supports ActionText, Hotwire, Import maps, and Sprockets. It provides generators for resources, views, and custom fields, and includes flexible options for authentication (including Devise and Rails 8), asset management, and attribute visibility configuration.

Tokens
7.2K
Snippets
34
Records
43
Agent score
73%

What's inside Madmin

  1. Restrict Madmin routes using Devise/Rails authentication blocks

    main

    To prevent non-admin users from even discovering the /madmin path, wrap your Madmin namespace in an authenticated block within config/routes.rb. Using authenticated is recommended over authenticate because it causes the route to return a 404 for unauthorized users, which helps hide the admin area from malicious attempts.

    authenticated :user, lambda { |u| u.admin? } do
      namespace :madmin do
      end
    end
  2. Generate a custom field

    main

    To create a new custom field type in Madmin, use the madmin:field generator. This creates a CustomField class in app/madmin/fields/custom_field.rb and generates the necessary partial views for the Madmin interface.

    rails g madmin:field Custom
  3. Customize Madmin views using the generator

    main

    Madmin provides a generator to copy its default views into your Rails application. Once copied, you can modify these files to customize the admin interface's appearance and behavior. Running this generator copies all views used for every resource into your application.

    rails generate madmin:views
  4. Add CSS from your Rails application

    main

    To include custom stylesheets from your Rails application's asset pipeline in the Madmin interface, append the stylesheet name to the Madmin.stylesheets array. This is typically done in a Madmin initializer. The name should correspond to the file in app/assets/stylesheets/ (without the .css extension).

    Madmin.stylesheets << "my_stylesheet"
  5. Implement HTTP Basic Authentication for Madmin

    main

    For a simple authentication layer, you can use http_basic_authenticate_with inside app/controllers/madmin/application_controller.rb. It is best practice to pull the credentials from environment variables or Rails credentials.

    module Madmin
      class ApplicationController < Madmin::BaseController
        http_basic_authenticate_with(
          name: ENV['ADMIN_USERNAME'] || Rails.application.credentials.admin_username,
          password: ENV['ADMIN_PASSWORD'] || Rails.application.credentials.admin_password
        )
      end
    end
  6. Customize Madmin resource controllers

    main

    When you generate a Madmin resource, the generator creates a matching controller file that you can override to implement custom logic. For example, running rails g madmin:resource Post will generate app/controllers/madmin/posts_controller.rb.

    To understand the available methods and the structure of standard actions, you should refer to the base Madmin::ResourceController class.

    # Example command that generates a custom controller
    rails g madmin:resource Post
  7. Perform a complete asset override

    main
    If you need full control over the assets rendered in the Madmin interface, you can override the default asset rendering by creating a custom partial in your Rails application at app/views/madmin/_javascript.html.erb. In this file, you can manually define the javascript_importmap_tags and stylesheet_link_tag calls required for your specific setup.
  8. Add JavaScript using Importmaps

    main

    To add JavaScript packages to the Madmin interface, use Madmin.importmap.draw. This method accepts a path to a file containing your importmap lines. This is useful for integrating Gem-specific importmaps into the admin layout.

    Madmin.importmap.draw HotwireCombobox::Engine.root.join("config/hw_importmap.rb")
  9. Install Madmin in a Rails application

    main

    To install Madmin, add the gem to your Gemfile and run the installation generator. The generator will install the gem and automatically create resources for every model it detects in your application.

    bundle add madmin
    rails g madmin:install
  10. Customize Madmin views

    main

    Available view generators:

    • All views: rails generate madmin:views (includes index, new, edit, show, _form, application layout, javascript, and navigation)
    • Navigation only: rails g madmin:views:navigation -> app/views/madmin/_navigation.html.erb
    • Layout files: rails g madmin:views:layout (includes layout, javascript, and navigation) -> app/views/madmin/application.html.erb, app/views/madmin/_javascript.html.erb, app/views/madmin/_navigation.html.erb
    • Specific action: rails g madmin:views:index -> app/views/madmin/application/index.html.erb
    • Scoped to a model: rails generate madmin:views:index Book -> app/views/madmin/books/index.html.erb
    rails generate madmin:views
  11. Add CSS from a Gem

    main

    To include stylesheets provided by a Gem (for example, Hotwire Combobox), append the asset name to the Madmin.stylesheets array in a Madmin initializer. Madmin will resolve these assets through the Rails asset pipeline.

    Madmin.stylesheets << "hotwire_combobox"
  12. Create a custom field type

    main

    If the built-in field types are insufficient, you can generate a custom field class. This creates the class file and the necessary partials for the form, index, and show views.

    rails g madmin:field Custom

    After generating, use it in a resource:

    class PostResource < Madmin::Resource
      attribute :title, field: CustomField
    end