mongoid_slug 0.2.1 → 0.3.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.rdoc +1 -1
- data/lib/mongoid_slug.rb +21 -16
- data/spec/mongoid_slug_spec.rb +4 -8
- data/spec/spec_helper.rb +4 -3
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -15,5 +15,5 @@ This gem generates a URL slug/permalink based on fields in a Mongoid model.
|
|
15
15
|
>> book.update_attributes(:title => "Anti Oedipus")
|
16
16
|
>> book.to_param
|
17
17
|
"anti-oedipus"
|
18
|
-
>> Book.find_by_slug("anti-oedipus")
|
18
|
+
>> Book.find_by_slug("anti-oedipus").first
|
19
19
|
#<Book _id: 4c23b1f7faa4a7479a000009, slug: "anti-oedipus", title: "Anti Oedipus">
|
data/lib/mongoid_slug.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
# Generates a URL slug/permalink based on fields in a Mongoid model.
|
2
2
|
module Mongoid::Slug
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
cattr_accessor :slugged
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods #:nodoc:
|
10
|
-
|
11
10
|
# Set a field or a number of fields as source of slug
|
12
11
|
def slug(*fields)
|
13
|
-
|
12
|
+
self.slugged = fields
|
13
|
+
field :slug; index :slug, :unique => true; before_save :slugify
|
14
14
|
end
|
15
15
|
|
16
|
+
# This returns an array containing the match rather than
|
17
|
+
# the match itself.
|
18
|
+
#
|
19
|
+
# http://groups.google.com/group/mongoid/browse_thread/thread/5905589e108d7cc0
|
16
20
|
def find_by_slug(slug)
|
17
|
-
|
18
|
-
raise 'I have not implemented this method yet for embedded docs. Try: parent.children.where(:slug => "foo") instead'
|
19
|
-
else
|
20
|
-
where(:slug => slug).first
|
21
|
-
end
|
21
|
+
where(:slug => slug).limit(1)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -29,19 +29,24 @@ module Mongoid::Slug
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def slugify
|
32
|
-
|
32
|
+
if new_record? || slugged_changed?
|
33
|
+
self.slug = find_unique_slug
|
34
|
+
end
|
33
35
|
end
|
34
36
|
|
35
37
|
def slugged_changed?
|
36
|
-
self.class.
|
38
|
+
self.class.slugged.any? do |field|
|
37
39
|
self.send(field.to_s + '_changed?')
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
43
|
def find_unique_slug(suffix='')
|
42
44
|
slug = ("#{slug_base} #{suffix}").parameterize
|
43
|
-
|
44
|
-
|
45
|
+
|
46
|
+
if (embedded? ?
|
47
|
+
_parent.collection.find("#{association_name}.slug" => slug) :
|
48
|
+
collection.find(:slug => slug)
|
49
|
+
).reject{ |doc| doc.id == self.id }.empty?
|
45
50
|
slug
|
46
51
|
else
|
47
52
|
new_suffix = suffix.blank? ? '1' : "#{suffix.to_i + 1}"
|
@@ -50,6 +55,6 @@ module Mongoid::Slug
|
|
50
55
|
end
|
51
56
|
|
52
57
|
def slug_base
|
53
|
-
self.class.
|
58
|
+
self.class.slugged.collect{ |field| self.send(field) }.join(" ")
|
54
59
|
end
|
55
60
|
end
|
data/spec/mongoid_slug_spec.rb
CHANGED
@@ -45,7 +45,7 @@ describe Mongoid::Slug do
|
|
45
45
|
|
46
46
|
context ".find_by_slug" do
|
47
47
|
it "finds by slug" do
|
48
|
-
Book.find_by_slug(@book.slug).should eql @book
|
48
|
+
Book.find_by_slug(@book.slug).first.should eql @book
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -87,12 +87,8 @@ describe Mongoid::Slug do
|
|
87
87
|
end
|
88
88
|
|
89
89
|
context ".find_by_slug" do
|
90
|
-
it "
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
it "does find by a regular where" do
|
95
|
-
@book.subjects.where(:slug => @subject.slug).first.should eql @subject
|
90
|
+
it "finds by slug" do
|
91
|
+
@book.subjects.find_by_slug(@subject.slug).first.should eql @subject
|
96
92
|
end
|
97
93
|
end
|
98
94
|
|
@@ -153,7 +149,7 @@ describe Mongoid::Slug do
|
|
153
149
|
|
154
150
|
context ".find_by_slug" do
|
155
151
|
it "finds by slug" do
|
156
|
-
Author.find_by_slug("gilles-deleuze").should eql @author
|
152
|
+
Author.find_by_slug("gilles-deleuze").first.should eql @author
|
157
153
|
end
|
158
154
|
end
|
159
155
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,16 +2,17 @@ require "rubygems"
|
|
2
2
|
require "bundler"
|
3
3
|
Bundler.require(:default)
|
4
4
|
|
5
|
-
require File.expand_path("../../lib/mongoid_slug", __FILE__)
|
6
|
-
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
7
|
-
|
8
5
|
Mongoid.configure do |config|
|
9
6
|
name = "mongoid_slug_test"
|
10
7
|
host = "localhost"
|
11
8
|
config.master = Mongo::Connection.new.db(name)
|
12
9
|
end
|
13
10
|
|
11
|
+
require File.expand_path("../../lib/mongoid_slug", __FILE__)
|
12
|
+
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
13
|
+
|
14
14
|
DatabaseCleaner.orm = "mongoid"
|
15
|
+
|
15
16
|
Rspec.configure do |config|
|
16
17
|
config.before(:all) do
|
17
18
|
DatabaseCleaner.strategy = :truncation
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
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-06-
|
19
|
+
date: 2010-06-30 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|