godmin 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/.hound.yml +2 -0
  4. data/.rubocop.yml +7 -0
  5. data/.travis.yml +7 -0
  6. data/Gemfile +11 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +523 -0
  9. data/Rakefile +34 -0
  10. data/app/assets/images/godmin/.keep +0 -0
  11. data/app/assets/javascripts/godmin/application.js +19 -0
  12. data/app/assets/javascripts/godmin/batch_actions.js +42 -0
  13. data/app/assets/javascripts/godmin/navigation.js +9 -0
  14. data/app/assets/javascripts/godmin/select_tags.js +8 -0
  15. data/app/assets/stylesheets/godmin/application.css.scss +62 -0
  16. data/app/views/godmin/application/welcome.html.erb +65 -0
  17. data/app/views/godmin/resource/_actions.html.erb +10 -0
  18. data/app/views/godmin/resource/_batch_actions.html.erb +12 -0
  19. data/app/views/godmin/resource/_breadcrumb.html.erb +21 -0
  20. data/app/views/godmin/resource/_errors.html.erb +9 -0
  21. data/app/views/godmin/resource/_filters.html.erb +23 -0
  22. data/app/views/godmin/resource/_form.html.erb +12 -0
  23. data/app/views/godmin/resource/_pagination.html.erb +6 -0
  24. data/app/views/godmin/resource/_scopes.html.erb +11 -0
  25. data/app/views/godmin/resource/_table.html.erb +36 -0
  26. data/app/views/godmin/resource/columns/_actions.html.erb +11 -0
  27. data/app/views/godmin/resource/edit.html.erb +5 -0
  28. data/app/views/godmin/resource/index.html.erb +17 -0
  29. data/app/views/godmin/resource/new.html.erb +5 -0
  30. data/app/views/godmin/resource/show.html.erb +1 -0
  31. data/app/views/godmin/sessions/new.html.erb +9 -0
  32. data/app/views/godmin/shared/_navigation.html.erb +29 -0
  33. data/app/views/kaminari/_first_page.html.erb +3 -0
  34. data/app/views/kaminari/_gap.html.erb +3 -0
  35. data/app/views/kaminari/_last_page.html.erb +3 -0
  36. data/app/views/kaminari/_next_page.html.erb +3 -0
  37. data/app/views/kaminari/_page.html.erb +3 -0
  38. data/app/views/kaminari/_paginator.html.erb +15 -0
  39. data/app/views/kaminari/_prev_page.html.erb +3 -0
  40. data/app/views/layouts/godmin/_content.html.erb +13 -0
  41. data/app/views/layouts/godmin/_layout.html.erb +13 -0
  42. data/app/views/layouts/godmin/application.html.erb +17 -0
  43. data/app/views/layouts/godmin/login.html.erb +18 -0
  44. data/bin/rails +8 -0
  45. data/config/locales/en.yml +41 -0
  46. data/config/locales/sv.yml +41 -0
  47. data/config/routes.rb +2 -0
  48. data/godmin.gemspec +32 -0
  49. data/lib/generators/godmin/authentication/authentication_generator.rb +61 -0
  50. data/lib/generators/godmin/install/install_generator.rb +34 -0
  51. data/lib/generators/godmin/policy/policy_generator.rb +29 -0
  52. data/lib/generators/godmin/resource/resource_generator.rb +56 -0
  53. data/lib/godmin.rb +24 -0
  54. data/lib/godmin/application.rb +37 -0
  55. data/lib/godmin/authentication.rb +35 -0
  56. data/lib/godmin/authentication/sessions.rb +45 -0
  57. data/lib/godmin/authentication/user.rb +27 -0
  58. data/lib/godmin/authorization.rb +30 -0
  59. data/lib/godmin/authorization/policy.rb +40 -0
  60. data/lib/godmin/authorization/policy_finder.rb +28 -0
  61. data/lib/godmin/engine.rb +4 -0
  62. data/lib/godmin/generators/base.rb +13 -0
  63. data/lib/godmin/helpers/application.rb +6 -0
  64. data/lib/godmin/helpers/batch_actions.rb +17 -0
  65. data/lib/godmin/helpers/filters.rb +108 -0
  66. data/lib/godmin/helpers/tables.rb +41 -0
  67. data/lib/godmin/helpers/translations.rb +19 -0
  68. data/lib/godmin/rails.rb +36 -0
  69. data/lib/godmin/resolver.rb +46 -0
  70. data/lib/godmin/resource.rb +126 -0
  71. data/lib/godmin/resource/batch_actions.rb +45 -0
  72. data/lib/godmin/resource/filters.rb +41 -0
  73. data/lib/godmin/resource/ordering.rb +25 -0
  74. data/lib/godmin/resource/pagination.rb +11 -0
  75. data/lib/godmin/resource/scopes.rb +49 -0
  76. data/lib/godmin/version.rb +3 -0
  77. data/lib/tasks/godmin_tasks.rake +4 -0
  78. data/test/dummy/README.rdoc +28 -0
  79. data/test/dummy/Rakefile +6 -0
  80. data/test/dummy/app/assets/images/.keep +0 -0
  81. data/test/dummy/app/assets/javascripts/application.js +13 -0
  82. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  83. data/test/dummy/app/controllers/application_controller.rb +5 -0
  84. data/test/dummy/app/controllers/concerns/.keep +0 -0
  85. data/test/dummy/app/helpers/application_helper.rb +2 -0
  86. data/test/dummy/app/mailers/.keep +0 -0
  87. data/test/dummy/app/models/.keep +0 -0
  88. data/test/dummy/app/models/concerns/.keep +0 -0
  89. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  90. data/test/dummy/bin/bundle +3 -0
  91. data/test/dummy/bin/rails +4 -0
  92. data/test/dummy/bin/rake +4 -0
  93. data/test/dummy/config.ru +4 -0
  94. data/test/dummy/config/application.rb +23 -0
  95. data/test/dummy/config/boot.rb +5 -0
  96. data/test/dummy/config/database.yml +25 -0
  97. data/test/dummy/config/environment.rb +5 -0
  98. data/test/dummy/config/environments/development.rb +29 -0
  99. data/test/dummy/config/environments/production.rb +80 -0
  100. data/test/dummy/config/environments/test.rb +36 -0
  101. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  102. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  103. data/test/dummy/config/initializers/inflections.rb +16 -0
  104. data/test/dummy/config/initializers/mime_types.rb +5 -0
  105. data/test/dummy/config/initializers/secret_token.rb +12 -0
  106. data/test/dummy/config/initializers/session_store.rb +3 -0
  107. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  108. data/test/dummy/config/locales/en.yml +23 -0
  109. data/test/dummy/config/routes.rb +4 -0
  110. data/test/dummy/db/test.sqlite3 +0 -0
  111. data/test/dummy/lib/assets/.keep +0 -0
  112. data/test/dummy/public/404.html +58 -0
  113. data/test/dummy/public/422.html +58 -0
  114. data/test/dummy/public/500.html +57 -0
  115. data/test/dummy/public/favicon.ico +0 -0
  116. data/test/godmin_test.rb +7 -0
  117. data/test/integration/navigation_test.rb +10 -0
  118. data/test/lib/godmin/policy_finder_test.rb +30 -0
  119. data/test/lib/godmin/resolver_test.rb +31 -0
  120. data/test/test_helper.rb +21 -0
  121. data/vendor/assets/images/godmin/chosen-sprite.png +0 -0
  122. data/vendor/assets/images/godmin/chosen-sprite@2x.png +0 -0
  123. data/vendor/assets/javascripts/.keep +0 -0
  124. data/vendor/assets/stylesheets/.keep +0 -0
  125. metadata +361 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2323184734c0c148b5bc68e99b95990a6e4febe
