administrate 0.10.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/administrate/components/associative.js +5 -0
  3. data/app/assets/javascripts/administrate/components/date_time_picker.js +6 -2
  4. data/app/assets/javascripts/administrate/components/table.js +1 -1
  5. data/app/assets/stylesheets/administrate/base/_tables.scss +3 -0
  6. data/app/assets/stylesheets/administrate/base/_typography.scss +1 -1
  7. data/app/assets/stylesheets/administrate/components/_attributes.scss +4 -3
  8. data/app/assets/stylesheets/administrate/components/_buttons.scss +8 -0
  9. data/app/assets/stylesheets/administrate/components/_cells.scss +2 -0
  10. data/app/assets/stylesheets/administrate/components/_field-unit.scss +17 -4
  11. data/app/assets/stylesheets/administrate/components/_flashes.scss +0 -8
  12. data/app/assets/stylesheets/administrate/components/_main-content.scss +1 -0
  13. data/app/assets/stylesheets/administrate/components/_navigation.scss +2 -3
  14. data/app/assets/stylesheets/administrate/library/_variables.scss +10 -8
  15. data/app/controllers/administrate/application_controller.rb +33 -9
  16. data/app/controllers/concerns/administrate/punditize.rb +3 -3
  17. data/app/helpers/administrate/application_helper.rb +47 -15
  18. data/app/views/administrate/application/_collection.html.erb +8 -7
  19. data/app/views/administrate/application/_form.html.erb +2 -2
  20. data/app/views/administrate/application/{_icons.erb → _icons.html.erb} +0 -0
  21. data/app/views/administrate/application/_navigation.html.erb +5 -3
  22. data/app/views/administrate/application/index.html.erb +4 -2
  23. data/app/views/administrate/application/show.html.erb +2 -2
  24. data/app/views/fields/belongs_to/_form.html.erb +3 -3
  25. data/app/views/fields/date/_form.html.erb +24 -0
  26. data/app/views/fields/date/_index.html.erb +21 -0
  27. data/app/views/fields/date/_show.html.erb +21 -0
  28. data/app/views/fields/has_many/_form.html.erb +2 -2
  29. data/app/views/fields/has_many/_show.html.erb +8 -5
  30. data/app/views/fields/has_one/_form.html.erb +1 -1
  31. data/app/views/fields/has_one/_index.html.erb +1 -1
  32. data/app/views/fields/has_one/_show.html.erb +4 -4
  33. data/app/views/fields/number/_form.html.erb +1 -1
  34. data/app/views/fields/polymorphic/_form.html.erb +0 -3
  35. data/app/views/fields/polymorphic/_show.html.erb +1 -1
  36. data/app/views/fields/select/_form.html.erb +22 -10
  37. data/app/views/fields/string/_show.html.erb +2 -2
  38. data/app/views/fields/text/_show.html.erb +2 -3
  39. data/app/views/fields/time/_form.html.erb +3 -2
  40. data/app/views/fields/time/_index.html.erb +3 -1
  41. data/app/views/fields/time/_show.html.erb +3 -1
  42. data/app/views/fields/url/_form.html.erb +23 -0
  43. data/app/views/fields/url/_index.html.erb +20 -0
  44. data/app/views/fields/url/_show.html.erb +20 -0
  45. data/app/views/layouts/administrate/application.html.erb +2 -1
  46. data/config/locales/administrate.ar.yml +2 -0
  47. data/config/locales/administrate.bs.yml +2 -0
  48. data/config/locales/administrate.ca.yml +2 -0
  49. data/config/locales/administrate.da.yml +2 -0
  50. data/config/locales/administrate.de.yml +2 -0
  51. data/config/locales/administrate.en.yml +2 -0
  52. data/config/locales/administrate.es.yml +3 -1
  53. data/config/locales/administrate.fi.yml +30 -0
  54. data/config/locales/administrate.fr.yml +4 -2
  55. data/config/locales/administrate.id.yml +30 -0
  56. data/config/locales/administrate.it.yml +2 -0
  57. data/config/locales/administrate.ja.yml +2 -0
  58. data/config/locales/administrate.ko.yml +12 -10
  59. data/config/locales/administrate.nl.yml +7 -5
  60. data/config/locales/administrate.pl.yml +2 -0
  61. data/config/locales/administrate.pt-BR.yml +4 -2
  62. data/config/locales/administrate.pt.yml +4 -2
  63. data/config/locales/administrate.ru.yml +2 -0
  64. data/config/locales/{administrate.al.yml → administrate.sq.yml} +3 -1
  65. data/config/locales/administrate.sv.yml +2 -0
  66. data/config/locales/administrate.tr.yml +30 -0
  67. data/config/locales/administrate.uk.yml +2 -0
  68. data/config/locales/administrate.vi.yml +2 -0
  69. data/config/locales/administrate.zh-CN.yml +4 -2
  70. data/config/locales/administrate.zh-TW.yml +6 -4
  71. data/config/unicorn.rb +8 -13
  72. data/docs/adding_controllers_without_related_model.md +54 -0
  73. data/docs/adding_custom_field_types.md +3 -1
  74. data/docs/authentication.md +3 -1
  75. data/docs/authorization.md +5 -3
  76. data/docs/customizing_attribute_partials.md +5 -2
  77. data/docs/customizing_controller_actions.md +32 -2
  78. data/docs/customizing_dashboards.md +74 -34
  79. data/docs/customizing_page_views.md +18 -4
  80. data/docs/extending_administrate.md +27 -0
  81. data/docs/getting_started.md +49 -56
  82. data/docs/guides.md +5 -0
  83. data/docs/guides/hiding_dashboards_from_sidebar.md +19 -0
  84. data/docs/rails_api.md +45 -0
  85. data/lib/administrate.rb +19 -0
  86. data/lib/administrate/base_dashboard.rb +35 -9
  87. data/lib/administrate/custom_dashboard.rb +15 -0
  88. data/lib/administrate/engine.rb +8 -1
  89. data/lib/administrate/field/associative.rb +49 -5
  90. data/lib/administrate/field/base.rb +35 -9
  91. data/lib/administrate/field/belongs_to.rb +13 -3
  92. data/lib/administrate/field/date.rb +20 -0
  93. data/lib/administrate/field/deferred.rb +22 -3
  94. data/lib/administrate/field/has_many.rb +31 -11
  95. data/lib/administrate/field/has_one.rb +32 -12
  96. data/lib/administrate/field/number.rb +19 -2
  97. data/lib/administrate/field/polymorphic.rb +5 -5
  98. data/lib/administrate/field/select.rb +6 -1
  99. data/lib/administrate/field/url.rb +21 -0
  100. data/lib/administrate/namespace.rb +4 -0
  101. data/lib/administrate/order.rb +17 -7
  102. data/lib/administrate/page/base.rb +9 -3
  103. data/lib/administrate/page/collection.rb +5 -1
  104. data/lib/administrate/page/form.rb +1 -1
  105. data/lib/administrate/resource_resolver.rb +1 -1
  106. data/lib/administrate/search.rb +107 -23
  107. data/lib/administrate/version.rb +1 -1
  108. data/lib/administrate/view_generator.rb +9 -3
  109. data/lib/generators/administrate/dashboard/dashboard_generator.rb +21 -15
  110. data/lib/generators/administrate/dashboard/templates/controller.rb.erb +33 -8
  111. data/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +18 -6
  112. data/lib/generators/administrate/install/install_generator.rb +37 -1
  113. data/lib/generators/administrate/install/templates/application_controller.rb.erb +3 -3
  114. data/lib/generators/administrate/routes/routes_generator.rb +21 -26
  115. data/lib/generators/administrate/views/layout_generator.rb +1 -0
  116. data/lib/generators/administrate/views/views_generator.rb +5 -4
  117. metadata +50 -53
  118. data/app/assets/javascripts/administrate/components/has_many_form.js +0 -3
  119. data/config/secrets.yml +0 -14
