mongoid-slug 7.1.0 → 7.2.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/README.md +37 -47
- data/lib/mongoid/slug/criteria.rb +1 -1
- data/lib/mongoid/slug/version.rb +1 -1
- data/lib/mongoid/slug.rb +3 -4
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 335484d0b0e6b3c713697a824912c2e8d5613cce026788f7815288a7dc5ce85a
|
|
4
|
+
data.tar.gz: 5353aeb55175435134e50d84ed6bce477850208c46ac5e4671bae41ea716b96c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c63da8868b585168e7c60dabe1380d09c86982d0c8a122943e2d0edd721163b50f5ea5651275ba438a43d3b6d755318b090f2a06762848c7c5bf68d377c6efd1
|
|
7
|
+
data.tar.gz: ae0b39ff2107704a052d8ddde4f79cb7776a7cb3843c4f5232ea93b6ba39bd20a462018361bc7dec3d4519abdf9566cb9eb990db85b674b357f33bd0d6b537f2
|
data/README.md
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
Mongoid Slug
|
|
2
|
-
============
|
|
1
|
+
# Mongoid Slug
|
|
3
2
|
|
|
4
|
-
Mongoid Slug generates a URL slug or permalink based on one or more fields in a Mongoid model.
|
|
5
|
-
It sits idly on top of [stringex](https://github.com/rsl/stringex), supporting non-Latin characters.
|
|
3
|
+
Mongoid Slug generates a URL slug or permalink based on one or more fields in a Mongoid model. It sits idly on top of [stringex](https://github.com/rsl/stringex), supporting non-Latin characters.
|
|
6
4
|
|
|
7
|
-
[](http://badge.fury.io/rb/mongoid-slug)
|
|
9
|
-
[](https://codeclimate.com/github/mongoid/mongoid-slug)
|
|
5
|
+
[](https://github.com/mongoid/mongoid-slug/actions/workflows/test.yml?query=branch%3Amaster) [](https://coveralls.io/github/mongoid/mongoid-slug?branch=master) [](https://badge.fury.io/rb/mongoid-slug)
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
CI collects SimpleCov coverage on Ruby 3.4 with Mongoid 9 and MongoDB 8.0 and submits one report to Coveralls using GitHub's workflow token. To generate a local report, run `COVERAGE=true MONGOID_VERSION=9 bundle install` followed by `COVERAGE=true MONGOID_VERSION=9 bundle exec rake spec`. Reports are written to `coverage/`.
|
|
8
|
+
|
|
9
|
+
## Version Support
|
|
12
10
|
|
|
13
11
|
Mongoid Slug 7.x requires at least Mongoid 7.0.0 and Ruby 2.7.0. For earlier Mongoid and Ruby version support, please use an earlier version of Mongoid Slug.
|
|
14
12
|
|
|
15
13
|
Mongoid Slug is compatible with all MongoDB versions which Mongoid supports, however, please see "Slug Max Length" section below for MongoDB 4.0 and earlier.
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
## Installation
|
|
18
16
|
|
|
19
17
|
Add to your Gemfile:
|
|
20
18
|
|
|
@@ -22,7 +20,7 @@ Add to your Gemfile:
|
|
|
22
20
|
gem 'mongoid-slug'
|
|
23
21
|
```
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
## Usage
|
|
26
24
|
|
|
27
25
|
### Set Up a Slug
|
|
28
26
|
|
|
@@ -36,7 +34,7 @@ class Book
|
|
|
36
34
|
end
|
|
37
35
|
```
|
|
38
36
|
|
|
39
|
-
### Find a Document by
|
|
37
|
+
### Find a Document by Its Slug
|
|
40
38
|
|
|
41
39
|
```ruby
|
|
42
40
|
# GET /books/a-thousand-plateaus
|
|
@@ -48,14 +46,14 @@ Mongoid Slug will attempt to determine whether you want to find using the `slugs
|
|
|
48
46
|
* Mongoid Slug will perform a find based on `slugs` only if all arguments passed to `find` are of the type `String`.
|
|
49
47
|
* If your document uses `BSON::ObjectId` identifiers, and all arguments look like valid `BSON::ObjectId`, then Mongoid Slug will perform a find based on `_id`.
|
|
50
48
|
* If your document uses any other type of identifiers, and all arguments passed to `find` are of the same type, then Mongoid Slug will perform a find based on `_id`.
|
|
51
|
-
* If your document uses `String` identifiers and you want to be able find by slugs or ids, to get the correct
|
|
49
|
+
* If your document uses `String` identifiers and you want to be able find by slugs or ids, to get the correct behavior, you should add a `slug_id_strategy` option to your `_id` field definition. This option should return something that responds to `call` (a callable) and takes one string argument, e.g. a lambda. This callable must return true if the string looks like one of your ids.
|
|
52
50
|
|
|
53
51
|
```ruby
|
|
54
52
|
Book.fields['_id'].type
|
|
55
|
-
=> String
|
|
53
|
+
# => String
|
|
56
54
|
|
|
57
55
|
book = Book.find 'a-thousand-plateaus' # Finds by slugs
|
|
58
|
-
=> ...
|
|
56
|
+
# => ...
|
|
59
57
|
|
|
60
58
|
class Post
|
|
61
59
|
include Mongoid::Document
|
|
@@ -68,13 +66,13 @@ class Post
|
|
|
68
66
|
end
|
|
69
67
|
|
|
70
68
|
Post.fields['_id'].type
|
|
71
|
-
=> String
|
|
69
|
+
# => String
|
|
72
70
|
|
|
73
71
|
post = Post.find 'a-thousand-plateaus' # Finds by slugs
|
|
74
|
-
=> ...
|
|
72
|
+
# => ...
|
|
75
73
|
|
|
76
74
|
post = Post.find '50b1386a0482939864000001' # Finds by bson ids
|
|
77
|
-
=> ...
|
|
75
|
+
# => ...
|
|
78
76
|
```
|
|
79
77
|
|
|
80
78
|
[Examine slug.rb](lib/mongoid/slug.rb) for all available options.
|
|
@@ -147,10 +145,12 @@ class Employee
|
|
|
147
145
|
|
|
148
146
|
field :name
|
|
149
147
|
|
|
150
|
-
slug :name, index:
|
|
148
|
+
slug :name, index: false
|
|
151
149
|
end
|
|
152
150
|
```
|
|
153
151
|
|
|
152
|
+
With automatic declaration disabled, declare your own Mongoid index with `index(...)`. Changing a declaration does not replace an existing database index: inspect the deployed indexes, check for conflicting data, and explicitly migrate the affected index before recreating it. Do not drop unrelated application indexes.
|
|
153
|
+
|
|
154
154
|
### Scoping
|
|
155
155
|
|
|
156
156
|
To scope a slug by a reference association, pass `:scope`:
|
|
@@ -159,7 +159,7 @@ To scope a slug by a reference association, pass `:scope`:
|
|
|
159
159
|
class Company
|
|
160
160
|
include Mongoid::Document
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
has_many :employees
|
|
163
163
|
end
|
|
164
164
|
|
|
165
165
|
class Employee
|
|
@@ -167,7 +167,7 @@ class Employee
|
|
|
167
167
|
include Mongoid::Slug
|
|
168
168
|
|
|
169
169
|
field :name
|
|
170
|
-
|
|
170
|
+
belongs_to :company, optional: true
|
|
171
171
|
|
|
172
172
|
slug :name, scope: :company
|
|
173
173
|
end
|
|
@@ -175,11 +175,11 @@ end
|
|
|
175
175
|
|
|
176
176
|
In this example, if you create an employee without associating it with any company, the scope will fall back to the root employees collection.
|
|
177
177
|
|
|
178
|
-
Currently, if you have an irregular association name, you **must** specify the `:inverse_of` option on the other side of the
|
|
178
|
+
Currently, if you have an irregular association name, you **must** specify the `:inverse_of` option on the other side of the association.
|
|
179
179
|
|
|
180
180
|
Embedded objects are automatically scoped by their parent.
|
|
181
181
|
|
|
182
|
-
|
|
182
|
+
The index on `Employee` is derived from the scope and uses `{ company_id: 1, _slugs: 1 }`. Scoped indexes are not declared unique by default. Declare `belongs_to :company` before `slug` so the association resolves to `company_id` rather than `company`. Use `optional: true` only when employees without a company are allowed. An alternative is to scope to the field itself as follows:
|
|
183
183
|
|
|
184
184
|
```ruby
|
|
185
185
|
class Employee
|
|
@@ -193,6 +193,8 @@ class Employee
|
|
|
193
193
|
end
|
|
194
194
|
```
|
|
195
195
|
|
|
196
|
+
Scoped slugs are not globally unique. Routes must carry the scope as well as the slug, for example nested company/employee routes, and controllers must look up the employee through that scope (`company.employees.find(params[:id])`), not `Employee.find(params[:id])`. If the route cannot carry the scope, pass the database ID to the route helper instead.
|
|
197
|
+
|
|
196
198
|
You may scope slugs using multiple fields as per the following example:
|
|
197
199
|
|
|
198
200
|
```ruby
|
|
@@ -211,14 +213,7 @@ end
|
|
|
211
213
|
|
|
212
214
|
### Slug Max Length
|
|
213
215
|
|
|
214
|
-
MongoDB [featureCompatibilityVersion](https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv)
|
|
215
|
-
"4.0" and earlier applies an [Index Key Limit](https://docs.mongodb.com/manual/reference/limits/#mongodb-limit-Index-Key-Limit)
|
|
216
|
-
which limits the total size of an index entry to around 1KB and will raise error,
|
|
217
|
-
`17280 - key too large to index` when trying to create a record that causes an index key to exceed that limit.
|
|
218
|
-
By default slugs are of the form `text[-number]` and the text portion is limited in size
|
|
219
|
-
to `Mongoid::Slug::MONGO_INDEX_KEY_LIMIT_BYTES - 32` bytes.
|
|
220
|
-
You can change this limit with `max_length` or set it to `nil` if you're running MongoDB
|
|
221
|
-
with [failIndexKeyTooLong](https://docs.mongodb.org/manual/reference/parameters/#param.failIndexKeyTooLong) set to `false`.
|
|
216
|
+
MongoDB [featureCompatibilityVersion](https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv) "4.0" and earlier applies an [Index Key Limit](https://docs.mongodb.com/manual/reference/limits/#mongodb-limit-Index-Key-Limit) which limits the total size of an index entry to around 1KB and will raise error, `17280 - key too large to index` when trying to create a record that causes an index key to exceed that limit. By default slugs are of the form `text[-number]` and the text portion is limited in size to `Mongoid::Slug::MONGO_INDEX_KEY_LIMIT_BYTES - 32` bytes. You can change this limit with `max_length` or set it to `nil` if you're running MongoDB with [failIndexKeyTooLong](https://docs.mongodb.org/manual/reference/parameters/#param.failIndexKeyTooLong) set to `false`.
|
|
222
217
|
|
|
223
218
|
```ruby
|
|
224
219
|
class Company
|
|
@@ -279,6 +274,8 @@ comic_book.slugs.should eql(book.slugs)
|
|
|
279
274
|
|
|
280
275
|
Enable slug history tracking by setting `history: true`.
|
|
281
276
|
|
|
277
|
+
History retains previous slugs for lookup; it does not generate multiple independent slugs from different source fields. The most recent slug is used for URLs. To switch new URLs back to database IDs while keeping historical lookup, retain `Mongoid::Slug`, its slug declaration, and the stored history, but override the model's `to_param` to return `id.to_s`. Existing slug URLs can still resolve through `find`; your controller can redirect them to the canonical ID URL. Do not change the slug source to `:id`, which generates slugs instead of changing URL selection.
|
|
278
|
+
|
|
282
279
|
```ruby
|
|
283
280
|
class Page
|
|
284
281
|
include Mongoid::Document
|
|
@@ -317,13 +314,11 @@ Friend.find('admin') # => nil
|
|
|
317
314
|
friend.slug # => 'admin-1'
|
|
318
315
|
```
|
|
319
316
|
|
|
320
|
-
When reserved words are not specified, the words 'new' and 'edit' are considered reserved by default.
|
|
321
|
-
Specifying an array of custom reserved words will overwrite these defaults.
|
|
317
|
+
When reserved words are not specified, the words 'new' and 'edit' are considered reserved by default. Specifying an array of custom reserved words will overwrite these defaults.
|
|
322
318
|
|
|
323
319
|
### Localize Slugs
|
|
324
320
|
|
|
325
|
-
The slugs can be localized. This feature is built upon Mongoid localized fields,
|
|
326
|
-
so fallbacks and localization works as documented in the Mongoid manual.
|
|
321
|
+
The slugs can be localized. This feature is built upon Mongoid localized fields, so fallbacks and localization works as documented in the Mongoid manual.
|
|
327
322
|
|
|
328
323
|
```ruby
|
|
329
324
|
class PageSlugLocalize
|
|
@@ -335,9 +330,7 @@ class PageSlugLocalize
|
|
|
335
330
|
end
|
|
336
331
|
```
|
|
337
332
|
|
|
338
|
-
By specifying `localize: true`, the slug index will be created on the
|
|
339
|
-
[I18n.default_locale](http://guides.rubyonrails.org/i18n.html#the-public-i18n-api) field only.
|
|
340
|
-
For example, if `I18n.default_locale` is `:en`, the index will be generated as follows:
|
|
333
|
+
By specifying `localize: true`, the slug index will be created on the [I18n.default_locale](http://guides.rubyonrails.org/i18n.html#the-public-i18n-api) field only. For example, if `I18n.default_locale` is `:en`, the index will be generated as follows:
|
|
341
334
|
|
|
342
335
|
```ruby
|
|
343
336
|
slug :title, localize: true
|
|
@@ -346,8 +339,7 @@ slug :title, localize: true
|
|
|
346
339
|
index({ '_slugs.en' => 1 }, { unique: true, sparse: true })
|
|
347
340
|
```
|
|
348
341
|
|
|
349
|
-
If you are supporting multiple locales, you may specify the list of locales on which
|
|
350
|
-
to create indexes as an `Array`.
|
|
342
|
+
If you are supporting multiple locales, you may specify the list of locales on which to create indexes as an `Array`.
|
|
351
343
|
|
|
352
344
|
```ruby
|
|
353
345
|
slug :title, localize: [:fr, :es, :de]
|
|
@@ -360,7 +352,7 @@ index({ '_slugs.de' => 1 }, { unique: true, sparse: true })
|
|
|
360
352
|
|
|
361
353
|
### Custom Find Strategies
|
|
362
354
|
|
|
363
|
-
By default find will search for the document by the id field if the provided id looks like a `BSON::ObjectId`, and it will otherwise find by the _slugs field. However, custom strategies can
|
|
355
|
+
By default find will search for the document by the id field if the provided id looks like a `BSON::ObjectId`, and it will otherwise find by the _slugs field. However, custom strategies can override the default behavior, like e.g:
|
|
364
356
|
|
|
365
357
|
```ruby
|
|
366
358
|
module Mongoid::Slug::UuidIdStrategy
|
|
@@ -384,7 +376,7 @@ class Entity
|
|
|
384
376
|
end
|
|
385
377
|
```
|
|
386
378
|
|
|
387
|
-
### Adhoc Checking Whether a Slug
|
|
379
|
+
### Adhoc Checking Whether a Slug Is Unique
|
|
388
380
|
|
|
389
381
|
Lets say you want to have a auto-suggest function on your GUI that could provide a preview of what the url or slug could be before the form to create the record was submitted.
|
|
390
382
|
|
|
@@ -393,16 +385,14 @@ You can use the UniqueSlug class in your server side code to do this, e.g.
|
|
|
393
385
|
```ruby
|
|
394
386
|
title = params[:title]
|
|
395
387
|
unique = Mongoid::Slug::UniqueSlug.new(Book.new).find_unique(title)
|
|
396
|
-
...
|
|
388
|
+
# ...
|
|
397
389
|
# return some representation of unique
|
|
398
390
|
```
|
|
399
391
|
|
|
400
|
-
Contributing
|
|
401
|
-
------------
|
|
392
|
+
## Contributing
|
|
402
393
|
|
|
403
|
-
|
|
394
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md), [CHANGELOG.md](CHANGELOG.md), [UPGRADING.md](UPGRADING.md), and [RELEASING.md](RELEASING.md).
|
|
404
395
|
|
|
405
|
-
Copyright & License
|
|
406
|
-
-------------------
|
|
396
|
+
## Copyright & License
|
|
407
397
|
|
|
408
398
|
Copyright (c) 2010-2017 Hakan Ensari & Contributors, see [LICENSE](LICENSE) for details.
|
data/lib/mongoid/slug/version.rb
CHANGED
data/lib/mongoid/slug.rb
CHANGED
|
@@ -197,9 +197,6 @@ module Mongoid
|
|
|
197
197
|
# to find document instead
|
|
198
198
|
return true if new_slug.empty?
|
|
199
199
|
|
|
200
|
-
# avoid duplicate slugs
|
|
201
|
-
_slugs&.delete(new_slug)
|
|
202
|
-
|
|
203
200
|
if !!slug_history && _slugs.is_a?(Array)
|
|
204
201
|
append_slug(new_slug)
|
|
205
202
|
else
|
|
@@ -284,10 +281,12 @@ module Mongoid
|
|
|
284
281
|
# This is necessary for the scenario in which the slugged locale is not yet present
|
|
285
282
|
# but the default locale is. In this situation, self._slugs falls back to the default
|
|
286
283
|
# which is undesired
|
|
287
|
-
current_slugs = _slugs_translations.fetch(I18n.locale.to_s, [])
|
|
284
|
+
current_slugs = _slugs_translations.fetch(I18n.locale.to_s, []).dup
|
|
285
|
+
current_slugs.delete(value)
|
|
288
286
|
current_slugs << value
|
|
289
287
|
self._slugs_translations = _slugs_translations.merge(I18n.locale.to_s => current_slugs)
|
|
290
288
|
else
|
|
289
|
+
_slugs.delete(value)
|
|
291
290
|
_slugs << value
|
|
292
291
|
end
|
|
293
292
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mongoid-slug
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 7.
|
|
4
|
+
version: 7.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andreas Saebjoernsen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: mongoid
|
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
76
76
|
- !ruby/object:Gem::Version
|
|
77
77
|
version: '0'
|
|
78
78
|
requirements: []
|
|
79
|
-
rubygems_version: 3.5.
|
|
79
|
+
rubygems_version: 3.5.22
|
|
80
80
|
signing_key:
|
|
81
81
|
specification_version: 4
|
|
82
82
|
summary: Mongoid URL slugs
|