permalink_for 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 +15 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/README.md +30 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/permalink_for/pretty_impl.rb +20 -0
- data/lib/permalink_for/slug_impl.rb +57 -0
- data/lib/permalink_for/version.rb +3 -0
- data/lib/permalink_for.rb +28 -0
- data/permalink_for.gemspec +24 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3e5ca25f2e8325e1cd27c4917e7f24788463976f
|
4
|
+
data.tar.gz: c2e0473342ecf313182634455271c4aafe0f05b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d10c7b9f16906fea1ee8b261c85ece4cf086f8b268d9401f3637455a53d62a8f46587cfcb13fe31f2e24e56e0a16901933edd0b4add280326faf769c3231aa0
|
7
|
+
data.tar.gz: ee5a412f25771d0e08e1e5b3b87fe70364f9e4c48b832f04e0373e91d069d0cc60509056b367f6c61b61b761b88d24f95f282c2ccb41c8004dedb749904d862b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at sk@httplab.ru. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# PermalinkFor
|
2
|
+
|
3
|
+
Crazy simple alternative to FriendlyId.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'permalink_for'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
TODO: Write usage instructions here
|
20
|
+
|
21
|
+
## Development
|
22
|
+
|
23
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
24
|
+
|
25
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/permalink_for. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
30
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pretty_permalink'
|
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,20 @@
|
|
1
|
+
module PermalinkFor::PrettyImpl
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
def to_param
|
5
|
+
return id if new_record?
|
6
|
+
target_field_value = send(permalink_configuration[:target_field])
|
7
|
+
arr = [id, I18n.transliterate(target_field_value).parameterize]
|
8
|
+
arr.delete('')
|
9
|
+
arr = arr.reverse if self.class.permalink_configuration[:reverse]
|
10
|
+
arr.join('-')
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def find(id)
|
15
|
+
position = permalink_configuration[:reverse] ? :last : :first
|
16
|
+
id = id.split('-').send(position).to_i if id.is_a? String
|
17
|
+
super id
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module PermalinkFor::SlugImpl
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def permalink_validation_opts
|
6
|
+
return super if defined?(super)
|
7
|
+
{
|
8
|
+
presence: true,
|
9
|
+
uniqueness: true,
|
10
|
+
format: { with: /\A[a-z0-9_-]+\Z/ }
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
included do
|
16
|
+
validates :permalink, permalink_validation_opts
|
17
|
+
before_validation :build_permalink
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_param
|
21
|
+
return permalink if permalink.present?
|
22
|
+
id
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_permalink
|
26
|
+
target_field_value = send(permalink_configuration[:target_field])
|
27
|
+
|
28
|
+
return if permalink.present? || target_field_value.blank?
|
29
|
+
permalink_base = I18n.transliterate(target_field_value).parameterize.downcase
|
30
|
+
permalink_candidate = permalink_base
|
31
|
+
generated_permalink = nil
|
32
|
+
|
33
|
+
attempts_number = 100
|
34
|
+
attempts = attempts_number
|
35
|
+
while attempts > 0 && generated_permalink.nil?
|
36
|
+
if self.class.find_by(permalink: permalink_candidate)
|
37
|
+
attempts -= 1
|
38
|
+
permalink_candidate = permalink_base + (attempts_number - attempts).to_s
|
39
|
+
else
|
40
|
+
generated_permalink = permalink_candidate
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
fail 'Failed to generate permalink' if generated_permalink.nil?
|
45
|
+
self.permalink = generated_permalink
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def find(id)
|
50
|
+
if id.is_a?(Array) || id.is_a?(Fixnum) || (id.is_a?(String) && id[/^\d+$/])
|
51
|
+
super
|
52
|
+
else
|
53
|
+
find_by!(permalink: id.to_s.downcase)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'permalink_for/pretty_impl'
|
2
|
+
require 'permalink_for/slug_impl'
|
3
|
+
require 'permalink_for/version'
|
4
|
+
|
5
|
+
module PermalinkFor
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
PERMALINK_TYPES = {
|
9
|
+
slug: SlugImpl,
|
10
|
+
pretty: PrettyImpl
|
11
|
+
}
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def permalink_for(field, as: :pretty, reverse: false)
|
15
|
+
as = as.to_sym
|
16
|
+
unless PERMALINK_TYPES.has_key? as
|
17
|
+
raise "Unknown permalink type :#{as} (may be :slug or :pretty)"
|
18
|
+
end
|
19
|
+
include PERMALINK_TYPES[as]
|
20
|
+
|
21
|
+
self::ActiveRecord_AssociationRelation.include PERMALINK_TYPES[as]::ClassMethods
|
22
|
+
|
23
|
+
config = { target_field: field, reverse: reverse }
|
24
|
+
cattr_reader :permalink_configuration
|
25
|
+
class_variable_set(:@@permalink_configuration, config)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'permalink_for/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'permalink_for'
|
8
|
+
spec.version = PermalinkFor::VERSION
|
9
|
+
spec.authors = ['rainrb', 'kovalevsky']
|
10
|
+
spec.email = ['dev@httplab.ru']
|
11
|
+
|
12
|
+
spec.summary = 'Slugs for ActiveRecord Models'
|
13
|
+
spec.description = 'Simple alternative to FriendlyId.'
|
14
|
+
spec.homepage = 'https://httplab.ru'
|
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 'bundler', '~> 1.11'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: permalink_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rainrb
|
8
|
+
- kovalevsky
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.11'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.11'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
description: Simple alternative to FriendlyId.
|
57
|
+
email:
|
58
|
+
- dev@httplab.ru
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/permalink_for.rb
|
73
|
+
- lib/permalink_for/pretty_impl.rb
|
74
|
+
- lib/permalink_for/slug_impl.rb
|
75
|
+
- lib/permalink_for/version.rb
|
76
|
+
- permalink_for.gemspec
|
77
|
+
homepage: https://httplab.ru
|
78
|
+
licenses: []
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.5.1
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Slugs for ActiveRecord Models
|
100
|
+
test_files: []
|