{"id":13747413,"url":"https://github.com/varvet/godmin","last_synced_at":"2025-11-11T18:23:06.706Z","repository":{"id":11044668,"uuid":"13380820","full_name":"varvet/godmin","owner":"varvet","description":"Admin framework for Rails 5+","archived":false,"fork":false,"pushed_at":"2022-10-20T17:45:24.000Z","size":1304,"stargazers_count":482,"open_issues_count":18,"forks_count":47,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-07-27T19:51:01.372Z","etag":null,"topics":["admin","rails","ruby"],"latest_commit_sha":null,"homepage":"http://godmin-sandbox.herokuapp.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/varvet.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-07T10:23:19.000Z","updated_at":"2025-07-27T12:10:30.000Z","dependencies_parsed_at":"2022-09-02T20:00:25.078Z","dependency_job_id":null,"html_url":"https://github.com/varvet/godmin","commit_stats":null,"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"purl":"pkg:github/varvet/godmin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varvet%2Fgodmin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varvet%2Fgodmin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varvet%2Fgodmin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varvet%2Fgodmin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/varvet","download_url":"https://codeload.github.com/varvet/godmin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/varvet%2Fgodmin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270423155,"owners_count":24580936,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-14T02:00:10.309Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["admin","rails","ruby"],"created_at":"2024-08-03T06:01:28.056Z","updated_at":"2025-11-11T18:23:06.653Z","avatar_url":"https://github.com/varvet.png","language":"Ruby","readme":"# Godmin\n\n[![Gem Version](http://img.shields.io/gem/v/godmin.svg)](https://rubygems.org/gems/godmin)\n[![Build Status](https://img.shields.io/travis/varvet/godmin/master.svg)](https://travis-ci.org/varvet/godmin)\n[![Code Climate](https://api.codeclimate.com/v1/badges/d8e5c7c54c1dba073689/maintainability)](https://codeclimate.com/github/varvet/godmin)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/d8e5c7c54c1dba073689/test_coverage)](https://codeclimate.com/github/varvet/godmin)\n\nGodmin is an admin framework for Rails 5+. Use it to build dedicated admin sections for your apps, or stand alone admin apps such as internal tools. It has support for common features such as scoping, filtering and performing batch actions on your models. Check out the [demo app](http://godmin-sandbox.herokuapp.com) and its [source code](https://github.com/varvet/godmin-sandbox) to get a feel for how it works.\n\nGodmin differs from tools like [ActiveAdmin](http://activeadmin.info/) and [RailsAdmin](https://github.com/sferik/rails_admin) in how admin sections are created. Rather than being DSL-based, Godmin is a set of opt-in modules and helpers that can be applied to regular Rails apps and engines. An admin section built with Godmin is just that, a regular Rails app or Rails engine, with regular routes, controllers and views. That means there is less to learn, because you already know most of it, and fewer constraints on what you can do. After all, administrators are users too, and what better way to provide them with a tailor made experience than building them a Rails app?\n\n![Screenshot](https://raw.githubusercontent.com/varvet/godmin/master/screenshot.png)\n\n- [Installation](#installation)\n  - [Standalone installation](#standalone-installation)\n  - [Engine installation](#engine-installation)\n  - [Installation artefacts](#installation-artefacts)\n- [Getting started](#getting-started)\n- [Resources](#resources)\n  - [Scopes](#scopes)\n  - [Filters](#filters)\n  - [Batch actions](#batch-actions)\n  - [Custom ordering](#custom-ordering)\n  - [Resource fetching, building and saving](#resource-fetching-building-and-saving)\n  - [Redirecting](#redirecting)\n  - [Pagination](#pagination)\n  - [Exporting](#exporting)\n  - [Nested resources](#nested-resources)\n- [Views](#views)\n  - [Forms](#forms)\n  - [Navigation](#navigation)\n- [Authentication](#authentication)\n  - [Built in authentication](#built-in-authentication)\n  - [Shared authentication](#shared-authentication)\n- [Authorization](#authorization)\n- [Localization](#localization)\n- [JavaScript](#javascript)\n- [Plugins](#plugins)\n- [Contributors](#contributors)\n- [License](#license)\n\n## Installation\n\nGodmin supports two common admin scenarios:\n\n1. Standalone installation\n2. Engine installation\n\nIf you want to set up an example app that you can play around with, run the following:\n```sh\nrails new sandbox --skip-spring -m https://raw.githubusercontent.com/varvet/godmin/master/template.rb\n```\n\n### Standalone installation\nUse for admin-only applications, or for architectures where the admin lives in its own app. E.g. you want to access the admin section at `localhost:3000`.\n\nAdd the gem to the application's `Gemfile`:\n```ruby\ngem \"godmin\"\n```\n\nBundle, then run the install generator:\n```sh\n$ bundle install\n$ bin/rails generate godmin:install\n```\n\nGodmin should be up and running at `localhost:3000`.\n\n### Engine installation\nUse when the admin is part of the same codebase as the main application. E.g. you want to access the admin section at `localhost:3000/admin`.\n\nGenerate a [mountable engine](http://guides.rubyonrails.org/engines.html):\n```sh\n$ bin/rails plugin new admin --mountable\n```\n\nAdd the engine to the application's `Gemfile`:\n```ruby\ngem \"admin\", path: \"admin\"\n```\n\nMount the engine in the application's `config/routes.rb`:\n```ruby\nmount Admin::Engine, at: \"admin\"\n```\n\nAdd the gem to the engine's gemspec, `admin/admin.gemspec`:\n```ruby\ns.add_dependency \"godmin\", \"~\u003e x.x.x\"\n```\n\nBundle, then run the install generator within the scope of the engine, i.e. note the leading `admin/`:\n```sh\n$ bundle install\n$ admin/bin/rails generate godmin:install\n```\n\nGodmin should be up and running at `localhost:3000/admin`\n\n### Installation artefacts\n\nInstalling Godmin does a number of things to the Rails application.\n\nThe application controller is modified as such:\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include Godmin::ApplicationController\nend\n```\n\nRequire statements are placed in both `app/assets/javascripts/application.js` and `app/assets/stylesheets/application.css`.\n\nIf Godmin was installed inside an engine, a `require \"godmin\"` statement is placed in `{namespace}/lib/{namespace}.rb`.\n\nAn `app/views/shared/_navigation.html.erb` partial is created.\n\nAnd finally, the `app/views/layouts` folder is removed by default, so as not to interfere with the Godmin layouts. It can be added back in case you wish to override the built in layouts.\n\n## Getting started\n\nGodmin deals primarily with resources. A resource is something that can be administered through the Godmin user interface, often a Rails model. Let's say the application has an `Article` model with attributes such as `title`, `body` and `published`. To get going quickly, we can use a generator:\n\n```sh\n$ bin/rails generate godmin:resource article title published\n```\n\nOr for an engine install:\n```sh\n$ admin/bin/rails generate godmin:resource article title published\n```\n\nThis does a number of things.\n\nIt inserts a route in the `config/routes.rb` file:\n\n```ruby\nresources :articles\n```\n\nIt inserts a `navbar_item` in the `app/views/shared/_navigation.html.erb` partial:\n\n```erb\n\u003c%= navbar_item Article %\u003e\n```\n\nIf Godmin was installed inside an engine, it creates a model class:\n\n```ruby\nmodule Admin\n  class Article \u003c ::Article\n  end\nend\n```\n\nIt creates a controller:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\nend\n```\n\nIt creates a service object:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  attrs_for_index :title, :published\n  attrs_for_show :title, :published\n  attrs_for_form :title, :published\nend\n```\n\nUsing `attrs_for_index` we can control what fields are displayed in the table listing, using `attrs_for_show` we can control what fields are displayed on the show page, and using `attrs_for_form` we can control what fields are available in the new and edit forms. We can, for instance, add the `body` field to `attrs_for_form` to make it appear in forms:\n\n```ruby\nattrs_for_form :title, :body, :published\n```\n\nFor quick prototyping, we could build the parameters this way (if appropriate).\n```ruby\nattrs_for_show *Article.column_names\n```\n\nBy now we have a basic admin interface for managing articles.\n\n## Resources\n\nAs we saw in the example above, resources are divided into controllers and service objects. Actions, redirects, params permitting etc go in the controller while resource fetching, building, sorting, filtering etc go in the service object. This makes the service objects small and easy to test.\n\nWe have already seen three methods at play: `attrs_for_index`, `attrs_for_show` and `attrs_for_form`. We will now look at some additional resource concepts.\n\n### Scopes\n\nScopes are a way of sectioning resources, useful for quick navigation, and can be created as follows:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  scope :unpublished, default: true\n  scope :published\n\n  def scope_unpublished(resources)\n    resources.where(published: false)\n  end\n\n  def scope_published(resources)\n    resources.where(published: true)\n  end\nend\n```\n\n### Filters\n\nFilters offer great flexibility when it comes to searching for resources, and can be created as follows:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  filter :title\n\n  def filter_title(resources, value)\n    resources.where(\"title LIKE ?\", \"%#{value}%\")\n  end\nend\n```\n\nThere are three types of filters: `string`, `select` and `multiselect`, specified using the `as` parameter.\n\nWhen using `select` or `multiselect`, a collection must be specified. The collection must conform to the format used by Rails `options_for_select` helpers. It can be either an array consisting of name/value tuples, or a collection of ActiveRecords.\n\n```ruby\nfilter :category, as: :select, collection: -\u003e { [[\"News\", 1], [\"Posts\", 2]] }\n```\n\nWhen specifying a collection of ActiveRecords, two additional parameters, `option_text` and `option_value` can be specified. They default to `to_s` and `id` respectively.\n\n```ruby\nfilter :category, as: :select, collection: -\u003e { Category.all }, option_text: \"title\"\n```\n\n### Batch actions\n\nBatch actions can be created as follows:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  batch_action :publish\n  batch_action :unpublish\n  batch_action :destroy, confirm: true\n\n  def batch_action_publish(resources)\n    resources.each(\u0026:publish!)\n  end\nend\n```\n\nIn addition, batch actions can be defined per scope using `only` and `except`:\n\n```ruby\nbatch_action :publish, only: [:unpublished]\nbatch_action :unpublish, only: [:published]\n```\n\nIf you wish to implement your own redirect after a batch action, it needs to be implemented in the controller:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\n\n  private\n\n  def redirect_after_batch_action_publish\n    articles_path(scope: :published)\n  end\nend\n```\n\nIf you are using Godmin's built in authorization functionality you must [authorize your batch actions in your policy](#batch-action-authorization).\n\n### Custom ordering\n\nBy default, Godmin supports ordering of database columns in the index view table. However, it cannot automatically sort associations, custom attributes and so on.\nIf you want to order something that Godmin doesn't support out of the box, or you just want to customize how a columns is ordered, you can implement your own ordering functionality in the service object by creating a `order_by_\u003cattribute\u003e` method.\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n  attrs_for_index :title, :author\n\n  # resources is an ActiveRecord::Relation object\n  # direction is the order direction (\"asc\" or \"desc\")\n  def order_by_author(resources, direction)\n    resources.joins(:authors).order(\"authors.name #{direction}\")\n  end\nend\n```\n\n### Resource fetching, building and saving\n\nResources are made available to the views through instance variables. The index view can access the resources using `@resources` while show, new and edit can access the single resource using `@resource`. In addition, the resource class is available as `@resource_class` and the service object is available as `@resource_service`.\n\nIn order to modify resource fetching and construction, these methods can be overridden per resource service:\n\n- `resource_class`\n- `resources_relation`\n- `resources`\n- `find_resource`\n- `build_resource`\n- `create_resource`\n- `update_resource`\n- `destroy_resource`\n\nTo change the class name of the resource from the default based on the service class name:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  def resource_class\n    FooArticle\n  end\nend\n```\n\nTo scope resources for quering and building, e.g. based on the signed in user:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  # The signed in admin user is available to all service objects via the options hash\n  def resources_relation\n    super.where(user: options[:admin_user])\n  end\nend\n```\n\nTo add to the index page resources query, e.g. to change the default order:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  def resources(params)\n    super(params).order(author: :desc)\n  end\nend\n```\n\nTo change the way a resource is fetched for `show`, `edit`, `update` and `destroy` actions:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  def find_resource(id)\n    resources_relation.find_by(slug: id)\n  end\nend\n```\n\nTo change the way a resource is constructed for `new` and `create` actions:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  def build_resource(_params)\n    article = super\n    article.setup_more_things\n    article\n  end\nend\n```\n\nTo change the way a resource is saved in the `create` action:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  # This method should return true or false\n  def create_resource(resource)\n    resource.save_in_some_interesting_way\n  end\nend\n```\n\nTo change the way a resource is saved in the `update` action:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  # This method should return true or false\n  def update_resource(resource, params)\n    resource.assign_attributes(params)\n    resource.save_in_some_interesting_way\n  end\nend\n```\n\nTo change the way a resource is destroyed in the `destroy` action:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  def destroy_resource(resource)\n    resource.paranoid_destroy\n  end\nend\n```\n\n#### Strong parameters\n\nWhen using `attrs_for_form`, parameters are automatically permitted. If building a custom form, see the [forms](#forms) section, parameters can be permitted by overriding the `resource_params` method in the controller:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\n\n  private\n\n  def resource_params\n    params.require(:article).permit(:title, :body)\n  end\nend\n```\n\n#### Passing parameters to the service object\n\nSometimes you want to pass additional params to the service object, other that those passed in `resource_params`. In order to do this, you need to pass them along when initializing the service object in the controller:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\n\n  private\n\n  def resource_service\n    service = super\n    service.options[:some_param] = params[:some_param]\n    service\n  end\nend\n```\n\nYou can then access it from the service object:\n\n```ruby\nclass ArticleService\n  include Godmin::Resources::ResourceService\n\n  def some_method\n    options[:some_param]\n  end\nend\n```\n\n### Redirecting\n\nBy default the user is redirected to the resource show page after create and update, and to the index page after destroy. To change this, there are four controller methods that can be overridden: `redirect_after_create`, `redirect_after_update`, `redirect_after_save`, and `redirect_after_destroy`.\n\nFor instance, to have the article controller redirect to the index page after both create and update:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\n\n  private\n\n  def redirect_after_save\n    articles_path\n  end\nend\n```\n\nOr, to have the article controller redirect to the index page after create and the edit page after update:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\n\n  private\n\n  def redirect_after_create\n    articles_path\n  end\n\n  def redirect_after_update\n    edit_article_path(@resource)\n  end\nend\n```\n\nIf you wish to change the behaviour for every resource controller, consider creating a common resource controller that your other controllers can inherit from:\n\n```ruby\nclass ResourceController \u003c ApplicationController\n  include Godmin::Resources::ResourceController\n\n  private\n\n  def redirect_after_save\n    resource_class.model_name.route_key.to_sym\n  end\nend\n```\n\n### Pagination\n\nIf you wish to change the number of resources per page, you can override the `per_page` method in the service object:\n\n```ruby\nclass ArticlesService\n  include Godmin::Resources::ResourceService\n\n  def per_page\n    50\n  end\nend\n```\n\n### Exporting\n\nThe `attrs_for_export` method in the service object makes it possible to mark attributes or methods on the model as exportable. When implemented, an export button will appear on the index page with options for both CSV and JSON export.\n\n```ruby\nclass ArticlesService\n  include Godmin::Resources::ResourceService\n\n  attrs_for_export :id, :title, :created_at, :updated_at\nend\n```\n\n### Nested resources\n\nNested resources can be implemented by nesting your routes:\n\n```ruby\nresources :blogs do\n  resources :blog_posts\nend\n```\n\nThis will set up scoping of the nested resource as well as correct links in the breadcrumb.\n\nIf you want to add a link to the nested resource from the parent's show and edit pages, you can add the following to the service object:\n\n```ruby\nclass BlogService\n  include Godmin::Resources::ResourceService\n\n  has_many :blog_posts\nend\n```\n\nOtherwise, simply add links as you see fit using partial overrides.\n\n## Views\n\nIt's easy to override view templates and partials in Godmin, both globally and per resource. All you have to do is place a file with an identical name in your `app/views` directory. For instance, to override the `godmin/resource/index.html.erb` template for all resources, place a file under `app/views/resource/index.html.erb`. If you only wish to override it for articles, place it instead under `app/views/articles/index.html.erb`.\n\nYou can also inherit from the default template as such:\n```erb\n\u003c%= render template: \"godmin/resource/show\" %\u003e\n\n\u003cp\u003eAppend stuff here\u003c/p\u003e\n```\n\nIf you wish to customize the content of a table column, you can place a partial under `app/views/{resource}/columns/{column_name}.html.erb`, e.g. `app/views/articles/columns/_title.html.erb`. The resource is available to the partial through the `resource` variable.\n\nThe full list of templates and partials that can be overridden [can be found here](https://github.com/varvet/godmin/tree/master/app/views/godmin).\n\n### Forms\n\nOftentimes, the default form provided by Godmin doesn't cut it. The `godmin/resource/_form.html.erb` partial is therefore one of the most common to override per resource.\n\nGodmin comes with its own FormBuilder that automatically generates bootstrapped markup. It is based on the [Rails Bootstrap Forms](https://github.com/bootstrap-ruby/rails-bootstrap-forms) FormBuilder, and all its methods are directly available. In addition it has a few convenience methods that can be leveraged.\n\nThe `input` method will automatically detect the type of field from the database and generate an appropriate form field:\n\n```ruby\nform_for @resource do |f|\n  f.input :attribute\nend\n```\n\n### Navigation\n\nGodmin comes with built in view helpers for generating the navbar.\n\nThe `navbar_item` helper generates a link in the navbar. It can be used in a number of different ways.\n\n```ruby\n# Links to the index page of the article resource\nnavbar_item Article\n\n# Links to a custom path with a custom link text\nnavbar_item Article, articles_path(scope: :published) do\n  \"Published articles\"\nend\n\n# Links to a custom path with a custom link text without specifying resource\nnavbar_item \"Some text\", some_path\n```\n\nThe `show` option can be passed a proc that evaluates to true or false. This is used to control if the link should be shown or not. By default it checks against the resource policy object if authorization is enabled.\n\n```ruby\nnavbar_item Article, show: -\u003e { show? }\n```\n\nThe `icon` option can be passed a glyphicon:\n\n```ruby\nnavbar_item Article, icon: \"book\"\n```\n\nThe `navbar_dropdown` and `navbar_divider` helpers can be used to build dropdown menus.\n\n```ruby\nnavbar_dropdown \"Multiple things\" do\n  navbar_item Article\n  navbar_item Comment\n  navbar_divider\n  navbar_item User\nend\n```\n\n## Authentication\n\nMultiple authentication scenarios are supported. Godmin comes with a lightweight built in authentication solution that can be used to sign in to the admin section via the admin interface. In addition, when running an admin engine, it is possible to set up a shared authentication solution so that administrators can sign in via the main app.\n\n### Built in authentication\n\nThis example uses the built in authentication solution. Authentication is isolated to the admin section and administrators sign in via the admin interface.\n\nGodmin comes with a generator that creates an admin user model and enables the built in authentication:\n\n```sh\n$ bin/rails generate godmin:authentication\n$ bin/rake db:migrate\n```\n\nPlease note: when installing to an admin engine, the migration needs to be moved to the main app before it can be found by `db:migrate`. Rails has a solution in place for this:\n\n```sh\n$ admin/bin/rails generate godmin:authentication\n$ bin/rake admin:install:migrations\n$ bin/rake db:migrate\n```\n\nA model is generated:\n\n```ruby\nclass AdminUser \u003c ActiveRecord::Base\n  include Godmin::Authentication::User\n\n  def self.login_column\n    :email\n  end\nend\n```\n\nBy default the user model is called `AdminUser`. If you'd like to change this, you can pass an argument to the authentication generator:\n\n```\n$ bin/rails generate godmin:authentication SuperUser\nor for an engine:\n$ admin/bin/rails generate godmin:authentication SuperUser\n```\n\nBy default the model is generated with an `email` field as the login column. This can changed in the migration prior to migrating if, for instance, a `username` column is more appropriate.\n\nThe following route is generated:\n\n```ruby\nresource :session, only: [:new, :create, :destroy]\n```\n\nAlong with a sessions controller:\n\n```ruby\nclass SessionsController \u003c ApplicationController\n  include Godmin::Authentication::SessionsController\nend\n```\n\nFinally, the application controller is modified:\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include Godmin::ApplicationController\n  include Godmin::Authentication\n\n  def admin_user_class\n    AdminUser\n  end\nend\n```\n\nAuthentication is now required when visiting the admin section.\n\n### Shared authentication\n\nThis example uses [Devise](https://github.com/plataformatec/devise) to set up a shared authentication solution between the main app and an admin engine. Administrators sign in and out via the main application.\n\nThere is no need to run a generator in this instance. Simply add the authentication module to the admin application controller like so:\n\n```ruby\nmodule Admin\n  class ApplicationController \u003c ActionController::Base\n    include Godmin::ApplicationController\n    include Godmin::Authentication\n  end\nend\n```\n\nProvided you have `User` model set up with Devise in the main application, override the following three methods in the admin application controller:\n\n```ruby\nmodule Admin\n  class ApplicationController \u003c ActionController::Base\n    include Godmin::ApplicationController\n    include Godmin::Authentication\n\n    def authenticate\n      authenticate_user!\n    end\n\n    def admin_user\n      current_user\n    end\n\n    def admin_user_signed_in?\n      user_signed_in?\n    end\n  end\nend\n```\n\nThe admin section is now authenticated using Devise.\n\n### Disable authentication\n\nIf you want to disable authentication for a single controller or controller action, use the following `before_action`:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  prepend_before_action :disable_authentication\nend\n```\n\n## Authorization\n\nIn order to enable authorization, authentication must first be enabled. See the previous section. The Godmin authorization system uses [Pundit](https://github.com/elabs/pundit).\n\nAdd the authorization module to the application controller:\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include Godmin::ApplicationController\n  include Godmin::Authentication\n  include Godmin::Authorization\n\n  ...\nend\n```\n\nPolicies can be generated using the following command:\n\n```sh\n$ bin/rails generate godmin:policy article\n```\n\nThis file `app/policies/article_policy.rb` will be created:\n\n```ruby\nclass ArticlePolicy \u003c Godmin::Authorization::Policy\nend\n```\n\nPermissions are specified by implementing methods on this class. Two methods are available to the methods, `user` and `record`, the signed in user and the record being authorized. An implemented policy can look something like this:\n\n```ruby\nclass ArticlePolicy \u003c Godmin::Authorization::Policy\n  def index?\n    true\n  end\n\n  def show?\n    true\n  end\n\n  def create?\n    user.editor?\n  end\n\n  def update?\n    user.editor? \u0026\u0026 record.unpublished?\n  end\n\n  def destroy?\n    update?\n  end\n\n  def batch_action_destroy?\n    destroy?\n  end\nend\n```\n\nThat is, everyone can list and view articles, only editors can create them, and only unpublished articles can be updated and destroyed.\n\n### Handle unauthorized access\n\nWhen a user is not authorized to access a resource, a `Pundit::NotAuthorizedError` is raised. By default this error is rescued by Godmin and turned into a status code `403 Forbidden` response. If you want to change this behaviour you can rescue the error yourself in the appropriate `ApplicationController`:\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  include Godmin::ApplicationController\n  include Godmin::Authentication\n  include Godmin::Authorization\n\n  # Renders 404 page and returns status code 404.\n  rescue_from Pundit::NotAuthorizedError do\n    render file: \"#{Rails.root}/public/404.html\", status: 404, layout: false\n  end\nend\n```\n\n### Override policy object\n\nIf you wish to specify what policy to use manually, override the following method in your model. It does not have to be an ActiveRecord object, but any object will do.\n\n```ruby\nclass Article\n  def policy_class(_record)\n    FooArticlePolicy\n  end\nend\n```\n\n### Batch action authorization\n\nBatch actions must be authorized in your policy if you are using Godmin's built in authorization functionality. The policy method is called with the relation containing all records to be processed.\n\n```ruby\nclass ArticlePolicy \u003c Godmin::Authorization::Policy\n  def batch_action_destroy?\n    record.all? { |r| r.user_id == user.id }\n  end\nend\n```\n\n### Disable authorization\n\nIf you want to disable authorization for a single controller or controller action, use the following `before_action`:\n\n```ruby\nclass ArticlesController \u003c ApplicationController\n  prepend_before_action :disable_authorization\nend\n```\n\n### Authorization in Engines\n\nWhen Godmin is installed as an engine, it expects policies to be defined\nwithin the engine: eg. `Admin::ArticlePolicy` defined in\n`admin/app/policies/article_policy.rb`.\n\nIf your admin application is itself broken up into several engines, then\neither\n\n1. the policies for those engines need to live in the main engine, or\n2. those engines need to be namespaced under the namespace of the main engine.\n\nHere is one example of a directory structure for approach 2:\n\n```\nadmin\n  ├── app\n  │   └── policies\n  │       └── admin\n  │           └── article_policy.rb\n  └── engines\n      └── content\n          └── policies\n              └── admin\n                  └── content\n                      └── text_block_policy.rb\napp\n  └── models\n      └── article.rb\nengines\n  └── content\n        └── models\n            └── content\n                └── text_block.rb\n```\n```ruby\n# admin/engines/content/policies/admin/content/text_block_policy.rb\nmodule Admin\n  module Content\n    class TextBlockPolicy \u003c ::Admin::ApplicationPolicy\n    end\n  end\nend\n```\n\n## Localization\n\nGodmin supports localization out of the box. For a list of translatable strings, [look here](https://github.com/varvet/godmin/blob/master/config/locales/en.yml).\n\nStrings can be translated both globally and per resource, similar to how views work. For instance, to translate the `godmin.batch_actions.buttons.select_all` string globally:\n\n```yml\ngodmin:\n  batch_actions:\n    buttons:\n      select_all: {translation}\n```\n\nOr, translate for a specific resource:\n\n```yml\ngodmin:\n  article:\n    batch_actions:\n      buttons:\n        select_all: {translation}\n```\n\nIn addition, all scopes, filters and batch actions that are added, can be localized:\n\n```yml\ngodmin:\n  article:\n    batch_actions:\n      labels:\n        publish: {translation}\n        unpublish: {translation}\n    filters:\n      labels:\n        title: {translation}\n    scopes:\n      labels:\n        unpublished: {translation}\n        published: {translation}\n```\n\nGodmin comes with built in support for English and Swedish.\n\nThere is a view helper available named `translate_scoped` that can be used in overridden views. Please see the source code for information on how to use it.\n\n## JavaScript\n\nGodmin comes with a small set of JavaScript components and APIs.\n\n### Datetimepickers\n\nMake a [bootstrap-datetimepicker](https://github.com/Eonasdan/bootstrap-datetimepicker) out of a text field:\n\n```ruby\nf.date_field :date\nf.datetime_field :date\n```\n\nIf the field is added post page render, it can be initialized manually:\n\n```js\nGodmin.Datetimepickers.initializeDatepicker($el);\nGodmin.Datetimepickers.initializeTimepicker($el);\nGodmin.Datetimepickers.initializeDatetimepicker($el);\n```\n\nAdditional options can be passed down to bootstrap-datetimepicker:\n\n```js\nGodmin.Datetimepickers.initializeDatetimepicker($el, {\n  useMinutes: false,\n  useSeconds: false\n});\n```\n\nIf you wish to translate the datetimepicker, change `moment/en-gb` in your `app/assets/javascripts/application.js` to your desired locale:\n\n```js\n//= require moment\n//= require moment/{locale} // e.g. moment/sv\n//= require godmin\n```\n\nPlease note that the datepickers default to en-GB, not en-US, because Rails cannot automatically parse en-US dates.\n\nTo use an alternative format, use the format option.\n\n```js\nGodmin.Datetimepickers.initializeDatepicker($elems, {\n  format: 'YYYY-MM-DD'\n});\n```\n\n### Select boxes\n\nMake a [selectize.js](http://brianreavis.github.io/selectize.js/) select box out of a text field or select box:\n\n```ruby\nf.select :authors, Author.all, {}, data: { behavior: \"select-box\" }\nf.text_field :tag_list, data: { behavior: \"select-box\" }\n```\n\nIf you want to change the text that appears when an option does not exist and will be created, set the data attribute `data-add-label`.\n\n```ruby\nf.text_field :tag_list, data: { behavior: \"select-box\", add_label: \"Create:\" }\n#=\u003e Create: foobar...\n```\n\nIf the field is added post page render, it can be initialized manually:\n\n```js\nGodmin.SelectBoxes.initializeSelectBox($el);\n```\n\nAdditional options can be passed down to selectize:\n\n```js\nGodmin.SelectBoxes.initializeSelectBox($el, {\n\tcreate: true\n});\n```\n\n## Plugins\n\nSome additional features are available as plugins:\n\n- [Godmin Uploads](https://github.com/varvet/godmin-uploads)\n- [Godmin Tags](https://github.com/varvet/godmin-tags)\n- [Godmin Redactor](https://github.com/varvet/godmin-redactor)\n\n## Contributors\n\nhttps://github.com/varvet/godmin/graphs/contributors\n\n## License\n\nLicensed under the MIT license. See the separate MIT-LICENSE file.\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvarvet%2Fgodmin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvarvet%2Fgodmin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvarvet%2Fgodmin/lists"}