layered-resource-rails 0.3.1 → 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 +2 -1
- data/CHANGELOG.md +7 -0
- data/README.md +27 -7
- 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/lib/layered/resource/base.rb +34 -0
- data/lib/layered/resource/version.rb +1 -1
- metadata +1 -1
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)
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
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
|
+
|
|
5
12
|
## [0.3.1] - 2026-10-04
|
|
6
13
|
|
|
7
14
|
### Security
|
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
|
|
@@ -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
|
|
@@ -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
|