Low commit activity in last 3 years
No release in over a year
Extends the ApplicationController in Administrate
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.13.0
~> 2.0
~> 5.0
~> 6.0
~> 13.0
 Project Readme

Administrate::BaseController

Build Status: master Gem Version MIT license

Extends the ApplicationController in Administrate

Installation

Add this line to your application's Gemfile:

gem 'administrate-base_controller'

And then execute:

$ bundle

Or install it yourself as:

$ gem install administrate-base_controller

Usage

When you require this gem, you can include Administrate::BaseController in your admin ApplicationController. Alternatively require administrate/base_controller/engine instead (e.g. in your Gemfile). The base controller functionality will be added to the administrate/application_controller.

You get the following protected methods for free, which you may override:

method short description
search_term params[:search] Allows you to assign custom behaviour for the search_term on index pages
index_scoped_resource scoped_resource Allows you to overwrite which resources are shown on index or passed to search
index_resources Administrate::Search Allows you to turn off search on index, by overwriting this
index_page Administrate::Page::Collection Allows you to overwrite the index page
show_page Administrate::Page::Show Allows you to overwrite the show page
new_page Administrate::Page::Form Allows you to overwrite the new page
edit_page new_page Allows you to overwrite the edit page
edit_page new_page Allows you to overwrite the edit page
authorize_resource show_action?(action_name.to_sym, resource) Allows you to change how resources are authorized
resource_params Calls read_param(k, v) for each instead of transform_values Allows you to change how params are read
read_param Calls read_param_value if applicable Allows you to change how a param is read based on its key
render_index_json json: resources.to_json Easily override json responses on the index route without changing the resources logic
render_index_any locals: resources, page, ... Easily override any responses on the index route without changing the resources logic
render_show_json json: resource.to_json Easily override json responses on the show route without changing the resources logic
render_show_any locals: resource, page, ... Easily override any responses on the show route without changing the resources logic
module Admin
  class ApplicationController < Administrate::ApplicationController
    # everything is available now
  end
end

Integrate with CanCan(Can)

This automatically hides links if the current_admin_user does not have the ability to action the resource.

module Admin
  class ApplicationController < Administrate::ApplicationController
    def current_ability
      @_current_ability ||= Ability.new(current_admin_user)
     end

    def show_action?(action, resource)
      current_ability.can?(action.to_sym, resource)
    end
  end
end

Additionally you can correctly hide resources the current_ability can not action:

module Admin
  class ApplicationController < Administrate::ApplicationController

    # ...

    def scoped_resource
      super.accessible_by(current_ability, action_name.to_sym)
    end
  end
end

Integrate with FriendlyId

This works without this gem, but this gem allows you to changed scoped_resource and index_scoped_resource easily.

module Admin
  class BookController < ::Admin::ApplicationController
    def find_resource(param)
      scoped_resource.friendly.find(param)
    end
  end
end

Only show subset of resources on index

You might want to scope the index to a current view (like stored in Admin::Current.view), but not 404 if the resource is accessed directly:

module Admin
  class BookController < ::Admin::ApplicationController
    def index_scoped_resource
      super.where(author: Current.author)
    end
  end
end

This only shows the books with the Current.author, but if you access /book/uuid, it will find the book even if its by a different author.

Preset attributes on a new resource

You might want to preset certain attributes when creating a new resource. You can do so by overriding new_resource.

module Admin
  class BookController < ::Admin::ApplicationController
    def new_resource
      resource_class.new(author: Current.author)
    end
  end
end

Deserialize attributes (e.g. JSON field)

If you want to deserialize an attribute's value before it's assigned to the resource, for example from a custom JSON field, which contents have been serialized, you can overwrite read_param:

module Admin
  class BookController < ::Admin::ApplicationController
    JSON_FIELDS = %w[options content].freeze

    def read_param(key, value)
      return Oj.load(value) if JSON_FIELDS.include?(String(key))
      super(key, value)
    end
  end
end

Alternatively you can use the administrate-serialized_fields gem.

Related

  • Administrate: A Rails engine that helps you put together a super-flexible admin dashboard.

Concerns

Fields

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at XPBytes/administrate-base_controller.