permalink 2.0.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/FUNDING.yml +3 -0
- data/.github/workflows/test.yml +44 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +16 -0
- data/CHANGELOG.md +18 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +18 -0
- data/README.md +54 -21
- data/Rakefile +6 -1
- data/lib/permalink/active_record.rb +33 -20
- data/lib/permalink/normalizations/contraction.rb +2 -0
- data/lib/permalink/normalizations/downcase.rb +2 -0
- data/lib/permalink/normalizations/leading_trailing_dashes.rb +2 -0
- data/lib/permalink/normalizations/multiple_dashes.rb +2 -0
- data/lib/permalink/normalizations/non_alphanumeric.rb +2 -0
- data/lib/permalink/normalizations/transliteration.rb +4 -2
- data/lib/permalink/version.rb +4 -2
- data/lib/permalink.rb +21 -7
- data/permalink.gemspec +11 -3
- data/test/permalink/active_record_test.rb +10 -7
- data/test/permalink/normalizations_test.rb +18 -6
- data/test/permalink/permalink_test.rb +15 -9
- data/test/support/post.rb +3 -0
- data/test/support/schema.rb +4 -3
- data/test/support/user.rb +2 -0
- data/test/test_helper.rb +5 -1
- metadata +60 -21
- data/.travis.yml +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2ba3402f7ea2dc013ff8162130c6cb1753d382eb3cbf875a862a5d70b695434a
|
4
|
+
data.tar.gz: 0b03c85505106a762905f31437ea052f1278d7bb559755eda3e5ca1f7eccb00b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f4fb178b99a2884e48c4a08b8e3e6f287eb5072626305aebb0aaf42cacd77945e6b007139a7320a4ff02dea046d2ce7dda5d7ff62205301cf0e37c04525fc64
|
7
|
+
data.tar.gz: 0e40038144ba10a2a757f3cc2684d336b79a9f508a159747071cb27690d69e18aa16a6e67672c51dbabdebd16a53e4f9963ff41a4ee399f97ff844c718a527e9
|
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
---
|
2
|
+
name: test
|
3
|
+
|
4
|
+
on:
|
5
|
+
pull_request_target:
|
6
|
+
push:
|
7
|
+
branches:
|
8
|
+
- main
|
9
|
+
workflow_dispatch:
|
10
|
+
inputs: {}
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
name: test
|
15
|
+
runs-on: "ubuntu-latest"
|
16
|
+
if: |
|
17
|
+
github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target' ||
|
18
|
+
github.actor != 'dependabot[bot]'
|
19
|
+
strategy:
|
20
|
+
fail-fast: false
|
21
|
+
matrix:
|
22
|
+
ruby: ["3.2", "3.3"]
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v4
|
26
|
+
|
27
|
+
- uses: actions/cache@v4
|
28
|
+
with:
|
29
|
+
path: vendor/bundle
|
30
|
+
key: >
|
31
|
+
${{ runner.os }}-${{ matrix.ruby }}-gems
|
32
|
+
|
33
|
+
- name: Set up Ruby
|
34
|
+
uses: ruby/setup-ruby@v1
|
35
|
+
with:
|
36
|
+
ruby-version: ${{ matrix.ruby }}
|
37
|
+
|
38
|
+
- name: Install Ruby dependencies
|
39
|
+
run: |
|
40
|
+
bundle install
|
41
|
+
|
42
|
+
- name: Run tests
|
43
|
+
run: |
|
44
|
+
bundle exec rake
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
---
|
2
|
+
inherit_gem:
|
3
|
+
rubocop-fnando: .rubocop.yml
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.2
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Metrics/MethodLength:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Metrics/ClassLength:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Minitest/EmptyLineBeforeAssertionMethods:
|
16
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
<!--
|
4
|
+
Prefix your message with one of the following:
|
5
|
+
|
6
|
+
- [Added] for new features.
|
7
|
+
- [Changed] for changes in existing functionality.
|
8
|
+
- [Deprecated] for soon-to-be removed features.
|
9
|
+
- [Removed] for now removed features.
|
10
|
+
- [Fixed] for any bug fixes.
|
11
|
+
- [Security] in case of vulnerabilities.
|
12
|
+
-->
|
13
|
+
|
14
|
+
## 3.0.0 - Apr 22, 2024
|
15
|
+
|
16
|
+
- [Changed] Plugin is not activated globally by default anymore; add
|
17
|
+
`Permalink.active_record` to your model or `ApplicationRecord`.
|
18
|
+
- [Changed] Required ruby version is now 3.2 or newer.
|
data/Gemfile
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2011-2024 Nando Vieira, released under the MIT license
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
7
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
8
|
+
subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# Permalink
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![Gem](https://img.shields.io/gem/
|
6
|
-
[![Gem](https://img.shields.io/gem/dt/permalink.svg)](https://rubygems.org/permalink)
|
3
|
+
[![test](https://github.com/fnando/permalink/actions/workflows/test.yml/badge.svg)](https://github.com/fnando/permalink/actions/workflows/test.yml)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/permalink.svg)](https://rubygems.org/gems/permalink)
|
5
|
+
[![Gem](https://img.shields.io/gem/dt/permalink.svg)](https://rubygems.org/gems/permalink)
|
7
6
|
|
8
7
|
## Installation
|
9
8
|
|
@@ -11,10 +10,24 @@
|
|
11
10
|
|
12
11
|
## Usage
|
13
12
|
|
14
|
-
|
13
|
+
> [!NOTE]
|
14
|
+
>
|
15
|
+
> To have the method `permalink` available, you need to inject
|
16
|
+
> `Permalink.active_record` on each model, or use something like your
|
17
|
+
> `ApplicationModel`.
|
15
18
|
|
16
19
|
```ruby
|
17
|
-
class
|
20
|
+
class ApplicationRecord < ActiveRecord::Base
|
21
|
+
primary_abstract_class
|
22
|
+
include Permalink.active_record
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
Add the method call `permalink` to your model. Your model should have a
|
27
|
+
`permalink` attribute.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Page < ApplicationRecord
|
18
31
|
permalink :title
|
19
32
|
end
|
20
33
|
```
|
@@ -22,14 +35,16 @@ end
|
|
22
35
|
You can specify the permalink field:
|
23
36
|
|
24
37
|
```ruby
|
25
|
-
class
|
38
|
+
class Page < ApplicationRecord
|
26
39
|
permalink :title, to: "title_permalink"
|
27
40
|
end
|
28
41
|
```
|
29
42
|
|
30
|
-
If you don't want to use `permalink`, you can call `Permalink.
|
43
|
+
If you don't want to use `permalink`, you can call `Permalink.call("some text")`
|
44
|
+
string method and manage the permalink process by yourself.
|
31
45
|
|
32
|
-
Permalinks are not unique by default. `permalink` overrides `to_param` as
|
46
|
+
Permalinks are not unique by default. `permalink` overrides `to_param` as
|
47
|
+
following:
|
33
48
|
|
34
49
|
```ruby
|
35
50
|
def to_param
|
@@ -40,25 +55,28 @@ end
|
|
40
55
|
You can define the `to_param` format:
|
41
56
|
|
42
57
|
```ruby
|
43
|
-
class Page <
|
58
|
+
class Page < ApplicationRecord
|
44
59
|
permalink :title, to_param: %w(id permalink page)
|
45
60
|
end
|
46
61
|
```
|
47
62
|
|
48
|
-
The above settings will generate something link `100-some-title-page`. By
|
63
|
+
The above settings will generate something link `100-some-title-page`. By
|
64
|
+
overriding `to_param` method you don't have to change a thing on your app
|
65
|
+
routes.
|
49
66
|
|
50
67
|
If you want to generate unique permalink, use the `:unique` option:
|
51
68
|
|
52
69
|
```ruby
|
53
|
-
class Page <
|
70
|
+
class Page < ApplicationRecord
|
54
71
|
permalink :title, unique: true, to_param: "permalink"
|
55
72
|
end
|
56
73
|
```
|
57
74
|
|
58
|
-
The permalink can be tied to a given scope. Let's say you want to have unique
|
75
|
+
The permalink can be tied to a given scope. Let's say you want to have unique
|
76
|
+
permalinks by user. Just set the `:scope` option.
|
59
77
|
|
60
78
|
```ruby
|
61
|
-
class Page <
|
79
|
+
class Page < ApplicationRecord
|
62
80
|
belongs_to :user
|
63
81
|
permalink :title, unique: true, scope: "user_id"
|
64
82
|
end
|
@@ -73,24 +91,39 @@ another_page = another_user.pages.create(title: 'Hello')
|
|
73
91
|
another_page.permalink #=> hello
|
74
92
|
```
|
75
93
|
|
76
|
-
The permalink is generated using `ActiveSupport::Multibyte::Chars` class; this
|
94
|
+
The permalink is generated using `ActiveSupport::Multibyte::Chars` class; this
|
95
|
+
means that characters will properly replaced from `áéíó` to `aeio`, for
|
96
|
+
instance.
|
77
97
|
|
78
|
-
The permalink is created when `before_validation` callback is evaluated. This
|
98
|
+
The permalink is created when `before_validation` callback is evaluated. This
|
99
|
+
plugin also tries to generate a permalink when `before_save` callback is
|
100
|
+
evaluated and the instance has no permalink set.
|
79
101
|
|
80
102
|
You can force the permalink generation by setting the `:force` option.
|
81
103
|
|
82
104
|
```ruby
|
83
|
-
class Page <
|
105
|
+
class Page < ApplicationRecord
|
84
106
|
permalink :title, force: true
|
85
107
|
end
|
86
108
|
```
|
87
109
|
|
88
110
|
## License
|
89
111
|
|
90
|
-
Copyright (c) 2011-
|
112
|
+
Copyright (c) 2011-2024 Nando Vieira, released under the MIT license
|
91
113
|
|
92
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
114
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
115
|
+
this software and associated documentation files (the "Software"), to deal in
|
116
|
+
the Software without restriction, including without limitation the rights to
|
117
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
118
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
119
|
+
subject to the following conditions:
|
93
120
|
|
94
|
-
The above copyright notice and this permission notice shall be included in all
|
121
|
+
The above copyright notice and this permission notice shall be included in all
|
122
|
+
copies or substantial portions of the Software.
|
95
123
|
|
96
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
124
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
125
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
126
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
127
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
128
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
129
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "bundler/gem_tasks"
|
2
4
|
require "rake/testtask"
|
5
|
+
require "rubocop/rake_task"
|
3
6
|
|
4
7
|
Rake::TestTask.new(:test) do |t|
|
5
8
|
t.libs << "test"
|
@@ -7,4 +10,6 @@ Rake::TestTask.new(:test) do |t|
|
|
7
10
|
t.warning = false
|
8
11
|
end
|
9
12
|
|
10
|
-
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Permalink
|
2
4
|
module ActiveRecord
|
3
5
|
def self.included(base)
|
@@ -16,14 +18,14 @@ module Permalink
|
|
16
18
|
def permalink(from_column, options = {})
|
17
19
|
include InstanceMethods
|
18
20
|
|
19
|
-
options = options.reverse_merge(
|
20
|
-
to_param: [
|
21
|
+
options = options.reverse_merge(
|
22
|
+
to_param: %i[id permalink],
|
21
23
|
to: :permalink,
|
22
24
|
unique: false,
|
23
25
|
force: false,
|
24
26
|
separator: "-",
|
25
27
|
normalizations: Permalink::DEFAULT_NORMALIZATIONS
|
26
|
-
|
28
|
+
)
|
27
29
|
|
28
30
|
self.permalink_options = {
|
29
31
|
from_column_name: from_column,
|
@@ -45,14 +47,12 @@ module Permalink
|
|
45
47
|
def to_param
|
46
48
|
to_param_option = permalink_options[:to_param]
|
47
49
|
|
48
|
-
to_param_option.compact.map
|
50
|
+
to_param_option.compact.map do |name|
|
49
51
|
respond_to?(name) ? public_send(name).to_s : name.to_s
|
50
|
-
|
52
|
+
end.reject(&:blank?).join(permalink_options[:separator])
|
51
53
|
end
|
52
54
|
|
53
|
-
private
|
54
|
-
|
55
|
-
def next_available_permalink(permalink)
|
55
|
+
private def next_available_permalink(permalink)
|
56
56
|
unique_permalink = permalink
|
57
57
|
scope = build_scope_for_permalink
|
58
58
|
|
@@ -60,7 +60,9 @@ module Permalink
|
|
60
60
|
suffix = 2
|
61
61
|
|
62
62
|
while scope.where(to_permalink_name => unique_permalink).first
|
63
|
-
unique_permalink =
|
63
|
+
unique_permalink =
|
64
|
+
[permalink, suffix].join(permalink_options[:separator])
|
65
|
+
|
64
66
|
suffix += 1
|
65
67
|
end
|
66
68
|
end
|
@@ -68,42 +70,53 @@ module Permalink
|
|
68
70
|
unique_permalink
|
69
71
|
end
|
70
72
|
|
71
|
-
def build_scope_for_permalink
|
73
|
+
private def build_scope_for_permalink
|
72
74
|
search_scope = permalink_options[:scope]
|
73
75
|
scope = self.class.unscoped
|
74
|
-
|
76
|
+
|
77
|
+
if search_scope
|
78
|
+
scope = scope.where(search_scope => public_send(search_scope))
|
79
|
+
end
|
80
|
+
|
75
81
|
scope
|
76
82
|
end
|
77
83
|
|
78
|
-
def from_permalink_name
|
84
|
+
private def from_permalink_name
|
79
85
|
permalink_options[:from_column_name]
|
80
86
|
end
|
81
87
|
|
82
|
-
def to_permalink_name
|
88
|
+
private def to_permalink_name
|
83
89
|
permalink_options[:to_column_name]
|
84
90
|
end
|
85
91
|
|
86
|
-
def from_permalink_value
|
92
|
+
private def from_permalink_value
|
87
93
|
read_attribute(from_permalink_name)
|
88
94
|
end
|
89
95
|
|
90
|
-
def to_permalink_value
|
96
|
+
private def to_permalink_value
|
91
97
|
read_attribute(to_permalink_name)
|
92
98
|
end
|
93
99
|
|
94
|
-
def update_permalink?
|
100
|
+
private def update_permalink?
|
95
101
|
changes[from_permalink_name] &&
|
96
102
|
(permalink_options[:force] || to_permalink_value.blank?)
|
97
103
|
end
|
98
104
|
|
99
|
-
def create_permalink
|
105
|
+
private def create_permalink
|
106
|
+
return unless update_permalink?
|
107
|
+
|
108
|
+
permalink = Permalink.call(
|
109
|
+
from_permalink_value.to_s,
|
110
|
+
**permalink_generator_options
|
111
|
+
)
|
112
|
+
|
100
113
|
write_attribute(
|
101
114
|
to_permalink_name,
|
102
|
-
next_available_permalink(
|
103
|
-
)
|
115
|
+
next_available_permalink(permalink)
|
116
|
+
)
|
104
117
|
end
|
105
118
|
|
106
|
-
def permalink_generator_options
|
119
|
+
private def permalink_generator_options
|
107
120
|
{
|
108
121
|
separator: permalink_options[:separator],
|
109
122
|
normalizations: permalink_options[:normalizations]
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Permalink
|
2
4
|
module Normalizations
|
3
5
|
module Transliteration
|
4
6
|
def self.call(input, _options = nil)
|
5
|
-
|
6
|
-
.
|
7
|
+
input
|
8
|
+
.unicode_normalize(:nfkd)
|
7
9
|
.gsub(/[^\x00-\x7F]/, "")
|
8
10
|
.to_s
|
9
11
|
end
|
data/lib/permalink/version.rb
CHANGED
data/lib/permalink.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
require "permalink/active_record"
|
3
5
|
require "permalink/normalizations/contraction"
|
@@ -15,15 +17,25 @@ module Permalink
|
|
15
17
|
Normalizations::NonAlphanumeric,
|
16
18
|
Normalizations::MultipleDashes,
|
17
19
|
Normalizations::LeadingTrailingDashes
|
18
|
-
]
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
DEFAULT_SEPARATOR = "-"
|
19
23
|
|
20
24
|
DEFAULT_OPTIONS = {
|
21
25
|
normalizations: DEFAULT_NORMALIZATIONS,
|
22
|
-
separator:
|
23
|
-
}
|
26
|
+
separator: DEFAULT_SEPARATOR
|
27
|
+
}.freeze
|
28
|
+
|
29
|
+
def self.active_record
|
30
|
+
ActiveRecord
|
31
|
+
end
|
24
32
|
|
25
|
-
def self.
|
26
|
-
|
33
|
+
def self.call(
|
34
|
+
input,
|
35
|
+
normalizations: DEFAULT_NORMALIZATIONS,
|
36
|
+
separator: DEFAULT_SEPARATOR
|
37
|
+
)
|
38
|
+
options = DEFAULT_OPTIONS.merge(normalizations:, separator:)
|
27
39
|
|
28
40
|
options[:normalizations].each do |normalization|
|
29
41
|
input = normalization.call(input, options)
|
@@ -31,6 +43,8 @@ module Permalink
|
|
31
43
|
|
32
44
|
input
|
33
45
|
end
|
34
|
-
end
|
35
46
|
|
36
|
-
|
47
|
+
class << self
|
48
|
+
alias generate call
|
49
|
+
end
|
50
|
+
end
|
data/permalink.gemspec
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "./lib/permalink/version"
|
2
4
|
|
3
5
|
Gem::Specification.new do |s|
|
@@ -10,14 +12,20 @@ Gem::Specification.new do |s|
|
|
10
12
|
s.summary = "Generate permalink attributes on ActiveModel/ActiveRecord"
|
11
13
|
s.description = s.summary
|
12
14
|
s.license = "MIT"
|
15
|
+
s.required_ruby_version = Gem::Requirement.new(">= 3.2")
|
16
|
+
s.metadata["rubygems_mfa_required"] = "true"
|
13
17
|
|
14
18
|
s.files = `git ls-files`.split("\n")
|
15
|
-
s.
|
16
|
-
|
19
|
+
s.executables = `git ls-files -- bin/*`
|
20
|
+
.split("\n")
|
21
|
+
.map {|f| File.basename(f) }
|
17
22
|
s.require_paths = ["lib"]
|
18
23
|
|
19
24
|
s.add_dependency "activerecord"
|
20
|
-
s.add_development_dependency "sqlite3"
|
21
25
|
s.add_development_dependency "minitest-utils"
|
22
26
|
s.add_development_dependency "rake"
|
27
|
+
s.add_development_dependency "rubocop"
|
28
|
+
s.add_development_dependency "rubocop-fnando"
|
29
|
+
s.add_development_dependency "simplecov"
|
30
|
+
s.add_development_dependency "sqlite3", "~> 1.4"
|
23
31
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
class ActiveRecordTest < Minitest::Test
|
@@ -9,7 +11,7 @@ class ActiveRecordTest < Minitest::Test
|
|
9
11
|
end
|
10
12
|
|
11
13
|
test "responds to options" do
|
12
|
-
|
14
|
+
assert_respond_to model, :permalink_options
|
13
15
|
end
|
14
16
|
|
15
17
|
test "uses default options" do
|
@@ -61,10 +63,10 @@ class ActiveRecordTest < Minitest::Test
|
|
61
63
|
another_user = User.create!
|
62
64
|
|
63
65
|
# Create posts for user
|
64
|
-
record = model.create(title: "Some nice post", user:
|
66
|
+
record = model.create(title: "Some nice post", user:)
|
65
67
|
assert_equal "some-nice-post", record.permalink
|
66
68
|
|
67
|
-
record = model.create(title: "Some nice post", user:
|
69
|
+
record = model.create(title: "Some nice post", user:)
|
68
70
|
assert_equal "some-nice-post-2", record.permalink
|
69
71
|
|
70
72
|
# Create posts for another user
|
@@ -123,7 +125,7 @@ class ActiveRecordTest < Minitest::Test
|
|
123
125
|
model.permalink :title, force: true
|
124
126
|
|
125
127
|
record = model.create(title: "Some nice post")
|
126
|
-
record.
|
128
|
+
record.update title: "Awesome post"
|
127
129
|
|
128
130
|
assert_equal "awesome-post", record.permalink
|
129
131
|
end
|
@@ -133,7 +135,7 @@ class ActiveRecordTest < Minitest::Test
|
|
133
135
|
|
134
136
|
record = model.create(title: "Some nice post")
|
135
137
|
|
136
|
-
record.
|
138
|
+
record.update title: "Awesome post"
|
137
139
|
assert_equal "awesome-post", record.permalink
|
138
140
|
|
139
141
|
record = model.create(title: "Awesome post")
|
@@ -144,7 +146,7 @@ class ActiveRecordTest < Minitest::Test
|
|
144
146
|
model.permalink :title, force: true, unique: true
|
145
147
|
|
146
148
|
record = model.create(title: "Some nice post")
|
147
|
-
record.
|
149
|
+
record.update description: "some description"
|
148
150
|
|
149
151
|
assert_equal "some-nice-post", record.permalink
|
150
152
|
end
|
@@ -164,7 +166,8 @@ class ActiveRecordTest < Minitest::Test
|
|
164
166
|
end
|
165
167
|
|
166
168
|
test "uses custom normalization" do
|
167
|
-
model.permalink :title,
|
169
|
+
model.permalink :title,
|
170
|
+
normalizations: [->(input, _options) { input.to_s.reverse }]
|
168
171
|
record = model.create(title: "Some nice post")
|
169
172
|
|
170
173
|
assert_equal "Some nice post".reverse, record.permalink
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
class NormalizationsTest < Minitest::Test
|
@@ -7,7 +9,8 @@ class NormalizationsTest < Minitest::Test
|
|
7
9
|
end
|
8
10
|
|
9
11
|
test "applies transliteration" do
|
10
|
-
assert_equal "aeiou",
|
12
|
+
assert_equal "aeiou",
|
13
|
+
Permalink::Normalizations::Transliteration.call("áéíóú")
|
11
14
|
end
|
12
15
|
|
13
16
|
test "applies downcasing" do
|
@@ -15,22 +18,31 @@ class NormalizationsTest < Minitest::Test
|
|
15
18
|
end
|
16
19
|
|
17
20
|
test "applies lead/trailing dashes replacement" do
|
18
|
-
|
21
|
+
permalink = Permalink::Normalizations::LeadingTrailingDashes.call("-test-")
|
22
|
+
assert_equal "test", permalink
|
19
23
|
end
|
20
24
|
|
21
25
|
test "applies multiple dashes replacement" do
|
22
|
-
|
26
|
+
permalink =
|
27
|
+
Permalink::Normalizations::MultipleDashes.call("nice----permalink")
|
28
|
+
assert_equal "nice-permalink", permalink
|
23
29
|
end
|
24
30
|
|
25
31
|
test "applies multiple dashes replacement with custom separator" do
|
26
|
-
|
32
|
+
permalink = Permalink::Normalizations::MultipleDashes
|
33
|
+
.call("nice----permalink", separator: "_")
|
34
|
+
assert_equal "nice_permalink", permalink
|
27
35
|
end
|
28
36
|
|
29
37
|
test "applies non-alphanumeric replacement" do
|
30
|
-
|
38
|
+
permalink =
|
39
|
+
Permalink::Normalizations::NonAlphanumeric.call("nice-permalink!")
|
40
|
+
assert_equal "nice-permalink-", permalink
|
31
41
|
end
|
32
42
|
|
33
43
|
test "applies non-alphanumeric replacement with custom separator" do
|
34
|
-
|
44
|
+
permalink = Permalink::Normalizations::NonAlphanumeric
|
45
|
+
.call("nice-permalink!", separator: "_")
|
46
|
+
assert_equal "nice_permalink_", permalink
|
35
47
|
end
|
36
48
|
end
|
@@ -1,20 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
class PermalinkTest < Minitest::Test
|
4
6
|
{
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
'some-)()()-ExtRa!/// .data==?>
|
10
|
-
|
11
|
-
"Don't Repeat Yourself (DRY)" =>
|
12
|
-
"Text\nwith\nline\n\n\tbreaks" =>
|
7
|
+
"This IS a Tripped out title!!.!1 (well/ not really)" => "this-is-a-tripped-out-title-1-well-not-really", # rubocop:disable Layout/LineLength
|
8
|
+
"////// meph1sto r0x ! \\\\\\" => "meph1sto-r0x",
|
9
|
+
"āčēģīķļņū" => "acegiklnu",
|
10
|
+
"中文測試 chinese text" => "chinese-text",
|
11
|
+
'some-)()()-ExtRa!/// .data==?> to \/\/test' => "some-extra-data-to-test",
|
12
|
+
"http://simplesideias.com.br/tags/" => "http-simplesideias-com-br-tags",
|
13
|
+
"Don't Repeat Yourself (DRY)" => "dont-repeat-yourself-dry",
|
14
|
+
"Text\nwith\nline\n\n\tbreaks" => "text-with-line-breaks",
|
13
15
|
"can't do it" => "cant-do-it",
|
14
16
|
"i'm a dog" => "im-a-dog"
|
15
17
|
}.each do |from, to|
|
16
18
|
test "creates permalink for #{from}" do
|
17
|
-
assert_equal to, Permalink.
|
19
|
+
assert_equal to, Permalink.call(from)
|
18
20
|
end
|
19
21
|
end
|
22
|
+
|
23
|
+
test "returns active record module" do
|
24
|
+
assert_equal Permalink::ActiveRecord, Permalink.active_record
|
25
|
+
end
|
20
26
|
end
|
data/test/support/post.rb
CHANGED
data/test/support/schema.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define(version: 0) do
|
2
4
|
create_table :posts do |t|
|
3
5
|
t.string :title, :permalink, :description, :slug
|
4
6
|
t.belongs_to :user
|
5
7
|
end
|
6
8
|
|
7
|
-
create_table :users
|
8
|
-
end
|
9
|
+
create_table :users
|
9
10
|
end
|
data/test/support/user.rb
CHANGED
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permalink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: minitest-utils
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rubocop
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -66,6 +66,48 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-fnando
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.4'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.4'
|
69
111
|
description: Generate permalink attributes on ActiveModel/ActiveRecord
|
70
112
|
email:
|
71
113
|
- fnando.vieira@gmail.com
|
@@ -73,9 +115,13 @@ executables: []
|
|
73
115
|
extensions: []
|
74
116
|
extra_rdoc_files: []
|
75
117
|
files:
|
118
|
+
- ".github/FUNDING.yml"
|
119
|
+
- ".github/workflows/test.yml"
|
76
120
|
- ".gitignore"
|
77
|
-
- ".
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- CHANGELOG.md
|
78
123
|
- Gemfile
|
124
|
+
- LICENSE.txt
|
79
125
|
- README.md
|
80
126
|
- Rakefile
|
81
127
|
- lib/permalink.rb
|
@@ -98,8 +144,9 @@ files:
|
|
98
144
|
homepage: http://rubygems.org/gems/permalink
|
99
145
|
licenses:
|
100
146
|
- MIT
|
101
|
-
metadata:
|
102
|
-
|
147
|
+
metadata:
|
148
|
+
rubygems_mfa_required: 'true'
|
149
|
+
post_install_message:
|
103
150
|
rdoc_options: []
|
104
151
|
require_paths:
|
105
152
|
- lib
|
@@ -107,23 +154,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
154
|
requirements:
|
108
155
|
- - ">="
|
109
156
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
157
|
+
version: '3.2'
|
111
158
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
159
|
requirements:
|
113
160
|
- - ">="
|
114
161
|
- !ruby/object:Gem::Version
|
115
162
|
version: '0'
|
116
163
|
requirements: []
|
117
|
-
|
118
|
-
|
119
|
-
signing_key:
|
164
|
+
rubygems_version: 3.5.8
|
165
|
+
signing_key:
|
120
166
|
specification_version: 4
|
121
167
|
summary: Generate permalink attributes on ActiveModel/ActiveRecord
|
122
|
-
test_files:
|
123
|
-
- test/permalink/active_record_test.rb
|
124
|
-
- test/permalink/normalizations_test.rb
|
125
|
-
- test/permalink/permalink_test.rb
|
126
|
-
- test/support/post.rb
|
127
|
-
- test/support/schema.rb
|
128
|
-
- test/support/user.rb
|
129
|
-
- test/test_helper.rb
|
168
|
+
test_files: []
|