mongoid_slug 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -6
- data/lib/mongoid/slug.rb +8 -6
- data/lib/mongoid/slug/version.rb +1 -1
- data/spec/mongoid/slug_spec.rb +5 -1
- data/spec/spec_helper.rb +1 -2
- metadata +9 -9
data/README.md
CHANGED
@@ -29,16 +29,24 @@ Say you have a book that embeds many authors. You can set up slugs for both reso
|
|
29
29
|
embedded_in :book, :inverse_of => :authors
|
30
30
|
end
|
31
31
|
|
32
|
-
In your controller,
|
32
|
+
In your controller, use the `find_by_slug` helper:
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
Book.find_by_slug(params[:book_id])
|
35
|
+
book.authors.find_by_slug(params[:id])
|
36
|
+
|
37
|
+
You can customize the name of the field that stores the slug:
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
class Person
|
40
|
+
include Mongoid::Document
|
41
|
+
include Mongoid::Slug
|
42
|
+
field :name
|
43
|
+
slug :name, :as => :permalink
|
40
44
|
end
|
41
45
|
|
46
|
+
The finder now becomes:
|
47
|
+
|
48
|
+
Person.find_by_permalink(params[:id])
|
49
|
+
|
42
50
|
To demo some more functionality in the console:
|
43
51
|
|
44
52
|
>> book = Book.create(:title => "A Thousand Plateaus")
|
data/lib/mongoid/slug.rb
CHANGED
@@ -41,7 +41,7 @@ module Mongoid #:nodoc:
|
|
41
41
|
field slug_name
|
42
42
|
|
43
43
|
if options[:index]
|
44
|
-
index slug_name, :unique =>
|
44
|
+
index slug_name, :unique => !slug_scope
|
45
45
|
end
|
46
46
|
|
47
47
|
if options[:permanent]
|
@@ -49,11 +49,12 @@ module Mongoid #:nodoc:
|
|
49
49
|
else
|
50
50
|
before_save :generate_slug
|
51
51
|
end
|
52
|
-
end
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
instance_eval <<-CODE
|
54
|
+
def self.find_by_#{slug_name}(slug)
|
55
|
+
where(slug_name => slug).first rescue nil
|
56
|
+
end
|
57
|
+
CODE
|
57
58
|
end
|
58
59
|
end
|
59
60
|
|
@@ -121,7 +122,8 @@ module Mongoid #:nodoc:
|
|
121
122
|
inverse = metadata.inverse_of || collection_name
|
122
123
|
parent.respond_to?(inverse) ? parent.send(inverse) : self.class
|
123
124
|
elsif embedded?
|
124
|
-
|
125
|
+
metadata = reflect_on_all_associations(:embedded_in).first
|
126
|
+
_parent.send(metadata.inverse_of)
|
125
127
|
else
|
126
128
|
self.class
|
127
129
|
end
|
data/lib/mongoid/slug/version.rb
CHANGED
data/spec/mongoid/slug_spec.rb
CHANGED
@@ -160,7 +160,7 @@ module Mongoid
|
|
160
160
|
end
|
161
161
|
|
162
162
|
context "when the field name for the slug is set with the :as option" do
|
163
|
-
let(:person) do
|
163
|
+
let!(:person) do
|
164
164
|
Person.create(:name => "John Doe")
|
165
165
|
end
|
166
166
|
|
@@ -168,6 +168,10 @@ module Mongoid
|
|
168
168
|
person.should respond_to(:permalink)
|
169
169
|
person.permalink.should eql "john-doe"
|
170
170
|
end
|
171
|
+
|
172
|
+
it "finds by slug" do
|
173
|
+
Person.find_by_permalink("john-doe").should eql person
|
174
|
+
end
|
171
175
|
end
|
172
176
|
|
173
177
|
context "when slug is set to be permanent with the :permanent option" do
|
data/spec/spec_helper.rb
CHANGED
@@ -17,6 +17,5 @@ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
|
17
17
|
|
18
18
|
Rspec.configure do |c|
|
19
19
|
c.before(:all) { DatabaseCleaner.strategy = :truncation }
|
20
|
-
c.before(:each) { DatabaseCleaner.
|
21
|
-
c.after(:each) { DatabaseCleaner.clean }
|
20
|
+
c.before(:each) { DatabaseCleaner.clean }
|
22
21
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 6
|
8
|
+
- 0
|
9
|
+
version: 0.6.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paper Cavalier
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-17 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -29,9 +29,9 @@ dependencies:
|
|
29
29
|
- 2
|
30
30
|
- 0
|
31
31
|
- 0
|
32
|
-
-
|
33
|
-
-
|
34
|
-
version: 2.0.0.
|
32
|
+
- rc
|
33
|
+
- 5
|
34
|
+
version: 2.0.0.rc.5
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
@@ -89,9 +89,9 @@ dependencies:
|
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
segments:
|
91
91
|
- 2
|
92
|
-
-
|
92
|
+
- 4
|
93
93
|
- 0
|
94
|
-
version: 2.
|
94
|
+
version: 2.4.0
|
95
95
|
type: :development
|
96
96
|
version_requirements: *id005
|
97
97
|
- !ruby/object:Gem::Dependency
|