@@ -0,0 +1,54 @@
1
+ ---
2
+ title: Adding Controllers without a related Model
3
+ ---
4
+
5
+ # Adding Controllers without a related Model
6
+
7
+ Sometimes you may want to add a custom controller that has no resource
8
+ related to it (for example for a statistics page).
9
+
10
+ To do that, you must define an `index` route, as only controllers with index
11
+ routes are displayed in the sidebar and then add a custom dashboard:
12
+
13
+ ```erb
14
+ # app/views/admin/stats/index.html.erb
15
+
16
+ <div style="padding: 20px">
17
+ <h1>Stats</h1>
18
+ <br>
19
+ <p><b>Total Customers:</b> <%= @stats[:customer_count] %></h1>
20
+ <br>
21
+ <p><b>Total Orders:</b> <%= @stats[:order_count] %></h1>
22
+ </div>
23
+ ```
24
+
25
+ ```ruby
26
+ # app/dashboards/stat_dashboard.rb
27
+ require "administrate/custom_dashboard"
28
+
29
+ class StatDashboard < Administrate::CustomDashboard
30
+ resource "Stats" # used by administrate in the views
31
+ end
32
+ ```
33
+
34
+ ```ruby
35
+ # app/controllers/admin/stats_controller.rb
36
+ module Admin
37
+ class StatsController < Admin::ApplicationController
38
+ def index
39
+ @stats = {
40
+ customer_count: Customer.count,
41
+ order_count: Order.count,
42
+ }
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ ```ruby
49
+ # config/routes.rb
50
+ namespace :admin do
51
+ # ...
52
+ resources :stats, only: [:index]
53
+ end
54
+ ```
@@ -1,4 +1,6 @@
1
- # Adding Custom Field Types
1
+ ---
2
+ title: Adding Custom Field Types
3
+ ---
2
4
 
3
5
  If your application deals with a nonstandard data type,
4
6
  you can create an `Administrate::Field` object to help display
@@ -1,4 +1,6 @@
1
- # Authenticating admin users
1
+ ---
2
+ title: Authenticating admin users
3
+ ---
2
4
 
3
5
  Authentication is left for you to implement after you install Administrate into
4
6
  your app. It's expected that you can plugin your existing authentication
@@ -1,4 +1,6 @@
1
- # Authorization
1
+ ---
2
+ title: Authorization
3
+ ---
2
4
 
3
5
  The default configuration of Administrate is "authenticate-only" - once a
4
6
  user is authenticated, that user has access to every action of every object.
@@ -37,7 +39,7 @@ class PostPolicy < ApplicationPolicy
37
39
  def resolve
38
40
  scope.all
39
41
  end
40
-
42
+
41
43
  def resolve_admin
42
44
  scope.where(owner: user)
43
45
  end
@@ -62,7 +64,7 @@ def authorize_resource(resource)
62
64
  raise "Erg!" unless show_action?(params[:action], resource)
63
65
  end
64
66
 
65
- # Hide links to actions if the user is not allowed to do them
67
+ # Hide links to actions if the user is not allowed to do them
66
68
  def show_action?(action, resource)
67
69
  current_user.can? action, resource
68
70
  end
@@ -1,4 +1,6 @@
1
- # Customizing attribute partials
1
+ ---
2
+ title: Customizing attribute partials
3
+ ---
2
4
 
3
5
  Occasionally you might want to change how specific types of attributes appear
4
6
  across all dashboards. You can customize the following built in field types:
@@ -6,11 +8,12 @@ across all dashboards. You can customize the following built in field types:
6
8
  - `belongs_to`
7
9
  - `boolean`
8
10
  - `date_time`
11
+ - `date`
9
12
  - `email`
10
13
  - `has_many`
11
14
  - `has_one`
12
15
  - `number`
13
- - `polymporphic`
16
+ - `polymorphic`
14
17
  - `select`
15
18
  - `string`
16
19
  - `text`
@@ -1,4 +1,6 @@
1
- # Customizing controller actions
1
+ ---
2
+ title: Customizing controller actions
3
+ ---
2
4
 
3
5
  When you install Administrate into your app,
4
6
  we generate empty controllers for each of your resources.
@@ -28,9 +30,10 @@ class Admin::FoosController < Admin::ApplicationController
28
30
  # def find_resource(param)
29
31
  # Foo.find_by!(slug: param)
30
32
  # end
31
- #
33
+
32
34
  # Override this if you have certain roles that require a subset
33
35
  # this will be used to set the records shown on the `index` action.
36
+ #
34
37
  # def scoped_resource
35
38
  # if current_user.super_admin?
36
39
  # resource_class
@@ -40,3 +43,30 @@ class Admin::FoosController < Admin::ApplicationController
40
43
  # end
41
44
  end
42
45
  ```