4
+ data.tar.gz: ab3409ae076a133ad899330152f8cea23f6a120b
5
+ SHA512:
6
+ metadata.gz: e8ab917763ef63814cd44d76623be142488eb5480bf12d7ef3ae248fd26dea65fcf7488b8bd3ae4358581f5d708125875d6f7ba56a7cccf8da17ec781219d8de
7
+ data.tar.gz: ff995006764b61a48b03b5f5ec9a261b1f04e4986687f45c5db197f511a3c5735d14dc21354686a578f1edc1a8137cc86e6f7edb93ea61a6f376d71ef5034188
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ .rvmrc
7
+ /.bundle
8
+ /vendor/bundle
9
+ /log/*
10
+ /tmp/*
11
+ /db/*.sqlite3
12
+ /public/system/*
13
+ /public/uploads/*
14
+ /coverage/
15
+ /spec/tmp/*
16
+ **.orig
17
+ rerun.txt
18
+ pickle-email-*.html
19
+ .project
20
+ config/initializers/secret_token.rb
21
+ .DS_Store
22
+ *.swp
23
+ /test/dummy/log/*
24
+ Gemfile.lock
data/.hound.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ LineLength:
2
+ Description: Limit lines to 120 characters.
3
+ Max: 120
4
+
5
+ StringLiterals:
6
+ EnforcedStyle: double_quotes
7
+
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ bundler_args: --without debug
2
+
3
+ script: "bundle exec rake test"
4
+
5
+ rvm:
6
+ - 2.0.0
7
+ - 2.1
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in godmin.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,523 @@
1
+ # Godmin
2
+
3
+ [![Build Status](https://travis-ci.org/varvet/godmin.svg?branch=master)](https://travis-ci.org/varvet/godmin)
4
+
5
+ Godmin is an admin engine for Rails 4+.
6
+
7
+ - [Installation](#installation)
8
+ - [Standalone installation](#standalone-installation)
9
+ - [Engine installation](#engine-installation)
10
+ - [Installation artefacts](#installation-artefacts)
11
+ - [Getting started](#getting-started)
12
+ - [Controllers](#controllers)
13
+ - [Scopes](#scopes)
14
+ - [Filters](#filters)
15
+ - [Batch actions](#batch-actions)
16
+ - [Resource fetching](#resource-fetching)
17
+ - [Views](#views)
18
+ - [Models](#models)
19
+ - [Authentication](#authentication)
20
+ - [Simple authentication](#simple-authentication)
21
+ - [Shared authentication](#shared-authentication)
22
+ - [Authorization](#authorization)
23
+ - [Localization](#localization)
24
+ - [Contributors](#contributors)
25
+
26
+ ## Installation
27
+
28
+ Godmin supports two common admin scenarios:
29
+
30
+ 1. Standalone installation
31
+ 2. Engine installation
32
+
33
+ ### Standalone installation
34
+ For admin-only applications, or for architectures where the admin lives in its own app, i.e. you want to access the admin section at `localhost:3000`.
35
+
36
+ Add the gem to the application's `Gemfile`:
37
+ ```ruby
38
+ gem "godmin"
39
+ ```
40
+
41
+ Run the install generator:
42
+ ```sh
43
+ $ bin/rails generate godmin:install
44
+ ```
45
+
46
+ Godmin should be up and running at `localhost:3000`
47
+
48
+ ### Engine installation
49
+ For when the admin is part of the same codebase as the main application, i.e. you want to access the admin section at `localhost:3000/admin`.
50
+
51
+ Generate a [mountable engine](http://guides.rubyonrails.org/engines.html):
52
+ ```sh
53
+ $ bin/rails plugin new admin --mountable
54
+ ```
55
+
56
+ Add the engine to the application's `Gemfile`:
57
+ ```ruby
58
+ gem "admin", path: "admin"
59
+ ```
60
+
61
+ Mount the engine in the application's `config/routes.rb`:
62
+ ```ruby
63
+ mount Admin::Engine, at: "/admin"
64
+ ```
65
+
66
+ Add the gem to the engine's gemspec, `admin/admin.gemspec`:
67
+ ```ruby
68
+ s.add_dependency("godmin", "~> 1.0.0")
69
+ ```
70
+
71
+ Run the install generator within the scope of the engine, i.e. note the leading `admin/`:
72
+ ```sh
73
+ $ admin/bin/rails generate godmin:install
74
+ ```
75
+
76
+ Godmin should be up and running at `localhost:3000/admin`
77
+
78
+ ### Installation artefacts
79
+
80
+ Installing Godmin does a number of things to the Rails application.
81
+
82
+ A `config/initializers/godmin.rb` is created:
83
+ ```ruby
84
+ Godmin.configure do |config|
85
+ config.namespace = nil
86
+ end
87
+ ```
88
+
89
+ If Godmin was installed inside an engine, as in the previous section, the namespace is the underscored name of the engine, e.g. `"admin"`.
90
+
91
+ The `config/routes.rb` file is modified as such:
92
+ ```ruby
93
+ Rails.application.routes.draw do
94
+ godmin do
95
+ end
96
+ end
97
+ ```
98
+
99
+ Resource routes placed within the `godmin` block are automatically added to the default navigation, and set up to work with batch actions. More on this in later sections.
100
+
101
+ The application controller is modified as such:
102
+ ```ruby
103
+ class ApplicationController < ActionController::Base
104
+ include Godmin::Application
105
+ end
106
+ ```
107
+
108
+ And 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.
109
+
110
+ ## Getting started
111
+
112
+ Godmin 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:
113
+
114
+ ```sh
115
+ $ bin/rails generate godmin:resource article title published
116
+ ```
117
+
118
+ Or for an engine install:
119
+ ```sh
120
+ $ admin/bin/rails generate godmin:resource article title published
121
+ ```
122
+
123
+ This does a number of things.
124
+
125
+ First, it inserts a route in the `config/routes.rb` file that looks like this:
126
+
127
+ ```ruby
128
+ godmin do
129
+ resources :articles
130
+ end
131
+ ```
132
+
133
+ Second, it creates a controller that looks something like this:
134
+
135
+ ```ruby
136
+ class ArticlesController < ApplicationController
137
+ include Godmin::Resource
138
+
139
+ def attrs_for_index
140
+ [:title, :published]
141
+ end
142
+
143
+ def attrs_for_form
144
+ [:title, :published]
145
+ end
146
+ end
147
+ ```
148
+
149
+ Using `attrs_for_index` we can control what fields are displayed in the table listing, 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:
150
+
151
+ ```ruby
152
+ def attrs_for_form
153
+ [:title, :body, :published]
154
+ end
155
+ ```
156
+
157
+ By now we have a basic admin interface for managing articles.
158
+
159
+ ## Controllers
160
+
161
+ We have already seen two controller methods at play: `attrs_for_index` and `attrs_for_form`. Now we will look at four additional controller concepts, namely:
162
+
163
+ - Scopes
164
+ - Filters
165
+ - Batch actions
166
+ - Resource fetching
167
+
168
+ ### Scopes
169
+
170
+ Scopes are a way of sectioning resources, useful for quick navigation, and can be created as follows:
171
+
172
+ ```ruby
173
+ class ArticlesController < ApplicationController
174
+ include Godmin::Resource
175
+
176
+ scope :unpublished, default: true
177
+ scope :published
178
+
179
+ def scope_unpublished(resources)
180
+ resources.where(published: false)
181
+ end
182
+
183
+ def scope_published(resources)
184
+ resources.where(published: true)
185
+ end
186
+ end
187
+ ```
188
+
189
+ ### Filters
190
+
191
+ Filters offer great flexibility when it comes to searching for resources.
192
+
193
+ Filters can be created as follows:
194
+
195
+ ```ruby
196
+ class ArticlesController < ApplicationController
197
+ include Godmin::Resource
198
+
199
+ filter :title
200
+
201
+ def filter_title(resources, value)
202
+ resources.where("title LIKE ?", "%#{value}%")
203
+ end
204
+ end
205
+ ```
206
+
207
+ There are four types of filters: `string`, `select`, `multiselect` and `checkboxes`, specified using the `as` parameter.
208
+
209
+ When 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.
210
+
211
+ ```ruby
212
+ filter :category, as: :select, collection: -> { [["News", 1], ["Posts", 2]] }
213
+ ```
214
+
215
+ When specifying a collection of ActiveRecords, two additional parameters, `option_text` and `option_value` can be specified. They default to `to_s` and `id` respectively.
216
+
217
+ ```ruby
218
+ filter :category, as: :select, collection: -> { Category.all }, option_text: "title"
219
+ ```
220
+
221
+ ### Batch actions
222
+
223
+ Batch actions can be created as follows:
224
+
225
+ ```ruby
226
+ class ArticlesController < ApplicationController
227
+ include Godmin::Resource
228
+
229
+ batch_action :publish
230
+ batch_action :unpublish
231
+ batch_action :destroy, confirm: true
232
+
233
+ def batch_action_publish(resources)
234
+ resources.each { |r| r.update_attributes(published: true) }
235
+ end
236
+
237
+ ...
238
+ end
239
+ ```
240
+
241
+ In addition, batch actions can be defined per scope using `only` and `except`:
242
+
243
+ ```ruby
244
+ batch_action :publish, only: [:unpublished]
245
+ batch_action :unpublish, only: [:published]
246
+ ```
247
+
248
+ If you wish to implement your own redirect after a batch action, return false afterwards:
249
+
250
+ ```ruby
251
+ def batch_action_publish(resources)
252
+ resources.each { |r| r.update_attributes(published: true) }
253
+ redirect_to articles_path(scope: published) and return false
254
+ end
255
+ ```
256
+
257
+ ### Resource fetching
258
+
259
+ Resources 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`.
260
+
261
+ In order to modify what resources to fetch, there are three methods that can be overridden per resource controller:
262
+
263
+ - `resource_class`
264
+ - `resource_relation`
265
+ - `resources`
266
+
267
+ To change the class name of the resource from the default based on the controller name:
268
+
269
+ ```ruby
270
+ class ArticlesController
271
+ include Godmin::Resource
272
+
273
+ def resource_class
274
+ FooArticle
275
+ end
276
+ end
277
+ ```
278
+
279
+ To scope resources, e.g. based on the signed in user:
280
+
281
+ ```ruby
282
+ class ArticlesController
283
+ include Godmin::Resource
284
+
285
+ def resources_relation
286
+ admin_user.articles
287
+ end
288
+ end
289
+ ```
290
+
291
+ To add to the resources query, e.g. to change the default order:
292
+
293
+ ```ruby
294
+ class ArticlesController
295
+ include Godmin::Resource
296
+
297
+ def resources
298
+ super.order(author: :desc)
299
+ end
300
+ end
301
+ ```
302
+
303
+ ## Views
304
+
305
+ It is 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`.
306
+
307
+ If 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.
308
+
309
+ Oftentimes, 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.
310
+
311
+ Likewise, the `godmin/shared/_navigation.html.erb` partial can be overridden to build a custom navigation bar.
312
+
313
+ The full list of templates and partials that can be overridden [can be found here](https://github.com/varvet/godmin/tree/master/app/views/godmin)
314
+
315
+ ## Models
316
+
317
+ ## Authentication
318
+
319
+ Multiple authentication scenarios are supported. Godmin comes with a 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.
320
+
321
+ ### Simple authentication
322
+
323
+ This example uses the built in authentication solution. Authentication is isolated to the admin section and administrators sign in via the admin interface.
324
+
325
+ Godmin comes with a generator that creates an admin user model and enables the built in authentication:
326
+
327
+ ```sh
328
+ $ bin/rails generate godmin:authentication
329
+ $ bin/rake db:migrate
330
+ ```
331
+
332
+ Please 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:
333
+
334
+ ```sh
335
+ $ admin/bin/rails generate godmin:authentication
336
+ $ bin/rake admin:install:migrations
337
+ $ bin/rake db:migrate
338
+ ```
339
+
340
+ The generated model looks like this:
341
+
342
+ ```ruby
343
+ class AdminUser < ActiveRecord::Base
344
+ include Godmin::Authentication::User
345
+
346
+ def self.login_column
347
+ :email
348
+ end
349
+ end
350
+ ```
351
+
352
+ By 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.
353
+
354
+ The following route is generated:
355
+
356
+ ```ruby
357
+ resource :session, only: [:new, :create, :destroy]
358
+ ```
359
+
360
+ Along with a sessions controller:
361
+
362
+ ```ruby
363
+ class SessionsController < ApplicationController
364
+ include Godmin::Authentication::Sessions
365
+ end
366
+ ```
367
+
368
+ Finally, the application controller is tweaked to look something like this:
369
+
370
+ ```ruby
371
+ class ApplicationController < ActionController::Base
372
+ include Godmin::Application
373
+ include Godmin::Authentication
374
+
375
+ def admin_user_class
376
+ AdminUser
377
+ end
378
+ end
379
+ ```
380
+
381
+ Authentication is now required when visiting the admin section.
382
+
383
+ ### Shared authentication
384
+
385
+ This 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.
386
+
387
+ There is no need to run a generator in this instance. Simple add the authentication module to the admin application controller like so:
388
+
389
+ ```ruby
390
+ module Admin
391
+ class ApplicationController < ActionController::Base
392
+ include Godmin::Application
393
+ include Godmin::Authentication
394
+ end
395
+ end
396
+ ```
397
+
398
+ Provided you have `User` model set up with Devise in the main application, override the following three methods in the admin application controller:
399
+
400
+ ```ruby
401
+ module Admin
402
+ class ApplicationController < ActionController::Base
403
+ include Godmin::Application
404
+ include Godmin::Authentication
405
+
406
+ def authenticate_admin_user
407
+ authenticate_user!
408
+ end
409
+
410
+ def admin_user
411
+ current_user
412
+ end
413
+
414
+ def admin_user_signed_in?
415
+ user_signed_in?
416
+ end
417
+ end
418
+ end
419
+ ```
420
+
421
+ That's it. The admin section is now authenticated using Devise.
422
+
423
+ ## Authorization
424
+
425
+ In order to enable authorization, authentication must first be enabled. See the previous section. The Godmin authorization system is heavily inspired by [Pundit](https://github.com/elabs/pundit) and implements the same interface.
426
+
427
+ Add the authorization module to the application controller:
428
+
429
+ ```ruby
430
+ class ApplicationController < ActionController::Base
431
+ include Godmin::Application
432
+ include Godmin::Authentication
433
+ include Godmin::Authorization
434
+
435
+ ...
436
+ end
437
+ ```
438
+
439
+ Policies can be generated using the following command:
440
+
441
+ ```sh
442
+ $ bin/rails generate godmin:policy article
443
+ ```
444
+
445
+ This file `app/policies/article_policy.rb` will be created:
446
+
447
+ ```ruby
448
+ class ArticlePolicy < Godmin::Authorization::Policy
449
+ end
450
+ ```
451
+
452
+ Permissions 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:
453
+
454
+ ```ruby
455
+ class ArticlePolicy < Godmin::Authorization::Policy
456
+ def index?
457
+ true
458
+ end
459
+
460
+ def show?
461
+ true
462
+ end
463
+
464
+ def create?
465
+ user.editor?
466
+ end
467
+
468
+ def update?
469
+ user.editor? && record.unpublished?
470
+ end
471
+
472
+ def destroy?
473
+ update?
474
+ end
475
+ end
476
+ ```
477
+
478
+ That is, everyone can list and view articles, only editors can create them, and only unpublished articles can be updated and destroyed.
479
+
480
+ ## Localization
481
+
482
+ Godmin supports localization out of the box. Strings can be translated both globally and per resource, similar to how views work.
483
+
484
+ For a list of translatable strings, [look here](https://github.com/varvet/godmin/blob/resurrection/config/locales/en.yml).
485
+
486
+ For instance, to translate the `godmin.batch_actions.select_all` string globally:
487
+
488
+ ```yml
489
+ godmin:
490
+ batch_actions:
491
+ select_all: {translation}
492
+ ```
493
+
494
+ Or, translate for a specific resource:
495
+
496
+ ```yml
497
+ godmin:
498
+ articles:
499
+ batch_actions:
500
+ select_all: {translation}
501
+ ```
502
+
503
+ In addition, all scopes, filters and batch actions that are added, can be localized:
504
+
505
+ ```yml
506
+ godmin:
507
+ articles:
508
+ batch_actions:
509
+ publish: {translation}
510
+ unpublish: {translation}
511
+ filters:
512
+ labels:
513
+ title: {translation}
514
+ scopes:
515
+ unpublished: {translation}
516
+ published: {translation}
517
+ ```
518
+
519
+ Godmin comes with built in support for English and Swedish.
520
+
521
+ There is a view helper available named `translated_scoped` that can be used in overridden views. Please see the source code for information on how to use it.
522
+
523
+ ## Contributors