simple_slug 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_slug/model_addition.rb +52 -18
- data/lib/simple_slug/version.rb +1 -1
- data/lib/simple_slug.rb +4 -0
- data/spec/simple_slug/model_addition_spec.rb +35 -6
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e8cbeedf05abbf945614e72846da70ca8e21379
|
4
|
+
data.tar.gz: 550eada3c913277bbe43a57d64a22d1215d3a483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eab35d261a855fcfb5a95dd626a8faa3447dff6e65fd8f7dace62dbb3e0e16a6f3ed75b14b625bfdd256412ab3bc9a4708b00461c57c9f1401ff3394d9cc0a9b
|
7
|
+
data.tar.gz: 973807508c775c94a059b437a7fc8afee529a2963113d3b65ff4fd561bf2feb58ba919be92646137e9b32dfea809bf91cf683ac9f6d611b6af53f956f0a01fc1
|
@@ -56,15 +56,43 @@ module SimpleSlug
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def should_generate_new_slug?
|
59
|
-
|
59
|
+
return true if simple_slug_options[:history]
|
60
|
+
return simple_slug_get.blank? unless simple_slug_options[:locales]
|
61
|
+
simple_slug_options[:locales].any? { |locale| simple_slug_get(locale).blank? }
|
60
62
|
end
|
61
63
|
|
62
64
|
def simple_slug_generate(force=false)
|
65
|
+
(simple_slug_options[:locales] || [nil]).each do |locale|
|
66
|
+
simple_slug_generate_for_locale(locale, force)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def simple_slug_generate_for_locale(locale=nil, force=false)
|
71
|
+
simple_slug_with_locale(locale) do
|
72
|
+
simple_slug = simple_slug_normalize(simple_slug_base)
|
73
|
+
simple_slug = simple_slug.first(simple_slug_options[:max_length]) if simple_slug_options[:max_length]
|
74
|
+
return if !force && simple_slug == simple_slug_get(locale).to_s.sub(/--\d+\z/, '')
|
75
|
+
resolved_simple_slug = simple_slug_resolve(simple_slug, locale)
|
76
|
+
simple_slug_set(resolved_simple_slug, locale)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def simple_slug_with_locale(locale)
|
81
|
+
if defined? Globalize
|
82
|
+
Globalize.with_locale(locale) do
|
83
|
+
I18n.with_locale(locale) { yield }
|
84
|
+
end
|
85
|
+
else
|
86
|
+
I18n.with_locale(locale) { yield }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def simple_slug_generate_(force=false, locale=nil)
|
63
91
|
simple_slug = simple_slug_normalize(simple_slug_base)
|
64
92
|
simple_slug = simple_slug.first(simple_slug_options[:max_length]) if simple_slug_options[:max_length]
|
65
|
-
return true if !force && simple_slug ==
|
66
|
-
resolved_simple_slug = simple_slug_resolve(simple_slug)
|
67
|
-
|
93
|
+
return true if !force && simple_slug == simple_slug_get(locale).to_s.sub(/--\d+\z/, '')
|
94
|
+
resolved_simple_slug = simple_slug_resolve(simple_slug, locale)
|
95
|
+
simple_slug_set(resolved_simple_slug, locale)
|
68
96
|
end
|
69
97
|
|
70
98
|
def simple_slug_base
|
@@ -72,15 +100,17 @@ module SimpleSlug
|
|
72
100
|
end
|
73
101
|
|
74
102
|
def simple_slug_normalize(base)
|
75
|
-
|
103
|
+
base = SimpleSlug.normalize_cyrillic(base)
|
104
|
+
parameterize_args = ActiveSupport::VERSION::MAJOR > 4 ? {separator: '-'} : '-'
|
105
|
+
normalized = I18n.transliterate(base).parameterize(parameterize_args).downcase
|
76
106
|
normalized.to_s =~ SimpleSlug::STARTS_WITH_NUMBER_REGEXP ? "_#{normalized}" : normalized
|
77
107
|
end
|
78
108
|
|
79
|
-
def simple_slug_resolve(slug_value)
|
80
|
-
if simple_slug_exists?(slug_value)
|
109
|
+
def simple_slug_resolve(slug_value, locale=nil)
|
110
|
+
if simple_slug_exists?(slug_value, locale)
|
81
111
|
loop do
|
82
112
|
slug_value_with_suffix = simple_slug_next(slug_value)
|
83
|
-
break slug_value_with_suffix unless simple_slug_exists?(slug_value_with_suffix)
|
113
|
+
break slug_value_with_suffix unless simple_slug_exists?(slug_value_with_suffix, locale)
|
84
114
|
end
|
85
115
|
else
|
86
116
|
slug_value
|
@@ -91,12 +121,12 @@ module SimpleSlug
|
|
91
121
|
"#{slug_value}--#{rand(99999)}"
|
92
122
|
end
|
93
123
|
|
94
|
-
def simple_slug_exists?(slug_value)
|
95
|
-
simple_slug_base_exists?(slug_value) || simple_slug_history_exists?(slug_value)
|
124
|
+
def simple_slug_exists?(slug_value, locale=nil)
|
125
|
+
simple_slug_base_exists?(slug_value, locale) || simple_slug_history_exists?(slug_value)
|
96
126
|
end
|
97
127
|
|
98
|
-
def simple_slug_base_exists?(slug_value)
|
99
|
-
base_scope = self.class.unscoped.where(
|
128
|
+
def simple_slug_base_exists?(slug_value, locale=nil)
|
129
|
+
base_scope = self.class.unscoped.where(simple_slug_column(locale) => slug_value)
|
100
130
|
base_scope = base_scope.where('id != ?', id) if persisted?
|
101
131
|
base_scope.exists?
|
102
132
|
end
|
@@ -108,16 +138,20 @@ module SimpleSlug
|
|
108
138
|
base_scope.exists?
|
109
139
|
end
|
110
140
|
|
111
|
-
def simple_slug_set(value)
|
112
|
-
send "#{
|
141
|
+
def simple_slug_set(value, locale=nil)
|
142
|
+
send "#{simple_slug_column(locale)}=", value
|
143
|
+
end
|
144
|
+
|
145
|
+
def simple_slug_get(locale=nil)
|
146
|
+
send simple_slug_column(locale)
|
113
147
|
end
|
114
148
|
|
115
|
-
def
|
116
|
-
send
|
149
|
+
def simple_slug_stored_slug(locale=nil)
|
150
|
+
send("#{simple_slug_column(locale)}_was")
|
117
151
|
end
|
118
152
|
|
119
|
-
def
|
120
|
-
|
153
|
+
def simple_slug_column(locale=nil)
|
154
|
+
[simple_slug_options[:slug_column], locale].compact.join('_')
|
121
155
|
end
|
122
156
|
end
|
123
157
|
|
data/lib/simple_slug/version.rb
CHANGED
data/lib/simple_slug.rb
CHANGED
@@ -12,6 +12,17 @@ class SlugGenerationRspecModelWithoutCallback < RspecActiveModelBase
|
|
12
12
|
simple_slug :name, callback_type: nil
|
13
13
|
end
|
14
14
|
|
15
|
+
class SlugGenerationRspecModelLocalized < RspecActiveModelBase
|
16
|
+
attr_accessor :slug_en, :name_en
|
17
|
+
alias_method :slug_en_was, :slug_en
|
18
|
+
|
19
|
+
simple_slug :name, locales: [nil, :en]
|
20
|
+
|
21
|
+
def name
|
22
|
+
I18n.locale == :en ? name_en : @name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
15
26
|
describe SimpleSlug::ModelAddition do
|
16
27
|
describe 'slug generation' do
|
17
28
|
before do
|
@@ -39,15 +50,15 @@ describe SimpleSlug::ModelAddition do
|
|
39
50
|
describe 'resolve conflicts' do
|
40
51
|
it 'duplicate slug' do
|
41
52
|
record = SlugGenerationRspecModel.new(name: 'Hi')
|
42
|
-
expect(record).to receive(:simple_slug_exists?).once.ordered.with('hi').and_return(true)
|
43
|
-
expect(record).to receive(:simple_slug_exists?).once.ordered.with(/hi--\d
|
53
|
+
expect(record).to receive(:simple_slug_exists?).once.ordered.with('hi', nil).and_return(true)
|
54
|
+
expect(record).to receive(:simple_slug_exists?).once.ordered.with(/hi--\d+/, nil).and_return(false)
|
44
55
|
record.save
|
45
56
|
expect(record.slug).to start_with('hi--')
|
46
57
|
end
|
47
58
|
|
48
59
|
it 'numeric slug' do
|
49
60
|
record = SlugGenerationRspecModel.new(name: '123')
|
50
|
-
expect(record).to receive(:simple_slug_exists?).with('_123').and_return(false)
|
61
|
+
expect(record).to receive(:simple_slug_exists?).with('_123', nil).and_return(false)
|
51
62
|
record.save
|
52
63
|
expect(record.slug).to eq '_123'
|
53
64
|
end
|
@@ -94,17 +105,23 @@ describe SimpleSlug::ModelAddition do
|
|
94
105
|
end
|
95
106
|
|
96
107
|
it 'cuts slug to max length' do
|
97
|
-
|
108
|
+
record = SlugGenerationRspecModel.new(name: 'Hello' * 100)
|
109
|
+
record.simple_slug_generate
|
110
|
+
expect(record.slug.length).to eq 240
|
98
111
|
end
|
99
112
|
|
100
113
|
it 'use max length from per model options' do
|
101
114
|
SlugGenerationRspecModel.simple_slug_options[:max_length] = 100
|
102
|
-
|
115
|
+
record = SlugGenerationRspecModel.new(name: 'Hello' * 100)
|
116
|
+
record.simple_slug_generate
|
117
|
+
expect(record.slug.length).to eq 100
|
103
118
|
end
|
104
119
|
|
105
120
|
it 'omit max length' do
|
106
121
|
SimpleSlug.max_length = nil
|
107
|
-
|
122
|
+
record = SlugGenerationRspecModel.new(name: 'Hello' * 100)
|
123
|
+
record.simple_slug_generate
|
124
|
+
expect(record.slug.length).to eq 500
|
108
125
|
end
|
109
126
|
end
|
110
127
|
|
@@ -119,4 +136,16 @@ describe SimpleSlug::ModelAddition do
|
|
119
136
|
expect(SlugGenerationRspecModelWithoutCallback.new).not_to receive(:should_generate_new_slug?)
|
120
137
|
end
|
121
138
|
end
|
139
|
+
|
140
|
+
describe 'localized' do
|
141
|
+
before do
|
142
|
+
allow_any_instance_of(SlugGenerationRspecModelLocalized).to receive(:simple_slug_exists?).and_return(false)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'generate slug for locales' do
|
146
|
+
record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
|
147
|
+
expect(record.slug).to eq 'hello'
|
148
|
+
expect(record.slug_en).to eq 'hello-en'
|
149
|
+
end
|
150
|
+
end
|
122
151
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_slug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Leschenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|