layered-resource-rails 0.3.0 → 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/.claude/skills/layered-resource-rails/SKILL.md +3 -2
- data/CHANGELOG.md +13 -0
- data/README.md +28 -8
- data/app/controllers/layered/resource/controller.rb +6 -6
- data/app/controllers/layered/resource/internal/breadcrumbs.rb +10 -7
- data/app/controllers/layered/resource/internal/columns.rb +8 -4
- data/app/controllers/layered/resource/internal/routing.rb +1 -1
- data/app/helpers/layered/resource/filters_helper.rb +11 -3
- data/lib/layered/resource/base.rb +34 -0
- data/lib/layered/resource/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 403ad781bad901c2882718ad739de8346faa81dcce400046aa2092de5c235ab3
|
|
4
|
+
data.tar.gz: a78bb873e92d3b814c87a1d989febe815baf67fdb8b74bab8c7193a53d29a7f7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 16d28757cc081b902fad287938d6668893b12cc17e9e39d6c0419ef79ea6908ad796236796725dadfc3c14e3540fad66bd85e579f7c0b6308d361cc654131524
|
|
7
|
+
data.tar.gz: 59cbe7172af51a80c7107792e9109d5fb4b41a2934933462ee39543b2e7e6807daa012cbdc6d3d82a220681ab913b27c6652fdabaaf51555fdfc4e5920ed3dd8
|
|
@@ -108,6 +108,7 @@ end
|
|
|
108
108
|
| `search_placeholder "..."` | Replaces the search box placeholder. Default derives from `search_fields` via `human_attribute_name`, so `activerecord.attributes.<model>.<attr>` i18n renames flow through (association walks resolve each half against its own model) |
|
|
109
109
|
| `filters :a, :b, c: {...}` | Structured filter controls on the index — an "Add filter" popover plus removable tags. Control + Ransack predicate inferred per column; trailing hash overrides per attribute. See [Filters](#filters) |
|
|
110
110
|
| `label_attribute :title` | Attribute a record is labelled by (page titles, row action menus, another resource's picker). Defaults to the `primary:` column, else the first. Falls back through `name`/`title`/`label`/`email`, then the model's own `to_s`, then `"Post #12"` |
|
|
111
|
+
| `lookup_attribute :uid` | Attribute a record is looked up by from its URL `:id` (`find_by!(uid:)` within `scope`, so out of scope is still a 404), and that the gem's links put there - primary column, row actions, `link:` columns, parent breadcrumbs. Defaults to `:id` (`scope.find` + `to_param`, so FriendlyId still works). Override `self.find_record(controller, id)` / `self.record_param(record)` for anything else |
|
|
111
112
|
| `default_sort attribute:, direction:` | Default sort order for the index |
|
|
112
113
|
| `per_page n` | Pagination size (default 15) |
|
|
113
114
|
| `root_breadcrumb "Home", "/"` | Static first crumb in the breadcrumb trail (e.g. back to the host app's dashboard). Without it, top-level resources render no trail; nested routes prepend it to the derived parent trail |
|
|
@@ -117,7 +118,7 @@ The trail always ends with the current page (collection name on `index`, record
|
|
|
117
118
|
### Column options
|
|
118
119
|
|
|
119
120
|
- `as: :type` - pins the cell to a column partial instead of the type-inferred default. Built in: `:text`, `:datetime`, `:badge`, `:boolean`; an unrecognised type raises `ArgumentError` at render time rather than rendering an empty cell. See [Column rendering](#column-rendering)
|
|
120
|
-
- `primary: true` - marks the cell that links to the record's edit (or show) page (defaults to first column)
|
|
121
|
+
- `primary: true` - marks the cell that links to the record's edit (or show) page (defaults to first column). Add `link: false` to leave it unlinked - for a `render:` proc that links somewhere else itself (e.g. a conversation's own page rather than its edit form)
|
|
121
122
|
- `label: "Custom"` - overrides the humanised attribute name
|
|
122
123
|
- `sortable:` - whether the header renders a sort link. **Defaults to `false` for any attribute that isn't a real DB column** (virtual attributes, delegated association values), because a Ransack sort link on one 500s when clicked. Pass `sortable: true` to opt back in - you then own the associated model's `ransackable_attributes` (see [Associations](#associations))
|
|
123
124
|
- `link: :route_key` - wraps the column's rendered value in a link to a nested route (e.g. `:users_posts`); composes with `as:` (pair with `as: :badge` for a badge link)
|
|
@@ -177,7 +178,7 @@ Inference by column type: `enum`/`belongs_to` -> multi-select (`_in`; `multiple:
|
|
|
177
178
|
|
|
178
179
|
**Long option lists switch control.** A select-type filter renders as the plain list (multi-select: checkboxes + Apply; single-choice: instant-apply links) up to `Layered::Resource.filter_combobox_threshold` options (10), and as a type-ahead **combobox** past it — a checkbox list of every user is no way to pick one. A single-choice combobox posts a scalar (no `[]`), so `_eq` gets the value rather than an array. The count is taken per request, after a `collection:` callable resolves, so the control follows the data; the decision lives in `layered_filter_control` (the view helper), not in `resolved_filters`, for exactly that reason. Declaring `as:` pins the control and opts out: `as: :select` keeps the list however long, `as: :combobox` uses the combobox however short.
|
|
179
180
|
|
|
180
|
-
**Remote options.** `url:` fetches the options from an endpoint as the user types instead of rendering a collection — such a filter is always a combobox (nothing to render or count). Give it as a callable (`-> { user_options_path }`) so it resolves per request in the view, where route helpers exist. The endpoint is an ordinary host-app action including `Layered::Ui::ComboboxOptions` and returning `l_ui_combobox_options(scope, label:, search:)` — the gem never routes it, so it's authorised however any index is. Since the browser has no collection to look a label up in, an active remote filter's values are labelled server-side from the records (same `name`/`title`/`label`/`email` fallbacks), so the tag reads "User: Alice" not "User: 12"; a `url:` over a plain column has no records and its values label themselves. `min_chars:` and `text:` pass through too; the write-side options (`create:`, `create_name:`, `reorder:`) deliberately don't — a filter picks among values that already exist.
|
|
181
|
+
**Remote options.** `url:` fetches the options from an endpoint as the user types instead of rendering a collection — such a filter is always a combobox (nothing to render or count). Give it as a callable (`-> { user_options_path }`) so it resolves per request in the view, where route helpers exist. The endpoint is an ordinary host-app action including `Layered::Ui::ComboboxOptions` and returning `l_ui_combobox_options(scope, label:, search:)` — the gem never routes it, so it's authorised however any index is. Since the browser has no collection to look a label up in, an active remote filter's values are labelled server-side from the records (same `name`/`title`/`label`/`email` fallbacks), so the tag reads "User: Alice" not "User: 12"; a `url:` over a plain column has no records and its values label themselves. The label lookup is confined to records the resource's `scope(controller)` references (via the foreign key), so a hand-edited id outside it - e.g. another tenant's user - labels itself instead of leaking a name; the cost is that an in-scope option with no matching records also shows its raw id. `min_chars:` and `text:` pass through too; the write-side options (`create:`, `create_name:`, `reorder:`) deliberately don't — a filter picks among values that already exist.
|
|
181
182
|
|
|
182
183
|
**The blank a combobox posts.** `l_ui_combobox` emits a leading blank hidden input so clearing every token submits an empty collection rather than omitting the key. Ransack prunes blanks from an `_in` array itself, and `layered_filter_query_params` prunes them too — an array left with nothing becomes an explicit `""`, the same shape the Clear link writes, so the tag reads as inactive *and* a `default:` doesn't immediately re-apply.
|
|
183
184
|
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. This project follows [Semantic Versioning](https://semver.org/).
|
|
4
4
|
|
|
5
|
+
## [0.4.0] - 2026-10-04
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- `lookup_attribute :uid` on a resource looks its records up by an attribute other than `id` - an unguessable token, a slug - with `find_by!` inside `scope`, so a record outside it is still a 404. The gem's own links (the primary column, row actions, `link:` columns into a nested resource, and parent breadcrumbs) put the same attribute in the URL. The default stays `:id`, with `scope.find` and `to_param` as before. `find_record(controller, id)` and `record_param(record)` are the override points behind it
|
|
10
|
+
- `link: false` on a column leaves the primary column unlinked, for a `render:` proc that links somewhere other than the edit form itself. Before, the gem wrapped such a proc's link in its own
|
|
11
|
+
|
|
12
|
+
## [0.3.1] - 2026-10-04
|
|
13
|
+
|
|
14
|
+
### Security
|
|
15
|
+
|
|
16
|
+
- A remote (`url:`) `belongs_to` filter no longer labels records outside the resource's `scope`. Its current values come from the query string, and the labels for the tag and combobox tokens were looked up with an unscoped `klass.where(id: ...)`, so hand-editing the id (`?q[user_id_in][]=`) revealed the name or e-mail of any record, including another tenant's. The lookup now only reads records the resource's `scope(controller)` references through the foreign key. Any other value shows its raw id instead.
|
|
17
|
+
|
|
5
18
|
## [0.3.0] - 2026-09-21
|
|
6
19
|
|
|
7
20
|
### Changed
|
data/README.md
CHANGED
|
@@ -205,6 +205,18 @@ end
|
|
|
205
205
|
|
|
206
206
|
When the attribute has no value, the label falls back to the first of `name`/`title`/`label`/`email` that does, then to the model's own `to_s` if it defines one, and finally to `"Post #12"` — never a bare `#<Post:0x...>`.
|
|
207
207
|
|
|
208
|
+
**Lookup attribute:** records are looked up by the `:id` in their URL, the Rails way: member links use `to_param` and the lookup is `scope.find`. Declare `lookup_attribute` when records are addressed by something else - an unguessable token, say, so ids can't be enumerated:
|
|
209
|
+
|
|
210
|
+
```ruby
|
|
211
|
+
class ConversationResource < Layered::Resource::Base
|
|
212
|
+
model Conversation
|
|
213
|
+
|
|
214
|
+
lookup_attribute :uid # /conversations/Xk3…/edit, found with find_by!(uid:)
|
|
215
|
+
end
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Every member action (`show`, `edit`, `update`, `destroy`, and custom `member` routes) finds the record by it within `scope`, so one outside the scope is still a 404. The gem's own links - the primary column, the row actions, `link:` columns into a nested resource, and parent breadcrumbs - put the same attribute in the URL. For a lookup the attribute can't express, override `self.find_record(controller, id)` (and `self.record_param(record)`, which produces the URL value).
|
|
219
|
+
|
|
208
220
|
**Custom scope (e.g. tenant isolation):**
|
|
209
221
|
|
|
210
222
|
```ruby
|
|
@@ -564,7 +576,7 @@ end
|
|
|
564
576
|
|
|
565
577
|
A remote combobox has no collection in the browser to look a label up in, so an active filter's current values are labelled server-side from the records themselves (the same `name`/`title`/`label`/`email` fallbacks) — the tag reads "User: Alice", not "User: 12". A `url:` on a plain column has no records to read, so its values label themselves.
|
|
566
578
|
|
|
567
|
-
|
|
579
|
+
Current values arrive in the query string, so they can be hand-edited to any id — and the gem can't see whatever scoping the `url:` endpoint applies. Labels are therefore only read for records the resource's own `scope` references (users with a post the viewer can see, say); any other value labels itself. A guessed `?q[user_id_in][]=` for another tenant's user shows "User: 42", not their name. The flip side is that a value from the endpoint with no matching records in scope also shows its id, next to an empty table. (A `belongs_to` filter *without* `url:` renders `klass.all` as its options — pass a scoped `collection:` if those labels are private.)
|
|
568
580
|
|
|
569
581
|
`min_chars:` and `text:` pass through to `l_ui_combobox` alongside `url:`. The write-side combobox options (`create:`, `create_name:`, `reorder:`) don't: a filter picks among values that already exist.
|
|
570
582
|
|
|
@@ -639,6 +651,15 @@ column a `render:` proc that produces the value.
|
|
|
639
651
|
|
|
640
652
|
A column with a `render:` proc is exempt - the proc decides what to call, and `attribute:` is then just the header and sort key.
|
|
641
653
|
|
|
654
|
+
The primary column links to the record's edit page (or its show page when the resource isn't editable). Pass `link: false` to leave it unlinked - when its `render:` proc links somewhere more useful itself, say:
|
|
655
|
+
|
|
656
|
+
```ruby
|
|
657
|
+
columns [
|
|
658
|
+
{ attribute: :name, primary: true, link: false,
|
|
659
|
+
render: ->(record, view) { view.link_to(record.name, view.conversation_path(record)) } }
|
|
660
|
+
]
|
|
661
|
+
```
|
|
662
|
+
|
|
642
663
|
### Sortable headers
|
|
643
664
|
|
|
644
665
|
A column header renders a sort link only when the attribute is sortable. This defaults to `true` for real DB columns and `false` for anything else - virtual attributes and delegated association values - because Ransack can't sort those without the associated model allowlisting the underlying field, and the sort link would 500 when clicked. Set `sortable: true` on the column to opt back in; you're then responsible for that model's `ransackable_attributes` (see [Associations](#associations)).
|
|
@@ -917,17 +938,16 @@ cd test/dummy && bin/rails db:setup && bin/dev
|
|
|
917
938
|
|
|
918
939
|
### Deploying the dummy app
|
|
919
940
|
|
|
920
|
-
The dummy app can be deployed with [Kamal](https://kamal-deploy.org).
|
|
941
|
+
The dummy app can be deployed with [Kamal](https://kamal-deploy.org). Copy `.env.deploy.example` to `.env.deploy` (gitignored), fill in `KAMAL_DEPLOY_IP`, `KAMAL_DEPLOY_DOMAIN`, and `KAMAL_SSH_KEY`, then run `bin/deploy`:
|
|
921
942
|
|
|
922
943
|
```bash
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
kamal deploy
|
|
944
|
+
cp .env.deploy.example .env.deploy
|
|
945
|
+
bin/deploy setup # first run on a new server
|
|
946
|
+
bin/deploy # kamal deploy
|
|
947
|
+
bin/deploy logs # any other kamal subcommand passes straight through
|
|
928
948
|
```
|
|
929
949
|
|
|
930
|
-
`
|
|
950
|
+
`SECRET_KEY_BASE` is read from `test/dummy/.kamal/secrets`, which is gitignored - create it locally before the first deploy.
|
|
931
951
|
|
|
932
952
|
## Contributing
|
|
933
953
|
|
|
@@ -63,7 +63,7 @@ module Layered
|
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def show
|
|
66
|
-
@record = @resource.
|
|
66
|
+
@record = @resource.find_record(self, params[:id])
|
|
67
67
|
authorize_layered_record(@record)
|
|
68
68
|
@page_title = layered_record_label(@record)
|
|
69
69
|
end
|
|
@@ -89,14 +89,14 @@ module Layered
|
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def edit
|
|
92
|
-
@record = @resource.
|
|
92
|
+
@record = @resource.find_record(self, params[:id])
|
|
93
93
|
authorize_layered_record(@record)
|
|
94
94
|
@form_url = layered_member_path(@record)
|
|
95
95
|
@page_title = "Edit #{layered_record_label(@record)}"
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
def update
|
|
99
|
-
@record = @resource.
|
|
99
|
+
@record = @resource.find_record(self, params[:id])
|
|
100
100
|
authorize_layered_record(@record)
|
|
101
101
|
if @record.update(layered_resource_params)
|
|
102
102
|
redirect_to @resource.after_save_path(self, @record),
|
|
@@ -109,7 +109,7 @@ module Layered
|
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
def destroy
|
|
112
|
-
@record = @resource.
|
|
112
|
+
@record = @resource.find_record(self, params[:id])
|
|
113
113
|
authorize_layered_record(@record)
|
|
114
114
|
redirect_path = @resource.after_save_path(self, @record)
|
|
115
115
|
model_name = @resource.model.model_name.human
|
|
@@ -217,7 +217,7 @@ module Layered
|
|
|
217
217
|
|
|
218
218
|
# For custom member actions declared in a `layered_resources` block,
|
|
219
219
|
# populate @record from params[:id] so action bodies don't have to
|
|
220
|
-
# repeat `@resource.
|
|
220
|
+
# repeat `@resource.find_record(self, params[:id])`. Skip this with
|
|
221
221
|
# `skip_before_action :load_layered_member_record, only: [:foo]` if
|
|
222
222
|
# the action doesn't need the record (or shouldn't 404 on a missing
|
|
223
223
|
# one).
|
|
@@ -228,7 +228,7 @@ module Layered
|
|
|
228
228
|
member_actions = @_route_entry[:member_actions] || []
|
|
229
229
|
return unless member_actions.include?(action_name.to_sym)
|
|
230
230
|
|
|
231
|
-
@record = @resource.
|
|
231
|
+
@record = @resource.find_record(self, params[:id])
|
|
232
232
|
authorize_layered_record(@record)
|
|
233
233
|
end
|
|
234
234
|
|
|
@@ -35,9 +35,10 @@ module Layered
|
|
|
35
35
|
# parent that lives in the same namespace.
|
|
36
36
|
collection_key = parent_collection_keys[key]
|
|
37
37
|
collection_entry = collection_key && Layered::Resource::Routing.lookup(collection_key)
|
|
38
|
+
parent_resource = collection_entry && collection_entry[:resource].constantize
|
|
38
39
|
model_class =
|
|
39
|
-
if
|
|
40
|
-
|
|
40
|
+
if parent_resource
|
|
41
|
+
parent_resource.model
|
|
41
42
|
elsif current_namespace
|
|
42
43
|
"#{current_namespace}::#{model_name.classify}".safe_constantize ||
|
|
43
44
|
model_name.classify.safe_constantize
|
|
@@ -67,11 +68,13 @@ module Layered
|
|
|
67
68
|
|
|
68
69
|
# Add the specific record breadcrumb, linked to its own show
|
|
69
70
|
# page when the parent resource has one - a crumb that isn't
|
|
70
|
-
# the current page shouldn't be a dead end.
|
|
71
|
-
|
|
71
|
+
# the current page shouldn't be a dead end. The param holds
|
|
72
|
+
# whatever the parent resource addresses its records by.
|
|
73
|
+
lookup_attribute = parent_resource ? parent_resource.lookup_attribute : :id
|
|
74
|
+
record = model_class.find_by(lookup_attribute => params[key])
|
|
72
75
|
if record
|
|
73
76
|
label = record.try(:name) || record.try(:title) || "#{model_class.model_name.human} ##{record.id}"
|
|
74
|
-
crumbs << { label: label, path: layered_parent_record_path(collection_entry, collection_key, ancestor_args, record) }
|
|
77
|
+
crumbs << { label: label, path: layered_parent_record_path(collection_entry, parent_resource, collection_key, ancestor_args, record) }
|
|
75
78
|
end
|
|
76
79
|
|
|
77
80
|
crumbs
|
|
@@ -82,7 +85,7 @@ module Layered
|
|
|
82
85
|
# The parent record's show path, or nil when the parent isn't a
|
|
83
86
|
# layered resource, doesn't route :show, or can't be addressed
|
|
84
87
|
# from here.
|
|
85
|
-
def layered_parent_record_path(collection_entry, collection_key, ancestor_args, record)
|
|
88
|
+
def layered_parent_record_path(collection_entry, parent_resource, collection_key, ancestor_args, record)
|
|
86
89
|
return nil unless collection_entry && ancestor_args
|
|
87
90
|
return nil unless collection_entry[:actions].include?(:show)
|
|
88
91
|
|
|
@@ -90,7 +93,7 @@ module Layered
|
|
|
90
93
|
helper = :"#{collection_key.to_s.singularize}_path"
|
|
91
94
|
return nil unless rs.url_helpers.method_defined?(helper)
|
|
92
95
|
|
|
93
|
-
rs.url_helpers.send(helper, default_url_options.merge(ancestor_args).merge(id: record
|
|
96
|
+
rs.url_helpers.send(helper, default_url_options.merge(ancestor_args).merge(id: parent_resource.record_param(record)))
|
|
94
97
|
end
|
|
95
98
|
end
|
|
96
99
|
end
|
|
@@ -154,7 +154,8 @@ module Layered
|
|
|
154
154
|
# and only falls back to show for read-only resources that still
|
|
155
155
|
# expose a detail page. The "primary" column is the one marked
|
|
156
156
|
# primary: true (or the first column if none is). Columns that already
|
|
157
|
-
# declare a custom link: are left alone
|
|
157
|
+
# declare a custom link: are left alone, and `link: false` leaves the
|
|
158
|
+
# column unlinked - for a `render:` proc that links somewhere else.
|
|
158
159
|
def apply_primary_column_link
|
|
159
160
|
singular = @layered_route_key.singularize
|
|
160
161
|
helper = if @resource_can_edit
|
|
@@ -172,13 +173,13 @@ module Layered
|
|
|
172
173
|
|
|
173
174
|
@columns = @columns.each_with_index.map do |col, i|
|
|
174
175
|
next col unless i == primary_index
|
|
175
|
-
next col if col
|
|
176
|
+
next col if col.key?(:link)
|
|
176
177
|
|
|
177
178
|
inner_render = col[:render]
|
|
178
179
|
col.merge(
|
|
179
180
|
render: ->(record) {
|
|
180
181
|
value = inner_render.call(record)
|
|
181
|
-
view.link_to value, routes_proxy.send(helper, record), data: { turbo_frame: "_top" }
|
|
182
|
+
view.link_to value, routes_proxy.send(helper, @resource.record_param(record)), data: { turbo_frame: "_top" }
|
|
182
183
|
}
|
|
183
184
|
)
|
|
184
185
|
end
|
|
@@ -224,7 +225,10 @@ module Layered
|
|
|
224
225
|
value = inner_render.call(record)
|
|
225
226
|
args = opts.dup
|
|
226
227
|
ancestor_params.each { |p| args[p] = record.public_send(p) }
|
|
227
|
-
|
|
228
|
+
# The parent is this resource's record, so it goes in the URL
|
|
229
|
+
# the way this resource's own links put it - its id unless
|
|
230
|
+
# the resource declares a `lookup_attribute`.
|
|
231
|
+
args[immediate_parent] = @resource.lookup_attribute == :id ? record.id : @resource.record_param(record)
|
|
228
232
|
path = rs.url_helpers.send(path_helper, args)
|
|
229
233
|
view.link_to value, path, data: { turbo_frame: "_top" }
|
|
230
234
|
}
|
|
@@ -52,7 +52,7 @@ module Layered
|
|
|
52
52
|
"No #{action || 'member'} route registered for #{@layered_route_key}. " \
|
|
53
53
|
"Include the matching action in only:."
|
|
54
54
|
end
|
|
55
|
-
layered_routes.send(helper, record)
|
|
55
|
+
layered_routes.send(helper, @resource.record_param(record))
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
end
|
|
@@ -286,15 +286,23 @@ module Layered
|
|
|
286
286
|
|
|
287
287
|
# Value => label for the filter's currently-set values. A remote filter
|
|
288
288
|
# has no collection to read them from, so an association's are looked up
|
|
289
|
-
# by
|
|
289
|
+
# by key; anything else (a remote filter over a plain column) resolves to
|
|
290
290
|
# nothing and the value stands in for its own label.
|
|
291
|
+
#
|
|
292
|
+
# The values come straight from the query string, and the gem can't see
|
|
293
|
+
# whatever scoping the `url:` endpoint applies, so the lookup is confined
|
|
294
|
+
# to records the resource's own `scope` references: a hand-edited id for,
|
|
295
|
+
# say, another tenant's user labels itself rather than disclosing that
|
|
296
|
+
# user's name.
|
|
291
297
|
def layered_filter_option_labels(filter, values)
|
|
292
298
|
if filter[:url].present?
|
|
293
299
|
reflection = filter[:reflection]
|
|
294
300
|
return {} if reflection.nil?
|
|
295
301
|
|
|
296
|
-
reflection.
|
|
297
|
-
|
|
302
|
+
primary_key = reflection.association_primary_key
|
|
303
|
+
referenced = @resource.scope(controller).unscope(:order).select(reflection.foreign_key)
|
|
304
|
+
reflection.klass.where(primary_key => values).where(primary_key => referenced)
|
|
305
|
+
.to_h { |record| [record.public_send(primary_key).to_s, layered_filter_record_label(record)] }
|
|
298
306
|
else
|
|
299
307
|
layered_filter_collection(filter).to_h { |label, value| [value, label] }
|
|
300
308
|
end
|
|
@@ -126,6 +126,40 @@ module Layered
|
|
|
126
126
|
Layered::Resource.record_label(record, attribute: label_attribute)
|
|
127
127
|
end
|
|
128
128
|
|
|
129
|
+
# The attribute a record is looked up by from the `:id` in its URL,
|
|
130
|
+
# and that the gem's own member links put there. Defaults to `:id`,
|
|
131
|
+
# which keeps Rails' own behaviour: links use `to_param` and the
|
|
132
|
+
# lookup is `scope.find`, so a gem that overrides both (FriendlyId,
|
|
133
|
+
# say) still works. Declare it when records are addressed by
|
|
134
|
+
# something else - an unguessable token, a slug:
|
|
135
|
+
#
|
|
136
|
+
# lookup_attribute :uid
|
|
137
|
+
def lookup_attribute(value = nil)
|
|
138
|
+
if value
|
|
139
|
+
@lookup_attribute = value.to_sym
|
|
140
|
+
else
|
|
141
|
+
inherited_attribute(:@lookup_attribute) || :id
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The record named by `id`, within `scope` - raising
|
|
146
|
+
# ActiveRecord::RecordNotFound (a 404) when it is outside it. Every
|
|
147
|
+
# member action looks its record up through here, so override it for
|
|
148
|
+
# a lookup `lookup_attribute` can't express.
|
|
149
|
+
def find_record(controller, id)
|
|
150
|
+
relation = scope(controller)
|
|
151
|
+
return relation.find(id) if lookup_attribute == :id
|
|
152
|
+
|
|
153
|
+
relation.find_by!(lookup_attribute => id)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# What a member URL carries for `record`: the inverse of `find_record`.
|
|
157
|
+
def record_param(record)
|
|
158
|
+
return record.to_param if lookup_attribute == :id
|
|
159
|
+
|
|
160
|
+
record.public_send(lookup_attribute).to_s
|
|
161
|
+
end
|
|
162
|
+
|
|
129
163
|
# Declares structured filter controls for the index table. Each entry
|
|
130
164
|
# is either a bare attribute (control + Ransack predicate inferred from
|
|
131
165
|
# the column type, enum, or association) or an attribute with an
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: layered-resource-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- layered.ai
|
|
@@ -288,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
288
288
|
- !ruby/object:Gem::Version
|
|
289
289
|
version: '0'
|
|
290
290
|
requirements: []
|
|
291
|
-
rubygems_version:
|
|
291
|
+
rubygems_version: 3.6.9
|
|
292
292
|
specification_version: 4
|
|
293
293
|
summary: Convention-over-configuration CRUD for Rails 8+ with Ransack and Pagy built
|
|
294
294
|
in.
|