46
+
47
+ ## Customizing Actions
48
+
49
+ To enable or disable certain actions you could override `valid_action?` method in your dashboard controller like this:
50
+
51
+ ```ruby
52
+ # disable 'edit' and 'destroy' links
53
+ def valid_action?(name, resource = resource_class)
54
+ %w[edit destroy].exclude?(name.to_s) && super
55
+ end
56
+ ```
57
+
58
+ Action is one of `new`, `edit`, `show`, `destroy`.
59
+
60
+ ## Customizing Default Sorting
61
+
62
+ To set the default sorting on the index action you could override `default_sorting_attribute` or `default_sorting_direction` in your dashboard controller like this:
63
+
64
+ ```ruby
65
+ def default_sorting_attribute
66
+ :age
67
+ end
68
+
69
+ def default_sorting_direction
70
+ :desc
71
+ end
72
+ ```
@@ -1,4 +1,6 @@
1
- # Customizing Dashboards
1
+ ---
2
+ title: Customizing Dashboards
3
+ ---
2
4
 
3
5
  In order to customize which attributes get displayed for each resource,
4
6
  edit the dashboard file generated by the installation generator.
@@ -54,6 +56,7 @@ specify, including:
54
56
  - `Field::BelongsTo`
55
57
  - `Field::Boolean`
