mongoid_slug 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +40 -32
- data/lib/mongoid/slug.rb +1 -0
- data/lib/mongoid/slug/version.rb +1 -1
- data/lib/mongoid_slug.rb +1 -0
- data/spec/mongoid/slug_spec.rb +25 -17
- data/spec/spec_helper.rb +2 -4
- metadata +4 -3
data/README.md
CHANGED
@@ -11,37 +11,43 @@ Quick Start
|
|
11
11
|
|
12
12
|
Add mongoid_slug to your Gemfile:
|
13
13
|
|
14
|
-
|
14
|
+
```ruby
|
15
|
+
gem 'mongoid_slug'
|
16
|
+
```
|
15
17
|
|
16
18
|
Set up some slugs:
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
```ruby
|
21
|
+
class Book
|
22
|
+
include Mongoid::Document
|
23
|
+
include Mongoid::Slug
|
21
24
|
|
22
|
-
|
23
|
-
|
25
|
+
field :title
|
26
|
+
embeds_many :authors
|
24
27
|
|
25
|
-
|
26
|
-
|
28
|
+
slug :title
|
29
|
+
end
|
27
30
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
class Author
|
32
|
+
include Mongoid::Document
|
33
|
+
include Mongoid::Slug
|
31
34
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
+
field :first
|
36
|
+
field :last
|
37
|
+
embedded_in :book, :inverse_of => :authors
|
35
38
|
|
36
|
-
|
37
|
-
|
39
|
+
slug :first, :last, :as => :name
|
40
|
+
end
|
41
|
+
```
|
38
42
|
|
39
43
|
In your controller, use available finders:
|
40
44
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
```ruby
|
46
|
+
# GET /books/a-thousand-plateaus/authors/gilles-deleuze
|
47
|
+
author = Book.find_by_slug(params[:book_id]).
|
48
|
+
authors.
|
49
|
+
find_by_name(params[:id])
|
50
|
+
```
|
45
51
|
|
46
52
|
[Read here](https://github.com/papercavalier/mongoid-slug/blob/master/lib/mongoid/slug.rb)
|
47
53
|
for all available options.
|
@@ -51,18 +57,20 @@ Scoping
|
|
51
57
|
|
52
58
|
To scope a slug by a reference association, pass `:scope`:
|
53
59
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
60
|
+
```ruby
|
61
|
+
class Company
|
62
|
+
include Mongoid::Document
|
63
|
+
references_many :employees
|
64
|
+
end
|
65
|
+
|
66
|
+
class Employee
|
67
|
+
include Mongoid::Document
|
68
|
+
include Mongoid::Slug
|
69
|
+
field :name
|
70
|
+
slug :name, :scope => :company
|
71
|
+
referenced_in :company
|
72
|
+
end
|
73
|
+
```
|
66
74
|
|
67
75
|
In this example, if you create an employee without associating it with
|
68
76
|
any company, the scope will fall back to the root employees collection.
|
data/lib/mongoid/slug.rb
CHANGED
data/lib/mongoid/slug/version.rb
CHANGED
data/lib/mongoid_slug.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mongoid/slug'
|
data/spec/mongoid/slug_spec.rb
CHANGED
@@ -271,22 +271,30 @@ module Mongoid
|
|
271
271
|
end
|
272
272
|
end
|
273
273
|
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
274
|
+
context "when slugged field contains non-ASCII characters" do
|
275
|
+
it "slugs Cyrillic characters" do
|
276
|
+
book.title = "Капитал"
|
277
|
+
book.save
|
278
|
+
book.to_param.should eql "kapital"
|
279
|
+
end
|
280
|
+
|
281
|
+
it "slugs Greek characters" do
|
282
|
+
book.title = "Ελλάδα"
|
283
|
+
book.save
|
284
|
+
book.to_param.should eql "ellada"
|
285
|
+
end
|
286
|
+
|
287
|
+
it "slugs Chinese characters" do
|
288
|
+
book.title = "中文"
|
289
|
+
book.save
|
290
|
+
book.to_param.should eql 'zhong-wen'
|
291
|
+
end
|
292
|
+
|
293
|
+
it "slugs non-ASCII Latin characters" do
|
294
|
+
book.title = 'Paul Cézanne'
|
295
|
+
book.save
|
296
|
+
book.to_param.should eql 'paul-cezanne'
|
297
|
+
end
|
290
298
|
end
|
291
299
|
|
292
300
|
context "when :index is passed as an argument" do
|
@@ -326,7 +334,7 @@ module Mongoid
|
|
326
334
|
it "scopes by the superclass" do
|
327
335
|
book = Book.create(:title => "Anti Oedipus")
|
328
336
|
comic_book = ComicBook.create(:title => "Anti Oedipus")
|
329
|
-
comic_book.slug.should_not eql(book.
|
337
|
+
comic_book.slug.should_not eql(book.slug)
|
330
338
|
end
|
331
339
|
end
|
332
340
|
|
data/spec/spec_helper.rb
CHANGED
@@ -2,17 +2,15 @@ require "rubygems"
|
|
2
2
|
require "bundler/setup"
|
3
3
|
|
4
4
|
require "database_cleaner"
|
5
|
-
require "mongoid"
|
6
|
-
require "stringex"
|
7
5
|
require "rspec"
|
8
6
|
|
7
|
+
require File.expand_path("../../lib/mongoid/slug", __FILE__)
|
8
|
+
|
9
9
|
Mongoid.configure do |config|
|
10
10
|
name = "mongoid_slug_test"
|
11
11
|
config.master = Mongo::Connection.new.db(name)
|
12
12
|
end
|
13
13
|
|
14
|
-
require File.expand_path("../../lib/mongoid/slug", __FILE__)
|
15
|
-
|
16
14
|
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
17
15
|
|
18
16
|
RSpec.configure do |c|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 8
|
8
|
-
-
|
9
|
-
version: 0.8.
|
8
|
+
- 1
|
9
|
+
version: 0.8.1
|
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: 2011-06-
|
17
|
+
date: 2011-06-23 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -85,6 +85,7 @@ extra_rdoc_files: []
|
|
85
85
|
files:
|
86
86
|
- lib/mongoid/slug/version.rb
|
87
87
|
- lib/mongoid/slug.rb
|
88
|
+
- lib/mongoid_slug.rb
|
88
89
|
- LICENSE
|
89
90
|
- README.md
|
90
91
|
- spec/models/article.rb
|