sluggable-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +55 -0
- data/Rakefile +33 -0
- data/lib/sluggable/core_ext.rb +24 -0
- data/lib/sluggable/rails/definition.rb +21 -0
- data/lib/sluggable/rails/definitions.rb +19 -0
- data/lib/sluggable/rails/slug.rb +70 -0
- data/lib/sluggable/rails/slugs.rb +13 -0
- data/lib/sluggable/rails/version.rb +5 -0
- data/lib/sluggable/rails.rb +9 -0
- data/lib/tasks/sluggable/rails_tasks.rake +4 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 524d66a481f56da389dd239d9e1895c11e20740e
|
4
|
+
data.tar.gz: 5758997da24026a3fa780337259ea7b4315ba6bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66477a7d225c4d090f567069bd91974af60e4acc26bb424c16da812ee0bef86afaa54cd1f848408032b2344387108ef8b3e04b2a197b1b315f8eb24dbb670fa7
|
7
|
+
data.tar.gz: 48a5b48fb9b44e51df0753172db659dd6a215280e370368104d0a1f9ec424b608dd67786c9eae28254f6468c93a029f33fa736a53cc04993fe5ee4019ecc30e9
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Julien Dargelos
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Sluggable Rails
|
2
|
+
Provide a unique slug to your records, generated from the attribute of your choice.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
Use the `has_slug` method:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# == Schema Information
|
9
|
+
#
|
10
|
+
# Table name: Post
|
11
|
+
#
|
12
|
+
# id :integer not null, primary key
|
13
|
+
# title :string
|
14
|
+
# slug :string not null
|
15
|
+
# created_at :datetime not null
|
16
|
+
# updated_at :datetime not null
|
17
|
+
#
|
18
|
+
|
19
|
+
class Post < ActiveRecord::Base
|
20
|
+
has_slug by: :title
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
In this example, the slug will be generated from the post title. By default, it will be stored in the slug attribute.
|
25
|
+
You can choose another attribute to store it:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
has_slug :custom_attribute, by: :title
|
29
|
+
```
|
30
|
+
|
31
|
+
The slug is generated with the [`parameterize` method](http://api.rubyonrails.org/v5.1/classes/ActiveSupport/Inflector.html#method-i-parameterize), you can specify a separator as it excepts (default: `'-'`):
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
has_slug :custom_attribute, by: :title, separator: '_'
|
35
|
+
```
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
gem 'sluggable-rails'
|
42
|
+
```
|
43
|
+
|
44
|
+
And then execute:
|
45
|
+
```bash
|
46
|
+
$ bundle
|
47
|
+
```
|
48
|
+
|
49
|
+
Or install it yourself as:
|
50
|
+
```bash
|
51
|
+
$ gem install sluggable-rails
|
52
|
+
```
|
53
|
+
|
54
|
+
## License
|
55
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Sluggable::Rails'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task default: :test
|
@@ -0,0 +1,24 @@
|
|
1
|
+
ActiveRecord::Base.instance_eval do
|
2
|
+
before_save :update_slugs
|
3
|
+
|
4
|
+
def has_slug attribute = :slug, by:, separator: '-'
|
5
|
+
slug_definitions.add attribute, origin: by, separator: separator
|
6
|
+
validates attribute, presence: true, uniqueness: true
|
7
|
+
end
|
8
|
+
|
9
|
+
def slug_definitions
|
10
|
+
@slug_definitions ||= Sluggable::Rails::Definitions.new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Base.class_eval do
|
15
|
+
def slugs
|
16
|
+
self.class.slug_definitions.slugs from: self
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def update_slugs
|
22
|
+
slugs.each { |slug| slug.update if slug.needs_an_update? }
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Sluggable::Rails::Definition
|
2
|
+
attr_reader :attribute, :origin, :separator
|
3
|
+
|
4
|
+
def initialize(attribute = :slug, origin:, separator: '-')
|
5
|
+
self.attribute = attribute
|
6
|
+
self.origin = origin
|
7
|
+
self.separator = separator
|
8
|
+
end
|
9
|
+
|
10
|
+
def attribute=(v)
|
11
|
+
@attribute = v.to_s.to_sym
|
12
|
+
end
|
13
|
+
|
14
|
+
def origin=(v)
|
15
|
+
@origin = v.to_s.to_sym
|
16
|
+
end
|
17
|
+
|
18
|
+
def separator=(v)
|
19
|
+
@separator = v.to_s
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Sluggable::Rails::Definitions
|
2
|
+
def initialize
|
3
|
+
@definitions = {}
|
4
|
+
end
|
5
|
+
|
6
|
+
def [](attribute)
|
7
|
+
@definitions[attribute.to_s.to_sym]
|
8
|
+
end
|
9
|
+
|
10
|
+
def add attribute = :slug, origin:, separator: '-'
|
11
|
+
@definitions[attribute.to_s.to_sym] = Sluggable::Rails::Definition.new attribute, origin: origin, separator: separator
|
12
|
+
end
|
13
|
+
|
14
|
+
def slugs from:
|
15
|
+
slugs = Sluggable::Rails::Slugs.new
|
16
|
+
@definitions.each { |_, definition| slugs.add definition, from }
|
17
|
+
slugs
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
class Sluggable::Rails::Slug < String
|
2
|
+
attr_accessor :definition, :record
|
3
|
+
attr_reader :offset
|
4
|
+
|
5
|
+
def initialize(definition, record)
|
6
|
+
self.definition = definition
|
7
|
+
self.record = record
|
8
|
+
end
|
9
|
+
|
10
|
+
def update
|
11
|
+
if make
|
12
|
+
record.send "#{definition.attribute}=", self.to_s
|
13
|
+
true
|
14
|
+
else
|
15
|
+
false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def make
|
20
|
+
if makeable?
|
21
|
+
@offset = 0
|
22
|
+
generate until present? && unique?
|
23
|
+
true
|
24
|
+
else
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def base
|
30
|
+
String.try_convert(record.send(definition.origin)).to_s.parameterize separator: definition.separator
|
31
|
+
end
|
32
|
+
|
33
|
+
def makeable?
|
34
|
+
definition.is_a?(Sluggable::Rails::Definition) && record.is_a?(ActiveRecord::Base)
|
35
|
+
end
|
36
|
+
|
37
|
+
def shifted?
|
38
|
+
!offset.zero?
|
39
|
+
end
|
40
|
+
|
41
|
+
def changed?
|
42
|
+
record.send "#{definition.origin}_changed?"
|
43
|
+
end
|
44
|
+
|
45
|
+
def undefined?
|
46
|
+
record.send(definition.attribute).blank?
|
47
|
+
end
|
48
|
+
|
49
|
+
def needs_an_update?
|
50
|
+
undefined? || changed?
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
def generate
|
56
|
+
clear
|
57
|
+
insert 0, base
|
58
|
+
insert -1, (empty? ? '' : definition.separator) + offset.to_s if shifted?
|
59
|
+
shift
|
60
|
+
end
|
61
|
+
|
62
|
+
def shift
|
63
|
+
@offset += 1
|
64
|
+
end
|
65
|
+
|
66
|
+
def unique?
|
67
|
+
found = record.class.find_by definition.attribute => self.to_s
|
68
|
+
!found || found.id == record.id
|
69
|
+
end
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Dargelos
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-15 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.1.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.1.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Provide a unique slug to your records, generated from the attribute of
|
42
|
+
your choice.
|
43
|
+
email:
|
44
|
+
- contact@juliendargelos.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/sluggable/core_ext.rb
|
53
|
+
- lib/sluggable/rails.rb
|
54
|
+
- lib/sluggable/rails/definition.rb
|
55
|
+
- lib/sluggable/rails/definitions.rb
|
56
|
+
- lib/sluggable/rails/slug.rb
|
57
|
+
- lib/sluggable/rails/slugs.rb
|
58
|
+
- lib/sluggable/rails/version.rb
|
59
|
+
- lib/tasks/sluggable/rails_tasks.rake
|
60
|
+
homepage: https://www.github.com/juliendargelos/sluggable-rails
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.6.13
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Slugify your rails records.
|
84
|
+
test_files: []
|