sluggable_rubby 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a43e6821738c616f2af94a9f64f4cec513b2281fa11693c6187f7416d4d6cb75
4
- data.tar.gz: d69aa461f5bffaf84d8979582070904503ecc41a64e14a5bd0145cfe3c15a1f3
3
+ metadata.gz: 557a6a4a9989461919e3b3faa0965cb8c2230c185bd6cf1d1fedd31e7a3b0329
4
+ data.tar.gz: 781db1051775f793f6cf6aa28b8278947e5842b3bc1c13ae4d4c43d2525ae366
5
5
  SHA512:
6
- metadata.gz: e5f19ee96fcf4a2fe43751ace0e09a09d5e9b7ab8be28ea68af5fb258d309a92e53c1f6f5efad43b9cbcd8b9fd876da94020cf9ed0c7a3a6cbff9af04cb80ecd
7
- data.tar.gz: 53118db31d252fd095290302784a94a8afaccea8493701470eff522e15c3b86684672185f22171f86e1e33930c592c9f2f8a755b8623262880f21d55962ad4ce
6
+ metadata.gz: 83c948014c9390b1528df874765b8bc62d719c10ac8366008594b2a8d3906bfc1ed199eae0833aef5736af9eef0c021298d1512e747f515b3b19f4a4dd549267
7
+ data.tar.gz: 48dea71b88aa81c173500246bf95bac74be4c5155781033c06f6914c4e36683d406b92ffe5e601a1a6bfaf84bddd46803208af37493ed592b1decd4e4e7116e7
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SluggableRubby
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -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(attribute = :title)
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
- validates :slug, presence: true, uniqueness: true
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
- private
27
+ def unique_slug(base_slug, scope, scope_value)
28
+ slug = base_slug
29
+ count = 1
30
30
 
31
- def unique_slug(base_slug)
32
- slug = base_slug
33
- count = 1
31
+ relation = scope ? self.where(scope => scope_value) : self
34
32
 
35
- while self.class.exists?(slug: slug)
36
- slug = "#{base_slug}-#{count}"
37
- count += 1
38
- end
33
+ while relation.exists?(slug: slug)
34
+ slug = "#{base_slug}-#{count}"
35
+ count += 1
36
+ end
39
37
 
40
- slug
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.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
- - Bristina Christian
7
+ - Techvoot solutions
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-20 00:00:00.000000000 Z
11
+ date: 2024-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,8 +38,8 @@ homepage: https://github.com/techvootsolutions/rubby-sluggable
38
38
  licenses:
39
39
  - MIT
40
40
  metadata:
41
- owners: Techvoot solution
42
- pushed_by: Bristina Christian
41
+ owners: Techvoot solutions
42
+ pushed_by: Bristina
43
43
  homepage_uri: https://github.com/techvootsolutions/rubby-sluggable
44
44
  source_code_uri: https://github.com/techvootsolutions/rubby-sluggable
45
45
  changelog_uri: https://github.com/techvootsolutions/rubby-sluggable