administrate 0.19.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +9 -9
  3. data/app/assets/builds/administrate/application.css +2614 -0
  4. data/app/assets/builds/administrate/application.css.map +1 -0
  5. data/app/assets/builds/administrate/application.js +31661 -0
  6. data/app/assets/builds/administrate/application.js.map +7 -0
  7. data/app/assets/builds/administrate-internal/docs.css +102 -0
  8. data/app/assets/builds/administrate-internal/docs.css.map +1 -0
  9. data/app/assets/config/administrate_manifest.js +2 -0
  10. data/app/assets/javascripts/administrate/add_jquery.js +4 -0
  11. data/app/assets/javascripts/administrate/application.js +9 -4
  12. data/app/assets/javascripts/administrate/controllers/application.js +9 -0
  13. data/app/assets/javascripts/administrate/controllers/index.js +9 -0
  14. data/app/assets/javascripts/administrate/controllers/select_controller.js +25 -0
  15. data/app/assets/javascripts/administrate/{components/table.js → controllers/table_controller.js} +9 -9
  16. data/app/assets/javascripts/administrate/controllers/tooltip_controller.js +24 -0
  17. data/app/assets/javascripts/administrate/vendor/css-anchor-positioning.js +9310 -0
  18. data/app/assets/stylesheets/administrate/application.scss +3 -3
  19. data/app/assets/stylesheets/administrate/base/_forms.scss +4 -4
  20. data/app/assets/stylesheets/administrate/base/_layout.scss +5 -0
  21. data/app/assets/stylesheets/administrate/base/_tables.scss +1 -1
  22. data/app/assets/stylesheets/administrate/base/_typography.scss +15 -1
  23. data/app/assets/stylesheets/administrate/components/_attributes.scss +1 -0
  24. data/app/assets/stylesheets/administrate/components/_buttons.scss +37 -12
  25. data/app/assets/stylesheets/administrate/components/_cells.scss +26 -19
  26. data/app/assets/stylesheets/administrate/components/_field-unit.scss +10 -3
  27. data/app/assets/stylesheets/administrate/components/_main-content.scss +1 -1
  28. data/app/assets/stylesheets/administrate/components/_navigation.scss +3 -3
  29. data/app/assets/stylesheets/administrate/components/_search.scss +55 -14
  30. data/app/assets/stylesheets/administrate/library/_variables.scss +7 -3
  31. data/app/assets/stylesheets/administrate/reset/_normalize.scss +7 -1
  32. data/app/assets/stylesheets/{docs.scss → administrate-internal/docs.scss} +25 -23
  33. data/app/controllers/administrate/application_controller.rb +28 -39
  34. data/app/controllers/concerns/administrate/punditize.rb +19 -31
  35. data/app/helpers/administrate/application_helper.rb +13 -5
  36. data/app/views/administrate/application/_collection.html.erb +30 -20
  37. data/app/views/administrate/application/_collection_header_actions.html.erb +1 -1
  38. data/app/views/administrate/application/_collection_item_actions.html.erb +4 -4
  39. data/app/views/administrate/application/_form.html.erb +20 -5
  40. data/app/views/administrate/application/_icons.html.erb +14 -6
  41. data/app/views/administrate/application/_index_header.html.erb +23 -0
  42. data/app/views/administrate/application/_javascript.html.erb +1 -1
  43. data/app/views/administrate/application/edit.html.erb +15 -3
  44. data/app/views/administrate/application/index.html.erb +20 -11
  45. data/app/views/administrate/application/new.html.erb +16 -4
  46. data/app/views/administrate/application/show.html.erb +37 -17
  47. data/app/views/fields/belongs_to/_form.html.erb +3 -2
  48. data/app/views/fields/has_many/_form.html.erb +2 -2
  49. data/app/views/fields/has_one/_form.html.erb +20 -4
  50. data/app/views/fields/has_one/_show.html.erb +18 -12
  51. data/app/views/fields/polymorphic/_form.html.erb +1 -1
  52. data/app/views/fields/rich_text/_form.html.erb +22 -0
  53. data/app/views/fields/rich_text/_index.html.erb +18 -0
  54. data/app/views/fields/rich_text/_show.html.erb +18 -0
  55. data/app/views/fields/select/_form.html.erb +2 -1
  56. data/app/views/fields/text/_form.html.erb +1 -1
  57. data/app/views/layouts/administrate/application.html.erb +1 -2
  58. data/docs/customizing_controller_actions.md +14 -0
  59. data/docs/customizing_dashboards.md +268 -12
  60. data/docs/customizing_page_views.md +48 -1
  61. data/docs/getting_started.md +1 -1
  62. data/docs/guides/scoping_has_many_relations.md +2 -2
  63. data/docs/guides/switching_templates_with_view_variants.md +45 -0
  64. data/docs/guides.md +1 -0
  65. data/docs/migrating-to-v1.md +34 -0
  66. data/lib/administrate/base_dashboard.rb +20 -10
  67. data/lib/administrate/engine.rb +7 -6
  68. data/lib/administrate/field/associative.rb +8 -23
  69. data/lib/administrate/field/base.rb +40 -5
  70. data/lib/administrate/field/belongs_to.rb +8 -8
  71. data/lib/administrate/field/date.rb +6 -2
  72. data/lib/administrate/field/date_time.rb +3 -4
  73. data/lib/administrate/field/deferred.rb +14 -18
  74. data/lib/administrate/field/has_many.rb +29 -6
  75. data/lib/administrate/field/has_one.rb +11 -15
  76. data/lib/administrate/field/number.rb +2 -8
  77. data/lib/administrate/field/password.rb +4 -0
  78. data/lib/administrate/field/polymorphic.rb +4 -4
  79. data/lib/administrate/field/rich_text.rb +21 -0
  80. data/lib/administrate/field/select.rb +4 -0
  81. data/lib/administrate/field/time.rb +5 -4
  82. data/lib/administrate/generator_helpers.rb +1 -1
  83. data/lib/administrate/namespace/resource.rb +1 -1
  84. data/lib/administrate/namespace.rb +10 -10
  85. data/lib/administrate/order.rb +40 -35
  86. data/lib/administrate/page/base.rb +2 -7
  87. data/lib/administrate/page/collection.rb +3 -2
  88. data/lib/administrate/page/form.rb +10 -2
  89. data/lib/administrate/page/show.rb +10 -2
  90. data/lib/administrate/resource_resolver.rb +1 -1
  91. data/lib/administrate/search.rb +14 -16
  92. data/lib/administrate/version.rb +1 -1
  93. data/lib/administrate/view_generator.rb +4 -3
  94. data/lib/administrate.rb +3 -36
  95. data/lib/generators/administrate/dashboard/dashboard_generator.rb +12 -9
  96. data/lib/generators/administrate/field/field_generator.rb +2 -2
  97. data/lib/generators/administrate/install/install_generator.rb +3 -2
  98. data/lib/generators/administrate/routes/routes_generator.rb +6 -5
  99. data/lib/generators/administrate/views/field_generator.rb +2 -2
  100. data/lib/generators/administrate/views/index_generator.rb +5 -0
  101. data/lib/generators/administrate/views/layout_generator.rb +1 -1
  102. metadata +54 -77
  103. data/app/assets/javascripts/administrate/components/associative.js +0 -5
  104. data/app/assets/javascripts/administrate/components/select.js +0 -3
  105. data/app/assets/stylesheets/administrate/utilities/_text-color.scss +0 -3
  106. data/lib/generators/administrate/assets/assets_generator.rb +0 -12
  107. data/lib/generators/administrate/assets/javascripts_generator.rb +0 -17
  108. data/lib/generators/administrate/assets/stylesheets_generator.rb +0 -17