56
58
  - `Field::DateTime`
59
+ - `Field::Date`
57
60
  - `Field::Email`
58
61
  - `Field::HasMany`
59
62
  - `Field::HasOne`
@@ -76,35 +79,36 @@ which are specified through the `.with_options` class method:
76
79
  `:order` - Specifies the order of the dropdown menu, can be ordered by more
77
80
  than one column. e.g.: `"name, email DESC"`.
78
81
 
79
- `:primary_key` - Specifies object's primary_key. Defaults to `:id`.
80
-
81
- `:foreign_key` - Specifies the name of the foreign key directly.
82
- Defaults to `:#{attribute}_id`.
83
-
84
82
  `:scope` - Specifies a custom scope inside a callable. Useful for preloading.
85
83
  Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`
86
84
 
87
- `:class_name` - Specifies the name of the associated class.
88
- Defaults to `:#{attribute}.to_s.singularize.camelcase`.
85
+ `:include_blank` - Specifies if the select element to be rendered should include
86
+ blank option. Default is `true`.
89
87
 
90
88
  `:searchable` - Specify if the attribute should be considered when searching.
91
89
  Default is `false`.
92
90
 
93
- `searchable_field` - Specify which column to use on the search, only applies
91
+ `searchable_fields` - Specify which columns to use on the search, only applies
94
92
  if `searchable` is `true`
95
93
 
96
94
  For example:
97
95
 
98
96
  ```ruby
99
- country: Field::BelongsTo(
97
+ country: Field::BelongsTo.with_options(
100
98
  searchable: true,
101
- seachable_field: 'name',
99
+ searchable_fields: ['name'],
102
100
  )
103
101
  ```
104
102
 
105
103
  with this, you will be able to search through the column `name` from the
106
104
  association `belongs_to :country`, from your model.
107
105
 
106
+ `:primary_key` (deprecated) - Specifies the association's primary_key.
107
+
108
+ `:foreign_key` (deprecated) - Specifies the name of the foreign key directly.
109
+
110
+ `:class_name` (deprecated) - Specifies the name of the associated class.
111
+
108
112
  **Field::HasMany**
109
113
 
110
114
  `:limit` - Set the number of resources to display in the show view. Default is
@@ -114,36 +118,34 @@ association `belongs_to :country`, from your model.
114
118
 
115
119
  `:direction` - What direction the sort should be in, `:asc` (default) or `:desc`.
116
120
 
117
- `:primary_key` - Specifies object's primary_key. Defaults to `:id`.
121
+ `:primary_key` (deprecated) - Specifies object's primary_key.
118
122
 
