schnecke 0.1.0 → 0.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/Gemfile.lock +1 -1
- data/README.md +23 -2
- data/lib/schnecke/schnecke.rb +14 -0
- data/lib/schnecke/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: 144b6900260d2c6d5d9f293cee58c0ceb25ec663ff00c77bf1d2d72f9d79fc78
|
|
4
|
+
data.tar.gz: 6332f95d24fcc612fd1be9147f2e816c55a9484c4ac2582e9be21691902272e9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 906f07f820ffe12443424f5c0e1427a5b942537911a6026f6b9644b3acdd40a817227642f86b7d01123bac0beeaed31eebf522b6e7442206cf3b16c6e4b0444e
|
|
7
|
+
data.tar.gz: 61f35e4e5322dd7a7fc967c093697150e56d1d2c17b8f15890bf262ffdeec9306ebb7790748e06691b082ec9ac006bd163a427b4e963363fa0affbbdb8ff2e9b
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -53,6 +53,25 @@ end
|
|
|
53
53
|
|
|
54
54
|
The above will place the generated slug in `some_other_column`.
|
|
55
55
|
|
|
56
|
+
### Setting the maximum length of a slug
|
|
57
|
+
|
|
58
|
+
By default the maxium length of a slug is 32 characters *NOT INCLUDING* any potential sequence numbers added to make it unique (see the "Handling non-unique slugs" section). You can either change the maximum or remove it entirely as follows
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
class A
|
|
63
|
+
include Schnecke
|
|
64
|
+
slug :name, limit_length: 15
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
class A
|
|
70
|
+
include Schnecke
|
|
71
|
+
slug :name, limit_length: false
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
56
75
|
### Slug Uniquness
|
|
57
76
|
|
|
58
77
|
By default slugs are unique to the object that defines the slug. For example if we have the 2 objects, `A` and `B` as defined as below, then the slugs will be unique for all slugs for all type `A` objcets and all type `B` objects.
|
|
@@ -75,6 +94,8 @@ This means that the slug `foo` can exists 2 times; once for any object of type `
|
|
|
75
94
|
|
|
76
95
|
If a duplicate slug is to be created, a number is automatically appended to the end of the slug. For example, if there is a slug `foo`, the second one would become `foo-2`, the third `foo-3`, and so forth.
|
|
77
96
|
|
|
97
|
+
It is important to note that the maximum length of a slug does not include the addition of the sequence identifier at the end. By default the maximum length of a slug is 32 characters, but if a sequence number is added, it will be 34 characters when we append the `-2`, `-3`, etc. This was done on purpose so that the base slug always remains constant and does not get truncated.
|
|
98
|
+
|
|
78
99
|
### Defining a custom uniqueness scope
|
|
79
100
|
|
|
80
101
|
There are times when we want slugs not be unique for all objects of type `A`, but rather for a smaller scope. For example, let's say we have a system with multiple `Accounts`, each containing `Record`s. If we want the slug for the `Record` to be unique only within the scope of an `account` we can do by providing the uniqueness scope when setting up the slug.
|
|
@@ -82,7 +103,7 @@ There are times when we want slugs not be unique for all objects of type `A`, bu
|
|
|
82
103
|
```ruby
|
|
83
104
|
class Record
|
|
84
105
|
include Schnecke
|
|
85
|
-
slug :name, uniqueness: { scope: :account}
|
|
106
|
+
slug :name, uniqueness: { scope: :account }
|
|
86
107
|
|
|
87
108
|
belongs_to :account
|
|
88
109
|
end
|
|
@@ -93,7 +114,7 @@ When we do this, this will let us have the same slug 'foo' for multiple `record`
|
|
|
93
114
|
```ruby
|
|
94
115
|
class Tag
|
|
95
116
|
include Schnecke
|
|
96
|
-
slug :name, uniqueness: { scope: [:account, :record]}
|
|
117
|
+
slug :name, uniqueness: { scope: [:account, :record] }
|
|
97
118
|
|
|
98
119
|
belongs_to :account
|
|
99
120
|
belongs_to :record
|
data/lib/schnecke/schnecke.rb
CHANGED
|
@@ -10,9 +10,11 @@ module Schnecke
|
|
|
10
10
|
|
|
11
11
|
DEFAULT_SLUG_COLUMN = :slug
|
|
12
12
|
DEFAULT_SLUG_SEPARATOR = '-'
|
|
13
|
+
DEFAULT_MAX_LENGTH = 32
|
|
13
14
|
DEFAULT_REQUIRED_FORMAT = /\A[a-z0-9\-_]+\z/
|
|
14
15
|
|
|
15
16
|
class_methods do
|
|
17
|
+
# rubocop:disable Metrics/AbcSize
|
|
16
18
|
def slug(source, opts = {})
|
|
17
19
|
class_attribute :schnecke_config
|
|
18
20
|
|
|
@@ -21,6 +23,7 @@ module Schnecke
|
|
|
21
23
|
slug_source: source,
|
|
22
24
|
slug_column: opts.fetch(:column, DEFAULT_SLUG_COLUMN),
|
|
23
25
|
slug_separator: opts.fetch(:separator, DEFAULT_SLUG_SEPARATOR),
|
|
26
|
+
limit_length: opts.fetch(:limit_length, DEFAULT_MAX_LENGTH),
|
|
24
27
|
required: opts.fetch(:required, true),
|
|
25
28
|
generate_on_blank: opts.fetch(:generate_on_blank, true),
|
|
26
29
|
require_format: opts.fetch(:require_format, DEFAULT_REQUIRED_FORMAT),
|
|
@@ -50,6 +53,7 @@ module Schnecke
|
|
|
50
53
|
include InstanceMethods
|
|
51
54
|
end
|
|
52
55
|
end
|
|
56
|
+
# rubocop:enable Metrics/AbcSize
|
|
53
57
|
|
|
54
58
|
# Instance methods to include
|
|
55
59
|
module InstanceMethods
|
|
@@ -74,6 +78,9 @@ module Schnecke
|
|
|
74
78
|
candidate_slug = slugify_blank
|
|
75
79
|
end
|
|
76
80
|
|
|
81
|
+
# Make sure it is not too long
|
|
82
|
+
candidate_slug = truncate(candidate_slug)
|
|
83
|
+
|
|
77
84
|
# If there is a duplicate, create a unique one
|
|
78
85
|
if slug_exists?(candidate_slug)
|
|
79
86
|
candidate_slug = slugify_duplicate(candidate_slug)
|
|
@@ -178,6 +185,13 @@ module Schnecke
|
|
|
178
185
|
parts.join(schnecke_config[:slug_separator])
|
|
179
186
|
end
|
|
180
187
|
|
|
188
|
+
def truncate(slug)
|
|
189
|
+
return if slug.blank?
|
|
190
|
+
return if schnecke_config[:limit_length].blank?
|
|
191
|
+
|
|
192
|
+
slug[0, schnecke_config[:limit_length]]
|
|
193
|
+
end
|
|
194
|
+
|
|
181
195
|
def slug_exists?(slug)
|
|
182
196
|
slug_scope.exists?(schnecke_config[:slug_column] => slug)
|
|
183
197
|
end
|
data/lib/schnecke/version.rb
CHANGED