sluger 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/.gitignore +24 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/generators/sluger/locales_generator.rb +13 -0
- data/lib/generators/templates/ru.yml +73 -0
- data/lib/generators/templates/uk.yml +75 -0
- data/lib/sluger.rb +51 -0
- data/lib/sluger/version.rb +3 -0
- data/sluger.gemspec +25 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9b36771ebb81cf3bef684ec32dc061527ee21ae0
|
4
|
+
data.tar.gz: 21feb450d84edc38b77076e82efa11d51bfec2b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e35775ed9228a1c7f683be817d0228d9dc6043bef15d1bb1e696ec10acaaab57d3f4dc2b2dbe5df2732f1d5faebf0e237fc3556b8db0f094735017e9569b44d2
|
7
|
+
data.tar.gz: 60bee85d7d019631b261bdf7b81b5ead9099e3555aff52d2c52fbec6e36d493c9ba7a160f5519f576912496a270f9d56e3e5aeac7e159c3eebdde4e026d5d142
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.bundle
|
11
|
+
*.so
|
12
|
+
*.o
|
13
|
+
*.a
|
14
|
+
mkmf.log
|
15
|
+
|
16
|
+
spec/dummy/db/*.sqlite3
|
17
|
+
spec/dummy/db/*.sqlite3-journal
|
18
|
+
spec/dummy/log/*.log
|
19
|
+
spec/dummy/tmp/
|
20
|
+
spec/dummy/.sass-cache
|
21
|
+
.idea/
|
22
|
+
*~
|
23
|
+
.DS_Store
|
24
|
+
*.gem
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Eugene Kondratyuk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Sluger
|
2
|
+
|
3
|
+
Sluger is a plugin for generating SEO friendly permalinks. It adds ability to use
|
4
|
+
alphabetic ID for entities instead of numeric ID.
|
5
|
+
|
6
|
+
For instance, without sluger the helper `article_url(article)` returns:
|
7
|
+
|
8
|
+
http://localhost:3000/articles/1
|
9
|
+
|
10
|
+
But with sluger this helper will return:
|
11
|
+
|
12
|
+
http://localhost:3000/articles/seo-friendly-name
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'sluger'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install sluger
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
Add a column which will store role/roles as integer:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
add_column :articles, :slug, :string, null: false, limit: 150
|
36
|
+
add_index :articles, :slug, unique: true
|
37
|
+
```
|
38
|
+
|
39
|
+
And then tell a model that you use this gem functionality:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# app/models/article.rb
|
43
|
+
class Article < ActiveRecord::Base
|
44
|
+
sluger :title, :slug
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
## Options
|
49
|
+
|
50
|
+
For now the gem has only one option - `limit`. It helps to limit a slug's length. If you set
|
51
|
+
the limit all extra characters will be cut.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# app/models/article.rb
|
55
|
+
class Article < ActiveRecord::Base
|
56
|
+
sluger :title, :slug, limit: 150
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
So you can do this:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
article = Article.create!(title: 'Hello World!')
|
64
|
+
article.slug # => hello-world
|
65
|
+
article.title = 'New World!'
|
66
|
+
article.save! # => hello-world
|
67
|
+
article.slug = nil
|
68
|
+
article.save! # => new-world
|
69
|
+
article.to_param # => new-world
|
70
|
+
```
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it ( https://github.com/ekondr/sluger/fork )
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sluger"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Sluger
|
2
|
+
module Generators
|
3
|
+
class LocalesGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../../templates', __FILE__)
|
5
|
+
argument :locale_code, type: :string, default: 'uk'
|
6
|
+
|
7
|
+
desc 'Add localizations for transliterating.'
|
8
|
+
def copy_initializer_file
|
9
|
+
copy_file "#{locale_code.downcase}.yml", "config/locales/sluger.#{locale_code.downcase}.yml"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
ru:
|
2
|
+
i18n:
|
3
|
+
transliterate:
|
4
|
+
rule:
|
5
|
+
А: "A"
|
6
|
+
Б: "B"
|
7
|
+
В: "V"
|
8
|
+
Г: "G"
|
9
|
+
Д: "D"
|
10
|
+
Е: "E"
|
11
|
+
Ё: "E"
|
12
|
+
Ж: "J"
|
13
|
+
З: "Z"
|
14
|
+
И: "I"
|
15
|
+
Й: "Y"
|
16
|
+
К: "K"
|
17
|
+
Л: "L"
|
18
|
+
М: "M"
|
19
|
+
Н: "N"
|
20
|
+
О: "O"
|
21
|
+
П: "P"
|
22
|
+
Р: "R"
|
23
|
+
С: "S"
|
24
|
+
Т: "T"
|
25
|
+
У: "U"
|
26
|
+
Ф: "F"
|
27
|
+
Х: "H"
|
28
|
+
Ц: "C"
|
29
|
+
Ч: "CH"
|
30
|
+
Ш: "SH"
|
31
|
+
Щ: "SCH"
|
32
|
+
Ъ: ""
|
33
|
+
Ы: "Y"
|
34
|
+
Ь: ""
|
35
|
+
Э: "E"
|
36
|
+
Ю: "U"
|
37
|
+
Я: "YA"
|
38
|
+
а: "a"
|
39
|
+
б: "b"
|
40
|
+
в: "v"
|
41
|
+
г: "g"
|
42
|
+
д: "d"
|
43
|
+
е: "e"
|
44
|
+
ё: "e"
|
45
|
+
ж: "j"
|
46
|
+
з: "z"
|
47
|
+
и: "i"
|
48
|
+
й: "y"
|
49
|
+
к: "k"
|
50
|
+
л: "l"
|
51
|
+
м: "m"
|
52
|
+
н: "n"
|
53
|
+
о: "o"
|
54
|
+
п: "p"
|
55
|
+
р: "r"
|
56
|
+
с: "s"
|
57
|
+
т: "t"
|
58
|
+
у: "u"
|
59
|
+
ф: "f"
|
60
|
+
х: "h"
|
61
|
+
ц: "c"
|
62
|
+
ч: "ch"
|
63
|
+
ш: "sh"
|
64
|
+
щ: "sch"
|
65
|
+
ъ: ""
|
66
|
+
ы: "y"
|
67
|
+
ь: ""
|
68
|
+
э: "e"
|
69
|
+
ю: "u"
|
70
|
+
я: "ya"
|
71
|
+
№: "#"
|
72
|
+
—: "-"
|
73
|
+
/: "-"
|
@@ -0,0 +1,75 @@
|
|
1
|
+
uk:
|
2
|
+
i18n:
|
3
|
+
transliterate:
|
4
|
+
rule:
|
5
|
+
А: "A"
|
6
|
+
Б: "B"
|
7
|
+
В: "V"
|
8
|
+
Г: "h"
|
9
|
+
Ѓ: "G"
|
10
|
+
Д: "D"
|
11
|
+
Е: "E"
|
12
|
+
Є: "YE"
|
13
|
+
Ж: "ZH"
|
14
|
+
З: "Z"
|
15
|
+
И: "Y"
|
16
|
+
І: "I"
|
17
|
+
Ї: "YI"
|
18
|
+
Й: "J"
|
19
|
+
К: "K"
|
20
|
+
Л: "L"
|
21
|
+
М: "M"
|
22
|
+
Н: "N"
|
23
|
+
О: "O"
|
24
|
+
П: "P"
|
25
|
+
Р: "R"
|
26
|
+
С: "S"
|
27
|
+
Т: "T"
|
28
|
+
У: "U"
|
29
|
+
Ф: "F"
|
30
|
+
Х: "X"
|
31
|
+
Ц: "C"
|
32
|
+
Ч: "CH"
|
33
|
+
Ш: "SH"
|
34
|
+
Щ: "SHH"
|
35
|
+
Ь: ""
|
36
|
+
Ю: "YU"
|
37
|
+
Я: "YA"
|
38
|
+
а: "a"
|
39
|
+
б: "b"
|
40
|
+
в: "v"
|
41
|
+
г: "h"
|
42
|
+
ѓ: "g"
|
43
|
+
д: "d"
|
44
|
+
е: "e"
|
45
|
+
є: "ye"
|
46
|
+
ж: "zh"
|
47
|
+
з: "z"
|
48
|
+
и: "y"
|
49
|
+
і: "i"
|
50
|
+
ї: "yi"
|
51
|
+
й: "j"
|
52
|
+
к: "k"
|
53
|
+
л: "l"
|
54
|
+
м: "m"
|
55
|
+
н: "n"
|
56
|
+
о: "o"
|
57
|
+
п: "p"
|
58
|
+
р: "r"
|
59
|
+
с: "s"
|
60
|
+
т: "t"
|
61
|
+
у: "u"
|
62
|
+
ф: "f"
|
63
|
+
х: "x"
|
64
|
+
ц: "c"
|
65
|
+
ч: "ch"
|
66
|
+
ш: "sh"
|
67
|
+
щ: "shh"
|
68
|
+
ы: "y"
|
69
|
+
ь: ""
|
70
|
+
э: "e"
|
71
|
+
ю: "yu"
|
72
|
+
я: "ya"
|
73
|
+
№: "#"
|
74
|
+
—: "-"
|
75
|
+
/: "-"
|
data/lib/sluger.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'sluger/version'
|
2
|
+
|
3
|
+
module Sluger
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
class_methods do
|
7
|
+
def sluger(*args)
|
8
|
+
options = { limit: 255 }
|
9
|
+
title_column = args.length > 0 ? args[0] : :title
|
10
|
+
slug_column = args.length > 1 ? args[1] : :slug
|
11
|
+
options.merge!(args[2]) if args.length > 2 && args[2].is_a?(Hash)
|
12
|
+
has_limit = options[:limit] > 0
|
13
|
+
|
14
|
+
self.class_eval do
|
15
|
+
before_validation :process_slugering
|
16
|
+
end
|
17
|
+
|
18
|
+
define_singleton_method(:slugering) do |entity, title|
|
19
|
+
return nil if title.blank?
|
20
|
+
|
21
|
+
new_slug = title.parameterize
|
22
|
+
new_slug = title[0, options[:limit]] if has_limit && new_slug.length > options[:limit]
|
23
|
+
|
24
|
+
pk = self.primary_key
|
25
|
+
relation = self
|
26
|
+
relation = relation.where.not(pk => entity[pk]) unless entity.new_record?
|
27
|
+
|
28
|
+
index = 0
|
29
|
+
base_slug = new_slug
|
30
|
+
while relation.where("#{slug_column} = ?", new_slug).count > 0 do
|
31
|
+
index += 1
|
32
|
+
new_slug = base_slug[0, options[:limit] - index.to_s.length - 1] if has_limit
|
33
|
+
new_slug = "#{new_slug}-#{index}"
|
34
|
+
end
|
35
|
+
new_slug
|
36
|
+
end
|
37
|
+
|
38
|
+
define_method(:process_slugering) do
|
39
|
+
return unless self[slug_column].blank?
|
40
|
+
new_value = self[title_column]
|
41
|
+
self[slug_column] = self.class.send(:slugering, self, new_value)
|
42
|
+
end
|
43
|
+
|
44
|
+
define_method(:to_param) do
|
45
|
+
self[slug_column]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
ActiveRecord::Base.send(:include, Sluger)
|
data/sluger.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'sluger/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'sluger'
|
7
|
+
spec.version = Sluger::VERSION
|
8
|
+
spec.authors = ['Eugene Kondratiuk']
|
9
|
+
spec.email = ['ekondr@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Sluger is a plugin for generating SEO friendly permalinks.'
|
12
|
+
spec.description = 'Sluger is a plugin for generating SEO friendly permalinks. It adds ability to use alphabetic ID for entities instead of numeric ID.'
|
13
|
+
spec.homepage = 'https://github.com/ekondr/sluger'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'rails'
|
22
|
+
spec.add_development_dependency 'sqlite3'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'rspec-rails'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eugene Kondratiuk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-13 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Sluger is a plugin for generating SEO friendly permalinks. It adds ability
|
70
|
+
to use alphabetic ID for entities instead of numeric ID.
|
71
|
+
email:
|
72
|
+
- ekondr@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/generators/sluger/locales_generator.rb
|
87
|
+
- lib/generators/templates/ru.yml
|
88
|
+
- lib/generators/templates/uk.yml
|
89
|
+
- lib/sluger.rb
|
90
|
+
- lib/sluger/version.rb
|
91
|
+
- sluger.gemspec
|
92
|
+
homepage: https://github.com/ekondr/sluger
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Sluger is a plugin for generating SEO friendly permalinks.
|
116
|
+
test_files: []
|