@@ -82,7 +82,8 @@ the table views and in the dropdown menu on the record forms.
82
82
  You can set multiple columns as well with direction. E.g.: `"name, email DESC"`.
83
83
 
84
84
  `:scope` - Specifies a custom scope inside a callable. Useful for preloading.
85
- Example: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`
85
+ Example #1: `.with_options(scope: -> { MyModel.includes(:rel).limit(5) })`
86
+ Example #2: `.with_options(scope: -> (field) { field.resource.my_models.includes(:rel).limit(5) })`
86
87
 
87
88
  `:include_blank` - Specifies if the select element to be rendered should include
88
89
  blank option. Default is `true`.
@@ -105,14 +106,20 @@ For example:
105
106
  with this, you will be able to search through the column `name` from the
106
107
  association `belongs_to :country`, from your model.
107
108
 
108
- `:primary_key` (deprecated) - Specifies the association's primary_key.
109
+ `:sortable` - Specifies if sorting should be enabled in the table views.
110
+ Default is `true`.
109
111
 
110
- `:foreign_key` (deprecated) - Specifies the name of the foreign key directly.
112
+ `:sorting_column` - Specifies the column of the associated model to be used for sorting in the table views and dropdown menu.
113
+ If `sorting_column` is omitted and `order` is specified, the value of `order` will be used.
114
+ If neither is specified, sorting will be done using the foreign key.
111
115
 
112
- `:class_name` (deprecated) - Specifies the name of the associated class.
116
+ `:class_name` - Specifies the name of the associated class.
113
117
 
114
118
  **Field::HasMany**
115
119
 
120
+ `:collection_attributes` - Set the columns to display in the show view.
121
+ Default is COLLECTION_ATTRIBUTES in dashboard.
122
+
116
123
  `:limit` - The number of resources (paginated) to display in the show view. To disable pagination,
117
124
  set this to `0` or `false`. Default is `5`.
118
125
 
@@ -120,11 +127,15 @@ set this to `0` or `false`. Default is `5`.
120
127
 
121
128
  `:direction` - What direction the sort should be in, `:asc` (default) or `:desc`.
122
129
 
123
- `:primary_key` (deprecated) - Specifies object's primary_key.
130
+ `:sortable` - Specifies if sorting should be enabled in the table views.
131
+ Default is `true`.
124
132
 
125
- `:foreign_key` (deprecated) - Specifies the name of the foreign key directly.
133
+ `:sorting_column` - Specifies the column of the associated model to be used for sorting in the dropdown menu.
134
+ If `sorting_column` is omitted and `sort_by` is specified, the value of `sort_by` will be used.
135
+ If neither is specified, sorting will be done using the foreign key.
136
+ For `HasMany` associations, sorting is based on the count, so this option is not referenced in the table views.
126
137
 
127
- `:class_name` (deprecated) - Specifies the name of the associated class.
138
+ `:class_name` - Specifies the name of the associated class.
128
139
 
129
140
  **Field::HasOne**
130
141
 
@@ -141,16 +152,23 @@ Default is `false`.
141
152
  For example:
142
153
 
143
154
  ```ruby
