mongoid_slug 0.4.5 → 0.4.6

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.
@@ -1,12 +1,12 @@
1
- = Mongoid Slug
1
+ Mongoid Slug
2
+ ============
2
3
 
3
- == Summary
4
+ Mongoid Slug generates a URL slug or permalink based on a field or set of fields in a Mongoid model.
4
5
 
5
- This module, when included in a Mongoid model, generates a URL slug or permalink based on a field or set of fields.
6
+ Examples
7
+ --------
6
8
 
7
- == Examples
8
-
9
- Here we have a book model that embeds many authors:
9
+ Here's a book that embeds many authors:
10
10
 
11
11
  class Book
12
12
  include Mongoid::Document
@@ -25,26 +25,18 @@ Here we have a book model that embeds many authors:
25
25
  embedded_in :book, :inverse_of => :authors
26
26
  end
27
27
 
28
- The finders in our controllers would possibly look like:
29
-
30
- class BooksController
31
- def show
32
- @book = Book.where(:slug => params[:id]).first
33
- end
28
+ The finders in our controllers should look like:
34
29
 
35
- # and so on
30
+ def find_book
31
+ @book = Book.where(:slug => params[:id]).first
36
32
  end
37
33
 
38
- class AuthorsController
39
- def show
40
- @book = Book.where(:slug => params[:book_id]).first
41
- @author = @book.authors.where(:slug => params[:id]).first
42
- end
43
-
44
- # and so on
34
+ def find_book_and_author
35
+ @book = Book.where(:slug => params[:book_id]).first
36
+ @author = @book.authors.where(:slug => params[:id]).first
45
37
  end
46
38
 
47
- If you are wondering why I did not include a *find_by_slug* helper, {read on}[http://groups.google.com/group/mongoid/browse_thread/thread/5905589e108d7cc0?pli=1].
39
+ If you are wondering why I did not include a *find_by_slug* helper, [read on](http://groups.google.com/group/mongoid/browse_thread/thread/5905589e108d7cc0?pli=1).
48
40
 
49
41
  To demo some more functionality in the console:
50
42
 
@@ -65,4 +57,4 @@ To demo some more functionality in the console:
65
57
  >> book.authors.where(:slug => 'felix-guattari).first
66
58
  => #<Author _id: 4c31e362faa4a7050e000003, slug: "félix-guattari", last_name: "Guattari", first_name: "Félix">
67
59
 
68
- Last but not least, check out the specs and the sample models I used there for more examples.
60
+ `slug` takes `:as` and `:scoped` as arguments. See models in specs for more examples.
data/lib/mongoid_slug.rb CHANGED
@@ -3,7 +3,7 @@ module Mongoid::Slug
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- cattr_accessor :slug_name, :slugged_fields
6
+ cattr_accessor :slug_name, :slugged_fields, :slug_scoped
7
7
  end
8
8
 
9
9
  module ClassMethods #:nodoc:
@@ -11,10 +11,15 @@ module Mongoid::Slug
11
11
  def slug(*args)
12
12
  options = args.last.is_a?(Hash) ? args.pop : {}
13
13
  self.slug_name = options[:as] || :slug
14
+ self.slug_scoped = options[:scoped] || false
14
15
  self.slugged_fields = args
15
16
 
16
17
  field slug_name
17
- index slug_name, :unique => true
18
+ if slug_scoped
19
+ index slug_name
20
+ else
21
+ index slug_name, :unique => true
22
+ end
18
23
  before_save :generate_slug
19
24
  end
20
25
  end
@@ -27,19 +32,23 @@ module Mongoid::Slug
27
32
 
28
33
  def find_(slug, stack=[])
29
34
  if embedded?
30
- stack << association_name
31
- _parent.send :find_, slug, stack
35
+ if slug_scoped && stack.empty?
36
+ _parent.send(association_name).where( slug_name => slug ).to_a
37
+ else
38
+ stack << association_name
39
+ _parent.send :find_, slug, stack
40
+ end
32
41
  else
33
42
  stack.reverse!
34
43
  path = (stack + [slug_name]).join(".")
35
44
  found = collection.find(path => slug).to_a
36
-
45
+
37
46
  stack.each do |name|
38
47
  if found.any?
39
48
  found = found.first.send(name).to_a
40
49
  end
41
50
  end
42
-
51
+
43
52
  found
44
53
  end
45
54
  end
data/spec/models/foo.rb CHANGED
@@ -4,4 +4,5 @@ class Foo
4
4
  field :name
5
5
  slug :name
6
6
  embeds_many :bars
7
+ embeds_many :sars
7
8
  end
@@ -0,0 +1,7 @@
1
+ class Sar
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :name
5
+ slug :name, :scoped => true
6
+ embedded_in :foo, :inverse_of => :bars
7
+ end
@@ -1,3 +1,5 @@
1
+ #encoding: utf-8
2
+
1
3
  require "spec_helper"
2
4
 
3
5
  describe Mongoid::Slug do
@@ -290,5 +292,23 @@ describe Mongoid::Slug do
290
292
  end
291
293
 
292
294
  end
295
+
296
+ context ":scoped option" do
297
+ before(:each) do
298
+ @foo = Foo.create(:name => "foo")
299
+ @sar = @foo.sars.create(:name => "sar")
300
+ @foo2 = Foo.create(:name => "foo")
301
+ end
302
+
303
+ it "generates a unique slug inside the same parent object" do
304
+ similar_sar = @foo.sars.create(:name => @sar.name)
305
+ similar_sar.to_param.should_not eql @sar.to_param
306
+ end
307
+
308
+ it "generates the same slug in different parent object" do
309
+ other_sar = @foo2.sars.create(:name => @sar.name)
310
+ other_sar.to_param.should eql @sar.to_param
311
+ end
312
+ end
293
313
 
294
314
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require "rubygems"
2
- require "bundler"
3
- Bundler.require(:default)
2
+ require "bundler/setup"
3
+
4
+ require "rspec"
5
+ require "database_cleaner"
6
+ require "mongoid"
4
7
 
5
8
  Mongoid.configure do |config|
6
9
  name = "mongoid_slug_test"
7
- host = "localhost"
8
10
  config.master = Mongo::Connection.new.db(name)
9
11
  end
10
12
 
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: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 5
10
- version: 0.4.5
9
+ - 6
10
+ version: 0.4.6
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-24 00:00:00 +01:00
19
+ date: 2010-08-11 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -44,11 +44,11 @@ extensions: []
44
44
 
45
45
  extra_rdoc_files:
46
46
  - LICENSE
47
- - README.rdoc
47
+ - README.md
48
48
  files:
49
49
  - LICENSE
50
- - README.rdoc
51
50
  - lib/mongoid_slug.rb
51
+ - README.md
52
52
  - spec/models/author.rb
53
53
  - spec/models/bar.rb
54
54
  - spec/models/baz.rb
@@ -56,6 +56,7 @@ files:
56
56
  - spec/models/foo.rb
57
57
  - spec/models/person.rb
58
58
  - spec/models/publisher.rb
59
+ - spec/models/sar.rb
59
60
  - spec/models/subject.rb
60
61
  - spec/mongoid_slug_spec.rb
61
62
  - spec/spec_helper.rb
@@ -101,6 +102,7 @@ test_files:
101
102
  - spec/models/foo.rb
102
103
  - spec/models/person.rb
103
104
  - spec/models/publisher.rb
105
+ - spec/models/sar.rb
104
106
  - spec/models/subject.rb
105
107
  - spec/mongoid_slug_spec.rb
106
108
  - spec/spec_helper.rb