crumbly 2.0.0 → 2.1.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/CHANGELOG.md +16 -1
- data/README.md +203 -291
- data/lib/crumbly/converter/crumb_definition.rb +4 -4
- data/lib/crumbly/converter/source.rb +10 -3
- data/lib/crumbly/converter.rb +11 -3
- data/lib/crumbly/derived_call_sites.rb +2 -2
- data/lib/crumbly/errors.rb +2 -0
- data/lib/crumbly/json_ld.rb +8 -4
- data/lib/crumbly/trail.rb +34 -2
- data/lib/crumbly/verifier.rb +3 -1
- data/lib/crumbly/version.rb +1 -1
- data/lib/crumbly.rb +7 -1
- metadata +10 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 79dd182434d2559579a926dc460606527bea5ab63bda1e1c00bcef7ecd3e221d
|
|
4
|
+
data.tar.gz: 265c0a56aa5ad03ee08d9d825b31432af7569dab24cd8d80974ed1a1e2fd2bb9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b465f51629faefb7bfce68edeb9c8c01cb4a5b9099ecc942add8fa9ff753f3d2bccc972c6d1b19c5ef34435121b2fca127c723c5297e10343298ed389c269920
|
|
7
|
+
data.tar.gz: 39682d63dc8464e7fc3881ec95d3834f34fa1c9a1975682fa53c016e6fe105611d92d2b4f3ce784fa2792500b46e6bf59f46453c4d2fd9a71584b34ca8b8a6b3
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [2.1.0] — 2026-11-06
|
|
6
|
+
|
|
7
|
+
Crumbs refuse `javascript:`, `vbscript:` and `data:` URLs, and a nil record, odd bytes or `=begin` comments no longer break Crumbly.
|
|
8
|
+
|
|
9
|
+
**New**
|
|
10
|
+
- `Crumbly::UnsafeUrl`, for a crumb or a `link` whose URL is `javascript:`, `vbscript:` or `data:`, in any case and with the spaces, control characters, tabs and line breaks that browsers drop. It raises in development and test; in production it goes to `Rails.error`, and the level keeps its label without the link. `Crumbly.raise_on_unsafe_url` chooses, like the other two settings.
|
|
11
|
+
|
|
12
|
+
**Fixed**
|
|
13
|
+
- loofah's floor moves to 2.25.3, and with it nokogiri's to 1.19.5: the first versions without the 2026 advisories, two of them in the HTML5 scrubber Crumbly calls on `html_safe` labels.
|
|
14
|
+
- `breadcrumb` with a nil record, or an object without a model, raises or reports `Crumbly::UnknownCrumb` instead of a `NoMethodError` that failed the page. A `parent:` that is not a crumb raises an `ArgumentError` that names it.
|
|
15
|
+
- `breadcrumb_json_ld` replaces invalid bytes in a label with U+FFFD, and reads `HTTPS://` as an absolute URL.
|
|
16
|
+
- `Crumbly.verify!` and `crumbly-convert --views` read files that are not UTF-8 instead of crashing.
|
|
17
|
+
- `crumbly-convert` writes `=begin` comments as `#` lines, and keeps a loop value interpolated in a regexp, a command, a `%W` array or `%Q/`, where it wrote Ruby that doesn't parse.
|
|
18
|
+
|
|
5
19
|
## [2.0.0] — 2026-11-05
|
|
6
20
|
|
|
7
21
|
GretelX is now Crumbly, in the levups organization: same breadcrumbs, new names.
|
|
@@ -62,7 +76,8 @@ tests. Replaces gretel: `crumbly-convert` turns its breadcrumbs files into Crumb
|
|
|
62
76
|
`breadcrumb` calls a derived trail may now cover.
|
|
63
77
|
- `Crumbly::Oracle.compare` draws trails with gretel and with Crumbly, from a test of an app migrating.
|
|
64
78
|
|
|
65
|
-
[Unreleased]: https://github.com/levups/crumbly/compare/v2.
|
|
79
|
+
[Unreleased]: https://github.com/levups/crumbly/compare/v2.1.0...HEAD
|
|
80
|
+
[2.1.0]: https://github.com/levups/crumbly/compare/v2.0.0...v2.1.0
|
|
66
81
|
[2.0.0]: https://github.com/levups/crumbly/compare/v1.1.0...v2.0.0
|
|
67
82
|
[1.1.0]: https://github.com/levups/crumbly/compare/v1.0.0...v1.1.0
|
|
68
83
|
[1.0.0]: https://github.com/levups/crumbly/releases/tag/v1.0.0
|
data/README.md
CHANGED
|
@@ -1,40 +1,53 @@
|
|
|
1
1
|
# Crumbly
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Crumbly builds breadcrumb trails for Rails apps. A breadcrumb trail is the line of links at the top of a page that
|
|
4
|
+
shows where it sits in the site:
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
```
|
|
7
|
+
Home › Customers › Alice › Orders › Order 2026-0042
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
You describe each level once, as a Ruby method that names its parent. Most pages need nothing more: when a view
|
|
11
|
+
doesn't say which trail it wants, Crumbly works it out from the controller, the action and the routes. A test
|
|
12
|
+
checks every trail in the app, so a broken one fails CI before anyone opens the page.
|
|
13
|
+
|
|
14
|
+
The guide, the migration notes and the benchmarks are also on the website: <https://levups.github.io/crumbly/>.
|
|
6
15
|
|
|
7
|
-
|
|
8
|
-
265 crumbs, 380 call sites), its shape is what hurts:
|
|
16
|
+
## Why Crumbly
|
|
9
17
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
- mistakes that only show when someone opens the page;
|
|
13
|
-
- blocks run through `instance_exec` and helpers reached through `method_missing`;
|
|
14
|
-
- every trail written by hand, even when the routes already describe it.
|
|
18
|
+
Crumbly was written to replace [gretel](https://github.com/kzkn/gretel) in a back office with 265 crumbs, in one
|
|
19
|
+
file of about 1,000 lines, and 380 calls in views. It works differently in a few ways:
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
- The crumbs of each part of the app live in their own class, `Admin::Breadcrumbs` or `Storefront::Breadcrumbs`,
|
|
22
|
+
so the admin and the storefront can both have an `orders` crumb.
|
|
23
|
+
- A crumb is a plain method. Route helpers work in it as in a view, and a backtrace points at its line.
|
|
24
|
+
- Index, show, new and edit pages get their trail from the routes and the record the controller loaded, so most
|
|
25
|
+
views name no crumb.
|
|
26
|
+
- One test reads every crumb and fails on a parent, a route helper or a crumb name that doesn't exist, without
|
|
27
|
+
touching the database.
|
|
28
|
+
- The trail comes back as data, and your layout writes the HTML. The gem has no styles or rendering options.
|
|
29
|
+
- Rendering a five-level trail as an `<ol>` takes 45.4 µs against 57.6 µs for gretel, with 274 allocations
|
|
30
|
+
instead of 345 (YJIT off). JSON-LD is a little slower: see [Performance](#performance).
|
|
17
31
|
|
|
18
|
-
|
|
19
|
-
under the same names if they like. Zeitwerk loads them, so they load before a prefork server forks.
|
|
20
|
-
- **One crumb per Ruby method.** The parent is a plain method call, and the URL helpers come from your routes.
|
|
21
|
-
There is no DSL to learn and no `method_missing`, and a backtrace points at the method.
|
|
22
|
-
- **Derived by default.** A view that names no crumb gets the trail of its action, built from the routes and
|
|
23
|
-
the record its controller loaded.
|
|
24
|
-
- **Mistakes raise early.** A crumb defined twice raises when the class loads, and an unknown crumb raises in
|
|
25
|
-
development and test. In production it is reported and the page renders without a trail.
|
|
26
|
-
- **Data, not HTML.** `breadcrumb_trail` returns levels (label, URL, extra links, `current?`), and your own
|
|
27
|
-
component renders them.
|
|
32
|
+
Its runtime dependencies are Action Pack, Active Support and Loofah, which Rails already installs.
|
|
28
33
|
|
|
29
34
|
## Installation
|
|
30
35
|
|
|
36
|
+
Add the gem to your Gemfile:
|
|
37
|
+
|
|
31
38
|
```ruby
|
|
32
39
|
gem "crumbly"
|
|
33
40
|
```
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
It needs Ruby 3.3 or later and Rails 8.0 or later. There is nothing to configure: the gem adds `breadcrumb`,
|
|
43
|
+
`breadcrumb_trail` and `breadcrumb_json_ld` to your views, and `breadcrumbs_with` to your controllers.
|
|
44
|
+
|
|
45
|
+
## Getting started
|
|
46
|
+
|
|
47
|
+
### 1. Write the crumbs
|
|
36
48
|
|
|
37
|
-
|
|
49
|
+
Crumbs live in a class that inherits from `Crumbly::Trail`. Every public method in it is a crumb. A crumb returns
|
|
50
|
+
a label and a URL, and its parent if it has one:
|
|
38
51
|
|
|
39
52
|
```ruby
|
|
40
53
|
# app/breadcrumbs/admin/breadcrumbs.rb
|
|
@@ -55,10 +68,17 @@ module Admin
|
|
|
55
68
|
end
|
|
56
69
|
```
|
|
57
70
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
71
|
+
The parent of `order` is the result of calling `orders`, so the trail of an order page is Home › Orders ›
|
|
72
|
+
Order 42. When `root` exists, every trail starts with it. Your route helpers work inside the class as they do in
|
|
73
|
+
a view. Rails loads the file like any other in `app/`, so `app/breadcrumbs/admin/breadcrumbs.rb` defines
|
|
74
|
+
`Admin::Breadcrumbs`.
|
|
75
|
+
|
|
76
|
+
### 2. Render the trail in the layout
|
|
77
|
+
|
|
78
|
+
`breadcrumb_trail` returns the levels of the current page, from the root to the page itself. Each level has a
|
|
79
|
+
`label`, a `url` and `current?`, which is true for the last one. The HTML is up to you:
|
|
61
80
|
|
|
81
|
+
```erb
|
|
62
82
|
<%# app/views/layouts/admin.html.erb %>
|
|
63
83
|
<nav aria-label="Breadcrumb">
|
|
64
84
|
<ol>
|
|
@@ -69,175 +89,114 @@ end
|
|
|
69
89
|
</nav>
|
|
70
90
|
```
|
|
71
91
|
|
|
72
|
-
|
|
73
|
-
after the record's model (`order` for an `Order`). For a single-table inheritance subclass with no crumb of
|
|
74
|
-
its own, it falls back on its parent class's.
|
|
75
|
-
- `root`, when you define it, starts every trail.
|
|
76
|
-
- `crumb label, url = nil, parent: nil, links: []`. `url` takes a string, a record or a polymorphic array
|
|
77
|
-
(`[:admin, owner, :returns]`), or nothing for a level without a link. `links:` holds extra
|
|
78
|
-
`link(label, url)`s for a level that points to more than one page.
|
|
79
|
-
- `crumb_for(record)` is the crumb of a record found by its model, which is how you write a polymorphic parent.
|
|
80
|
-
- Private methods are helpers, not crumbs. View helpers are reached through `helpers` (`helpers.t(".orders")`).
|
|
81
|
-
- Every name that works in a view is a crumb, so `Crumbly::Trail` keeps its own methods private. The ones
|
|
82
|
-
your crumbs must not be named after are `crumb`, `link`, `crumb_for` and `helpers`.
|
|
83
|
-
|
|
84
|
-
### Namespaces
|
|
85
|
-
|
|
86
|
-
`Admin::OrdersController` reads `Admin::Breadcrumbs`, falling back on enclosing modules and then on a
|
|
87
|
-
top-level `Breadcrumbs`. A controller elsewhere picks one explicitly:
|
|
92
|
+
On the page of an order whose `to_s` is "Order 42", with the admin routes at `/admin`, this writes:
|
|
88
93
|
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
```html
|
|
95
|
+
<nav aria-label="Breadcrumb">
|
|
96
|
+
<ol>
|
|
97
|
+
<li><a href="/admin">Home</a></li>
|
|
98
|
+
<li><a href="/admin/orders">Orders</a></li>
|
|
99
|
+
<li>Order 42</li>
|
|
100
|
+
</ol>
|
|
101
|
+
</nav>
|
|
93
102
|
```
|
|
94
103
|
|
|
95
|
-
###
|
|
104
|
+
### 3. Pick a crumb in a view, when you need to
|
|
96
105
|
|
|
97
|
-
|
|
106
|
+
A view can name the crumb it wants:
|
|
98
107
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
| `new`, `create` | "New" (`crumbly.new`) under the collection |
|
|
104
|
-
| `edit`, `update` | "Edit" (`crumbly.edit`) under the record |
|
|
108
|
+
```erb
|
|
109
|
+
<%# app/views/admin/orders/show.html.erb %>
|
|
110
|
+
<% breadcrumb @order %>
|
|
111
|
+
```
|
|
105
112
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
`new_record_crumb` or `edit_record_crumb`.
|
|
113
|
+
`breadcrumb @order` calls the crumb named after the record's model, `order` for an `Order`. To call a crumb by
|
|
114
|
+
name, write `breadcrumb :orders`, and pass its arguments after the name (`breadcrumb :order, @order`).
|
|
109
115
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
loaded it: your `post` crumb, or else its `to_s` linked to its `show` route, under the posts collection. A
|
|
113
|
-
parent the controller did not load is never queried: the trail skips the nesting's crumb, keeps that parent's
|
|
114
|
-
collection (Posts) and goes straight to the current level.
|
|
116
|
+
In this example the line isn't needed. A `show` page with no `breadcrumb` call already gets the trail of its
|
|
117
|
+
record (see [Trails Crumbly works out on its own](#trails-crumbly-works-out-on-its-own)).
|
|
115
118
|
|
|
116
|
-
|
|
119
|
+
## Writing crumbs
|
|
117
120
|
|
|
118
|
-
|
|
121
|
+
A crumb calls `crumb label, url = nil, parent: nil, links: []`.
|
|
119
122
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
3. The derived trail, whose parent levels follow the same order: your crumb for the parent's model, or else
|
|
125
|
-
the derived level.
|
|
123
|
+
`url` can be a string, a record or a polymorphic array such as `[:admin, owner, :returns]`. Leave it out for a
|
|
124
|
+
level without a link. A `javascript:`, `vbscript:` or `data:` URL would run a script once your layout links it,
|
|
125
|
+
so Crumbly refuses it, however it is spelled (`" JavaScript:"`, `"java\tscript:"`): see
|
|
126
|
+
[Mistakes in development and in production](#mistakes-in-development-and-in-production).
|
|
126
127
|
|
|
127
|
-
|
|
128
|
+
`links:` is for a level that points to more than one page. It takes extra `link(label, url)` entries, which your
|
|
129
|
+
layout reads from `level.links`.
|
|
128
130
|
|
|
129
|
-
|
|
131
|
+
`crumb_for(record)` finds the crumb of a record from its model. Use it when the parent can be one of several
|
|
132
|
+
models:
|
|
130
133
|
|
|
131
134
|
```ruby
|
|
132
|
-
|
|
133
|
-
crumb
|
|
134
|
-
link "Messages", admin_order_messages_path(order)
|
|
135
|
-
parent :order, order
|
|
136
|
-
end
|
|
137
|
-
```
|
|
135
|
+
def returns(owner = nil)
|
|
136
|
+
return crumb("Returns", admin_returns_path) unless owner
|
|
138
137
|
|
|
139
|
-
|
|
140
|
-
# Crumbly, app/breadcrumbs/admin/breadcrumbs.rb: optional, the nested route derives the same trail
|
|
141
|
-
def order_messages(order)
|
|
142
|
-
crumb "Messages", admin_order_messages_path(order), parent: order(order)
|
|
138
|
+
crumb "Returns", [:admin, owner, :returns], parent: crumb_for(owner)
|
|
143
139
|
end
|
|
144
140
|
```
|
|
145
141
|
|
|
146
|
-
|
|
142
|
+
For a single-table inheritance subclass that has no crumb of its own, `crumb_for` and `breadcrumb @record` use the
|
|
143
|
+
crumb of its parent class.
|
|
147
144
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
crumb :returns do |owner|
|
|
151
|
-
if owner
|
|
152
|
-
link "Returns", [:admin, owner, :returns]
|
|
153
|
-
parent owner
|
|
154
|
-
else
|
|
155
|
-
link "Returns", admin_returns_path
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
```
|
|
145
|
+
Private methods are helpers, not crumbs, so you can put shared code in them. View helpers such as `t` or
|
|
146
|
+
`current_user` are reached through `helpers`: `helpers.t(".orders")`.
|
|
159
147
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
def returns(owner = nil)
|
|
163
|
-
return crumb("Returns", admin_returns_path) unless owner
|
|
148
|
+
Any public method can be called from a view, so `Crumbly::Trail` keeps its own methods private. Don't name a crumb
|
|
149
|
+
`crumb`, `link`, `crumb_for` or `helpers`.
|
|
164
150
|
|
|
165
|
-
|
|
166
|
-
end
|
|
167
|
-
```
|
|
151
|
+
## One trail class per namespace
|
|
168
152
|
|
|
169
|
-
|
|
153
|
+
`Admin::OrdersController` uses `Admin::Breadcrumbs`. If there is none, Crumbly looks in the enclosing modules,
|
|
154
|
+
then for a top-level `Breadcrumbs`. An admin area and a storefront can each have an `orders` crumb, and they
|
|
155
|
+
don't clash.
|
|
170
156
|
|
|
171
|
-
|
|
172
|
-
# gretel
|
|
173
|
-
crumb :order do |order|
|
|
174
|
-
link "Order #{order.number}", admin_order_path(order)
|
|
175
|
-
if order.customer
|
|
176
|
-
parent :customer_orders, order.customer
|
|
177
|
-
else
|
|
178
|
-
parent :orders
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
```
|
|
157
|
+
A controller outside the namespace picks its class with `breadcrumbs_with`:
|
|
182
158
|
|
|
183
159
|
```ruby
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
crumb "Order #{order.number}", admin_order_path(order),
|
|
187
|
-
parent: order.customer ? customer_orders(order.customer) : orders
|
|
160
|
+
class CheckoutController < ApplicationController
|
|
161
|
+
breadcrumbs_with Storefront::Breadcrumbs
|
|
188
162
|
end
|
|
189
163
|
```
|
|
190
164
|
|
|
191
|
-
|
|
165
|
+
## Trails Crumbly works out on its own
|
|
192
166
|
|
|
193
|
-
|
|
167
|
+
When a view doesn't call `breadcrumb`, Crumbly builds the trail from the action:
|
|
194
168
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
end
|
|
202
|
-
```
|
|
169
|
+
| Action | Trail |
|
|
170
|
+
|---|---|
|
|
171
|
+
| `index` | the collection: your `posts` crumb if you wrote one, or else the model's plural name linked to the `index` route |
|
|
172
|
+
| `show` | the record: your `post` crumb, or else its `to_s` linked to the `show` route, under the collection |
|
|
173
|
+
| `new`, `create` | "New" under the collection |
|
|
174
|
+
| `edit`, `update` | "Edit" under the record |
|
|
203
175
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
*grandparents, parent_category = ancestors
|
|
208
|
-
crumb category.name, admin_category_path(category),
|
|
209
|
-
parent: parent_category ? category(parent_category, grandparents) : categories
|
|
210
|
-
end
|
|
211
|
-
```
|
|
176
|
+
The record is the one the controller has already loaded, `@post` in `PostsController`. Crumbly never runs a query
|
|
177
|
+
to find it. "New" and "Edit" come from the `crumbly.new` and `crumbly.edit` translations, so you can translate
|
|
178
|
+
them.
|
|
212
179
|
|
|
213
|
-
|
|
180
|
+
Nested routes work too. On `/admin/posts/5/comments`, the trail is Posts › Post 5 › Comments, and the post is
|
|
181
|
+
the `@post` the controller loaded. If the controller didn't load it, Crumbly skips that level and keeps Posts.
|
|
214
182
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
crumb :generic_form do |model|
|
|
218
|
-
if model.new_record?
|
|
219
|
-
link "New", nil
|
|
220
|
-
parent model.class.model_name.plural.to_sym
|
|
221
|
-
else
|
|
222
|
-
link "Edit", nil
|
|
223
|
-
parent model
|
|
224
|
-
end
|
|
225
|
-
end
|
|
226
|
-
```
|
|
183
|
+
To change how these trails are built, override the private methods `collection_crumb`, `record_crumb`,
|
|
184
|
+
`new_record_crumb` or `edit_record_crumb` in your trail class.
|
|
227
185
|
|
|
228
|
-
|
|
229
|
-
<%# app/views/admin/posts/edit.html.erb %>
|
|
230
|
-
<% breadcrumb :generic_form, @post %>
|
|
231
|
-
```
|
|
186
|
+
### Which crumb wins
|
|
232
187
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
188
|
+
Crumbly takes the first of these that applies:
|
|
189
|
+
|
|
190
|
+
1. The crumb the view names with `breadcrumb`.
|
|
191
|
+
2. Your crumb named after the controller: `posts` in `Admin::PostsController#index`. Under a nested route it is
|
|
192
|
+
named after the whole nesting and gets the parent records, so `/admin/orders/42/messages` calls
|
|
193
|
+
`order_messages(order)`. On `show` and `edit`, it is the crumb named after the record's model (`post`).
|
|
194
|
+
3. The trail Crumbly works out. Its parent levels follow the same rule: your crumb for the parent's model if there
|
|
195
|
+
is one, or else a level Crumbly builds.
|
|
236
196
|
|
|
237
|
-
##
|
|
197
|
+
## Testing your trails
|
|
238
198
|
|
|
239
|
-
|
|
240
|
-
or loading a record:
|
|
199
|
+
Add one test:
|
|
241
200
|
|
|
242
201
|
```ruby
|
|
243
202
|
# test/breadcrumbs_test.rb
|
|
@@ -251,24 +210,51 @@ class BreadcrumbsTest < ActiveSupport::TestCase
|
|
|
251
210
|
end
|
|
252
211
|
```
|
|
253
212
|
|
|
254
|
-
It
|
|
213
|
+
It loads the whole app and reads the source of every crumb, without running them or touching the database. It
|
|
214
|
+
fails when:
|
|
215
|
+
|
|
216
|
+
- a crumb's parent is not a crumb, or a `_path` or `_url` it calls is not a route helper;
|
|
217
|
+
- a `breadcrumb :name` in `app/views` or `app/controllers` names a crumb that no trail class defines;
|
|
218
|
+
- a view or another crumb calls a crumb with the wrong number of arguments (calls spread over several lines, or
|
|
219
|
+
with a splat, are skipped);
|
|
220
|
+
- a crumb is never used. A crumb is used when another crumb, a view or a controller calls it, when a trail
|
|
221
|
+
Crumbly works out reads it, or when it is named after a model.
|
|
255
222
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
- every crumb is called with as many arguments as it takes, by its views and by other crumbs: gretel ignored
|
|
259
|
-
extra arguments and passed `nil` for missing ones, a method raises `ArgumentError` (a view call spread over
|
|
260
|
-
several lines, or with a splat, is not counted);
|
|
261
|
-
- every crumb is reachable: called by another crumb, by a view or controller (`breadcrumb :name`,
|
|
262
|
-
`breadcrumb @record`), by a derived trail (the collection and record of a routed controller), or named after
|
|
263
|
-
a model. The others are dead, and fail the test.
|
|
223
|
+
A crumb defined twice in the same class is caught even earlier: the class raises `Crumbly::DuplicateCrumb` when it
|
|
224
|
+
loads.
|
|
264
225
|
|
|
265
|
-
`Crumbly.verify!`
|
|
266
|
-
|
|
267
|
-
`routes:` and `sources
|
|
226
|
+
Outside Minitest, `Crumbly.verify!` runs the same checks. It raises `Crumbly::InvalidTrail` with every problem in
|
|
227
|
+
one message, and returns the unused crumbs (`["Admin::Breadcrumbs#drafts"]`). Both accept `trail_classes:`,
|
|
228
|
+
`routes:` and `sources:`, the directories it scans for `breadcrumb` calls.
|
|
268
229
|
|
|
269
|
-
##
|
|
230
|
+
## Mistakes in development and in production
|
|
270
231
|
|
|
271
|
-
|
|
232
|
+
A view that asks for a crumb that doesn't exist raises `Crumbly::UnknownCrumb` in development and test. So does
|
|
233
|
+
`breadcrumb @order` when `@order` is `nil`. In production the error goes to `Rails.error`, and the page renders
|
|
234
|
+
without a trail instead of failing.
|
|
235
|
+
|
|
236
|
+
A crumb whose URL a browser would run as a script raises `Crumbly::UnsafeUrl` in development and test. In
|
|
237
|
+
production the error goes to `Rails.error`, and the level keeps its label without the link.
|
|
238
|
+
|
|
239
|
+
To raise in production too, on a staging server for example, set either in an initializer:
|
|
240
|
+
|
|
241
|
+
```ruby
|
|
242
|
+
# config/initializers/crumbly.rb
|
|
243
|
+
Crumbly.raise_on_unknown_crumb = true # on staging too
|
|
244
|
+
Crumbly.raise_on_unsafe_url = true
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Breadcrumbs for search engines
|
|
248
|
+
|
|
249
|
+
Google can show a page's trail in its results when the page describes it as schema.org structured data.
|
|
250
|
+
`breadcrumb_json_ld` writes that description for the current trail. Put it in the `<head>`:
|
|
251
|
+
|
|
252
|
+
```erb
|
|
253
|
+
<%# app/views/layouts/storefront.html.erb %>
|
|
254
|
+
<%= breadcrumb_json_ld %>
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Google needs absolute URLs, so use `_url` helpers in the crumbs of that namespace:
|
|
272
258
|
|
|
273
259
|
```ruby
|
|
274
260
|
module Storefront
|
|
@@ -281,129 +267,47 @@ module Storefront
|
|
|
281
267
|
end
|
|
282
268
|
```
|
|
283
269
|
|
|
284
|
-
|
|
285
|
-
|
|
270
|
+
A relative URL raises `Crumbly::RelativeUrl` in development and test. In production it is reported to
|
|
271
|
+
`Rails.error` and the page has no structured data. `Crumbly.raise_on_relative_url` changes this.
|
|
286
272
|
|
|
287
|
-
|
|
288
|
-
<%# app/views/layouts/storefront.html.erb, in <head> %>
|
|
289
|
-
<%= breadcrumb_json_ld %>
|
|
290
|
-
```
|
|
273
|
+
Some details of the output:
|
|
291
274
|
|
|
292
|
-
- Positions start at 1
|
|
293
|
-
|
|
294
|
-
-
|
|
295
|
-
- The JSON is escaped for a `<script>` tag, so a label
|
|
296
|
-
-
|
|
297
|
-
|
|
298
|
-
`Crumbly.raise_on_relative_url` to choose.
|
|
299
|
-
- `Crumbly::JsonLd.call(levels)` returns the `BreadcrumbList` as a hash, for a page that builds its own
|
|
300
|
-
structured data.
|
|
275
|
+
- Positions start at 1. The last level may have no URL. A level above it without a URL isn't a page, so it is
|
|
276
|
+
left out.
|
|
277
|
+
- Names are plain text. If a label is `html_safe` (an icon, a `<b>`), its tags are removed.
|
|
278
|
+
- The JSON is escaped for a `<script>` tag, so a label can't close it.
|
|
279
|
+
- `Crumbly::JsonLd.call(levels)` returns the same data as a hash, if you build the structured data of the page
|
|
280
|
+
yourself.
|
|
301
281
|
|
|
302
|
-
##
|
|
282
|
+
## Switching from gretel, loaf or breadcrumbs_on_rails
|
|
303
283
|
|
|
304
|
-
|
|
305
|
-
|
|
284
|
+
Crumbly uses the same view helper names as these gems, so an app switches in one go: there is no compatibility
|
|
285
|
+
layer, and the old gem leaves the Gemfile once the migration is merged.
|
|
306
286
|
|
|
307
|
-
|
|
287
|
+
From gretel, the calls in your views barely change (`breadcrumb :order, @order` and `breadcrumb @order` work as
|
|
288
|
+
before), and `crumbly-convert` turns `config/breadcrumbs.rb` into a trail class:
|
|
308
289
|
|
|
309
290
|
```sh
|
|
310
291
|
bundle exec crumbly-convert config/breadcrumbs.rb --namespace Admin \
|
|
311
292
|
--output app/breadcrumbs/admin/breadcrumbs.rb --views app/views/admin
|
|
312
293
|
```
|
|
313
294
|
|
|
314
|
-
It
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|---|---|
|
|
319
|
-
| `crumb :key do \|order\| ... end` | `def key(order) ... end`, a parameter tested for nil getting `= nil` |
|
|
320
|
-
| `link label, url` | `crumb label, url`; `link label` or `link label, nil` give a level without a URL |
|
|
321
|
-
| a second `link` | `links: [link(label, url)]` on the same level |
|
|
322
|
-
| `parent :other, order` | `parent: other(order)` |
|
|
323
|
-
| `parent record` | `parent: crumb_for(record)` |
|
|
324
|
-
| `parent :a` and `parent :b` in an `if`/`else` | `parent: condition ? a : b` |
|
|
325
|
-
| `parent :a`, then `parent :b if condition` | `parent: condition ? b : a` |
|
|
326
|
-
| `link` and `parent` inside an `if`, `elsif`, `unless` or `case`, modifiers included | one `crumb` call per branch, the statements after the conditional copied into each |
|
|
327
|
-
| `t(".x")`, `current_user`: view helpers | `helpers.t(".x")`, `helpers.current_user`; route helpers stay bare |
|
|
328
|
-
| `%i[a b].each { \|name\| crumb name do ... end }`, `{a: [:post, Comment]}.each { \|name, (parent, model)\| ... }` | one method per element |
|
|
329
|
-
| a local assigned before a crumb reads it: `path = :"admin_#{name}_path"` | copied into each crumb reading it, a literal written in place |
|
|
330
|
-
| `label = ->(record) { ... }` at the top of the file, reading no other local | a private method, `label.call(post)` becoming `label(post)` |
|
|
331
|
-
|
|
332
|
-
It lists, without converting them: a loop over anything but a literal array or hash (of symbols, strings,
|
|
333
|
-
numbers and constants), `link` or `parent` inside anything but a conditional (a loop, a block), `link` options
|
|
334
|
-
(`title:`, `class:`), crumbs whose name is a Ruby keyword or an existing method (`display`, `test`), and any
|
|
335
|
-
statement that is not a crumb, a loop or a local. Comments are kept above the method they precede.
|
|
336
|
-
|
|
337
|
-
The codemod is built on Prism 1.2 or later: Ruby 3.4 ships it, and on Ruby 3.3 add `gem "prism"` to the
|
|
338
|
-
development group.
|
|
339
|
-
|
|
340
|
-
### 2. Compare the trails
|
|
341
|
-
|
|
342
|
-
While gretel is still bundled, the tests draw each trail with both gems and list the differences in labels and
|
|
343
|
-
URLs. gretel shows a crumb's extra links as levels of their own: the comparison does the same with `links`. Keep
|
|
344
|
-
gretel in the test group with `require: false`: `crumbly/oracle` loads it, without the view helpers whose
|
|
345
|
-
`breadcrumb` would replace Crumbly's.
|
|
346
|
-
|
|
347
|
-
```ruby
|
|
348
|
-
# Gemfile, on the migration branch
|
|
349
|
-
gem "gretel", require: false, group: :test
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
The views already call `breadcrumb` the way Crumbly expects, so the app's own controller, integration and system
|
|
353
|
-
tests can do the comparing. With `Crumbly::Oracle.watch` in the test helper, every page they render is drawn by
|
|
354
|
-
gretel too, from the same `breadcrumb` call:
|
|
355
|
-
|
|
356
|
-
```ruby
|
|
357
|
-
# test/test_helper.rb, once the app is loaded
|
|
358
|
-
require "crumbly/oracle"
|
|
359
|
-
|
|
360
|
-
Crumbly::Oracle.watch
|
|
361
|
-
Minitest.after_run do
|
|
362
|
-
puts Crumbly::Oracle.differences.to_a, Crumbly::Oracle.redundant_calls.to_a
|
|
363
|
-
end
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
`differences` holds each page whose trail changed, gretel's and Crumbly's side by side, including the pages
|
|
367
|
-
without a `breadcrumb` call, which gretel left without a trail and which now derive one. `redundant_calls`
|
|
368
|
-
holds the calls whose trail the action derives anyway. `watch { ... }` watches a block only. Parallel test
|
|
369
|
-
workers each fill their own lists, which `after_run` in the parent process never sees: run the watched suite
|
|
370
|
-
with `PARALLEL_WORKERS=1`. A call gretel cannot draw shows as a difference, with gretel's error.
|
|
371
|
-
|
|
372
|
-
For the crumbs no test renders, `compare` takes the calls to check:
|
|
373
|
-
|
|
374
|
-
```ruby
|
|
375
|
-
class BreadcrumbsMigrationTest < ActionView::TestCase
|
|
376
|
-
test "Crumbly draws the trails gretel drew" do
|
|
377
|
-
differences = Crumbly::Oracle.compare(view, Admin::Breadcrumbs, [
|
|
378
|
-
[:orders], [:order, orders(:alice)], [orders(:alice)], [:returns], [:returns, customers(:alice)]
|
|
379
|
-
])
|
|
380
|
-
|
|
381
|
-
assert_empty differences, differences.join("\n")
|
|
382
|
-
end
|
|
383
|
-
end
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
### 3. Review the call sites
|
|
295
|
+
It prints each line it left out or couldn't convert exactly, and with `--views`, the `breadcrumb` calls that
|
|
296
|
+
Crumbly may now make unnecessary. It needs Prism 1.2 or later: Ruby 3.4 comes with it, and on Ruby 3.3 you add
|
|
297
|
+
`gem "prism"` to the development group. While gretel is still installed on the migration branch,
|
|
298
|
+
`Crumbly::Oracle` draws each trail your tests render with both gems and lists where the labels or URLs differ.
|
|
387
299
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
### 4. Remove gretel
|
|
394
|
-
|
|
395
|
-
Drop gretel and `config/breadcrumbs.rb`, and render `breadcrumb_trail` in the layout where `breadcrumbs` was.
|
|
396
|
-
|
|
397
|
-
## Requirements
|
|
398
|
-
|
|
399
|
-
Ruby 3.3 or later, and Rails 8.0 or later (Action Pack and Active Support).
|
|
300
|
+
The [migration guide](https://levups.github.io/crumbly/migration.html) has every step: what the converter maps to
|
|
301
|
+
what, what it leaves to you, the comparison in your tests, and the same walk-through for loaf and
|
|
302
|
+
breadcrumbs_on_rails. The [home page](https://levups.github.io/crumbly/) shows five crumbs from a real gretel app
|
|
303
|
+
next to their Crumbly version.
|
|
400
304
|
|
|
401
305
|
## Performance
|
|
402
306
|
|
|
403
307
|
`bench/render.rb` renders the same five-level trail (Home > Customers > Alice > Orders > Order 2026-0042) with
|
|
404
|
-
gretel and with Crumbly, and checks that both
|
|
405
|
-
`breadcrumbs(style: :ol)`
|
|
406
|
-
`structured_data(url_base:)
|
|
308
|
+
gretel and with Crumbly, and checks that both give the same output. The `<ol>` row compares gretel's
|
|
309
|
+
`breadcrumbs(style: :ol)` with a ten-line helper over `breadcrumb_trail`. The JSON-LD row compares gretel's
|
|
310
|
+
`structured_data(url_base:)`, with paths behind a prefix, and `breadcrumb_json_ld` with `_url` helpers. Both are
|
|
407
311
|
encoded and escaped the same way.
|
|
408
312
|
|
|
409
313
|
Ruby 4.0.9, Rails 8.1.4, gretel 5.1.0, Apple M5 (arm64 macOS), median of three runs of benchmark-ips:
|
|
@@ -415,14 +319,21 @@ Ruby 4.0.9, Rails 8.1.4, gretel 5.1.0, Apple M5 (arm64 macOS), median of three r
|
|
|
415
319
|
| gretel JSON-LD | 268 | 41.3 µs | 25.4 µs |
|
|
416
320
|
| Crumbly JSON-LD | 287 | 43.3 µs | 29.5 µs |
|
|
417
321
|
|
|
418
|
-
|
|
419
|
-
`BreadcrumbList` from the levels
|
|
322
|
+
Crumbly's JSON-LD is a little slower, mostly because Rails' `_url` helpers are slower than `_path` ones. Building
|
|
323
|
+
the `BreadcrumbList` from the levels takes 1.7 µs of the difference.
|
|
420
324
|
|
|
421
325
|
```sh
|
|
422
326
|
bundle exec ruby bench/render.rb
|
|
423
327
|
bundle exec ruby --yjit bench/render.rb
|
|
424
328
|
```
|
|
425
329
|
|
|
330
|
+
## How Crumbly is tested
|
|
331
|
+
|
|
332
|
+
CI runs the test suite on Ruby 3.3, 3.4 and 4.0, each against Rails 8.0 and 8.1, and on `ruby-head`. The
|
|
333
|
+
tests convert a sample gretel file, compare the result with the expected class line for line, then draw its trails
|
|
334
|
+
with gretel and with the converted class and check that the labels and URLs match. The production paths (an unknown crumb, a relative JSON-LD URL) are tested
|
|
335
|
+
as well as the raising ones.
|
|
336
|
+
|
|
426
337
|
## Development
|
|
427
338
|
|
|
428
339
|
```sh
|
|
@@ -430,26 +341,27 @@ bundle install
|
|
|
430
341
|
bundle exec rake test # latest Rails
|
|
431
342
|
BUNDLE_GEMFILE=gemfiles/rails_8_0.gemfile bundle exec rake test # Rails 8.0
|
|
432
343
|
bundle exec rubocop
|
|
344
|
+
bundle exec rake docs:highlight # after editing a code block in docs/
|
|
433
345
|
```
|
|
434
346
|
|
|
347
|
+
[CONTRIBUTING.md](CONTRIBUTING.md) says what a pull request needs.
|
|
348
|
+
|
|
435
349
|
### Releasing
|
|
436
350
|
|
|
437
|
-
Pull requests
|
|
438
|
-
pull requests merged since the last tag, without committing them. Then, from an up-to-date `main`
|
|
439
|
-
green:
|
|
351
|
+
Pull requests don't touch `CHANGELOG.md`. A release starts by writing its notes under `## [Unreleased]`, from the
|
|
352
|
+
pull requests merged since the last tag, without committing them. Then, from an up-to-date `main` with green CI:
|
|
440
353
|
|
|
441
354
|
```sh
|
|
442
|
-
bin/release minor --dry-run # the notes and the changes, nothing
|
|
355
|
+
bin/release minor --dry-run # shows the notes and the changes, writes nothing
|
|
443
356
|
bin/release minor # or patch, major, or an exact version such as 1.2.0
|
|
444
357
|
```
|
|
445
358
|
|
|
446
|
-
`
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
with the version's notes and the `.gem`.
|
|
359
|
+
`bin/release` bumps the version on a `release/vX.Y.Z` branch, opens its pull request, waits for it to merge
|
|
360
|
+
itself once CI is green, and pushes a signed tag `vX.Y.Z` on the merge commit (`bin/release --tag X.Y.Z` resumes
|
|
361
|
+
this step if you stopped it). The tag starts the Release workflow, which publishes the gem to RubyGems through
|
|
362
|
+
trusted publishing and creates the GitHub release.
|
|
451
363
|
|
|
452
364
|
## License
|
|
453
365
|
|
|
454
|
-
MIT, by Level UP Solutions.
|
|
455
|
-
|
|
366
|
+
MIT, by Level UP Solutions. The idea of a crumb that names its parent comes from
|
|
367
|
+
[gretel](https://github.com/kzkn/gretel), by Lasse Bunk and Kazuki Nishikawa.
|
|
@@ -450,12 +450,12 @@ module Crumbly
|
|
|
450
450
|
end
|
|
451
451
|
|
|
452
452
|
def loose_comments
|
|
453
|
-
source.comments.
|
|
453
|
+
source.comments.flat_map do |comment|
|
|
454
454
|
offset = comment.location.start_offset
|
|
455
|
-
next unless offset.between?(block.location.start_offset, block.location.end_offset)
|
|
456
|
-
next if copied_ranges.any? { |range| range.cover?(offset) }
|
|
455
|
+
next [] unless offset.between?(block.location.start_offset, block.location.end_offset)
|
|
456
|
+
next [] if copied_ranges.any? { |range| range.cover?(offset) }
|
|
457
457
|
|
|
458
|
-
comment
|
|
458
|
+
Source.comment_text(comment)
|
|
459
459
|
end
|
|
460
460
|
end
|
|
461
461
|
|
|
@@ -35,9 +35,16 @@ module Crumbly
|
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
def comment_lines(from_offset, to_offset)
|
|
38
|
-
comments.
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
comments.select { |comment| comment.location.start_offset.between?(from_offset, to_offset - 1) }
|
|
39
|
+
.flat_map { |comment| self.class.comment_text(comment) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The lines of a comment. An =begin block must start at column 0, which the class body indents: its lines
|
|
43
|
+
# become # comments.
|
|
44
|
+
def self.comment_text(comment)
|
|
45
|
+
return [comment.slice.rstrip] unless comment.is_a?(Prism::EmbDocComment)
|
|
46
|
+
|
|
47
|
+
comment.slice.lines[1...-1].map { |line| "# #{line.rstrip}".rstrip }
|
|
41
48
|
end
|
|
42
49
|
|
|
43
50
|
# Blanks everything before the expanded text, so that its lines and columns stay those of this file.
|
data/lib/crumbly/converter.rb
CHANGED
|
@@ -224,7 +224,11 @@ module Crumbly
|
|
|
224
224
|
end
|
|
225
225
|
end
|
|
226
226
|
|
|
227
|
-
|
|
227
|
+
# A string's interpolated value is written as text when only a plain string or symbol reads it: in a regexp,
|
|
228
|
+
# a command, a %W array or after a delimiter such as %Q/, the same text would mean something else.
|
|
229
|
+
INLINE_OPENING = /\A(?::?"|<<|%Q?[(\[{<])/
|
|
230
|
+
|
|
231
|
+
def substitutions(node, value_by_name, depth = 0, edits = [], inline: false)
|
|
228
232
|
case node
|
|
229
233
|
when Prism::LocalVariableReadNode
|
|
230
234
|
value = value_by_name[node.name] if node.depth == depth
|
|
@@ -236,17 +240,21 @@ module Crumbly
|
|
|
236
240
|
return edits << [node.location.start_offset, node.location.end_offset, text.inspect]
|
|
237
241
|
end
|
|
238
242
|
when Prism::EmbeddedStatementsNode
|
|
239
|
-
if node in {statements: {body: [Prism::LocalVariableReadNode[depth: ^depth, name:]]}}
|
|
243
|
+
if inline && (node in {statements: {body: [Prism::LocalVariableReadNode[depth: ^depth, name:]]}})
|
|
240
244
|
text = value_by_name[name]&.text
|
|
241
245
|
if text&.match?(%r{\A[\w ./-]*\z})
|
|
242
246
|
edits << [node.location.start_offset, node.location.end_offset, text]
|
|
243
247
|
return edits
|
|
244
248
|
end
|
|
245
249
|
end
|
|
250
|
+
when Prism::InterpolatedStringNode, Prism::InterpolatedSymbolNode
|
|
251
|
+
inline = INLINE_OPENING.match?(node.opening.to_s)
|
|
252
|
+
when Prism::InterpolatedRegularExpressionNode, Prism::InterpolatedMatchLastLineNode, Prism::InterpolatedXStringNode
|
|
253
|
+
inline = false
|
|
246
254
|
when Prism::BlockNode, Prism::LambdaNode then depth += 1
|
|
247
255
|
when Prism::DefNode then return edits
|
|
248
256
|
end
|
|
249
|
-
node&.compact_child_nodes&.each { |child| substitutions(child, value_by_name, depth, edits) }
|
|
257
|
+
node&.compact_child_nodes&.each { |child| substitutions(child, value_by_name, depth, edits, inline:) }
|
|
250
258
|
edits
|
|
251
259
|
end
|
|
252
260
|
|
|
@@ -25,8 +25,8 @@ module Crumbly
|
|
|
25
25
|
def self.call_sites(path)
|
|
26
26
|
action = File.basename(path).split(".").first
|
|
27
27
|
controller_name = File.basename(File.dirname(path))
|
|
28
|
-
File.foreach(path).with_index(1).filter_map do |line, number|
|
|
29
|
-
match = BREADCRUMB_CALL.match(line)
|
|
28
|
+
File.foreach(path, encoding: Encoding::UTF_8).with_index(1).filter_map do |line, number|
|
|
29
|
+
match = BREADCRUMB_CALL.match(line.scrub)
|
|
30
30
|
reason = match && reason(match[:target], match[:arguments].strip.delete_suffix(")").strip, action, controller_name)
|
|
31
31
|
CallSite.new(path:, line: number, code: match[0].strip, reason:) if reason
|
|
32
32
|
end
|
data/lib/crumbly/errors.rb
CHANGED
data/lib/crumbly/json_ld.rb
CHANGED
|
@@ -8,7 +8,8 @@ require "loofah"
|
|
|
8
8
|
module Crumbly
|
|
9
9
|
# A schema.org BreadcrumbList, as Google's Rich Results test reads it.
|
|
10
10
|
module JsonLd
|
|
11
|
-
|
|
11
|
+
# A scheme is case-insensitive: HTTPS://example.com is absolute too.
|
|
12
|
+
ABSOLUTE_URL = %r{\Ahttps?://}i
|
|
12
13
|
|
|
13
14
|
class << self
|
|
14
15
|
def call(levels)
|
|
@@ -19,7 +20,7 @@ module Crumbly
|
|
|
19
20
|
|
|
20
21
|
list_item = {"@type" => "ListItem", "position" => list_items.size + 1, "name" => plain_text(level.label)}
|
|
21
22
|
if level.url
|
|
22
|
-
return relative_url(level) unless level.url
|
|
23
|
+
return relative_url(level) unless ABSOLUTE_URL.match?(level.url)
|
|
23
24
|
|
|
24
25
|
list_item["item"] = level.url
|
|
25
26
|
end
|
|
@@ -32,10 +33,13 @@ module Crumbly
|
|
|
32
33
|
|
|
33
34
|
private
|
|
34
35
|
|
|
36
|
+
# A label read from bytes that are not UTF-8 gets replacement characters, instead of failing the page.
|
|
35
37
|
def plain_text(label)
|
|
36
|
-
|
|
38
|
+
text = label.to_s
|
|
39
|
+
text = text.scrub unless text.valid_encoding?
|
|
40
|
+
return text.squish unless label.html_safe?
|
|
37
41
|
|
|
38
|
-
Loofah.html5_fragment(
|
|
42
|
+
Loofah.html5_fragment(text).scrub!(:prune).text(encode_special_chars: false).squish
|
|
39
43
|
end
|
|
40
44
|
|
|
41
45
|
def relative_url(level)
|
data/lib/crumbly/trail.rb
CHANGED
|
@@ -4,6 +4,9 @@ module Crumbly
|
|
|
4
4
|
# One subclass per namespace (Admin::Breadcrumbs < Crumbly::Trail): each public method defined there is a crumb.
|
|
5
5
|
class Trail
|
|
6
6
|
RouteParent = Data.define(:name, :id, :record)
|
|
7
|
+
# Browsers drop leading spaces and control characters, and tabs and newlines anywhere: "java\tscript:" runs.
|
|
8
|
+
IGNORED_URL_CHARACTERS = /\A[\x00-\x20]+|[\t\n\r]/
|
|
9
|
+
UNSAFE_URL_SCHEME = /\A(?:javascript|vbscript|data):/i
|
|
7
10
|
|
|
8
11
|
# Included after the routes' url_helpers, whose own url_options would otherwise win.
|
|
9
12
|
module UrlOptionsFromView
|
|
@@ -62,6 +65,10 @@ module Crumbly
|
|
|
62
65
|
attr_reader :helpers
|
|
63
66
|
|
|
64
67
|
def crumb(label, url = nil, parent: nil, links: [])
|
|
68
|
+
unless parent.nil? || parent.is_a?(Crumb)
|
|
69
|
+
raise ArgumentError, "parent: takes a crumb, such as parent: orders, not #{parent.inspect}"
|
|
70
|
+
end
|
|
71
|
+
|
|
65
72
|
Crumb.new(label:, url: path_for(url), links:, parent:)
|
|
66
73
|
end
|
|
67
74
|
|
|
@@ -74,16 +81,39 @@ module Crumbly
|
|
|
74
81
|
name = crumb_name_for(record)
|
|
75
82
|
return public_send(name, record, *) if name
|
|
76
83
|
|
|
77
|
-
unknown_crumb(
|
|
84
|
+
unknown_crumb(unknown_record_message(record))
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# `breadcrumb @order` before the controller loads @order passes nil: it is reported like an unknown crumb.
|
|
88
|
+
def unknown_record_message(record)
|
|
89
|
+
if record.class.respond_to?(:model_name)
|
|
90
|
+
"#{self.class} has no crumb for #{record.class}: define ##{record.model_name.singular}"
|
|
91
|
+
else
|
|
92
|
+
"#{self.class} has no crumb for #{record.nil? ? "nil" : "a #{record.class}"}: pass a crumb name or a record"
|
|
93
|
+
end
|
|
78
94
|
end
|
|
79
95
|
|
|
80
96
|
def path_for(url)
|
|
81
97
|
case url
|
|
82
|
-
when nil
|
|
98
|
+
when nil then nil
|
|
99
|
+
when String then safe_url(url)
|
|
83
100
|
else polymorphic_path(url)
|
|
84
101
|
end
|
|
85
102
|
end
|
|
86
103
|
|
|
104
|
+
# A URL built from user input can run a script once a layout links it: the level keeps its label, not the link.
|
|
105
|
+
def safe_url(url)
|
|
106
|
+
return url if url.start_with?("/", "https://", "http://") || !url.include?(":")
|
|
107
|
+
return url unless UNSAFE_URL_SCHEME.match?(url.gsub(IGNORED_URL_CHARACTERS, ""))
|
|
108
|
+
|
|
109
|
+
exception = UnsafeUrl.new("#{self.class} links to #{url.inspect}, which a browser runs as a script: " \
|
|
110
|
+
"check its scheme before passing it to a crumb")
|
|
111
|
+
raise exception if Crumbly.raise_on_unsafe_url?
|
|
112
|
+
|
|
113
|
+
ActiveSupport.error_reporter.report(exception, handled: true)
|
|
114
|
+
nil
|
|
115
|
+
end
|
|
116
|
+
|
|
87
117
|
def levels_for(target, arguments)
|
|
88
118
|
levels(target_crumb(target, arguments))
|
|
89
119
|
end
|
|
@@ -121,6 +151,8 @@ module Crumbly
|
|
|
121
151
|
# STI: a subclass without a crumb of its own falls back on its parent class's.
|
|
122
152
|
def crumb_name_for(record)
|
|
123
153
|
model_class = record.class
|
|
154
|
+
return unless model_class.respond_to?(:model_name)
|
|
155
|
+
|
|
124
156
|
loop do
|
|
125
157
|
name = model_class.model_name.singular.to_sym
|
|
126
158
|
return name if self.class.crumb?(name)
|
data/lib/crumbly/verifier.rb
CHANGED
|
@@ -88,7 +88,9 @@ module Crumbly
|
|
|
88
88
|
def verify_sources
|
|
89
89
|
defined_names = trail_classes.flat_map { |trail_class| crumb_names(trail_class) }.to_set
|
|
90
90
|
source_files.each do |path|
|
|
91
|
-
|
|
91
|
+
# An image or a Latin-1 template is read too: an invalid byte cannot fail the verification.
|
|
92
|
+
File.foreach(path, encoding: Encoding::UTF_8).with_index(1) do |line, number|
|
|
93
|
+
line = line.scrub
|
|
92
94
|
line.to_enum(:scan, BREADCRUMB_CALL).each do
|
|
93
95
|
match = Regexp.last_match
|
|
94
96
|
symbol, string, instance_variable = match.captures
|
data/lib/crumbly/version.rb
CHANGED
data/lib/crumbly.rb
CHANGED
|
@@ -20,7 +20,7 @@ require_relative "crumbly/railtie" if defined?(Rails::Railtie)
|
|
|
20
20
|
|
|
21
21
|
module Crumbly
|
|
22
22
|
class << self
|
|
23
|
-
attr_writer :routes, :raise_on_unknown_crumb, :raise_on_relative_url
|
|
23
|
+
attr_writer :routes, :raise_on_unknown_crumb, :raise_on_relative_url, :raise_on_unsafe_url
|
|
24
24
|
|
|
25
25
|
def routes
|
|
26
26
|
@routes || (Rails.application&.routes if defined?(Rails.application))
|
|
@@ -38,6 +38,12 @@ module Crumbly
|
|
|
38
38
|
!production?
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
def raise_on_unsafe_url?
|
|
42
|
+
return @raise_on_unsafe_url unless @raise_on_unsafe_url.nil?
|
|
43
|
+
|
|
44
|
+
!production?
|
|
45
|
+
end
|
|
46
|
+
|
|
41
47
|
# Raises InvalidTrail with every problem found, and returns the dead crumbs ("Admin::Breadcrumbs#drafts").
|
|
42
48
|
def verify!(trail_classes: nil, routes: Crumbly.routes, sources: default_sources)
|
|
43
49
|
Rails.application.eager_load! if trail_classes.nil? && defined?(Rails.application) && Rails.application
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: crumbly
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Level UP Solutions
|
|
@@ -43,17 +43,18 @@ dependencies:
|
|
|
43
43
|
requirements:
|
|
44
44
|
- - ">="
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version:
|
|
46
|
+
version: 2.25.3
|
|
47
47
|
type: :runtime
|
|
48
48
|
prerelease: false
|
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
50
50
|
requirements:
|
|
51
51
|
- - ">="
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version:
|
|
54
|
-
description:
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
version: 2.25.3
|
|
54
|
+
description: Each level of a breadcrumb trail is a Ruby method that names its parent.
|
|
55
|
+
When a view names no crumb, Crumbly works the trail out from the controller and
|
|
56
|
+
the routes, and one test checks every trail in the app. The trail comes back as
|
|
57
|
+
data, so your layout writes the HTML.
|
|
57
58
|
executables:
|
|
58
59
|
- crumbly-convert
|
|
59
60
|
extensions: []
|
|
@@ -103,8 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
103
104
|
- !ruby/object:Gem::Version
|
|
104
105
|
version: '0'
|
|
105
106
|
requirements: []
|
|
106
|
-
rubygems_version: 4.0.
|
|
107
|
+
rubygems_version: 4.0.20
|
|
107
108
|
specification_version: 4
|
|
108
|
-
summary:
|
|
109
|
-
tests
|
|
109
|
+
summary: Breadcrumb trails for Rails, written as plain Ruby methods and checked in
|
|
110
|
+
tests
|
|
110
111
|
test_files: []
|