144
- cities: Field::HasMany.with_options(
155
+ city: Field::HasOne.with_options(
145
156
  searchable: true,
146
157
  searchable_fields: ['name'],
147
158
  )
148
159
  ```
149
160
 
150
161
  with this, you will be able to search through the column `name` from the
151
- association `has_many :cities`, from your model.
162
+ association `has_one :city`, from your model.
163
+
164
+ `:sortable` - Specifies if sorting should be enabled in the table views.
165
+ Default is `true`.
152
166
 
153
- `:class_name` (deprecated) - Specifies the name of the associated class.
167
+ `:sorting_column` - Specifies the column of the associated model to be used for sorting in the table views and dropdown menu.
168
+ If `sorting_column` is omitted and `order` is specified, the value of `order` will be used.
169
+ If neither is specified, sorting will be done using the foreign key.
170
+
171
+ `:class_name` - Specifies the name of the associated class.
154
172
 
155
173
  **Field::Number**
156
174
 
@@ -158,6 +176,13 @@ association `has_many :cities`, from your model.
158
176
  Note that currently number fields are searched like text, which may yield
159
177
  more results than expected. Default is `false`.
160
178
 
179
+ `:sortable` - Specifies if sorting should be enabled in the table views.
180
+ Default is `true`.
181
+
182
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
183
+ By default, the column itself is used, but when used as a virtual column,
184
+ a custom sorting column can be specified.
185
+
161
186
  `:decimals` - Set the number of decimals to display. Defaults to `0`.
162
187
 
163
188
  `:prefix` - Prefixes the number with a string. Defaults to `""`.
@@ -168,8 +193,7 @@ more results than expected. Default is `false`.
168
193
  and works by by passing a hash that includes the formatter (`formatter`) and
169
194
  the options for the formatter (`formatter_options`). Defaults to the locale's
170
195
  delimiter when `formatter_options` does not include a `delimiter`. See the
171
- example below. Note that currently only
172
- `ActiveSupport::NumberHelper.number_to_delimited` is supported.
196
+ example below. All helpers from `ActiveSupport::NumberHelper` are supported.
173
197
 
174
198
  For example, you might use the following to display U.S. currency:
175
199
 
@@ -207,8 +231,18 @@ Default is `[]`.
207
231
  `:order` - What to sort the association by in the form select.
208
232
  Default is `nil`.
209
233
 
234
+ `:sortable` - Specifies if sorting should be enabled in the table views.
235
+ Default is `true`.
236
+
210
237
  **Field::DateTime**
211
238
 
239
+ `:sortable` - Specifies if sorting should be enabled in the table views.
240
+ Default is `true`.
241
+
242
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
243
+ By default, the column itself is used, but when used as a virtual column,
244
+ a custom sorting column can be specified.
245
+
212
246
  `:format` - Specify what format, using `strftime` you would like `DateTime`
213
247
  objects to display as.
214
248
 
@@ -217,6 +251,13 @@ in.
217
251
 
218
252
  **Field::Date**
219
253
 
254
+ `:sortable` - Specifies if sorting should be enabled in the table views.
255
+ Default is `true`.
256
+
257
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
258
+ By default, the column itself is used, but when used as a virtual column,
259
+ a custom sorting column can be specified.
260
+
220
261
  `:format` - Specify what format, using `strftime` you would like `Date`
221
262
  objects to display as.
222
263
 
@@ -246,6 +287,13 @@ If no collection is provided and no enum can be detected, the list of options wi
246
287
  `:searchable` - Specify if the attribute should be considered when searching.
247
288
  Default is `true`.
248
289
 
290
+ `:sortable` - Specifies if sorting should be enabled in the table views.
291
+ Default is `true`.
292
+
293
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
294
+ By default, the column itself is used, but when used as a virtual column,
295
+ a custom sorting column can be specified.
296
+
249
297
  `:include_blank` - Similar to [the option of the same name accepted by Rails helpers](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html). If provided, a "blank" option will be added first to the list of options, with the value of `include_blank` as label.
250
298
 
251
299
  **Field::String**
@@ -253,6 +301,13 @@ Default is `true`.
253
301
  `:searchable` - Specify if the attribute should be considered when searching.
254
302
  Default is `true`.
255
303
 
304
+ `:sortable` - Specifies if sorting should be enabled in the table views.
305
+ Default is `true`.
306
+
307
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
308
+ By default, the column itself is used, but when used as a virtual column,
309
+ a custom sorting column can be specified.
310
+
256
311
  `:truncate` - Set the number of characters to display in the index view.
257
312
  Defaults to `50`.
258
313
 
@@ -261,14 +316,31 @@ Defaults to `50`.
261
316
  `:searchable` - Specify if the attribute should be considered when searching.
262
317
  Default is `false`.
263
318
 
319
+ `:sortable` - Specifies if sorting should be enabled in the table views.
320
+ Default is `true`.
321
+
322
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
323
+ By default, the column itself is used, but when used as a virtual column,
324
+ a custom sorting column can be specified.
325
+
264
326
  `:truncate` - Set the number of characters to display in the index view.
265
327
  Defaults to `50`.
266
328
 
329
+ `:input_options` - Options to customize the text area in form view.
330
+ Example: `.with_options(input_options: { rows: 20 })`
331
+
267
332
  **Field::Url**
268
333
 
269
334
  `:searchable` - Specify if the attribute should be considered when searching.
270
335
  Default is `true`.
271
336
 
337
+ `:sortable` - Specifies if sorting should be enabled in the table views.
338
+ Default is `true`.
339
+
340
+ `:sorting_column` - Specifies the column to be used for sorting in the table view.
341
+ By default, the column itself is used, but when used as a virtual column,
342
+ a custom sorting column can be specified.
343
+
272
344
  `:truncate` - Set the number of characters to display in the index view.
273
345
  Defaults to `50`.
274
346
 
@@ -280,6 +352,9 @@ Defaults is `{}`.
280
352
  `:searchable` - Specify if the attribute should be considered when searching.
281
353
  Default is `false`.
282
354
 
355
+ `:sortable` - Specifies if sorting should be enabled in the table views.
356
+ Default is `false`.
357
+
283
358
  `:truncate` - Set the number of characters to display in the views.
284
359
  Defaults to `50`.
285
360
 
@@ -383,3 +458,184 @@ FORM_ATTRIBUTES_EDIT = [
383
458
  ```
384
459
 
385
460
  Or for custom action with constant name `"FORM_ATTRIBUTES_#{action.upcase}"`
461
+
462
+ ### Form Fields' Hints
463
+
464
+ You can show a brief text element below an input field by setting the
465
+ corresponding translation key using the path:
466
+
467
+ `administrate.field_hints.#{model_name}.#{field_name}`
468
+
469
+ For example, with a Customer dashboard with an email field you can add a
470
+ string value that will be used as text hint:
471
+
472
+ ```yml
473
+ en:
474
+ administrate:
475
+ field_hints:
476
+ customer:
477
+ email: field_hint
478
+ ```
479
+
480
+ ## Grouped Attributes
481
+
482
+ You may have models with a large number of fields and therefore you might want to group them in a meaningul way:
483
+
484
+ ```ruby
485
+ class UserDashboard < Administrate::BaseDashboard
486
+ SHOW_PAGE_ATTRIBUTES = {
487
+ "" => [:username],
488
+ "Personal Information" => [:first_name, :last_name, :email],
489
+ "Address" => [:address_line_one, :address_line_two, :address_city, :address_state, :address_country]
490
+ }
491
+
492
+ FORM_ATTRIBUTES = {
493
+ "" => [:username, :password, :password_confirmation],
494
+ "personal_information" => [:first_name, :last_name, :email],
495
+ "address" => [:address_line_one, :address_line_two, :address_city, :address_state, :address_country]
496
+ }
497
+ end
498
+ ```
499
+
500
+ You can optionally translate your group labels:
501
+
502
+ ```yaml
503
+ en:
504
+ helpers:
505
+ label:
506
+ user:
507
+ address: Address
508
+ personal_information: Personal Information
509
+ ```
510
+
511
+ If not defined (see `SHOW_PAGE_ATTRIBUTES` above), Administrate will default to the given strings.
512
+
513
+ ## Virtual Attributes
514
+
515
+ For all field types, you can use the `getter` option to change where the data is retrieved from or to set the data directly.
516
+
517
+ By using this, you can define an attribute in `ATTRIBUTE_TYPES` that doesn’t exist in the model, and use it for various purposes.
518
+
519
+ ### Attribute Aliases
520
+
521
+ You can create an alias for an attribute. For example:
522
+
523
+ ```ruby
524
+ ATTRIBUTE_TYPES = {
525
+ shipped_at: Field::DateTime,
526
+ shipped_on: Field::Date.with_options(
527
+ getter: :shipped_at
528
+ )
529
+ }
530
+ COLLECTION_ATTRIBUTES = [
531
+ :shipped_on
532
+ }
533
+ SHOW_PAGE_ATTRIBUTES = [
534
+ :shipped_at
535
+ }
536
+ ```
537
+
538
+ In this example, a virtual attribute `shipped_on` based on the value of `shipped_at` is defined as a `Date` type and used for display on the index page (this can help save table cell space).
539
+
540
+ ### Decorated Attributes
541
+
542
+ You can also use this to decorate data. For example:
543
+
544
+ ```ruby
545
+ ATTRIBUTE_TYPES = {
546
+ price: Field::Number,
547
+ price_including_tax: Field::Number.with_options(
548
+ getter: -> (field) {
549
+ field.resource.price * 1.1 if field.resource.price.present?
550
+ }
551
+ )
552
+ }
553
+ ```
554
+
555
+ ### Composite Attributes
556
+
557
+ You can dynamically create a virtual attribute by combining multiple attributes for display. For example:
558
+
559
+ ```ruby
560
+ ATTRIBUTE_TYPES = {
561
+ first_name: Field::String,
562
+ last_name: Field::String,
563
+ full_name: Field::String.with_options(
564
+ getter: -> (field) {
565
+ [
566
+ field.resource.first_name,
567
+ field.resource.last_name
568
+ ].compact_blank.join(' ')
569
+ }
570
+ )
571
+ }
572
+ ```
573
+
574
+ ## Virtual Fields
575
+
576
+ Custom fields can also be defined using virtual fields.
577
+
578
+ ```ruby
579
+ ATTRIBUTE_TYPES = {
580
+ id: Field::Number,
581
+ receipt: Field::ReceiptLink
582
+ }
583
+ ```
584
+
585
+ ```ruby
586
+ module Administrate
587
+ module Field
588
+ class ReceiptLink < Base
589
+ def data
590
+ resource.id
591
+ end
592
+
593
+ def filename
594
+ "receipt-#{data}.pdf"
595
+ end
596
+
597
+ def url
598
+ "/files/receipts/#{filename}"
599
+ end
600
+ end
601
+ end
602
+ end
603
+ ```
604
+
605
+ ```erb
606
+ <%= link_to field.filename, field.url %>
607
+ ```
608
+
609
+ ### Custom Actions via Virtual Field
610
+
611
+ By creating custom fields that are not dependent on specific attributes, you can insert custom views into any screen.
612
+ For example, you can add custom buttons like this:
613
+
614
+ ```ruby
615
+ ATTRIBUTE_TYPES = {
616
+ id: Field::Number,
617
+ custom_index_actions: Field::CustomActionButtons,
618
+ custom_show_actions: Field::CustomActionButtons,
619
+ }
620
+ ```
621
+
622
+ ```ruby
623
+ module Administrate
624
+ module Field
625
+ class CustomActionButtons < Base
626
+ def data
627
+ resource.id
628
+ end
629
+ end
630
+ end
631
+ end
632
+ ```
633
+
634
+ ```erb
635
+ <%# app/views/fields/custom_action_buttons/_index.html.erb %>
636
+ <% if field.data.present? %>
637
+ <%= button_to "some action 1", [:some_action_1, namespace, field.resource] %>
638
+ <%= button_to "some action 2", [:some_action_2, namespace, field.resource] %>
639
+ <%= button_to "some action 3", [:some_action_3, namespace, field.resource] %>
640
+ <% end %>
641
+ ```
@@ -10,7 +10,7 @@ In general, you can override any of the views under Administrate's
10
10
  [/app/views][1].
11
11
  For example, say that you want to customize the template used for flash
12
12
  messages. You can provide your own as
13
- `/app/views/administrate/application/_flashes.html.erb`, and it will replace
13
+ `app/views/admin/application/_flashes.html.erb`, and it will replace
14
14
  Administrate's own.
15
15
 
16
16
  Figuring out which views are available and where can be repetitive. You can
@@ -94,3 +94,50 @@ rails generate administrate:views:layout
94
94
  # It only generates the sidebar partial
95
95
  # -> app/views/admin/application/_navigation.html.erb
96
96
  ```
97
+
98
+ ## Customizing for a specific layout
99
+
100
+ You can use several hook points to add elements to specific layouts or specific pages:
101
+
102
+ * header_middle
103
+ * header_last
104
+ * before_main
105
+ * main
106
+ * after_main
107
+
108
+ For example, you can add a button in the middle of the header as follows:
109
+
110
+ ```eruby
111
+ <%# app/views/admin/customers/_index_header.html.erb %>
112
+
113
+ <% content_for(:header_middle) do %>
114
+ <div>
115
+ You are logged in as <em><%= pundit_user.name %></em>.
116
+ <%= link_to("Become the Admin", become_admin_customer_path("admin")) unless pundit_user.admin? %>
117
+ </div>
118
+ <% end %>
119
+
120
+ <%= render template: 'administrate/application/_index_header', locals: local_assigns %>
121
+ ```
122
+
123
+ ## Adding custom CSS and JS
124
+
125
+ You can add custom CSS and JS to Administrate. Put the files in the
126
+ appropriate folders (typically under `assets`) and point Administrate to them
127
+ using the following API, preferably in an initializer. For example, if your
128
+ files are called `admin.css` and `admin.js`:
129
+
130
+ ```
131
+ /// config/initializers/administrate.rb
132
+ Administrate::Engine.add_stylesheet("admin")
133
+ Administrate::Engine.add_javascript("admin")
134
+ ```
135
+
136
+ Then make sure to list them in your manifest file (Rails will helpfully remind
137
+ you of this step if you miss it):
138
+
139
+ ```
140
+ /// app/assets/config/manifest.js
141
+ //= link admin.css
142
+ //= link admin.js
143
+ ```
@@ -3,7 +3,7 @@ title: Getting Started
3
3
  ---
4
4
 
5
5
  Administrate is released as a Ruby gem, and can be installed on Rails
6
- applications version 6.0 or greater. We support Ruby 2.7 and up.
6
+ applications version 6.0 or greater. We support Ruby 3.0 and up.
7
7
 
8
8
  First, add the following to your Gemfile:
9
9
 
@@ -18,10 +18,10 @@ Since ActiveRecord infers the class name from the first argument, the new `has_m
18
18
 
19
19
  ## Add new relationship to dashboard
20
20
 
21
- Your new scoped relation can be used in the dashboard just like the original `HasMany`. Notice the new field needs to specifiy the class name as an option like you did in the model.
21
+ Your new scoped relation can be used in the dashboard just like the original `HasMany`.
22
22
 
23
23
  ```ruby
24
24
  ATTRIBUTE_TYPES = {
25
25
  orders: Field::HasMany,
26
- processed_orders: Field::HasMany.with_options(class_name: 'Order')
26
+ processed_orders: Field::HasMany
27
27
  ```
@@ -0,0 +1,45 @@
1
+ ---
2
+ title: Switching templates with view variants
3
+ ---
4
+
5
+ You can switch to different templates using Rails' support for [view
6
+ variants][], which can be used to override the template used inside the
7
+ controller.
8
+
9
+ By setting the `request.variant` option to any value (in this case, `:admin`)
10
+ at any point in any controller, you can add a new variant to Rails' template
11
+ lookup tree (in this case, `.html+admin.erb`).
12
+
13
+ For example, to add a button to become the admin to view certain functionality:
14
+
15
+ ```ruby
16
+ class CustomersController < Admin::ApplicationController
17
+ before_action :with_variant, only: %i[index]
18
+
19
+ private
20
+
21
+ def with_variant
22
+ if @current_user.admin?
23
+ request.variant = :admin
24
+ end
25
+ end
26
+ end
27
+ ```
28
+
29
+ ```erb
30
+ <!-- app/views/admin/customers/_index_header.html.erb -->
31
+ <p class="identity__banner">
32
+ You are logged in as <em><%= pundit_user.name %></em>.
33
+ <%= link_to("Become the Admin", become_admin_customer_path("admin"),
34
+ class: "identity__become-action")%>
35
+ </p>
36
+ ```
37
+
38
+ ```erb
39
+ <!-- app/views/admin/customers/_index_header.html+admin.erb -->
40
+ <p class="identity__banner identity__banner--admin">
41
+ You are logged in as <em><%= pundit_user.name %></em>.
42
+ </p>
43
+ ```
44
+
45
+ [view variants]: https://guides.rubyonrails.org/layouts_and_rendering.html#the-variants-option
data/docs/guides.md CHANGED
@@ -5,3 +5,4 @@ title: Guides
5
5
  - [Hiding Dashboards from the Sidebar](./guides/hiding_dashboards_from_sidebar)