119
- `:foreign_key` - Specifies the name of the foreign key directly. Defaults to `:#{attribute}_id`
123
+ `:foreign_key` (deprecated) - Specifies the name of the foreign key directly.
120
124
 
121
- `:class_name` - Specifies the name of the associated class.
122
- Defaults to `:#{attribute}.to_s.singularize.camelcase`.
125
+ `:class_name` (deprecated) - Specifies the name of the associated class.
123
126
 
124
127
  **Field::HasOne**
125
128
 
126
- `:class_name` - Specifies the name of the associated class.
127
- Defaults to `:#{attribute}.to_s.singularize.camelcase`.
128
-
129
129
  `:searchable` - Specify if the attribute should be considered when searching.
130
130
  Default is `false`.
131
131
 
132
- `searchable_field` - Specify which column to use on the search, only applies if
132
+ `searchable_fields` - Specify which columns to use on the search, only applies if
133
133
  `searchable` is `true`
134
134
 
135
135
  For example:
136
136
 
137
137
  ```ruby
138
- cities: Field::HasMany(
138
+ cities: Field::HasMany.with_options(
139
139
  searchable: true,
140
- seachable_field: 'name',
140
+ searchable_fields: ['name'],
141
141
  )
142
142
  ```
143
143
 
144
144
  with this, you will be able to search through the column `name` from the
145
145
  association `has_many :cities`, from your model.
146
146
 
147
+ `:class_name` (deprecated) - Specifies the name of the associated class.
148
+
147
149
  **Field::Number**
148
150
 
149
151
  `:searchable` - Specify if the attribute should be considered when searching.
@@ -156,6 +158,13 @@ more results than expected. Default is `false`.
156
158
 
157
159
  `:suffix` - Suffixes the number with a string. Defaults to `""`.
158
160
 
161
+ `:format` - Specify a hash which defines a formatter. This uses ActiveSupport
162
+ and works by by passing a hash that includes the formatter (`formatter`) and
163
+ the options for the formatter (`formatter_options`). Defaults to the locale's
164
+ delimiter when `formatter_options` does not include a `delimiter`. See the
165
+ example below. Note that currently only
166
+ `ActiveSupport::NumberHelper.number_to_delimited` is supported.
167
+
159
168
  For example, you might use the following to display U.S. currency:
160
169
 
161
170
  ```ruby
@@ -163,15 +172,25 @@ For example, you might use the following to display U.S. currency:
163
172
  prefix: "$",
164
173
  decimals: 2,
165
174
  )
175
+
176
+ # "$5.99"
166
177
  ```
167
178
 
168
- Or, to display a distance in kilometers:
179
+ Or, to display a distance in kilometers, using a space as the delimiter:
169
180
 
170
181
  ```ruby
171
- unit_price: Field::Number.with_options(
182
+ distance: Field::Number.with_options(
172
183
  suffix: " km",
173
184
  decimals: 2,
185
+ format: {
186
+ formatter: :number_to_delimited,
187
+ formatter_options: {
188
+ delimiter: ' ',
189
+ },
190
+ },
174
191
  )
192
+
193
+ # "2 000.00 km"
175
194
  ```
176
195
 
177
196
  **Field::Polymorphic**
@@ -190,9 +209,15 @@ objects to display as.
190
209
  `:timezone` - Specify which timezone `Date` and `DateTime` objects are based
191
210
  in.
192
211
 
212
+ **Field::Date**
213
+
214
+ `:format` - Specify what format, using `strftime` you would like `Date`
215
+ objects to display as.
216
+
193
217
  **Field::Select**
194
218
 
195
- `:collection` - Specify the array or range to select from. Defaults to `[]`.
219
+ `:collection` - Specify the options shown on the select field. It accept either
220
+ an array or an object responding to `:call`. Defaults to `[]`.
196
221
 
197
222
  `:searchable` - Specify if the attribute should be considered when searching.
198
223
  Default is `true`.
@@ -246,11 +271,11 @@ Add this method to the dashboard for Users.
246
271
  Use whatever attribute or method you like.
247
272
  Example for *user*:
248
273
 
249
- ````ruby
274
+ ```ruby
250
275
  def display_resource(user)
251
276
  user.name
252
277
  end
253
- ````
278
+ ```
254
279
 
