mongoid_slug 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,53 +1,56 @@
1
1
  Mongoid Slug
2
2
  ============
3
3
 
4
- Mongoid Slug generates a URL slug or permalink based on one or more fields in a Mongoid model. It sits on top of [stringex](https://github.com/rsl/stringex) and works with non-Latin characters.
4
+ Mongoid Slug generates a URL slug or permalink based on one or more
5
+ fields in a Mongoid model.
6
+
7
+ It sits idly on top of [stringex](https://github.com/rsl/stringex) and
8
+ works with non-Latin characters.
5
9
 
6
10
  Quick Start
7
- ---------------
11
+ -----------
8
12
 
9
- First, add mongoid_slug to your Gemfile:
13
+ Add mongoid_slug to your Gemfile:
10
14
 
11
15
  gem 'mongoid_slug', :require => 'mongoid/slug'
12
16
 
13
- Say you have a book that embeds many authors. You can set up slugs for both resources like this:
17
+ Set up slugs in models like this:
14
18
 
15
19
  class Book
16
20
  include Mongoid::Document
17
21
  include Mongoid::Slug
22
+
18
23
  field :title
19
- slug :title
20
24
  embeds_many :authors
25
+
26
+ slug :title
21
27
  end
22
28
 
23
29
  class Author
24
30
  include Mongoid::Document
25
31
  include Mongoid::Slug
26
- field :first_name
27
- field :last_name
28
- slug :first_name, :last_name
32
+
33
+ field :first
34
+ field :last
29
35
  embedded_in :book, :inverse_of => :authors
36
+
37
+ slug :first, :last, :as => :name
30
38
  end
31
39
 
32
- In your controller, use the `find_by_slug` helper:
40
+ Finder
41
+ ------
33
42
 
34
- Book.find_by_slug(params[:book_id])
35
- book.authors.find_by_slug(params[:id])
43
+ In your controller, throw in some minimal magic:
36
44
 
37
- You can customize the name of the field that stores the slug:
45
+ # GET /books/a-thousand-plateaus/authors/authors/gilles-deleuze
46
+ author = Book.find_by_slug(params[:book_id]).
47
+ authors.
48
+ find_by_name(params[:id])
38
49
 
39
- class Person
40
- include Mongoid::Document
41
- include Mongoid::Slug
42
- field :name
43
- slug :name, :as => :permalink
44
- end
50
+ Permanence
51
+ ----------
45
52
 
46
- The finder now becomes:
47
-
48
- Person.find_by_permalink(params[:id])
49
-
50
- To demo some more functionality in the console:
53
+ By default, slugs are not permanent:
51
54
 
52
55
  >> book = Book.create(:title => "A Thousand Plateaus")
53
56
  >> book.to_param
@@ -56,47 +59,47 @@ To demo some more functionality in the console:
56
59
  >> book.save
57
60
  >> book.to_param
58
61
  "anti-oedipus"
59
- >> author = book.authors.create(:first_name => "Gilles", :last_name => "Deleuze")
60
- >> author.to_param
61
- => "gilles-deleuze"
62
- >> author.update_attributes(:first_name => "Félix", :last_name => "Guattari")
63
- >> author.to_param
64
- => "felix-guattari"
65
62
 
66
- Scoping by Associations
67
- -----------------------
63
+ If you require permanent slugs, pass the `:permanent` option when
64
+ defining the slug.
65
+
66
+ Scope
67
+ -----
68
+
69
+ Embedded objects that are automatically scoped by their parent.
68
70
 
69
- Objects that are embedded in a parent document automatically have their slug uniqueness scoped to the parent. If you wish to scope by a reference association, you can pass a `:scope` option to the `slug` class method:
71
+ To scope an object by a reference association, pass `:scope`:
70
72
 
71
73
  class Company
72
74
  include Mongoid::Document
73
- field :name
74
75
  references_many :employees
75
76
  end
76
77
 
77
78
  class Employee
78
79
  include Mongoid::Document
79
80
  include Mongoid::Slug
80
- field :first_name
81
- field :last_name
82
- slug :first_name, :last_name, :scope => :company
81
+ field :name
82
+ slug :name, :scope => :company
83
83
  referenced_in :company
84
84
  end
85
85
 
86
- In this example, if you create an employee without associating it with any company, the slug scope will fall back to the root employees collection. Currently if you have an irregular association name, for instance:
86
+ In this example, if you create an employee without associating it with
87
+ any company, the scope will fall back to the root employees collection.
87
88
 
88
- references_many :employees, :class_name => 'Person', :foreign_key => :company_id
89
+ Currently, if you have an irregular association name, you **must**
90
+ specify the `:inverse_of` option on the other side of the assocation.
89
91
 
90
- you **must** specify the `:inverse_of` option on the other side of the assocation.
92
+ Indexes
93
+ -------
91
94
 
92
- Indexing
93
- --------
94
-
95
- You may optionally pass an `:index` option to generate an index on the slug in top-level objects.
95
+ You may optionally pass an `:index` option to define an index on top-level
96
+ slugs.
96
97
 
97
98
  class Book
98
99
  field :title
99
100
  slug :title, :index => true
100
101
  end
101
102
 
102
- Indexes on non-scoped slugs will be unique.
103
+ Indexes on unscoped slugs will be unique.
104
+
105
+ This option has no effect if the object is embedded.
@@ -1,4 +1,3 @@
1
- #encoding: utf-8
2
1
  require 'stringex'
3
2
 
4
3
  module Mongoid #:nodoc:
@@ -125,7 +124,11 @@ module Mongoid #:nodoc:
125
124
  metadata = reflect_on_all_associations(:embedded_in).first
126
125
  _parent.send(metadata.inverse_of)
127
126
  else
128
- self.class
127
+ appropriate_class = self.class
128
+ while (appropriate_class.superclass.include?(Mongoid::Document))
129
+ appropriate_class = appropriate_class.superclass
130
+ end
131
+ appropriate_class
129
132
  end
130
133
  end
131
134
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Slug
3
- VERSION = "0.6.0"
3
+ VERSION = "0.6.1"
4
4
  end
5
5
  end
@@ -0,0 +1,3 @@
1
+ class ComicBook < Book
2
+
3
+ end
@@ -159,12 +159,12 @@ module Mongoid
159
159
  end
160
160
  end
161
161
 
162
- context "when the field name for the slug is set with the :as option" do
162
+ context "when :as is passed as an argument" do
163
163
  let!(:person) do
164
164
  Person.create(:name => "John Doe")
165
165
  end
166
166
 
167
- it "sets the slug field name" do
167
+ it "sets an alternative slug field name" do
168
168
  person.should respond_to(:permalink)
169
169
  person.permalink.should eql "john-doe"
170
170
  end
@@ -174,12 +174,12 @@ module Mongoid
174
174
  end
175
175
  end
176
176
 
177
- context "when slug is set to be permanent with the :permanent option" do
177
+ context "when :permanent is passed as an argument" do
178
178
  let(:person) do
179
179
  Person.create(:name => "John Doe")
180
180
  end
181
181
 
182
- it "does not change the slug when the slugged fields are updated" do
182
+ it "does not update the slug when the slugged fields change" do
183
183
  person.name = "Jane Doe"
184
184
  person.save
185
185
  person.to_param.should eql "john-doe"
@@ -251,41 +251,47 @@ module Mongoid
251
251
  end
252
252
  end
253
253
 
254
- context "when :index is set to true" do
254
+ context "when :index is passed as an argument" do
255
255
  before do
256
256
  Book.collection.drop_indexes
257
257
  Author.collection.drop_indexes
258
258
  end
259
259
 
260
- it "indexes slug in top-level objects" do
260
+ it "defines an index on the slug in top-level objects" do
261
261
  Book.create_indexes
262
262
  Book.collection.index_information.should have_key "slug_1"
263
263
  end
264
264
 
265
265
  context "when slug is scoped by a reference association" do
266
- it "creates a non-unique index" do
266
+ it "defines a non-unique index" do
267
267
  Author.create_indexes
268
268
  Author.index_information["slug_1"]["unique"].should be_false
269
269
  end
270
270
  end
271
271
 
272
272
  context "when slug is not scoped by a reference association" do
273
- it "creates a unique index" do
273
+ it "defines a unique index" do
274
274
  Book.create_indexes
275
275
  Book.index_information["slug_1"]["unique"].should be_true
276
276
  end
277
277
  end
278
278
 
279
- it "does not index slug in embedded objects" do
280
- pending "Would such an option even make sense?"
281
- end
279
+ it "has no effect in embedded objects"
282
280
  end
283
281
 
284
- context "when :index is not set" do
285
- it "does not index slug" do
282
+ context "when :index is not passed as an argument" do
283
+ it "does not define an index on the slug" do
286
284
  Person.create_indexes
287
285
  Person.collection.index_information.should_not have_key "permalink_1"
288
286
  end
289
287
  end
288
+
289
+ context "when the object has STI" do
290
+ it "scopes by the superclass" do
291
+ book = Book.create(:title => "Anti Oedipus")
292
+ comic_book = ComicBook.create(:title => "Anti Oedipus")
293
+ comic_book.slug.should_not eql(book.title)
294
+ end
295
+ end
290
296
  end
291
297
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_slug
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 5
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 6
8
- - 0
9
- version: 0.6.0
9
+ - 1
10
+ version: 0.6.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Paper Cavalier
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-01-17 00:00:00 +00:00
18
+ date: 2011-01-28 00:00:00 +00:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,13 +26,14 @@ dependencies:
25
26
  requirements:
26
27
  - - ~>
27
28
  - !ruby/object:Gem::Version
29
+ hash: 15424089
28
30
  segments:
29
31
  - 2
30
32
  - 0
31
33
  - 0
32
34
  - rc
33
- - 5
34
- version: 2.0.0.rc.5
35
+ - 6
36
+ version: 2.0.0.rc.6
35
37
  type: :runtime
36
38
  version_requirements: *id001
37
39
  - !ruby/object:Gem::Dependency
@@ -42,6 +44,7 @@ dependencies:
42
44
  requirements:
43
45
  - - ~>
44
46
  - !ruby/object:Gem::Version
47
+ hash: 31
45
48
  segments:
46
49
  - 1
47
50
  - 2
@@ -57,11 +60,12 @@ dependencies:
57
60
  requirements:
58
61
  - - ~>
59
62
  - !ruby/object:Gem::Version
63
+ hash: 31
60
64
  segments:
61
65
  - 1
62
- - 1
63
- - 5
64
- version: 1.1.5
66
+ - 2
67
+ - 0
68
+ version: 1.2.0
65
69
  type: :development
66
70
  version_requirements: *id003
67
71
  - !ruby/object:Gem::Dependency
@@ -72,6 +76,7 @@ dependencies:
72
76
  requirements:
73
77
  - - ~>
74
78
  - !ruby/object:Gem::Version
79
+ hash: 7
75
80
  segments:
76
81
  - 0
77
82
  - 6
@@ -87,6 +92,7 @@ dependencies:
87
92
  requirements:
88
93
  - - ~>
89
94
  - !ruby/object:Gem::Version
95
+ hash: 31
90
96
  segments:
91
97
  - 2
92
98
  - 4
@@ -94,21 +100,6 @@ dependencies:
94
100
  version: 2.4.0
95
101
  type: :development
96
102
  version_requirements: *id005
97
- - !ruby/object:Gem::Dependency
98
- name: ruby-debug19
99
- prerelease: false
100
- requirement: &id006 !ruby/object:Gem::Requirement
101
- none: false
102
- requirements:
103
- - - ~>
104
- - !ruby/object:Gem::Version
105
- segments:
106
- - 0
107
- - 11
108
- - 0
109
- version: 0.11.0
110
- type: :development
111
- version_requirements: *id006
112
103
  description: Mongoid Slug generates a URL slug or permalink based on one or more fields in a Mongoid model.
113
104
  email:
114
105
  - code@papercavalier.com
@@ -125,6 +116,7 @@ files:
125
116
  - README.md
126
117
  - spec/models/author.rb
127
118
  - spec/models/book.rb
119
+ - spec/models/comic_book.rb
128
120
  - spec/models/partner.rb
129
121
  - spec/models/person.rb
130
122
  - spec/models/relationship.rb
@@ -145,6 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
137
  requirements:
146
138
  - - ">="
147
139
  - !ruby/object:Gem::Version
140
+ hash: 3
148
141
  segments:
149
142
  - 0
150
143
  version: "0"
@@ -153,6 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
146
  requirements:
154
147
  - - ">="
155
148
  - !ruby/object:Gem::Version
149
+ hash: 3
156
150
  segments:
157
151
  - 0
158
152
  version: "0"
@@ -166,6 +160,7 @@ summary: Generates a URL slug
166
160
  test_files:
167
161
  - spec/models/author.rb
168
162
  - spec/models/book.rb
163
+ - spec/models/comic_book.rb
169
164
  - spec/models/partner.rb
170
165
  - spec/models/person.rb
171
166
  - spec/models/relationship.rb