6
6
  - [Customising the search](./guides/customising_search)
7
7
  - [Scoping HasMany Relations](./guides/scoping_has_many_relations.md)
8
+ - [Switching templates with view variants](./guides/switching_templates_with_view_variants.md)
@@ -0,0 +1,34 @@
1
+ ---
2
+ title: Migrating to v1
3
+ ---
4
+
5
+ In the release of v1.0.0, we [began bundling assets in the gem itself][2397].
6
+ In the future, this should mean that any changes made to assets (coming
7
+ from you or from Administrate updates) should not have an effect on you.
8
+ However, this is quite a big change and so you might find some problems.
9
+
10
+ As with most upgrades, if you're upgrading between versions with changes to the
11
+ templates, and if you've customised them, you may need to apply recent changes.
12
+ You can see those which changed in the [CHANGELOG][].
13
+
14
+ This applies to both modified files in `app/views/admin` and `app/views/fields`
15
+ if you have your own Field classes defined. A good way is to use the same
16
+ command to generate the view as for the earlier version:
17
+ `rails generate administrate:views:ACTION MyModel`
18
+ noting the changes and re-applying the modifications
19
+
20
+ [2397]: https://github.com/thoughtbot/administrate/pull/2397
21
+ [CHANGELOG]: https://github.com/thoughtbot/administrate/blob/main/CHANGELOG.md
22
+
23
+ ## Reported issues
24
+
25
+ ### Removal of the `sprockets-rails` transitive dependency
26
+
27
+ [Issue][2514]
28
+
29
+ Previously, Administrate depended on [`sprockets-rails`][], if you have a
30
+ dependency which requires this, you may now need to add a direct dependency on
31
+ your application.
32
+
33
+ [2514]: https://github.com/thoughtbot/administrate/issues/2514
34
+ [`sprockets-rails`]: https://rubygems.org/gems/sprockets-rails
@@ -7,6 +7,7 @@ require "administrate/field/has_many"
7
7
  require "administrate/field/has_one"
