the_friendly_id 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f59100aa2ef5b1ff6639386990d7c1a3e5fa678
4
+ data.tar.gz: 14e81a63f7e385662e15790ff0d953d73612e0f3
5
+ SHA512:
6
+ metadata.gz: ea8603bb85a6b5a16a46413157e4f5face2bbd93633871ad961f10b866b74d7b4255619283722b1eee68a0bd6b4c9f228045057405bbe96287c77e2c3ec7a4b5
7
+ data.tar.gz: 111a17a6468d59a186ec36c937eb96611c04deda0b1f5529b1914776a836109ccd7781870008b89bae93184ffc13c756cd662f4d0d85c98f3ff14149b6013393
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in the_friendly_id.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ilya N. Zykin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # TheFriendlyId
2
+
3
+ ```ruby
4
+ class Post < ActiveRecord::Base
5
+ include TheFriendlyId::Base
6
+ end
7
+
8
+ post = Post.new
9
+ post.title = "Привет"
10
+ post.save
11
+
12
+
13
+ post.title # => "Привет"
14
+ post.slug # => "privet"
15
+ post.short_id # => "pt1263"
16
+ post.friendly_id # => "pt1263+privet"
17
+
18
+ Post.friendly_where(:privet) # => [post ...]
19
+ Post.friendly_first(:privet) # => post
20
+ ```
21
+
22
+ ### MIT
23
+
24
+ zykin-ilya@ya.ru 2014
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/gem_version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module TheFriendlyId
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,118 @@
1
+ require "the_friendly_id/version"
2
+
3
+ module TheFriendlyId
4
+ class Engine < Rails::Engine; end
5
+ end
6
+
7
+ # friendly_id h11149+menu-10-u-5 # uniq
8
+ # short_id h11149 # uniq
9
+ # id 50 # uniq
10
+
11
+ module TheFriendlyId
12
+ module Base
13
+ extend ActiveSupport::Concern
14
+
15
+ SEPARATOR = "+"
16
+
17
+ def self.int? str
18
+ str.to_s.to_i.to_s == str
19
+ end
20
+
21
+ def self.friendly? str
22
+ Regexp.new("\\#{TheFriendlyId::Base::SEPARATOR}") =~ str
23
+ end
24
+
25
+ def self.short? str
26
+ str =~ /^[A-Z]+[0-9]+$/mix
27
+ end
28
+
29
+ included do
30
+ def to_param; self.slug end
31
+
32
+ validates_presence_of :short_id, :friendly_id
33
+ validates_uniqueness_of :short_id, :slug
34
+
35
+ before_validation :build_short_id
36
+ before_validation :build_slugs
37
+
38
+ def friendly_prefix
39
+ {
40
+ hub: :h,
41
+ page: :p,
42
+ post: :pt,
43
+ blog: :b,
44
+ recipe: :r,
45
+ note: :n
46
+ }
47
+ end
48
+
49
+ def build_short_id
50
+ return unless self.short_id.blank?
51
+ klass = self.class.to_s.downcase.to_sym
52
+ prefix = friendly_prefix[klass]
53
+
54
+ # build short id
55
+ prefix ||= 'x'
56
+ rnd_num = 9999
57
+ short_id = [prefix, rand(rnd_num)].join
58
+
59
+ # rebuild if find identically short_id
60
+ try_counter = 0
61
+ while self.class.where(short_id: short_id).first
62
+ short_id = [prefix, rand(rnd_num)].join
63
+ try_counter = try_counter + 1
64
+ break if try_counter > (rnd_num/10)
65
+ end
66
+
67
+ # set short_id
68
+ self.short_id = short_id
69
+ end
70
+
71
+ def build_slugs
72
+ unless self.title.blank?
73
+ _slug = slug.blank? ? title : slug
74
+ _slug = title if title_changed?
75
+ _slug = slug if slug_changed? && !slug.blank?
76
+
77
+ self.slug = uniq_slug(_slug)
78
+ self.friendly_id = [self.short_id, self.slug].join TheFriendlyId::Base::SEPARATOR
79
+ end
80
+ end
81
+
82
+ def uniq_slug str
83
+ _slug = str.to_s.to_slug_param
84
+
85
+ 10.times do |i|
86
+ objs_with_this_slug = self.class.where(slug: _slug)
87
+ break if objs_with_this_slug.size.zero?
88
+ break if objs_with_this_slug.size == 1 && objs_with_this_slug.include?(self)
89
+ _slug = [str, i.next].join('-').to_s.to_slug_param
90
+ end
91
+
92
+ _slug
93
+ end
94
+ end
95
+
96
+ module ClassMethods
97
+ def friendly_where id
98
+ if TheFriendlyId::Base.int?(id)
99
+ where(id: id)
100
+ elsif id.is_a? Array
101
+ ids = id.map(&:to_slug_param)
102
+ where(slug: ids)
103
+ elsif TheFriendlyId::Base.friendly?(id)
104
+ where(friendly_id: id)
105
+ elsif TheFriendlyId::Base.short?(id)
106
+ by_slug = where(slug: id.to_slug_param)
107
+ by_slug.present? ? by_slug : where(slug: id.to_slug_param)
108
+ else
109
+ where(slug: id.to_slug_param)
110
+ end
111
+ end
112
+
113
+ def friendly_first id
114
+ friendly_where(id).first
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1 @@
1
+ require_relative '../../gem_version'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'the_friendly_id/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "the_friendly_id"
8
+ spec.version = TheFriendlyId::VERSION
9
+ spec.authors = ["Ilya N. Zykin"]
10
+ spec.email = ["zykin-ilya@ya.ru"]
11
+ spec.summary = %q{Build Friendly ids}
12
+ spec.description = %q{Build Friendly ids for The!Profit CMS}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'the_string_to_slug', '~> 1.2'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the_friendly_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ilya N. Zykin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: the_string_to_slug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Build Friendly ids for The!Profit CMS
56
+ email:
57
+ - zykin-ilya@ya.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - gem_version.rb
68
+ - lib/the_friendly_id.rb
69
+ - lib/the_friendly_id/version.rb
70
+ - the_friendly_id.gemspec
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Build Friendly ids
95
+ test_files: []