mongoid_slug 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mongoid/slug.rb CHANGED
@@ -92,6 +92,10 @@ module Mongoid #:nodoc:
92
92
  def self.find_by_#{slug_name}(slug)
93
93
  where(slug_name => slug).first
94
94
  end
95
+
96
+ def self.find_by_#{slug_name}!(slug)
97
+ where(slug_name => slug).first || raise(Mongoid::Errors::DocumentNotFound.new(self.class, slug))
98
+ end
95
99
  CODE
96
100
  end
97
101
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Slug
3
- VERSION = '0.7.1'
3
+ VERSION = '0.7.2'
4
4
  end
5
5
  end
@@ -4,6 +4,13 @@ class Caption
4
4
  field :identity
5
5
  field :title
6
6
  field :medium
7
+
8
+ # A fairly complex scenario, where we want to create a slug out of an
9
+ # identity field, which comprises name of artist and some more bibliographic
10
+ # info in parantheses, and the title of the work.
11
+ #
12
+ # We are only interested in the name of the artist so we remove the
13
+ # paranthesized details.
7
14
  slug :identity, :title do |doc|
8
15
  [doc.identity.gsub(/\s*\([^)]+\)/, ''), doc.title].join(' ')
9
16
  end
@@ -349,10 +349,29 @@ module Mongoid
349
349
  end
350
350
 
351
351
  describe ".find_by_slug" do
352
+ let!(:book) { Book.create(:title => "A Thousand Plateaus") }
353
+
352
354
  it "returns nil if no document is found" do
353
- Book.create(:title => "A Thousand Plateaus")
354
355
  Book.find_by_slug(:title => "Anti Oedipus").should be_nil
355
356
  end
357
+
358
+ it "returns the document if it is found" do
359
+ Book.find_by_slug(book.slug).should == book
360
+ end
361
+ end
362
+
363
+ describe ".find_by_slug!" do
364
+ let!(:book) { Book.create(:title => "A Thousand Plateaus") }
365
+
366
+ it "raises a Mongoid::Errors::DocumentNotFound error if no document is found" do
367
+ lambda {
368
+ Book.find_by_slug!(:title => "Anti Oedipus")
369
+ }.should raise_error(Mongoid::Errors::DocumentNotFound)
370
+ end
371
+
372
+ it "returns the document when it is found" do
373
+ Book.find_by_slug!(book.slug).should == book
374
+ end
356
375
  end
357
376
  end
358
377
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid_slug
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.1
5
+ version: 0.7.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Paper Cavalier
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-02 00:00:00 +01:00
13
+ date: 2011-04-14 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency