mongoid-slugify 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +6 -1
- data/Appraisals +8 -0
- data/Gemfile +0 -2
- data/README.rdoc +7 -5
- data/Rakefile +2 -1
- data/lib/mongoid/slugify/version.rb +1 -1
- data/lib/mongoid/slugify.rb +11 -3
- data/mongoid-slugify.gemspec +2 -1
- data/spec/mongoid/slugify_spec.rb +47 -47
- data/spec/spec_helper.rb +6 -2
- metadata +117 -60
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
install: 'bundle install && bundle exec rake appraisal:install'
|
3
|
+
gemfile:
|
4
|
+
- gemfiles/mongoid2.gemfile
|
5
|
+
- gemfiles/mongoid3.gemfile
|
1
6
|
rvm:
|
2
7
|
- 1.8.7
|
3
8
|
- 1.9.2
|
4
9
|
- 1.9.3
|
5
10
|
- rbx-18mode
|
6
|
-
|
11
|
+
#- rbx-19mode
|
7
12
|
- jruby-18mode
|
8
13
|
- jruby-19mode
|
9
14
|
- ree
|
data/Appraisals
ADDED
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -4,12 +4,14 @@ Mongoid::Slufigy is a gem, which helps you with generating slugs for your Mongoi
|
|
4
4
|
|
5
5
|
{<img src="https://secure.travis-ci.org/fxposter/mongoid-slugify.png" />}[http://travis-ci.org/fxposter/mongoid-slugify]
|
6
6
|
|
7
|
-
== Installation
|
7
|
+
== Installation
|
8
8
|
|
9
|
-
Add
|
9
|
+
Add Mongoid::Slufigy to your Gemfile:
|
10
10
|
|
11
11
|
gem 'mongoid-slugify'
|
12
12
|
|
13
|
+
Since 0.1.0 Mongoid::Slufigy supports both Mongoid 2.x and 3.x releases (since 3.0.0.rc).
|
14
|
+
|
13
15
|
== Usage
|
14
16
|
|
15
17
|
class Product
|
@@ -19,9 +21,9 @@ Add mongoid-slugify to your Gemfile:
|
|
19
21
|
field :title
|
20
22
|
|
21
23
|
private
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
def generate_slug
|
25
|
+
title.parameterize
|
26
|
+
end
|
25
27
|
end
|
26
28
|
|
27
29
|
As you can see, you should make 2 things:
|
data/Rakefile
CHANGED
data/lib/mongoid/slugify.rb
CHANGED
@@ -4,11 +4,19 @@ require 'active_support/concern'
|
|
4
4
|
|
5
5
|
module Mongoid
|
6
6
|
module Slugify
|
7
|
+
def self.mongoid3?
|
8
|
+
defined?(Mongoid::VERSION) && Gem::Version.new(Mongoid::VERSION) >= Gem::Version.new('3.0.0.rc')
|
9
|
+
end
|
10
|
+
|
7
11
|
extend ActiveSupport::Concern
|
8
12
|
|
9
13
|
included do
|
10
14
|
field :slug
|
11
|
-
|
15
|
+
if Mongoid::Slugify.mongoid3?
|
16
|
+
index({ :slug => 1 }, { :unique => true })
|
17
|
+
else
|
18
|
+
index :slug, :unique => true
|
19
|
+
end
|
12
20
|
before_save :assign_slug, :if => :assign_slug?
|
13
21
|
end
|
14
22
|
|
@@ -18,7 +26,7 @@ module Mongoid
|
|
18
26
|
end
|
19
27
|
|
20
28
|
def find_by_slug!(slug)
|
21
|
-
find_by_slug(slug) || raise(Mongoid::Errors::DocumentNotFound.new(self, slug))
|
29
|
+
find_by_slug(slug) || raise(Mongoid::Errors::DocumentNotFound.new(self, { :slug => slug }))
|
22
30
|
end
|
23
31
|
|
24
32
|
def find_by_slug_or_id(slug_or_id)
|
@@ -26,7 +34,7 @@ module Mongoid
|
|
26
34
|
end
|
27
35
|
|
28
36
|
def find_by_slug_or_id!(slug_or_id)
|
29
|
-
find_by_slug(slug_or_id) || where(:_id => slug_or_id).first || raise(Mongoid::Errors::DocumentNotFound.new(self, slug_or_id))
|
37
|
+
find_by_slug(slug_or_id) || where(:_id => slug_or_id).first || raise(Mongoid::Errors::DocumentNotFound.new(self, { :slug => slug_or_id }))
|
30
38
|
end
|
31
39
|
end
|
32
40
|
|
data/mongoid-slugify.gemspec
CHANGED
@@ -16,9 +16,10 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.version = Mongoid::Slugify::VERSION
|
17
17
|
|
18
18
|
gem.add_runtime_dependency 'activesupport', '~> 3.0'
|
19
|
-
gem.add_runtime_dependency 'mongoid', '
|
19
|
+
gem.add_runtime_dependency 'mongoid', '>= 2.0', '< 4.0'
|
20
20
|
|
21
21
|
gem.add_development_dependency 'rake'
|
22
22
|
gem.add_development_dependency 'rspec'
|
23
23
|
gem.add_development_dependency 'database_cleaner'
|
24
|
+
gem.add_development_dependency 'appraisal'
|
24
25
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#encoding: utf-8
|
1
|
+
# encoding: utf-8
|
2
2
|
require "spec_helper"
|
3
3
|
|
4
4
|
class Author
|
@@ -6,27 +6,27 @@ class Author
|
|
6
6
|
include Mongoid::Slugify
|
7
7
|
field :first_name
|
8
8
|
field :last_name
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
belongs_to :book
|
10
|
+
has_many :characters,
|
11
|
+
:class_name => 'Person',
|
12
|
+
:foreign_key => :author_id
|
13
13
|
|
14
14
|
private
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def generate_slug
|
16
|
+
[first_name, last_name].reject(&:blank?).join('-').parameterize
|
17
|
+
end
|
18
18
|
end
|
19
19
|
|
20
20
|
class Book
|
21
21
|
include Mongoid::Document
|
22
22
|
include Mongoid::Slugify
|
23
23
|
field :title
|
24
|
-
|
24
|
+
has_many :authors
|
25
25
|
|
26
26
|
private
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def generate_slug
|
28
|
+
title.parameterize
|
29
|
+
end
|
30
30
|
end
|
31
31
|
|
32
32
|
class ComicBook < Book
|
@@ -44,25 +44,25 @@ class Person
|
|
44
44
|
include Mongoid::Slugify
|
45
45
|
field :name
|
46
46
|
embeds_many :relationships
|
47
|
-
|
47
|
+
belongs_to :author, :inverse_of => :characters
|
48
48
|
|
49
49
|
private
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
def generate_slug
|
51
|
+
name.parameterize
|
52
|
+
end
|
53
53
|
end
|
54
54
|
|
55
55
|
class Caption
|
56
56
|
include Mongoid::Document
|
57
57
|
include Mongoid::Slugify
|
58
|
-
field :
|
58
|
+
field :person
|
59
59
|
field :title
|
60
60
|
field :medium
|
61
61
|
|
62
62
|
private
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
def generate_slug
|
64
|
+
[person.gsub(/\s*\([^)]+\)/, ''), title].join(' ').parameterize
|
65
|
+
end
|
66
66
|
end
|
67
67
|
|
68
68
|
module Mongoid
|
@@ -180,7 +180,7 @@ module Mongoid
|
|
180
180
|
|
181
181
|
context "when :slug is given a block" do
|
182
182
|
let(:caption) do
|
183
|
-
Caption.create(:
|
183
|
+
Caption.create(:person => 'Edward Hopper (American, 1882-1967)',
|
184
184
|
:title => 'Soir Bleu, 1914',
|
185
185
|
:medium => 'Oil on Canvas')
|
186
186
|
end
|
@@ -196,7 +196,7 @@ module Mongoid
|
|
196
196
|
end
|
197
197
|
|
198
198
|
it "does not change slug if slugged fields have changed but generated slug is identical" do
|
199
|
-
caption.
|
199
|
+
caption.person = 'Edward Hopper'
|
200
200
|
caption.save
|
201
201
|
caption.to_param.should eql 'edward-hopper-soir-bleu-1914'
|
202
202
|
end
|
@@ -206,31 +206,31 @@ module Mongoid
|
|
206
206
|
end
|
207
207
|
end
|
208
208
|
|
209
|
-
context "when :index is passed as an argument" do
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
end
|
227
|
-
|
228
|
-
context "when :index is not passed as an argument" do
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
end
|
209
|
+
# context "when :index is passed as an argument" do
|
210
|
+
# before do
|
211
|
+
# Book.collection.drop_indexes
|
212
|
+
# Author.collection.drop_indexes
|
213
|
+
# end
|
214
|
+
|
215
|
+
# it "defines an index on the slug in top-level objects" do
|
216
|
+
# Book.create_indexes
|
217
|
+
# Book.collection.index_information.should have_key "slug_1"
|
218
|
+
# end
|
219
|
+
|
220
|
+
# context "when slug is not scoped by a reference association" do
|
221
|
+
# it "defines a unique index" do
|
222
|
+
# Book.create_indexes
|
223
|
+
# Book.index_information["slug_1"]["unique"].should be_true
|
224
|
+
# end
|
225
|
+
# end
|
226
|
+
# end
|
227
|
+
|
228
|
+
# context "when :index is not passed as an argument" do
|
229
|
+
# it "does not define an index on the slug" do
|
230
|
+
# Person.create_indexes
|
231
|
+
# Person.collection.index_information.should_not have_key "permalink_1"
|
232
|
+
# end
|
233
|
+
# end
|
234
234
|
|
235
235
|
context "when the object has STI" do
|
236
236
|
it "scopes by the superclass" do
|
data/spec/spec_helper.rb
CHANGED
@@ -2,11 +2,15 @@ require "bundler/setup"
|
|
2
2
|
Bundler.require(:default, :development)
|
3
3
|
|
4
4
|
Mongoid.configure do |config|
|
5
|
-
|
5
|
+
if Mongoid::Slugify.mongoid3?
|
6
|
+
config.sessions[:default] = { :database => 'mongoid_slugify_test', :hosts => ['localhost:27017'] }
|
7
|
+
else
|
8
|
+
config.master = Mongo::Connection.new.db('mongoid_slugify_test')
|
9
|
+
end
|
6
10
|
end
|
7
11
|
|
8
12
|
DatabaseCleaner.strategy = :truncation
|
9
|
-
DatabaseCleaner.orm = :mongoid
|
13
|
+
DatabaseCleaner.orm = Mongoid::Slugify.mongoid3? ? :moped : :mongoid
|
10
14
|
|
11
15
|
RSpec.configure do |config|
|
12
16
|
config.before :each do
|
metadata
CHANGED
@@ -1,80 +1,128 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-slugify
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 1322991910812937300
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Pavel Forkert
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-06-12 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: activesupport
|
16
|
-
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
25
|
+
requirements:
|
19
26
|
- - ~>
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 2224468019386370560
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: "3.0"
|
22
33
|
type: :runtime
|
23
|
-
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
26
36
|
name: mongoid
|
27
|
-
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 1017907466521848518
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 0
|
47
|
+
version: "2.0"
|
48
|
+
- - <
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3455592072196224488
|
51
|
+
segments:
|
52
|
+
- 4
|
53
|
+
- 0
|
54
|
+
version: "4.0"
|
33
55
|
type: :runtime
|
34
|
-
|
35
|
-
|
36
|
-
- !ruby/object:Gem::Dependency
|
56
|
+
version_requirements: *id002
|
57
|
+
- !ruby/object:Gem::Dependency
|
37
58
|
name: rake
|
38
|
-
|
59
|
+
prerelease: false
|
60
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
61
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 2002549777813010636
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
44
69
|
type: :development
|
45
|
-
|
46
|
-
|
47
|
-
- !ruby/object:Gem::Dependency
|
70
|
+
version_requirements: *id003
|
71
|
+
- !ruby/object:Gem::Dependency
|
48
72
|
name: rspec
|
49
|
-
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
75
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 2002549777813010636
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
55
83
|
type: :development
|
56
|
-
|
57
|
-
|
58
|
-
- !ruby/object:Gem::Dependency
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
59
86
|
name: database_cleaner
|
60
|
-
|
87
|
+
prerelease: false
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
61
89
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 2002549777813010636
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
66
97
|
type: :development
|
98
|
+
version_requirements: *id005
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: appraisal
|
67
101
|
prerelease: false
|
68
|
-
|
102
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 2002549777813010636
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
version: "0"
|
111
|
+
type: :development
|
112
|
+
version_requirements: *id006
|
69
113
|
description: Provides a simple way to add slug generation to a Mongoid model
|
70
|
-
email:
|
114
|
+
email:
|
71
115
|
- fxposter@gmail.com
|
72
116
|
executables: []
|
117
|
+
|
73
118
|
extensions: []
|
119
|
+
|
74
120
|
extra_rdoc_files: []
|
75
|
-
|
121
|
+
|
122
|
+
files:
|
76
123
|
- .gitignore
|
77
124
|
- .travis.yml
|
125
|
+
- Appraisals
|
78
126
|
- Gemfile
|
79
127
|
- README.rdoc
|
80
128
|
- Rakefile
|
@@ -84,30 +132,39 @@ files:
|
|
84
132
|
- mongoid-slugify.gemspec
|
85
133
|
- spec/mongoid/slugify_spec.rb
|
86
134
|
- spec/spec_helper.rb
|
87
|
-
homepage:
|
135
|
+
homepage: ""
|
88
136
|
licenses: []
|
137
|
+
|
89
138
|
post_install_message:
|
90
139
|
rdoc_options: []
|
91
|
-
|
140
|
+
|
141
|
+
require_paths:
|
92
142
|
- lib
|
93
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
144
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
hash: 2002549777813010636
|
149
|
+
segments:
|
150
|
+
- 0
|
151
|
+
version: "0"
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
153
|
none: false
|
101
|
-
requirements:
|
102
|
-
- -
|
103
|
-
- !ruby/object:Gem::Version
|
104
|
-
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
hash: 2002549777813010636
|
158
|
+
segments:
|
159
|
+
- 0
|
160
|
+
version: "0"
|
105
161
|
requirements: []
|
162
|
+
|
106
163
|
rubyforge_project:
|
107
|
-
rubygems_version: 1.8.
|
164
|
+
rubygems_version: 1.8.12
|
108
165
|
signing_key:
|
109
166
|
specification_version: 3
|
110
167
|
summary: Managing slugs in Mongoid models
|
111
|
-
test_files:
|
168
|
+
test_files:
|
112
169
|
- spec/mongoid/slugify_spec.rb
|
113
170
|
- spec/spec_helper.rb
|