sluggable_rubby 0.1.0
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 +7 -0
- data/lib/sluggable_rubby/version.rb +5 -0
- data/lib/sluggable_rubby.rb +45 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c0475d354fedd013ce8ec621866415db0ac7d61a6722aecc37a4af09063e47c1
|
4
|
+
data.tar.gz: 48a279f9f16a1f911780d0f29dff05e562f2b9c788826ea128225787471a216a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c89247b6a51285666e788ced9dc127a357febd180a81d815855bc155f374d2499b951b953c2da298c244cc94a34c1114dddf37f64249353de98d6a7a57ec8cdf
|
7
|
+
data.tar.gz: 0edc4ea9cafad68722d0e1d19911d87df8709c61b27ac8daafb97a6c7d77f802c0bc4710d2e486f18e8dacc8ed1dcf5a4e8b38b9f47b3f7ffe82ee463e820581
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "sluggable_rubby/version"
|
4
|
+
|
5
|
+
module SluggableRubby
|
6
|
+
module ActiveRecord
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
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
|
+
|
20
|
+
before_validation :generate_slug
|
21
|
+
validates :slug, presence: true, uniqueness: true
|
22
|
+
|
23
|
+
define_singleton_method :find_by_slug do |slug|
|
24
|
+
find_by(slug: slug)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def unique_slug(base_slug)
|
32
|
+
slug = base_slug
|
33
|
+
count = 1
|
34
|
+
|
35
|
+
while self.class.exists?(slug: slug)
|
36
|
+
slug = "#{base_slug}-#{count}"
|
37
|
+
count += 1
|
38
|
+
end
|
39
|
+
|
40
|
+
slug
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
ActiveRecord::Base.include(SluggableRubby::ActiveRecord)
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_rubby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bristina Christian
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
description: Adjust the has_slug method in your models accordingly to specify the
|
28
|
+
attribute for generating the slug
|
29
|
+
email:
|
30
|
+
- cbristina999@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/sluggable_rubby.rb
|
36
|
+
- lib/sluggable_rubby/version.rb
|
37
|
+
homepage: https://github.com/techvootsolutions/rubby-sluggable
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata:
|
41
|
+
homepage_uri: https://github.com/techvootsolutions/rubby-sluggable
|
42
|
+
source_code_uri: https://github.com/techvootsolutions/rubby-sluggable
|
43
|
+
changelog_uri: https://github.com/techvootsolutions/rubby-sluggable
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.6.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.4.1
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: This creates a gem structure that can be used to generate unique slugs for
|
63
|
+
ActiveRecord models in a Rails application based on specified attributes
|
64
|
+
test_files: []
|