8
8
  require "administrate/field/number"
9
9
  require "administrate/field/polymorphic"
10
+ require "administrate/field/rich_text"
10
11
  require "administrate/field/select"
11
12
  require "administrate/field/string"
12
13
  require "administrate/field/text"
@@ -69,11 +70,17 @@ module Administrate
69
70
  end
70
71
 
71
72
  def permitted_attributes(action = nil)
72
- form_attributes(action).map do |attr|
73
+ attributes = form_attributes action
74
+
75
+ if attributes.is_a? Hash
76
+ attributes = attributes.values.flatten
77
+ end
78
+
79
+ attributes.map do |attr|
73
80
  attribute_types[attr].permitted_attribute(
74
81
  attr,
75
82
  resource_class: self.class.model,
76
- action: action,
83
+ action: action
77
84
  )
78
85
  end.uniq
79
86
  end
@@ -83,7 +90,11 @@ module Administrate
83
90
  end
84
91
 
85
92
  def collection_attributes
86
- self.class::COLLECTION_ATTRIBUTES
93
+ if self.class::COLLECTION_ATTRIBUTES.is_a?(Hash)
94
+ self.class::COLLECTION_ATTRIBUTES.values.flatten
95
+ else
96
+ self.class::COLLECTION_ATTRIBUTES
97
+ end
87
98
  end