255
280
  [define your own]: /adding_custom_field_types
256
281
 
@@ -262,18 +287,33 @@ en:
262
287
  models:
263
288
  customer:
264
289
  one: Happy Customer
265
- others: Happy Customers
290
+ other: Happy Customers
266
291
  ```
267
292
 
268
- ## Customizing Actions
293
+ ## Collection Filters
269
294
 
270
- To enable or disable certain actions you could override `valid_action?` method in your dashboard controller like this:
295
+ Resources can be filtered with pre-set filters. For example if we added:
271
296
 
272
297
  ```ruby
273
- # disable 'edit' and 'destroy' links
274
- def valid_action?(name, resource = resource_class)
275
- %w[edit destroy].exclude?(name.to_s) && super
276
- end
298
+ COLLECTION_FILTERS = {
299
+ inactive: ->(resources) { resources.where("login_at < ?", 1.week.ago) }
300
+ }
301
+ ```
302
+
303
+ …to a dashboard, we can query the resources of that dashboard with:
304
+
305
+ ```ruby
306
+ bob inactive:
277
307
  ```
278
308
 
279
- Action is one of `new`, `edit`, `show`, `destroy`.
309
+ …to find users named "bob" who hasn't logged in the last week.
310
+
311
+ If you already had the `inactive` scope you could define the filter like so to
312
+ take advantage of existing ActiveRecord scopes (and other class methods on the
313
+ resource class).
314
+
315
+ ```ruby
316
+ COLLECTION_FILTERS = {
317
+ inactive: ->(resources) { resources.inactive }
318
+ }
319
+ ```
@@ -1,11 +1,25 @@
1
- # Customizing page views
1
+ ---
2
+ title: Customizing page views
3
+ ---
2
4
 
3
- In order to change the appearance of any page,
4
- you can write custom Rails views.
5
+ You can provide replacements for any of Administrate's templates.
6
+ This way you can change the appearance of any page or element of
7
+ the interface.
8
+
9
+ In general, you can override any of the views under Administrate's
10
+ [/app/views][1].
11
+ For example, say that you want to customize the template used for flash
12
+ messages. You can provide your own as
13
+ `/app/views/administrate/application/_flashes.html.erb`, and it will replace
14
+ Administrate's own.
15
+
16
+ Figuring out which views are available and where can be repetitive. You can
17
+ spare yourself some effort by using the built-in view generators.
18
+
19
+ [1]: https://github.com/thoughtbot/administrate/tree/master/app/views
5
20
 
6
21
  ## Customizing for all resources
7
22
 
8
- The easiest way to get started is by using the built-in generators.
9
23
  In order to change the appearance of views for all resource types,
10
24
  call the generators with no arguments.
11
25
 
@@ -0,0 +1,27 @@
1
+ ---
2
+ title: Extending Administrate
3
+ ---
4
+
5
+ Apart from the configuration described in these pages, it is possible to
6
+ extend Administrate's capabilities with the use of plugins. There are a
7
+ number of plugins available, many of which can be found at [RubyGems.org].
8
+ At the time of writing, these appear to be the most popular ones:
9
+
10
+ 1. [ActiveStorage support](https://github.com/Dreamersoul/administrate-field-active_storage)
11
+ 2. [Password field](https://github.com/valiot/administrate-field-password)
12
+ 3. [Enum field](https://github.com/Valiot/administrate-field-enum)
13
+ 4. [Nested has-many forms](https://github.com/nickcharlton/administrate-field-nested_has_many)
14
+ 5. [Belongs-to with Ajax search](https://github.com/fishbrain/administrate-field-belongs_to_search)
15
+
16
+ See many more at https://rubygems.org/gems/administrate/reverse_dependencies.
17
+
18
+ Please note that these plugins are written by third parties. We do not
19
+ have any control over them, and we cannot give any assurances as to how
20
+ well they perform their advertised functions.
21
+
22
+ You can write your own plugins too! We don't document this specifically,
23
+ but you can have a look at the existing plugins for some directions.
24
+ In general, Administrate tries to abide by Rails's conventions, so that
25
+ hopefully should help!
26
+
27
+ [RubyGems.org]: https://rubygems.org