mongoid_slug 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/mongoid_slug.rb +11 -10
- data/spec/{unit/mongoid_slug_spec.rb → mongoid_slug_spec.rb} +16 -10
- metadata +8 -7
data/README.rdoc
CHANGED
data/lib/mongoid_slug.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
# Generates a URL slug/permalink based on
|
1
|
+
# Generates a URL slug/permalink based on fields in a Mongoid model.
|
2
2
|
module Mongoid::Slug
|
3
3
|
|
4
|
-
def self.included(base)
|
4
|
+
def self.included(base) #:nodoc:
|
5
5
|
base.extend ClassMethods
|
6
6
|
base.class_eval { field :slug; before_save :slugify }
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods #:nodoc:
|
10
10
|
|
11
|
-
# Set a field as source of slug
|
12
|
-
def slug(*
|
13
|
-
class_variable_set(:@@
|
11
|
+
# Set a field or a number of fields as source of slug
|
12
|
+
def slug(*fields)
|
13
|
+
class_variable_set(:@@slugged, fields)
|
14
14
|
end
|
15
15
|
|
16
16
|
def find_by_slug(slug)
|
@@ -29,18 +29,19 @@ module Mongoid::Slug
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def slugify
|
32
|
-
self.slug = find_unique_slug if new_record? ||
|
32
|
+
self.slug = find_unique_slug if new_record? || slugged_changed?
|
33
33
|
end
|
34
34
|
|
35
|
-
def
|
36
|
-
self.class.class_eval('@@
|
35
|
+
def slugged_changed?
|
36
|
+
self.class.class_eval('@@slugged').any? do |field|
|
37
37
|
self.send(field.to_s + '_changed?')
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
def find_unique_slug(suffix='')
|
42
42
|
slug = ("#{slug_base} #{suffix}").parameterize
|
43
|
-
if (embedded? ?
|
43
|
+
if (embedded? ? _parent.collection.find("#{self.class.to_s.downcase.pluralize}.slug" => slug) : collection.find(:slug => slug)).
|
44
|
+
to_a.reject{ |doc| doc.id == self.id }.count == 0
|
44
45
|
slug
|
45
46
|
else
|
46
47
|
new_suffix = suffix.blank? ? '1' : "#{suffix.to_i + 1}"
|
@@ -49,6 +50,6 @@ module Mongoid::Slug
|
|
49
50
|
end
|
50
51
|
|
51
52
|
def slug_base
|
52
|
-
self.class.class_eval('@@
|
53
|
+
self.class.class_eval('@@slugged').collect{ |field| self.send(field) }.join(" ")
|
53
54
|
end
|
54
55
|
end
|
@@ -6,7 +6,7 @@ describe Mongoid::Slug do
|
|
6
6
|
@book = Book.create(:title => "A Thousand Plateaus", :isbn => "9789245242475")
|
7
7
|
end
|
8
8
|
|
9
|
-
context "root" do
|
9
|
+
context "root document" do
|
10
10
|
|
11
11
|
it "generates slug" do
|
12
12
|
@book.to_param.should eql @book.title.parameterize
|
@@ -31,10 +31,16 @@ describe Mongoid::Slug do
|
|
31
31
|
@book.slug.should_not match /\d$/
|
32
32
|
end
|
33
33
|
|
34
|
-
it "does not update slug if slugged
|
35
|
-
|
36
|
-
@book.update_attributes(
|
37
|
-
@book.slug.should eql
|
34
|
+
it "does not update slug if slugged fields have not changed" do
|
35
|
+
former_slug = @book.slug
|
36
|
+
@book.update_attributes(:isbn => "9785545858118")
|
37
|
+
@book.slug.should eql former_slug
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not update slug if slugged fields have changed but generated slug is the same" do
|
41
|
+
former_slug = @book.slug
|
42
|
+
@book.update_attributes(:title => "A thousand plateaus")
|
43
|
+
@book.slug.should eql former_slug
|
38
44
|
end
|
39
45
|
|
40
46
|
context ".find_by_slug" do
|
@@ -75,9 +81,9 @@ describe Mongoid::Slug do
|
|
75
81
|
end
|
76
82
|
|
77
83
|
it "does not update slug if slugged field has not changed" do
|
78
|
-
|
84
|
+
former_slug = @subject.slug
|
79
85
|
@subject.update_attributes(:description => "Lorem ipsum dolor sit amet")
|
80
|
-
@subject.slug.should eql
|
86
|
+
@subject.slug.should eql former_slug
|
81
87
|
end
|
82
88
|
|
83
89
|
context ".find_by_slug" do
|
@@ -108,14 +114,14 @@ describe Mongoid::Slug do
|
|
108
114
|
end
|
109
115
|
|
110
116
|
it "does not update slug if slugged field has not changed" do
|
111
|
-
|
117
|
+
former_slug = @publisher.slug
|
112
118
|
@publisher.update_attributes(:year => 2001)
|
113
|
-
@publisher.slug.should eql
|
119
|
+
@publisher.slug.should eql former_slug
|
114
120
|
end
|
115
121
|
|
116
122
|
end
|
117
123
|
|
118
|
-
context "
|
124
|
+
context "composite fields" do
|
119
125
|
before(:each) do
|
120
126
|
@author = Author.create(:first_name => "Gilles", :last_name => "Deleuze")
|
121
127
|
end
|
metadata
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_slug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Hakan Ensari
|
14
|
+
- Gerhard Lazu
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2010-06-
|
19
|
+
date: 2010-06-29 00:00:00 +01:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|
@@ -35,7 +36,7 @@ dependencies:
|
|
35
36
|
version: 2.0.0.beta7
|
36
37
|
type: :runtime
|
37
38
|
version_requirements: *id001
|
38
|
-
description: Mongoid Slug generates a URL slug/permalink based on
|
39
|
+
description: Mongoid Slug generates a URL slug/permalink based on fields in a Mongoid model.
|
39
40
|
email: code@papercavalier.com
|
40
41
|
executables: []
|
41
42
|
|
@@ -52,8 +53,8 @@ files:
|
|
52
53
|
- spec/models/book.rb
|
53
54
|
- spec/models/publisher.rb
|
54
55
|
- spec/models/subject.rb
|
56
|
+
- spec/mongoid_slug_spec.rb
|
55
57
|
- spec/spec_helper.rb
|
56
|
-
- spec/unit/mongoid_slug_spec.rb
|
57
58
|
has_rdoc: true
|
58
59
|
homepage: http://github.com/papercavalier/mongoid-slug
|
59
60
|
licenses: []
|
@@ -93,5 +94,5 @@ test_files:
|
|
93
94
|
- spec/models/book.rb
|
94
95
|
- spec/models/publisher.rb
|
95
96
|
- spec/models/subject.rb
|
97
|
+
- spec/mongoid_slug_spec.rb
|
96
98
|
- spec/spec_helper.rb
|
97
|
-
- spec/unit/mongoid_slug_spec.rb
|