Skip to content

jubilee2/administrate-field-button

Repository files navigation

Administrate::Field::Button

Overview

Administrate Field Button enhances your Administrate dashboard with customizable buttons. This gem provides a flexible way to add buttons to your resource views.

Installation

Step 1: Add Gem to Gemfile

gem 'administrate-field-button', github: 'jubilee2/administrate-field-button'

Step 2: Install Gem

bundle install

Usage

Step 1: Add Custom Field

class MyResourceDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    my_button: Field::Button,
    my_button2: Field::Button.with_options(
      label_of_button: 'Foo action',
      method: :post,
      branch: ->(field) { field.resource.active? },
      confirm: false
    )
  }.freeze
end

Step 2: Define Dummy Button Method

class Book < ApplicationRecord
  attr_reader :my_button, :my_button2
end

Step 3: Configure Route

namespace :admin do
  resources :my_resource do
    member do
      post :my_button
    end
  end
end

Step 4: Implement Button Action

def my_button
  # Custom logic here
  flash[:notice] = 'Action done successfully'
  redirect_back fallback_location: root_path
end

Customization Options

Field::Button Options

  • label_of_button (String): Button label.

  • method (Symbol): HTTP method (:post, :put, :delete). Default: :post.

  • branch (Proc): Conditional display based on resource attributes.

  • confirm (Boolean): Confirmation dialog. Default: false.

Example

my_button: Field::Button.with_options(
  label_of_button: 'Custom Action',
  method: :patch,
  branch: ->(field) { field.resource.published? && field.resource.approved? },
  confirm: true
)

Contributing

We welcome bug reports and pull requests on GitHub.

License

This gem is available as open source under the terms of the MIT License.

Additional Resources

For more information on Administrate and custom fields, refer to the following resources: