mongoid_slug 0.5.1 → 0.6.0

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.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, you can use the `find_by_slug` helper:
32
+ In your controller, use the `find_by_slug` helper:
33
33
 
34
- def find_book
35
- Book.find_by_slug(params[:book_id])
36
- end
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
- def find_author
39
- @book.authors.find_by_slug(params[:id])
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")
@@ -41,7 +41,7 @@ module Mongoid #:nodoc:
41
41
  field slug_name
42
42
 
43
43
  if options[:index]
44
- index slug_name, :unique => (slug_scope ? false : true)
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
- # Finds the document with the specified slug or returns nil.
55
- def find_by_slug(slug)
56
- where(slug_name => slug).first rescue nil
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
- _parent.send(association_name)
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
@@ -1,5 +1,5 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Slug
3
- VERSION = "0.5.1"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
@@ -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
@@ -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.start }
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
- - 5
8
- - 1
9
- version: 0.5.1
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: 2010-12-30 00:00:00 -03:00
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
- - beta
33
- - 20
34
- version: 2.0.0.beta.20
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
- - 3
92
+ - 4
93
93
  - 0
94
- version: 2.3.0
94
+ version: 2.4.0
95
95
  type: :development
96
96
  version_requirements: *id005
97
97
  - !ruby/object:Gem::Dependency