simple_slug 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/lib/simple_slug.rb +2 -2
- data/lib/simple_slug/model_addition.rb +41 -30
- data/lib/simple_slug/version.rb +1 -1
- data/simple_slug.gemspec +1 -1
- data/spec/simple_slug/base_spec.rb +0 -4
- data/spec/simple_slug/history_spec.rb +1 -1
- data/spec/simple_slug/model_addition_spec.rb +36 -0
- data/spec/spec_helper.rb +1 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ee4149b3d1f301a58ec9bc4ff2000727be10650
|
4
|
+
data.tar.gz: 8e9554c782d1b5b33187e3848873f0d12b81ca5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9392260bc6505c78f5044916e93d79dcfb3d0dc1bb64570da75c859fb5932d6034d87438af8c3ade9e911ccb728879a8c7712b4fab28488d19b356d99e7b7948
|
7
|
+
data.tar.gz: de23e545145e537dce36ca62973648724ef9dd5e748b0998de3cda46a5c10670004dc6f5e6f8a299ca77de997ec045486eef177c3eeb960b10748d1bf3f3bc67
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/lib/simple_slug.rb
CHANGED
@@ -9,8 +9,8 @@ module SimpleSlug
|
|
9
9
|
mattr_accessor :excludes
|
10
10
|
@@excludes = %w(new edit show index session login logout sign_in sign_out users admin stylesheets assets javascripts images)
|
11
11
|
|
12
|
-
mattr_accessor :
|
13
|
-
@@
|
12
|
+
mattr_accessor :slug_regexp
|
13
|
+
@@slug_regexp = /\A\w+[\w\d\-_]*\z/
|
14
14
|
|
15
15
|
mattr_accessor :slug_column
|
16
16
|
@@slug_column = 'slug'
|
@@ -11,6 +11,7 @@ module SimpleSlug
|
|
11
11
|
self.simple_slug_options = options.reverse_merge(
|
12
12
|
slug_column: SimpleSlug.slug_column,
|
13
13
|
slug_method: args,
|
14
|
+
slug_regexp: SimpleSlug.slug_regexp,
|
14
15
|
max_length: SimpleSlug.max_length,
|
15
16
|
callback_type: SimpleSlug.callback_type,
|
16
17
|
add_validation: SimpleSlug.add_validation
|
@@ -22,10 +23,12 @@ module SimpleSlug
|
|
22
23
|
send(simple_slug_options[:callback_type], :simple_slug_generate, if: :should_generate_new_slug?) if simple_slug_options[:callback_type]
|
23
24
|
|
24
25
|
if simple_slug_options[:add_validation]
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
simple_slug_locales.each do |locale|
|
27
|
+
validates simple_slug_column(locale),
|
28
|
+
presence: true,
|
29
|
+
exclusion: {in: SimpleSlug.excludes},
|
30
|
+
format: {with: simple_slug_options[:slug_regexp]}
|
31
|
+
end
|
29
32
|
end
|
30
33
|
|
31
34
|
if simple_slug_options[:history]
|
@@ -43,11 +46,32 @@ module SimpleSlug
|
|
43
46
|
find(id_param)
|
44
47
|
else
|
45
48
|
finder_method = simple_slug_options[:history] ? :find_by : :find_by!
|
46
|
-
send(finder_method,
|
49
|
+
send(finder_method, simple_slug_column => id_param) or find(::SimpleSlug::HistorySlug.find_by!(slug: id_param).sluggable_id)
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
50
53
|
alias_method :friendly_find, :simple_slug_find
|
54
|
+
|
55
|
+
def simple_slug_column(locale=I18n.locale)
|
56
|
+
if simple_slug_localized?(locale)
|
57
|
+
[simple_slug_options[:slug_column], locale].compact.join('_')
|
58
|
+
else
|
59
|
+
simple_slug_options[:slug_column]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def simple_slug_columns
|
64
|
+
simple_slug_locales.map{|locale| simple_slug_column(locale) }
|
65
|
+
end
|
66
|
+
|
67
|
+
def simple_slug_locales
|
68
|
+
Array(simple_slug_options[:locales] || [nil])
|
69
|
+
end
|
70
|
+
|
71
|
+
def simple_slug_localized?(locale=I18n.locale)
|
72
|
+
return unless locale
|
73
|
+
simple_slug_locales.include?(locale.to_sym)
|
74
|
+
end
|
51
75
|
end
|
52
76
|
|
53
77
|
module InstanceMethods
|
@@ -67,7 +91,7 @@ module SimpleSlug
|
|
67
91
|
end
|
68
92
|
end
|
69
93
|
|
70
|
-
def simple_slug_generate_for_locale(locale=
|
94
|
+
def simple_slug_generate_for_locale(locale=I18n.locale, force=false)
|
71
95
|
simple_slug_with_locale(locale) do
|
72
96
|
simple_slug = simple_slug_normalize(simple_slug_base)
|
73
97
|
simple_slug = simple_slug.first(simple_slug_options[:max_length]) if simple_slug_options[:max_length]
|
@@ -87,14 +111,6 @@ module SimpleSlug
|
|
87
111
|
end
|
88
112
|
end
|
89
113
|
|
90
|
-
def simple_slug_generate_(force=false, locale=nil)
|
91
|
-
simple_slug = simple_slug_normalize(simple_slug_base)
|
92
|
-
simple_slug = simple_slug.first(simple_slug_options[:max_length]) if simple_slug_options[:max_length]
|
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)
|
96
|
-
end
|
97
|
-
|
98
114
|
def simple_slug_base
|
99
115
|
simple_slug_options[:slug_method].map{|m| send(m).to_s }.reject(&:blank?).join(' ')
|
100
116
|
end
|
@@ -106,7 +122,7 @@ module SimpleSlug
|
|
106
122
|
normalized.to_s =~ SimpleSlug::STARTS_WITH_NUMBER_REGEXP ? "_#{normalized}" : normalized
|
107
123
|
end
|
108
124
|
|
109
|
-
def simple_slug_resolve(slug_value, locale=
|
125
|
+
def simple_slug_resolve(slug_value, locale=I18n.locale)
|
110
126
|
if simple_slug_exists?(slug_value, locale)
|
111
127
|
loop do
|
112
128
|
slug_value_with_suffix = simple_slug_next(slug_value)
|
@@ -121,12 +137,12 @@ module SimpleSlug
|
|
121
137
|
"#{slug_value}--#{rand(99999)}"
|
122
138
|
end
|
123
139
|
|
124
|
-
def simple_slug_exists?(slug_value, locale=
|
140
|
+
def simple_slug_exists?(slug_value, locale=I18n.locale)
|
125
141
|
simple_slug_base_exists?(slug_value, locale) || simple_slug_history_exists?(slug_value)
|
126
142
|
end
|
127
143
|
|
128
|
-
def simple_slug_base_exists?(slug_value, locale=
|
129
|
-
base_scope = self.class.unscoped.where(simple_slug_column(locale) => slug_value)
|
144
|
+
def simple_slug_base_exists?(slug_value, locale=I18n.locale)
|
145
|
+
base_scope = self.class.unscoped.where(self.class.simple_slug_column(locale) => slug_value)
|
130
146
|
base_scope = base_scope.where('id != ?', id) if persisted?
|
131
147
|
base_scope.exists?
|
132
148
|
end
|
@@ -138,20 +154,16 @@ module SimpleSlug
|
|
138
154
|
base_scope.exists?
|
139
155
|
end
|
140
156
|
|
141
|
-
def simple_slug_set(value, locale=
|
142
|
-
send "#{simple_slug_column(locale)}=", value
|
143
|
-
end
|
144
|
-
|
145
|
-
def simple_slug_get(locale=nil)
|
146
|
-
send simple_slug_column(locale)
|
157
|
+
def simple_slug_set(value, locale=I18n.locale)
|
158
|
+
send "#{self.class.simple_slug_column(locale)}=", value
|
147
159
|
end
|
148
160
|
|
149
|
-
def
|
150
|
-
send
|
161
|
+
def simple_slug_get(locale=I18n.locale)
|
162
|
+
send self.class.simple_slug_column(locale)
|
151
163
|
end
|
152
164
|
|
153
|
-
def
|
154
|
-
|
165
|
+
def simple_slug_stored_slug(locale=I18n.locale)
|
166
|
+
send("#{self.class.simple_slug_column(locale)}_was")
|
155
167
|
end
|
156
168
|
end
|
157
169
|
|
@@ -167,9 +179,8 @@ module SimpleSlug
|
|
167
179
|
|
168
180
|
def simple_slug_create_history_slug
|
169
181
|
return true unless slug_changed?
|
170
|
-
::SimpleSlug::HistorySlug.where(sluggable_type: self.class.name,
|
182
|
+
::SimpleSlug::HistorySlug.where(sluggable_type: self.class.name, slug: simple_slug_get).first_or_create{|hs| hs.sluggable_id = id }
|
171
183
|
end
|
172
184
|
end
|
173
|
-
|
174
185
|
end
|
175
186
|
end
|
data/lib/simple_slug/version.rb
CHANGED
data/simple_slug.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_dependency 'activerecord', '>= 4.0.0', '< 5.
|
21
|
+
spec.add_dependency 'activerecord', '>= 4.0.0', '< 5.2'
|
22
22
|
spec.add_dependency 'i18n', '~> 0.7'
|
23
23
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
@@ -12,7 +12,7 @@ describe 'slug history' do
|
|
12
12
|
|
13
13
|
it 'create' do
|
14
14
|
relation = double
|
15
|
-
expect(::SimpleSlug::HistorySlug).to receive(:where).once.ordered.with(sluggable_type: 'SlugHistoryRspecModel',
|
15
|
+
expect(::SimpleSlug::HistorySlug).to receive(:where).once.ordered.with(sluggable_type: 'SlugHistoryRspecModel', slug: 'hello').and_return(relation)
|
16
16
|
expect(relation).to receive(:first_or_create)
|
17
17
|
SlugHistoryRspecModel.create(id: 1, name: 'Hello')
|
18
18
|
end
|
@@ -41,6 +41,14 @@ describe SimpleSlug::ModelAddition do
|
|
41
41
|
expect(SlugGenerationRspecModel.new(name: '123')).not_to be_valid
|
42
42
|
end
|
43
43
|
|
44
|
+
it 'skip spaces' do
|
45
|
+
expect(SlugGenerationRspecModel.new(slug: 'test test')).not_to be_valid
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'skip punctuation' do
|
49
|
+
expect(SlugGenerationRspecModel.new(slug: 'test.test')).not_to be_valid
|
50
|
+
end
|
51
|
+
|
44
52
|
it 'skip slug generation' do
|
45
53
|
allow_any_instance_of(SlugGenerationRspecModel).to receive(:should_generate_new_slug?).and_return(false)
|
46
54
|
expect(SlugGenerationRspecModel.create(name: 'Hello').slug).to be_blank
|
@@ -147,5 +155,33 @@ describe SimpleSlug::ModelAddition do
|
|
147
155
|
expect(record.slug).to eq 'hello'
|
148
156
|
expect(record.slug_en).to eq 'hello-en'
|
149
157
|
end
|
158
|
+
|
159
|
+
describe '#to_param' do
|
160
|
+
it 'generate not localized for default locale' do
|
161
|
+
record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
|
162
|
+
expect(record.to_param).to eq 'hello'
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'generate localized' do
|
166
|
+
record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
|
167
|
+
I18n.with_locale(:en) do
|
168
|
+
expect(record.to_param).to eq 'hello-en'
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe '#simple_slug_find' do
|
174
|
+
it 'use default slug column with default locale' do
|
175
|
+
record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
|
176
|
+
expect(SlugGenerationRspecModelLocalized).to receive(:find_by!).with('slug' => 'hello').and_return(record)
|
177
|
+
SlugGenerationRspecModelLocalized.simple_slug_find('hello')
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'use localized slug column' do
|
181
|
+
record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
|
182
|
+
expect(SlugGenerationRspecModelLocalized).to receive(:find_by!).with('slug_en' => 'hello-en').and_return(record)
|
183
|
+
I18n.with_locale(:en) { SlugGenerationRspecModelLocalized.simple_slug_find('hello-en') }
|
184
|
+
end
|
185
|
+
end
|
150
186
|
end
|
151
187
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Leschenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: 4.0.0
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5.
|
22
|
+
version: '5.2'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: 4.0.0
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
32
|
+
version: '5.2'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: i18n
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|