mongoid-slugify 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +7 -0
- data/Gemfile +2 -3
- data/README.rdoc +6 -0
- data/lib/mongoid/slugify.rb +32 -34
- data/lib/mongoid/slugify/version.rb +1 -1
- data/mongoid-slugify.gemspec +6 -2
- data/spec/mongoid/slugify_spec.rb +2 -3
- metadata +42 -11
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
= Mongoid::Slufigy
|
2
|
+
|
3
|
+
Mongoid::Slufigy is a gem, which helps you with generating slugs for your Mongoid models.
|
4
|
+
|
5
|
+
{<img src="https://secure.travis-ci.org/fxposter/mongoid-slugify.png" />}[http://travis-ci.org/fxposter/mongoid-slugify]
|
6
|
+
|
1
7
|
== Installation
|
2
8
|
|
3
9
|
Add mongoid-slugify to your Gemfile:
|
data/lib/mongoid/slugify.rb
CHANGED
@@ -5,13 +5,43 @@ require 'active_support/concern'
|
|
5
5
|
module Mongoid
|
6
6
|
module Slugify
|
7
7
|
extend ActiveSupport::Concern
|
8
|
-
|
8
|
+
|
9
9
|
included do
|
10
10
|
field :slug
|
11
11
|
index :slug, :unique => true
|
12
12
|
before_save :assign_slug
|
13
13
|
end
|
14
14
|
|
15
|
+
def to_param
|
16
|
+
slug || super
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def generate_slug
|
21
|
+
raise NotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_unique_slug
|
25
|
+
current_slug = generate_slug
|
26
|
+
pattern = /^#{Regexp.escape(current_slug)}(?:-(\d+))?$/
|
27
|
+
|
28
|
+
appropriate_class = self.class
|
29
|
+
while (appropriate_class.superclass.include?(Mongoid::Document))
|
30
|
+
appropriate_class = appropriate_class.superclass
|
31
|
+
end
|
32
|
+
|
33
|
+
existing_slugs = appropriate_class.where(:slug => pattern, :_id.ne => _id).only(:slug).map { |record| record.slug }
|
34
|
+
if existing_slugs.count > 0
|
35
|
+
max_counter = existing_slugs.map { |slug| (pattern.match(slug)[1] || 0).to_i }.max
|
36
|
+
current_slug += "-#{max_counter + 1}"
|
37
|
+
end
|
38
|
+
current_slug
|
39
|
+
end
|
40
|
+
|
41
|
+
def assign_slug
|
42
|
+
self.slug = generate_unique_slug
|
43
|
+
end
|
44
|
+
|
15
45
|
module ClassMethods
|
16
46
|
def find_by_slug(slug)
|
17
47
|
where(:slug => slug).first
|
@@ -20,7 +50,7 @@ module Mongoid
|
|
20
50
|
def find_by_slug!(slug)
|
21
51
|
find_by_slug(slug) || raise(Mongoid::Errors::DocumentNotFound.new(self, slug))
|
22
52
|
end
|
23
|
-
|
53
|
+
|
24
54
|
def find_by_slug_or_id(slug_or_id)
|
25
55
|
find_by_slug(slug_or_id) || where(:id => id).first
|
26
56
|
end
|
@@ -29,37 +59,5 @@ module Mongoid
|
|
29
59
|
find_by_slug(slug_or_id) || find(slug_or_id)
|
30
60
|
end
|
31
61
|
end
|
32
|
-
|
33
|
-
module InstanceMethods
|
34
|
-
def to_param
|
35
|
-
slug || super
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
def generate_slug
|
40
|
-
raise NotImplementedError
|
41
|
-
end
|
42
|
-
|
43
|
-
def generate_unique_slug
|
44
|
-
current_slug = generate_slug
|
45
|
-
pattern = /^#{Regexp.escape(current_slug)}(?:-(\d+))?$/
|
46
|
-
|
47
|
-
appropriate_class = self.class
|
48
|
-
while (appropriate_class.superclass.include?(Mongoid::Document))
|
49
|
-
appropriate_class = appropriate_class.superclass
|
50
|
-
end
|
51
|
-
|
52
|
-
existing_slugs = appropriate_class.where(:slug => pattern, :_id.ne => _id).only(:slug).map { |record| record.slug }
|
53
|
-
if existing_slugs.count > 0
|
54
|
-
max_counter = existing_slugs.map { |slug| (pattern.match(slug)[1] || 0).to_i }.max
|
55
|
-
current_slug += "-#{max_counter + 1}"
|
56
|
-
end
|
57
|
-
current_slug
|
58
|
-
end
|
59
|
-
|
60
|
-
def assign_slug
|
61
|
-
self.slug = generate_unique_slug
|
62
|
-
end
|
63
|
-
end
|
64
62
|
end
|
65
63
|
end
|
data/mongoid-slugify.gemspec
CHANGED
@@ -14,7 +14,11 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "mongoid-slugify"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Mongoid::Slugify::VERSION
|
17
|
-
|
18
|
-
gem.add_runtime_dependency 'activesupport', '
|
17
|
+
|
18
|
+
gem.add_runtime_dependency 'activesupport', '~> 3.0'
|
19
19
|
gem.add_runtime_dependency 'mongoid', '~> 2.0'
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
gem.add_development_dependency 'database_cleaner'
|
20
24
|
end
|
@@ -21,9 +21,8 @@ class Book
|
|
21
21
|
include Mongoid::Document
|
22
22
|
include Mongoid::Slugify
|
23
23
|
field :title
|
24
|
-
embeds_many :subjects
|
25
24
|
references_many :authors
|
26
|
-
|
25
|
+
|
27
26
|
private
|
28
27
|
def generate_slug
|
29
28
|
title.parameterize
|
@@ -39,7 +38,7 @@ class Person
|
|
39
38
|
field :name
|
40
39
|
embeds_many :relationships
|
41
40
|
referenced_in :author, :inverse_of => :characters
|
42
|
-
|
41
|
+
|
43
42
|
private
|
44
43
|
def generate_slug
|
45
44
|
name.parameterize
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-slugify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,25 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70152738090960 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '3.0'
|
22
|
-
- - <
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version: '3.2'
|
25
22
|
type: :runtime
|
26
23
|
prerelease: false
|
27
|
-
version_requirements: *
|
24
|
+
version_requirements: *70152738090960
|
28
25
|
- !ruby/object:Gem::Dependency
|
29
26
|
name: mongoid
|
30
|
-
requirement: &
|
27
|
+
requirement: &70152738090440 !ruby/object:Gem::Requirement
|
31
28
|
none: false
|
32
29
|
requirements:
|
33
30
|
- - ~>
|
@@ -35,7 +32,40 @@ dependencies:
|
|
35
32
|
version: '2.0'
|
36
33
|
type: :runtime
|
37
34
|
prerelease: false
|
38
|
-
version_requirements: *
|
35
|
+
version_requirements: *70152738090440
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70152738089980 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70152738089980
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &70152738089400 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70152738089400
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: database_cleaner
|
60
|
+
requirement: &70152738088960 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70152738088960
|
39
69
|
description: Provides a simple way to add slug generation to a Mongoid model
|
40
70
|
email:
|
41
71
|
- fxposter@gmail.com
|
@@ -44,6 +74,7 @@ extensions: []
|
|
44
74
|
extra_rdoc_files: []
|
45
75
|
files:
|
46
76
|
- .gitignore
|
77
|
+
- .travis.yml
|
47
78
|
- Gemfile
|
48
79
|
- README.rdoc
|
49
80
|
- Rakefile
|
@@ -73,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
104
|
version: '0'
|
74
105
|
requirements: []
|
75
106
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.16
|
77
108
|
signing_key:
|
78
109
|
specification_version: 3
|
79
110
|
summary: Managing slugs in Mongoid models
|