simple_slug 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ee4149b3d1f301a58ec9bc4ff2000727be10650
4
- data.tar.gz: 8e9554c782d1b5b33187e3848873f0d12b81ca5d
3
+ metadata.gz: 4e372bf7c9afde482560d5e69ed52ce343e0c08e
4
+ data.tar.gz: 3f51178cc10919580d3e167839c93d1ceaa5fedd
5
5
  SHA512:
6
- metadata.gz: 9392260bc6505c78f5044916e93d79dcfb3d0dc1bb64570da75c859fb5932d6034d87438af8c3ade9e911ccb728879a8c7712b4fab28488d19b356d99e7b7948
7
- data.tar.gz: de23e545145e537dce36ca62973648724ef9dd5e748b0998de3cda46a5c10670004dc6f5e6f8a299ca77de997ec045486eef177c3eeb960b10748d1bf3f3bc67
6
+ metadata.gz: e6b3a471ce38ccb23d33ca1be285012b7980d1d5c15f6a982b869c8d348ee879d405728c1fe0eeea80e112b4471a2765e6ceec7b38da3b01c1ea7e1dcd64be6f
7
+ data.tar.gz: 5872ee8fe9b193d425ec3090c403a19fe7fec864f1fdf94f0a6039a65782248bef837600d49ba3b2679299ba6008353ea83bd8d6d749678f2b867d0bde8431ec
@@ -81,12 +81,25 @@ module SimpleSlug
81
81
 
82
82
  def should_generate_new_slug?
83
83
  return true if simple_slug_options[:history]
84
- return simple_slug_get.blank? unless simple_slug_options[:locales]
85
- simple_slug_options[:locales].any? { |locale| simple_slug_get(locale).blank? }
84
+ if simple_slug_options[:locales]
85
+ should_generate_new_slug_for_locales.present?
86
+ else
87
+ simple_slug_get.blank?
88
+ end
89
+ end
90
+
91
+ def should_generate_new_slug_for_locales
92
+ return unless simple_slug_options[:locales]
93
+ simple_slug_options[:locales].find_all {|locale| simple_slug_get(locale).blank?}
86
94
  end
87
95
 
88
96
  def simple_slug_generate(force=false)
89
- (simple_slug_options[:locales] || [nil]).each do |locale|
97
+ locales = if simple_slug_options[:locales]
98
+ ((!force && should_generate_new_slug_for_locales) || simple_slug_options[:locales])
99
+ else
100
+ [nil]
101
+ end
102
+ locales.each do |locale|
90
103
  simple_slug_generate_for_locale(locale, force)
91
104
  end
92
105
  end
@@ -95,6 +108,7 @@ module SimpleSlug
95
108
  simple_slug_with_locale(locale) do
96
109
  simple_slug = simple_slug_normalize(simple_slug_base)
97
110
  simple_slug = simple_slug.first(simple_slug_options[:max_length]) if simple_slug_options[:max_length]
111
+ simple_slug = "__#{id || rand(9999)}" if simple_slug.blank? && simple_slug_options[:fallback_on_blank]
98
112
  return if !force && simple_slug == simple_slug_get(locale).to_s.sub(/--\d+\z/, '')
99
113
  resolved_simple_slug = simple_slug_resolve(simple_slug, locale)
100
114
  simple_slug_set(resolved_simple_slug, locale)
@@ -116,9 +130,8 @@ module SimpleSlug
116
130
  end
117
131
 
118
132
  def simple_slug_normalize(base)
119
- base = SimpleSlug.normalize_cyrillic(base) unless SimpleSlug::CYRILLIC_LOCALES.include?(I18n.locale)
120
133
  parameterize_args = ActiveSupport::VERSION::MAJOR > 4 ? {separator: '-'} : '-'
121
- normalized = I18n.transliterate(base).parameterize(parameterize_args).downcase
134
+ normalized = I18n.transliterate(SimpleSlug.normalize_i18n_fix(base)).parameterize(parameterize_args).downcase
122
135
  normalized.to_s =~ SimpleSlug::STARTS_WITH_NUMBER_REGEXP ? "_#{normalized}" : normalized
123
136
  end
124
137
 
@@ -1,3 +1,3 @@
1
1
  module SimpleSlug
2
- VERSION = '0.3.4'
2
+ VERSION = '0.3.5'
3
3
  end
data/lib/simple_slug.rb CHANGED
@@ -10,7 +10,7 @@ module SimpleSlug
10
10
  @@excludes = %w(new edit show index session login logout sign_in sign_out users admin stylesheets assets javascripts images)
11
11
 
12
12
  mattr_accessor :slug_regexp
13
- @@slug_regexp = /\A\w+[\w\d\-_]*\z/
13
+ @@slug_regexp = /\A(?:\w+[\w\d\-_]*|--\d+)\z/
14
14
 
15
15
  mattr_accessor :slug_column
16
16
  @@slug_column = 'slug'
@@ -26,12 +26,37 @@ module SimpleSlug
26
26
 
27
27
  STARTS_WITH_NUMBER_REGEXP =/\A\d+/
28
28
  CYRILLIC_LOCALES = [:uk, :ru, :be].freeze
