mongoid_slug 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = Mongoid Slug
2
2
 
3
- This gem generates a URL slug/permalink based on fields in a Mongoid model.
3
+ This gem generates a URL slug/permalink based on a field or set of fields in a Mongoid model.
4
4
 
5
5
  class Book
6
6
  include Mongoid::Document
@@ -35,3 +35,5 @@ This gem generates a URL slug/permalink based on fields in a Mongoid model.
35
35
  => "félix-guattari"
36
36
  >> book.authors.where(:slug => 'felix-guattari).first
37
37
  => #<Author _id: 4c31e362faa4a7050e000003, slug: "félix-guattari", last_name: "Guattari", first_name: "Félix">
38
+
39
+ Check out the sample models in the spec folder for more examples.
data/lib/mongoid_slug.rb CHANGED
@@ -3,14 +3,18 @@ module Mongoid::Slug
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- cattr_accessor :slugged_fields
6
+ cattr_accessor :slug_name, :slugged_fields
7
7
  end
8
8
 
9
9
  module ClassMethods #:nodoc:
10
10
  # Set a field or a number of fields as source of slug
11
- def slug(*fields)
12
- self.slugged_fields = fields
13
- field :slug; before_save :generate_slug
11
+ def slug(*args)
12
+ options = args.last.is_a?(Hash) ? args.pop : {}
13
+ self.slug_name = options[:as] || :slug
14
+ self.slugged_fields = args
15
+ field slug_name
16
+ index slug_name, :unique => true
17
+ before_save :generate_slug
14
18
  end
15
19
  end
16
20
 
@@ -20,10 +24,10 @@ module Mongoid::Slug
20
24
 
21
25
  private
22
26
 
23
- def duplicates_of(slug, association_chain=[])
27
+ def duplicate_of(slug, association_chain=[])
24
28
  if embedded?
25
29
  association_chain << association_name
26
- _parent.send :duplicates_of, slug, association_chain
30
+ _parent.send :duplicate_of, slug, association_chain
27
31
  else
28
32
  association_chain.reverse! << "slug"
29
33
  collection.find(association_chain.join(".") => slug)
@@ -33,7 +37,7 @@ module Mongoid::Slug
33
37
  def find_unique_slug(suffix='')
34
38
  slug = ("#{slug_base} #{suffix}").parameterize
35
39
 
36
- if duplicates_of(slug).reject{ |doc| doc.id == self.id }.empty?
40
+ if duplicate_of(slug).reject{ |doc| doc.id == self.id }.empty?
37
41
  slug
38
42
  else
39
43
  suffix = suffix.blank? ? '1' : "#{suffix.to_i + 1}"
@@ -43,7 +47,7 @@ module Mongoid::Slug
43
47
 
44
48
  def generate_slug
45
49
  if new_record? || slugged_fields_changed?
46
- self.slug = find_unique_slug
50
+ self.send("#{slug_name}=", find_unique_slug)
47
51
  end
48
52
  end
49
53
 
@@ -0,0 +1,6 @@
1
+ class Person
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :name
5
+ slug :name, :as => :permalink
6
+ end
@@ -193,7 +193,20 @@ describe Mongoid::Slug do
193
193
 
194
194
  end
195
195
 
196
- describe "#duplicates_of" do
196
+ context ":as option" do
197
+
198
+ before(:each) do
199
+ @person = Person.create(:name => "John Doe")
200
+ end
201
+
202
+ it "should set the slug field name" do
203
+ @person.respond_to?(:permalink).should be_true
204
+ @person.send(:permalink).should eql "john-doe"
205
+ end
206
+
207
+ end
208
+
209
+ context "#duplicate_of" do
197
210
 
198
211
  before(:each) do
199
212
  @foo = Foo.create(:name => "foo")
@@ -201,16 +214,16 @@ describe Mongoid::Slug do
201
214
  @baz = @bar.bazes.create(:name => "baz")
202
215
  end
203
216
 
204
- it "should find duplicate slug of a root document" do
205
- @foo.send(:duplicates_of, @foo.slug).count.should eql 1
217
+ it "finds duplicate slug of a root document" do
218
+ @foo.send(:duplicate_of, @foo.slug).count.should eql 1
206
219
  end
207
220
 
208
- it "should find duplicate slug of an embedded document" do
209
- @bar.send(:duplicates_of, @bar.slug).count.should eql 1
221
+ it "finds duplicate slug of an embedded document" do
222
+ @bar.send(:duplicate_of, @bar.slug).count.should eql 1
210
223
  end
211
224
 
212
- it "should find duplicate slug of a deeply-embedded document" do
213
- @baz.send(:duplicates_of, @baz.slug).count.should eql 1
225
+ it "finds duplicate slug of a deeply-embedded document" do
226
+ @baz.send(:duplicate_of, @baz.slug).count.should eql 1
214
227
  end
215
228
 
216
229
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_slug
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 0
10
- version: 0.4.0
9
+ - 2
10
+ version: 0.4.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hakan Ensari
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-05 00:00:00 +01:00
19
+ date: 2010-07-07 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -54,6 +54,7 @@ files:
54
54
  - spec/models/baz.rb
55
55
  - spec/models/book.rb
56
56
  - spec/models/foo.rb
57
+ - spec/models/person.rb
57
58
  - spec/models/publisher.rb
58
59
  - spec/models/subject.rb
59
60
  - spec/mongoid_slug_spec.rb
@@ -98,6 +99,7 @@ test_files:
98
99
  - spec/models/baz.rb
99
100
  - spec/models/book.rb
100
101
  - spec/models/foo.rb
102
+ - spec/models/person.rb
101
103
  - spec/models/publisher.rb
102
104
  - spec/models/subject.rb
103
105
  - spec/mongoid_slug_spec.rb