sluggable_rubby 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sluggable_rubby/version.rb +1 -1
- data/lib/sluggable_rubby.rb +20 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 557a6a4a9989461919e3b3faa0965cb8c2230c185bd6cf1d1fedd31e7a3b0329
|
4
|
+
data.tar.gz: 781db1051775f793f6cf6aa28b8278947e5842b3bc1c13ae4d4c43d2525ae366
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83c948014c9390b1528df874765b8bc62d719c10ac8366008594b2a8d3906bfc1ed199eae0833aef5736af9eef0c021298d1512e747f515b3b19f4a4dd549267
|
7
|
+
data.tar.gz: 48dea71b88aa81c173500246bf95bac74be4c5155781033c06f6914c4e36683d406b92ffe5e601a1a6bfaf84bddd46803208af37493ed592b1decd4e4e7116e7
|
data/lib/sluggable_rubby.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
require_relative "sluggable_rubby/version"
|
4
2
|
|
5
3
|
module SluggableRubby
|
@@ -9,37 +7,36 @@ module SluggableRubby
|
|
9
7
|
end
|
10
8
|
|
11
9
|
module ClassMethods
|
12
|
-
def has_slug(
|
13
|
-
define_method(:generate_slug) do
|
14
|
-
return unless respond_to?(attribute) && send(attribute).present?
|
15
|
-
|
16
|
-
base_slug = send(attribute).parameterize
|
17
|
-
self.slug = unique_slug(base_slug)
|
18
|
-
end
|
19
|
-
|
10
|
+
def has_slug(*attributes, scope: nil)
|
20
11
|
before_validation :generate_slug
|
21
|
-
|
12
|
+
|
13
|
+
validates :slug, presence: true, uniqueness: { scope: scope }
|
22
14
|
|
23
15
|
define_singleton_method :find_by_slug do |slug|
|
24
16
|
find_by(slug: slug)
|
25
17
|
end
|
18
|
+
|
19
|
+
define_method(:generate_slug) do
|
20
|
+
return unless attributes.any? { |attr| !send(attr).nil? }
|
21
|
+
|
22
|
+
base_slug = attributes.map { |attr| send(attr).to_s.parameterize }.join('-').truncate(100, omission: '').downcase
|
23
|
+
self.slug = self.class.unique_slug(base_slug, scope, scope ? self.send(scope) : nil)
|
24
|
+
end
|
26
25
|
end
|
27
|
-
end
|
28
26
|
|
29
|
-
|
27
|
+
def unique_slug(base_slug, scope, scope_value)
|
28
|
+
slug = base_slug
|
29
|
+
count = 1
|
30
30
|
|
31
|
-
|
32
|
-
slug = base_slug
|
33
|
-
count = 1
|
31
|
+
relation = scope ? self.where(scope => scope_value) : self
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
while relation.exists?(slug: slug)
|
34
|
+
slug = "#{base_slug}-#{count}"
|
35
|
+
count += 1
|
36
|
+
end
|
39
37
|
|
40
|
-
|
38
|
+
slug
|
39
|
+
end
|
41
40
|
end
|
42
41
|
end
|
43
42
|
end
|
44
|
-
|
45
|
-
ActiveRecord::Base.include(SluggableRubby::ActiveRecord)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sluggable_rubby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Techvoot solutions
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|