administratum 0.2.4 → 0.4.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.
- checksums.yaml +4 -4
- data/DESIGN.md +34 -3
- data/README.md +93 -3
- data/app/assets/builds/administratum.css +1 -1
- data/app/assets/javascripts/administratum.js +429 -3
- data/app/assets/tailwind/administratum/components.css +48 -3
- data/app/assets/tailwind/administratum/theme.css +13 -3
- data/app/controllers/administratum/resource.rb +24 -1
- data/app/helpers/administratum/application_helper.rb +1 -0
- data/app/helpers/administratum/form_helper.rb +56 -4
- data/app/helpers/administratum/reorder_helper.rb +55 -0
- data/app/helpers/administratum/resource_helper.rb +44 -5
- data/app/views/administratum/fields/_belongs_to.html.erb +2 -1
- data/app/views/administratum/fields/_select.html.erb +2 -1
- data/app/views/administratum/resources/_table.html.erb +16 -3
- data/app/views/administratum/resources/edit.html.erb +3 -1
- data/app/views/administratum/resources/index.html.erb +4 -2
- data/app/views/administratum/resources/new.html.erb +3 -1
- data/app/views/administratum/resources/show.html.erb +3 -1
- data/config/locales/en.yml +14 -0
- data/lib/administratum/definition.rb +28 -6
- data/lib/administratum/dsl.rb +30 -0
- data/lib/administratum/reorder.rb +33 -0
- data/lib/administratum/version.rb +1 -1
- data/lib/administratum.rb +1 -0
- metadata +3 -1
|
@@ -56,13 +56,52 @@ module Administratum
|
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
# Options for select-style fields. `collection` may be an array, a
|
|
59
|
-
# relation, or a lambda (optionally receiving the record).
|
|
59
|
+
# relation, or a lambda (optionally receiving the record). `group_by:` (an
|
|
60
|
+
# attribute name, or a lambda receiving each item) groups them into optgroups.
|
|
60
61
|
def field_collection(field, record)
|
|
61
62
|
collection = field.options[:collection]
|
|
62
63
|
collection = collection.arity.zero? ? instance_exec(&collection) : instance_exec(record, &collection) if collection.respond_to?(:call)
|
|
63
64
|
collection ||= association_collection(field, record)
|
|
64
65
|
collection = collection.to_a if collection.is_a?(ActiveRecord::Relation)
|
|
65
|
-
|
|
66
|
+
group_by = field.options[:group_by]
|
|
67
|
+
return collection.map { |item| collection_choice(item) } unless group_by
|
|
68
|
+
|
|
69
|
+
collection.group_by { |item| collection_group(group_by, item) }
|
|
70
|
+
.map { |group, items| [group, items.map { |item| collection_choice(item) }] }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The `searchable:` option of a select field, resolved: nil, or a hash with
|
|
74
|
+
# the search `url:` (a string, or a lambda optionally receiving the record) and `min:`.
|
|
75
|
+
def field_search(field, record)
|
|
76
|
+
option = field.options[:searchable]
|
|
77
|
+
return unless option
|
|
78
|
+
|
|
79
|
+
option = {} if option == true
|
|
80
|
+
url = option[:url]
|
|
81
|
+
url = url.arity.zero? ? instance_exec(&url) : instance_exec(record, &url) if url.respond_to?(:call)
|
|
82
|
+
{ url:, min: option[:min] }
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Choices for a select field. One that searches remotely only renders the
|
|
86
|
+
# current value; the rest come from the search URL.
|
|
87
|
+
def select_field_choices(field, record, search = field_search(field, record))
|
|
88
|
+
return field_collection(field, record) unless search&.dig(:url)
|
|
89
|
+
|
|
90
|
+
value = record.try(field.name)
|
|
91
|
+
return [] if value.blank?
|
|
92
|
+
|
|
93
|
+
reflection = field_reflection(field, record)
|
|
94
|
+
return [[value.to_s, value]] unless reflection
|
|
95
|
+
|
|
96
|
+
reflection.klass.where(reflection.klass.primary_key => value).map { |item| collection_choice(item) }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Data attributes that make a select searchable (see administratum.js).
|
|
100
|
+
# Use them on hand-written selects too: `data: adm_combobox_data(url: …)`.
|
|
101
|
+
def adm_combobox_data(url: nil, min: nil)
|
|
102
|
+
{ adm_combobox: true, adm_combobox_url: url, adm_combobox_min: min,
|
|
103
|
+
adm_combobox_empty: t("administratum.combobox.empty"), adm_combobox_loading: t("administratum.combobox.loading"),
|
|
104
|
+
adm_combobox_short: t("administratum.combobox.short"), adm_combobox_error: t("administratum.combobox.error") }.compact
|
|
66
105
|
end
|
|
67
106
|
|
|
68
107
|
# HTML options for a field's input: the partial's defaults, then the
|
|
@@ -101,11 +140,24 @@ module Administratum
|
|
|
101
140
|
end
|
|
102
141
|
|
|
103
142
|
def association_collection(field, record)
|
|
104
|
-
|
|
105
|
-
reflection = record.class.reflect_on_association(name.to_sym) || record.class.reflect_on_association(name.pluralize.to_sym)
|
|
143
|
+
reflection = field_reflection(field, record)
|
|
106
144
|
reflection ? reflection.klass.all : []
|
|
107
145
|
end
|
|
108
146
|
|
|
147
|
+
def field_reflection(field, record)
|
|
148
|
+
name = field.name.to_s.delete_suffix("_ids").delete_suffix("_id")
|
|
149
|
+
record.class.reflect_on_association(name.to_sym) || record.class.reflect_on_association(name.pluralize.to_sym)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def collection_choice(item)
|
|
153
|
+
item.is_a?(ActiveRecord::Base) ? [record_title(item), item.id] : item
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def collection_group(group_by, item)
|
|
157
|
+
group = group_by.respond_to?(:call) ? instance_exec(item, &group_by) : item.public_send(group_by)
|
|
158
|
+
group.is_a?(ActiveRecord::Base) ? record_title(group) : group.to_s
|
|
159
|
+
end
|
|
160
|
+
|
|
109
161
|
# Dates and times are formatted; anything else (such as a submitted
|
|
110
162
|
# string that failed validation) is shown as typed.
|
|
111
163
|
def iso_value(value, format)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Administratum
|
|
4
|
+
# Controls for putting records in order. Mark the list's container with
|
|
5
|
+
# data-adm-sortable and each record's element with data-adm-sortable-item,
|
|
6
|
+
# put reorder_controls in each, and submit them with reorder_form and
|
|
7
|
+
# reorder_save_button:
|
|
8
|
+
#
|
|
9
|
+
# <%= reorder_form id: "chapter-order", url: action_path(:reorder) %>
|
|
10
|
+
# <ol data-adm-sortable>
|
|
11
|
+
# <% chapters.each do |chapter| %>
|
|
12
|
+
# <li data-adm-sortable-item><%= reorder_controls chapter, form: "chapter-order" %> <%= chapter.title %></li>
|
|
13
|
+
# <% end %>
|
|
14
|
+
# </ol>
|
|
15
|
+
# <%= reorder_save_button form: "chapter-order" %>
|
|
16
|
+
module ReorderHelper
|
|
17
|
+
# An empty form. The position fields and the save button join it through
|
|
18
|
+
# their form="…" attribute, so the list can sit anywhere, even around rows
|
|
19
|
+
# that hold forms of their own.
|
|
20
|
+
def reorder_form(id:, url:, method: :patch)
|
|
21
|
+
form_with(url:, method:, id:, class: "adm-reorder-form") { "" }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# A drag handle and move up/down buttons, shown by administratum.js, and
|
|
25
|
+
# the positions[id] number field they keep up to date. Without JS only the
|
|
26
|
+
# number field shows.
|
|
27
|
+
def reorder_controls(record, form:, column: :position, position: record.public_send(column))
|
|
28
|
+
input_id = "#{form}-position-#{record.id}"
|
|
29
|
+
fallback = tag.span(data: { adm_sortable_fallback: true }) do
|
|
30
|
+
label_tag(input_id, t("administratum.reorder.position"), class: "adm-sr-only") +
|
|
31
|
+
number_field_tag("positions[#{record.id}]", position, id: input_id, form:, min: 1,
|
|
32
|
+
class: "adm-input adm-sortable-position", data: { adm_sortable_position: true })
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
tag.span(class: "adm-sortable-controls", data: { adm_sortable_moved: t("administratum.reorder.moved") }) do
|
|
36
|
+
safe_join([
|
|
37
|
+
reorder_button("⠿", :handle, class: "adm-icon-btn adm-sortable-handle", data: { adm_sortable_handle: true }),
|
|
38
|
+
reorder_button("↑", :up, class: "adm-icon-btn", data: { adm_sortable_up: true }),
|
|
39
|
+
reorder_button("↓", :down, class: "adm-icon-btn", data: { adm_sortable_down: true }),
|
|
40
|
+
fallback
|
|
41
|
+
])
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def reorder_save_button(form:, label: t("administratum.reorder.save"), **options)
|
|
46
|
+
tag.button(label, **options.except(:class), type: "submit", form:, class: class_names("adm-btn adm-btn-primary", options[:class]))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def reorder_button(symbol, name, **)
|
|
52
|
+
tag.button(symbol, type: "button", hidden: true, aria: { label: t("administratum.reorder.#{name}") }, **)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -9,6 +9,13 @@ module Administratum
|
|
|
9
9
|
ghost: "adm-btn adm-btn-ghost"
|
|
10
10
|
}.freeze
|
|
11
11
|
|
|
12
|
+
MENU_STYLES = {
|
|
13
|
+
primary: "adm-menu-item adm-menu-item-primary",
|
|
14
|
+
secondary: "adm-menu-item",
|
|
15
|
+
danger: "adm-menu-item adm-menu-item-danger",
|
|
16
|
+
ghost: "adm-menu-item"
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
12
19
|
# The display name of any record: its resource's title attribute, else to_s.
|
|
13
20
|
def record_title(record)
|
|
14
21
|
return "" if record.nil?
|
|
@@ -86,28 +93,49 @@ module Administratum
|
|
|
86
93
|
safe_join(buttons, " ")
|
|
87
94
|
end
|
|
88
95
|
|
|
89
|
-
|
|
96
|
+
# `menu: true` renders it as an item of an aside menu instead of a button.
|
|
97
|
+
def render_button(button, record = @resource, menu: false)
|
|
90
98
|
return unless button_visible?(button, record)
|
|
91
99
|
|
|
92
100
|
label = button.label || t("administratum.actions.#{button.name}", default: button.name.to_s.humanize)
|
|
93
|
-
|
|
101
|
+
styles = menu ? MENU_STYLES : BUTTON_STYLES
|
|
102
|
+
classes = styles.fetch(button.style, styles[:secondary])
|
|
94
103
|
|
|
95
104
|
return panel_button(button, label, classes, record) if button.panel || button.upload
|
|
96
105
|
|
|
97
106
|
url, http_method = button_target(button, record)
|
|
98
|
-
administratum_button(label, url, method: http_method, confirm: button.confirm, class: classes
|
|
107
|
+
administratum_button(label, url, method: http_method, confirm: button.confirm, class: classes,
|
|
108
|
+
form_class: (menu ? "adm-menu-form" : "adm-inline-form"))
|
|
99
109
|
end
|
|
100
110
|
|
|
101
111
|
# A link for GET, otherwise a one-button form (no rails-ujs needed).
|
|
102
|
-
def administratum_button(label, url, method: :get, confirm: nil, **options)
|
|
112
|
+
def administratum_button(label, url, method: :get, confirm: nil, form_class: "adm-inline-form", **options)
|
|
103
113
|
if method.to_sym == :get
|
|
104
114
|
link_to label, url, **options
|
|
105
115
|
else
|
|
106
116
|
form = { data: confirm ? { adm_confirm: confirm } : {} }
|
|
107
|
-
button_to label, url, method:, form:, form_class
|
|
117
|
+
button_to label, url, method:, form:, form_class:, **options
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Lays the block out beside the page's asides, or returns it as is when the
|
|
122
|
+
# page has none (or every button in them is hidden by `if:`).
|
|
123
|
+
def with_asides(page, record = @resource, &)
|
|
124
|
+
content = capture(&)
|
|
125
|
+
asides = asides_for(page, record)
|
|
126
|
+
return content if asides.blank?
|
|
127
|
+
|
|
128
|
+
tag.div(class: "adm-with-aside") do
|
|
129
|
+
tag.div(content, class: "adm-with-aside-main") + tag.aside(asides, class: "adm-aside")
|
|
108
130
|
end
|
|
109
131
|
end
|
|
110
132
|
|
|
133
|
+
# The aside menus for a page, without the surrounding layout.
|
|
134
|
+
def asides_for(page, record = @resource)
|
|
135
|
+
menus = definition.asides_for(page).filter_map { |aside| render_aside(aside, record) }
|
|
136
|
+
safe_join(menus) if menus.any?
|
|
137
|
+
end
|
|
138
|
+
|
|
111
139
|
# Tabs for the resource's index scopes. Switching tab keeps filters and sorting
|
|
112
140
|
# but goes back to the first page.
|
|
113
141
|
def index_scope_tabs
|
|
@@ -169,6 +197,17 @@ module Administratum
|
|
|
169
197
|
class: class_names("adm-btn adm-btn-danger", "adm-btn-sm" => small))
|
|
170
198
|
end
|
|
171
199
|
|
|
200
|
+
def render_aside(aside, record)
|
|
201
|
+
items = aside.buttons.filter_map { |button| render_button(button, record, menu: true) }
|
|
202
|
+
return if items.empty?
|
|
203
|
+
|
|
204
|
+
title = aside.title || t("administratum.actions.title")
|
|
205
|
+
tag.nav(class: "adm-card", "aria-label": title) do
|
|
206
|
+
tag.header(tag.h2(title, class: "adm-card-title"), class: "adm-card-header") +
|
|
207
|
+
tag.ul(safe_join(items.map { |item| tag.li(item) }), class: "adm-menu")
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
172
211
|
def button_visible?(button, record)
|
|
173
212
|
condition_met?(button.condition, record)
|
|
174
213
|
end
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
<% search = field_search(field, resource) %>
|
|
2
|
+
<%= form.select field.name, select_field_choices(field, resource, search), { include_blank: field.options[:placeholder] || field.options.fetch(:include_blank, true) }, **input_options(field, form:, class: "adm-input", data: search ? adm_combobox_data(**search) : {}) %>
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
<% search = field_search(field, resource) %>
|
|
2
|
+
<%= form.select field.name, select_field_choices(field, resource, search), { include_blank: field.options[:placeholder] || field.options.fetch(:include_blank, false) }, **input_options(field, form:, class: "adm-input", data: search ? adm_combobox_data(**search) : {}) %>
|
|
@@ -1,17 +1,24 @@
|
|
|
1
|
+
<% reorderable = index_reorderable? %>
|
|
1
2
|
<section class="adm-card">
|
|
2
3
|
<div class="adm-table-wrap">
|
|
3
4
|
<table class="adm-table">
|
|
4
5
|
<thead>
|
|
5
6
|
<tr>
|
|
7
|
+
<% if reorderable %>
|
|
8
|
+
<th scope="col" class="w-px whitespace-nowrap"><%= t("administratum.reorder.column") %></th>
|
|
9
|
+
<% end %>
|
|
6
10
|
<% definition.index_columns.each do |column| %>
|
|
7
11
|
<th scope="col"><%= column.sortable ? sort_link(@search, column.name, column_label(column)) : column_label(column) %></th>
|
|
8
12
|
<% end %>
|
|
9
13
|
<th scope="col" class="adm-table-actions"><span class="adm-sr-only"><%= t("administratum.actions.title") %></span></th>
|
|
10
14
|
</tr>
|
|
11
15
|
</thead>
|
|
12
|
-
<tbody
|
|
16
|
+
<tbody<% if reorderable %> data-adm-sortable<% end %>>
|
|
13
17
|
<% @resources.each do |record| %>
|
|
14
|
-
<tr
|
|
18
|
+
<tr<% if reorderable %> data-adm-sortable-item<% end %>>
|
|
19
|
+
<% if reorderable %>
|
|
20
|
+
<td class="w-px whitespace-nowrap"><%= reorder_controls record, form: "adm-reorder", column: definition.reorder_column %></td>
|
|
21
|
+
<% end %>
|
|
15
22
|
<% definition.index_columns.each_with_index do |column, index| %>
|
|
16
23
|
<td>
|
|
17
24
|
<% if index.zero? && !column.block && definition.action_enabled?(:show) %>
|
|
@@ -25,7 +32,7 @@
|
|
|
25
32
|
</tr>
|
|
26
33
|
<% end %>
|
|
27
34
|
<% if @resources.empty? %>
|
|
28
|
-
<tr><td colspan="<%= definition.index_columns.size + 1 %>" class="adm-empty"><%= t("administratum.empty", resources: resource_class.plural_label.downcase) %></td></tr>
|
|
35
|
+
<tr><td colspan="<%= definition.index_columns.size + (reorderable ? 2 : 1) %>" class="adm-empty"><%= t("administratum.empty", resources: resource_class.plural_label.downcase) %></td></tr>
|
|
29
36
|
<% end %>
|
|
30
37
|
</tbody>
|
|
31
38
|
</table>
|
|
@@ -37,6 +44,12 @@
|
|
|
37
44
|
· <%= link_to t("administratum.download_csv"), url_for(format: :csv, q: request.query_parameters[:q], scope: current_index_scope&.name), class: "adm-link" %>
|
|
38
45
|
<% end %>
|
|
39
46
|
</div>
|
|
47
|
+
<% if reorderable %>
|
|
48
|
+
<div class="adm-form-footer">
|
|
49
|
+
<%= reorder_form id: "adm-reorder", url: action_path(:reorder) %>
|
|
50
|
+
<%= reorder_save_button form: "adm-reorder", class: "adm-btn-sm" %>
|
|
51
|
+
</div>
|
|
52
|
+
<% end %>
|
|
40
53
|
<div class="adm-pagy"><%== pagy_nav(@pagy) if @pagy.pages > 1 %></div>
|
|
41
54
|
</footer>
|
|
42
55
|
</section>
|
data/config/locales/en.yml
CHANGED
|
@@ -21,6 +21,7 @@ en:
|
|
|
21
21
|
created: "%{resource} was created."
|
|
22
22
|
updated: "%{resource} was updated."
|
|
23
23
|
destroyed: "%{resource} was deleted."
|
|
24
|
+
reordered: "%{resources} were reordered."
|
|
24
25
|
errors:
|
|
25
26
|
title:
|
|
26
27
|
one: 1 error prevented saving
|
|
@@ -33,6 +34,19 @@ en:
|
|
|
33
34
|
any: Any
|
|
34
35
|
from: "%{field} from"
|
|
35
36
|
to: "%{field} to"
|
|
37
|
+
combobox:
|
|
38
|
+
empty: No matches
|
|
39
|
+
loading: Searching…
|
|
40
|
+
short: Type at least %{count} characters
|
|
41
|
+
error: Search failed
|
|
42
|
+
reorder:
|
|
43
|
+
handle: Reorder (drag, or use the arrow keys)
|
|
44
|
+
up: Move up
|
|
45
|
+
down: Move down
|
|
46
|
+
position: Position
|
|
47
|
+
moved: Moved to position %{position} of %{count}
|
|
48
|
+
save: Save order
|
|
49
|
+
column: Order
|
|
36
50
|
upload:
|
|
37
51
|
file: File
|
|
38
52
|
submit: Upload
|
|
@@ -87,6 +87,9 @@ module Administratum
|
|
|
87
87
|
Button = Struct.new(:name, :label, :pages, :action, :to, :http_method, :confirm, :condition, :style, :panel, :upload,
|
|
88
88
|
keyword_init: true)
|
|
89
89
|
|
|
90
|
+
# A side menu of buttons beside a page's content, for actions that don't belong in the header.
|
|
91
|
+
Aside = Struct.new(:pages, :title, :buttons, keyword_init: true)
|
|
92
|
+
|
|
90
93
|
Parent = Struct.new(:name, :class_name, :shallow, :association, keyword_init: true) do
|
|
91
94
|
def model = class_name.constantize
|
|
92
95
|
def param = :"#{name}_id"
|
|
@@ -101,8 +104,9 @@ module Administratum
|
|
|
101
104
|
end
|
|
102
105
|
|
|
103
106
|
attr_accessor :model, :title_attribute, :permitted, :parent, :form_template, :scope_block, :route_key, :label,
|
|
104
|
-
:plural_label, :per_page, :default_sort, :csv
|
|
105
|
-
attr_reader :fields, :columns, :custom_actions, :buttons, :show_attributes, :enabled_actions, :menu,
|
|
107
|
+
:plural_label, :per_page, :default_sort, :csv, :reorder_column
|
|
108
|
+
attr_reader :fields, :columns, :custom_actions, :buttons, :asides, :show_attributes, :enabled_actions, :menu,
|
|
109
|
+
:index_scopes
|
|
106
110
|
|
|
107
111
|
def initialize
|
|
108
112
|
@fields = []
|
|
@@ -110,6 +114,7 @@ module Administratum
|
|
|
110
114
|
@filters = nil
|
|
111
115
|
@custom_actions = {}
|
|
112
116
|
@buttons = []
|
|
117
|
+
@asides = []
|
|
113
118
|
@show_attributes = []
|
|
114
119
|
@index_scopes = []
|
|
115
120
|
@enabled_actions = BUILT_IN_ACTIONS.dup
|
|
@@ -124,6 +129,7 @@ module Administratum
|
|
|
124
129
|
@filters = source.declared_filters && rebind(source.declared_filters)
|
|
125
130
|
@custom_actions = source.custom_actions.dup
|
|
126
131
|
@buttons = source.buttons.map(&:dup)
|
|
132
|
+
@asides = source.asides.map { |aside| aside.dup.tap { |copy| copy.buttons = aside.buttons.map(&:dup) } }
|
|
127
133
|
@show_attributes = rebind(source.show_attributes)
|
|
128
134
|
@index_scopes = source.index_scopes.dup
|
|
129
135
|
@enabled_actions = source.enabled_actions.dup
|
|
@@ -206,6 +212,7 @@ module Administratum
|
|
|
206
212
|
names = filters.map { |filter| filter.name.to_s }
|
|
207
213
|
names += index_columns.select(&:sortable).map { |column| column.name.to_s }
|
|
208
214
|
names << title.to_s if title
|
|
215
|
+
names << reorder_column.to_s if reorder_column
|
|
209
216
|
names += Array(default_sort).map { |sort| sort.to_s.split.first }
|
|
210
217
|
names.uniq & model.column_names
|
|
211
218
|
end
|
|
@@ -239,16 +246,31 @@ module Administratum
|
|
|
239
246
|
enabled_actions.include?(name.to_sym)
|
|
240
247
|
end
|
|
241
248
|
|
|
242
|
-
def add_button(name, on:,
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
249
|
+
def add_button(name, on:, **)
|
|
250
|
+
buttons << build_button(name, on:, **)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def build_button(name, on:, label: nil, action: nil, to: nil, method: nil, confirm: nil, condition: nil,
|
|
254
|
+
style: :secondary, panel: nil, upload: false)
|
|
255
|
+
Button.new(name: name.to_sym, label:, pages: Array(on).map(&:to_sym), action: (action || name).to_sym, to:,
|
|
256
|
+
http_method: method&.to_sym, confirm:, condition:, style: style.to_sym, panel:, upload:)
|
|
246
257
|
end
|
|
247
258
|
|
|
248
259
|
def buttons_for(page)
|
|
249
260
|
buttons.select { |button| button.pages.include?(page.to_sym) }
|
|
250
261
|
end
|
|
251
262
|
|
|
263
|
+
def add_aside(on:, title: nil)
|
|
264
|
+
pages = Array(on).map(&:to_sym)
|
|
265
|
+
raise Administratum::Error, "An aside can't go on :row" if pages.include?(:row)
|
|
266
|
+
|
|
267
|
+
Aside.new(pages:, title:, buttons: []).tap { |aside| asides << aside }
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def asides_for(page)
|
|
271
|
+
asides.select { |aside| aside.pages.include?(page.to_sym) }
|
|
272
|
+
end
|
|
273
|
+
|
|
252
274
|
def add_index_scope(name, label: nil, default: false, count: true, &block)
|
|
253
275
|
index_scopes.reject! { |index_scope| index_scope.name == name.to_sym }
|
|
254
276
|
index_scopes << IndexScope.new(name: name.to_sym, label:, block:, default:, count:)
|
data/lib/administratum/dsl.rb
CHANGED
|
@@ -35,6 +35,21 @@ module Administratum
|
|
|
35
35
|
|
|
36
36
|
include Common
|
|
37
37
|
|
|
38
|
+
# Builds `aside do ... end` button lists. Buttons take the same options as
|
|
39
|
+
# `button`, except `on:`, which comes from the aside.
|
|
40
|
+
class AsideBuilder
|
|
41
|
+
def initialize(definition, aside)
|
|
42
|
+
@definition = definition
|
|
43
|
+
@aside = aside
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def button(name, label: nil, action: nil, to: nil, method: nil, confirm: nil, if: nil, style: :secondary, panel: nil,
|
|
47
|
+
upload: false)
|
|
48
|
+
@aside.buttons << @definition.build_button(name, on: @aside.pages, label:, action:, to:, method:, confirm:,
|
|
49
|
+
condition: binding.local_variable_get(:if), style:, panel:, upload:)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
38
53
|
# Builds `form do ... end` field lists.
|
|
39
54
|
class FormBuilder
|
|
40
55
|
def initialize(definition)
|
|
@@ -124,6 +139,12 @@ module Administratum
|
|
|
124
139
|
ShowBuilder.new(definition).instance_eval(&)
|
|
125
140
|
end
|
|
126
141
|
|
|
142
|
+
# A menu of buttons beside the content of :index, :show, :new or :edit,
|
|
143
|
+
# for actions too minor for the page header. `title:` defaults to "Actions".
|
|
144
|
+
def aside(on:, title: nil, &)
|
|
145
|
+
AsideBuilder.new(definition, definition.add_aside(on:, title:)).instance_eval(&)
|
|
146
|
+
end
|
|
147
|
+
|
|
127
148
|
# Strong-params style list of writable attributes. Defaults to the form fields.
|
|
128
149
|
def permit(*attributes)
|
|
129
150
|
definition.permitted = attributes
|
|
@@ -152,6 +173,15 @@ module Administratum
|
|
|
152
173
|
definition.enable_actions(only:, except:)
|
|
153
174
|
end
|
|
154
175
|
|
|
176
|
+
# Records are put in order by an integer column. Adds a PATCH `reorder`
|
|
177
|
+
# collection action taking positions[id]=n, sorts the index by the column,
|
|
178
|
+
# and shows reorder controls there while every record is on the page.
|
|
179
|
+
def reorderable(column = :position)
|
|
180
|
+
definition.reorder_column = column.to_sym
|
|
181
|
+
definition.default_sort ||= "#{column} asc"
|
|
182
|
+
collection_action :reorder, method: :patch
|
|
183
|
+
end
|
|
184
|
+
|
|
155
185
|
def member_action(name, method: :get)
|
|
156
186
|
definition.add_action(name, http_method: method, scope: :member)
|
|
157
187
|
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Administratum
|
|
4
|
+
# Puts records in order from a positions[id]=n payload.
|
|
5
|
+
module Reorder
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Only records in `relation` move. Each submitted record is put at its
|
|
9
|
+
# number among the others (ties keep the current order); records left out
|
|
10
|
+
# keep their order around them. Positions are then renumbered from 1, and
|
|
11
|
+
# changed records saved with update!, so validations and callbacks run.
|
|
12
|
+
# Returns the records in their new order.
|
|
13
|
+
def call(relation, positions, column: :position)
|
|
14
|
+
positions = positions.to_unsafe_h if positions.respond_to?(:to_unsafe_h)
|
|
15
|
+
positions = {} unless positions.is_a?(Hash)
|
|
16
|
+
wanted = positions.to_h { |id, value| [id.to_s, Integer(value.to_s, exception: false)] }.compact
|
|
17
|
+
|
|
18
|
+
moved, sorted = relation.reorder(column, relation.klass.primary_key).to_a.partition { |record| wanted.key?(record.id.to_s) }
|
|
19
|
+
index = -1
|
|
20
|
+
moved.sort_by.with_index { |record, current| [wanted[record.id.to_s], current] }.each do |record|
|
|
21
|
+
index = (wanted[record.id.to_s] - 1).clamp(index + 1, sorted.size)
|
|
22
|
+
sorted.insert(index, record)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
relation.klass.transaction do
|
|
26
|
+
sorted.each.with_index(1) do |record, position|
|
|
27
|
+
record.update!(column => position) unless record.public_send(column) == position
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
sorted
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/lib/administratum.rb
CHANGED
|
@@ -12,6 +12,7 @@ require "administratum/definition"
|
|
|
12
12
|
require "administratum/dsl"
|
|
13
13
|
require "administratum/page_dsl"
|
|
14
14
|
require "administratum/paths"
|
|
15
|
+
require "administratum/reorder"
|
|
15
16
|
require "administratum/registry"
|
|
16
17
|
require "administratum/ransackable"
|
|
17
18
|
require "administratum/routing"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: administratum
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- buffpojken
|
|
@@ -114,6 +114,7 @@ files:
|
|
|
114
114
|
- app/helpers/administratum/form_helper.rb
|
|
115
115
|
- app/helpers/administratum/island_helper.rb
|
|
116
116
|
- app/helpers/administratum/layout_helper.rb
|
|
117
|
+
- app/helpers/administratum/reorder_helper.rb
|
|
117
118
|
- app/helpers/administratum/resource_helper.rb
|
|
118
119
|
- app/mailers/administratum/application_mailer.rb
|
|
119
120
|
- app/mailers/administratum/user_mailer.rb
|
|
@@ -172,6 +173,7 @@ files:
|
|
|
172
173
|
- lib/administratum/paths.rb
|
|
173
174
|
- lib/administratum/ransackable.rb
|
|
174
175
|
- lib/administratum/registry.rb
|
|
176
|
+
- lib/administratum/reorder.rb
|
|
175
177
|
- lib/administratum/routing.rb
|
|
176
178
|
- lib/administratum/tailwind.rb
|
|
177
179
|
- lib/administratum/version.rb
|