88
99
 
89
100
  def search_attributes
@@ -100,14 +111,13 @@ module Administrate
100
111
  attribute_includes(collection_attributes)
101
112
  end
102
113
 
103
- def item_includes
104
- # Deprecated, internal usage has moved to #item_associations
105
- Administrate.warn_of_deprecated_method(self.class, :item_includes)
106
- attribute_includes(show_page_attributes)
107
- end
108
-
109
114
  def item_associations
110
- attribute_associated(show_page_attributes)
115
+ attributes = if show_page_attributes.is_a?(Hash)
116
+ show_page_attributes.values.flatten
117
+ else
118
+ show_page_attributes
119
+ end
120
+ attribute_associated attributes
111
121
  end
112
122
 
113
123
  private
@@ -1,8 +1,4 @@
1
- require "jquery-rails"
2
1
  require "kaminari"
3
- require "sassc-rails"
4
- require "selectize-rails"
5
- require "sprockets/railtie"
6
2
 
7
3
  require "administrate/namespace/resource"
8
4
  require "administrate/not_authorized_error"
@@ -13,7 +9,6 @@ require "administrate/order"
13
9
  require "administrate/resource_resolver"
14
10
  require "administrate/search"
15
11
  require "administrate/namespace"
16
- require "administrate/namespace/resource"
17
12
 
18
13
  module Administrate
19
14
  class Engine < ::Rails::Engine
@@ -25,10 +20,16 @@ module Administrate
25
20
  initializer "administrate.assets.precompile" do |app|
26
21
  app.config.assets.precompile += [
27
22
  "administrate/application.js",
28
- "administrate/application.css",
23
+ "administrate/application.css"
29
24
  ]
30
25
  end
31
26
 
27
+ initializer "administrate.action_text" do
28
+ ActiveSupport.on_load :action_text_rich_text do
29
+ require "administrate/field/rich_text"
30
+ end
31
+ end
32
+
32
33
  def self.add_javascript(script)
33
34
  @@javascripts << script
34
35
  end