Extends the ApplicationController in Administrate
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
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_termonindexpages | 
| index_scoped_resource | scoped_resource | Allows you to overwrite which resources are shown on indexor 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 indexpage | 
| show_page | Administrate::Page::Show | Allows you to overwrite the showpage | 
| new_page | Administrate::Page::Form | Allows you to overwrite the newpage | 
| edit_page | new_page | Allows you to overwrite the editpage | 
| edit_page | new_page | Allows you to overwrite the editpage | 
| 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 oftransform_values | Allows you to change how params are read | 
| read_param | Calls read_param_valueif 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
endThis 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
endAdditionally 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
endThis 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
endYou 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
endThis 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.
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
endIf 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
endAlternatively you can use the administrate-serialized_fields gem.
- Administrate: A Rails engine that helps you put together a super-flexible admin dashboard.
- Administrate::DefaultOrder: π’ Sets the default order for a resource in a administrate controller.
- Administrate::SerializedFields: π Automatically deserialize administrate fields on form submit.
- Administrate::Field::Code: π A- textfield that shows code.
- Administrate::Field::Hyperlink: π A- stringfield that is shows a hyperlink.
- Adminisrtate::Field::JsonEditor: π A- textfield that shows a JSON editor.
- Administrate::Field::LazyBelongsTo: π An input field that lazy loads- belongs_tovalues.
- Administrate::Field::ScopedBelongsTo: π A- belongs_tofield that yields itself to the scope- lambda.
- Administrate::Field::ScopedHasMany: π A- has_manyfield that yields itself to the scope- lambda.
- Administrate::Field::TimeAgo: π A- date_timefield that shows its data as- time_agosince.
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.
Bug reports and pull requests are welcome on GitHub at XPBytes/administrate-base_controller.