29
+ ES_LOCALES = [:es].freeze
30
+
31
+ ES_NORMALIZE_SINGLE = [
32
+ 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåçèéêëìíîïñòóôõöøùúûüýÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıĴĵĶķĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſƒƠơƯưǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǺǻǾǿ',
33
+ 'AAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaceeeeiiiinoooooouuuuyyAaAaAaCcCcCcCcDdDdEeEeEeEeEeGgGgGgGgHhHhIiIiIiIiIiJjKkLlLlLlLlllNnNnNnnOoOoOoRrRrRrSsSsSsSsTtTtTtUuUuUuUuUuUuWwYyYZzZzZzsfOoUuAaIiOoUuUuUuUuUuAaOo'
34
+ ].freeze
35
+
36
+ ES_NORMALIZE_MULTI = [['Æ', 'AE'], ['æ', 'ae'], ['IJ', 'IJ'], ['ij', 'ij'], ['Œ', 'OE'], ['œ', 'oe'], ['Ǽ', 'AE'], ['ǽ', 'ae']].freeze
29
37
 
30
38
  def self.setup
31
39
  yield self
32
40
  end
33
41
 
42
+ def self.normalize_i18n_fix(base, locale=I18n.locale)
43
+ locale_sym = locale.to_sym
44
+ if CYRILLIC_LOCALES.include?(locale_sym)
45
+ normalize_cyrillic(base)
46
+ elsif ES_LOCALES.include?(locale_sym)
47
+ normalize_es(base)
48
+ else
49
+ base
50
+ end
51
+ end
52
+
34
53
  def self.normalize_cyrillic(base)
35
54
  base.tr('АаВЕеіКкМНОоРрСсТуХх', 'AaBEeiKkMHOoPpCcTyXx')
36
55
  end
56
+
57
+ def self.normalize_es(base)
58
+ base = base.tr(*ES_NORMALIZE_SINGLE)
59
+ ES_NORMALIZE_MULTI.each{|d| base.gsub!(*d) }
60
+ base
61
+ end
37
62
  end
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.2'
21
+ spec.add_dependency 'activerecord', '>= 4.0.0', '< 5.3'
22
22
  spec.add_dependency 'i18n', '~> 0.7'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.5'
@@ -12,6 +12,10 @@ class SlugGenerationRspecModelWithoutCallback < RspecActiveModelBase
12
12
  simple_slug :name, callback_type: nil
13
13
  end
14
14
 
15
+ class SlugGenerationRspecModelWithFallbackOnBlank < RspecActiveModelBase
16
+ simple_slug :name, fallback_on_blank: true
17
+ end
18
+
15
19
  class SlugGenerationRspecModelLocalized < RspecActiveModelBase
16
20
  attr_accessor :slug_en, :name_en
17
21
  alias_method :slug_en_was, :slug_en
@@ -27,6 +31,7 @@ describe SimpleSlug::ModelAddition do
27
31
  describe 'slug generation' do
28
32
  before do
29
33
  allow_any_instance_of(SlugGenerationRspecModel). to receive(:simple_slug_exists?).and_return(false)
34
+ allow_any_instance_of(SlugGenerationRspecModelWithFallbackOnBlank). to receive(:simple_slug_exists?).and_return(false)
30
35
  end
31
36
 
32
37
  it 'after save' do
@@ -49,6 +54,17 @@ describe SimpleSlug::ModelAddition do
49
54
  expect(SlugGenerationRspecModel.new(slug: 'test.test')).not_to be_valid
50
55
  end
51
56
 
57
+ it 'es chars' do
58
+ I18n.with_locale(:es) do
59
+ expect(SlugGenerationRspecModel.create(name: 'áǼßHello').slug).to eq 'aaeshello'
60
+ end
61
+ end
62
+
63
+ it 'fallback on blank' do
64
+ SlugGenerationRspecModelWithFallbackOnBlank.create({})
65
+ expect(SlugGenerationRspecModelWithFallbackOnBlank.create({}).slug).to start_with '__'
66
+ end
67
+
52
68
  it 'skip slug generation' do
53
69
  allow_any_instance_of(SlugGenerationRspecModel).to receive(:should_generate_new_slug?).and_return(false)
54
70
  expect(SlugGenerationRspecModel.create(name: 'Hello').slug).to be_blank
@@ -156,6 +172,24 @@ describe SimpleSlug::ModelAddition do
156
172
  expect(record.slug_en).to eq 'hello-en'
157
173
  end
158
174
 
175
+ describe '#should_generate_new_slug?' do
176
+ it 'keep generated slugs' do
177
+ record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
178
+ record.name = 'bye'
179
+ record.slug_en = nil
180
+ record.name_en = 'Bye en'
181
+ expect{ record.save }.not_to change{ record.slug }
182
+ end
183
+
184
+ it 'generate slug for locales with blank slug' do
185
+ record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
186
+ record.name = 'bye'
187
+ record.slug_en = nil
188
+ record.name_en = 'Bye en'
189
+ expect{ record.save }.to change{ record.slug_en }.to('bye-en')
190
+ end
191
+ end
192
+
159
193
  describe '#to_param' do
160
194
  it 'generate not localized for default locale' do
161
195
  record = SlugGenerationRspecModelLocalized.create(name: 'Hello', name_en: 'Hello en')
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
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Leschenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-27 00:00:00.000000000 Z
11
+ date: 2019-01-04 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.2'
22
+ version: '5.3'
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.2'
32
+ version: '5.3'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: i18n
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  version: '0'
131
131
  requirements: []
132
132
  rubyforge_project:
133
- rubygems_version: 2.5.1
133
+ rubygems_version: 2.6.14
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: Friendly url generator with history.