mongoid_slug 0.6.3 → 0.6.4

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,5 +1,5 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Slug
3
- VERSION = '0.6.3'
3
+ VERSION = '0.6.4'
4
4
  end
5
5
  end
data/lib/mongoid/slug.rb CHANGED
@@ -26,6 +26,13 @@ module Mongoid #:nodoc:
26
26
 
27
27
  self.slug_name = options[:as] || :slug
28
28
  self.slug_scope = options[:scope] || nil
29
+
30
+ class_eval <<-CODE
31
+ def slug_any?
32
+ #{!!options[:any]}
33
+ end
34
+ CODE
35
+
29
36
  self.slugged_fields = fields
30
37
 
31
38
  if options[:scoped]
@@ -101,9 +108,15 @@ module Mongoid #:nodoc:
101
108
  end
102
109
 
103
110
  def slug_base
104
- self.class.slugged_fields.map do |field|
111
+ values = self.class.slugged_fields.map do |field|
105
112
  self.send(field)
106
- end.join(" ")
113
+ end
114
+
115
+ if slug_any?
116
+ values.detect { |value| value.present? }
117
+ else
118
+ values.join(' ')
119
+ end
107
120
  end
108
121
 
109
122
  def slugged_fields_changed?
@@ -0,0 +1,7 @@
1
+ class Article
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :brief
5
+ field :title
6
+ slug :title, :brief, :any => true
7
+ end
@@ -157,6 +157,26 @@ module Mongoid
157
157
  it "finds by slug" do
158
158
  Author.find_by_slug("gilles-deleuze").should eql author
159
159
  end
160
+
161
+ end
162
+
163
+ context "when :any is passed as an argument" do
164
+ let!(:article) do
165
+ Article.create(
166
+ :brief => "This is the brief",
167
+ :title => "This is the title")
168
+ end
169
+
170
+ it "uses the first available field for the slug if any option is used" do
171
+ article.to_param.should eql 'this-is-the-title'
172
+ article.title = ""
173
+ article.save
174
+ article.to_param.should eql 'this-is-the-brief'
175
+
176
+ article.title = nil
177
+ article.save
178
+ article.to_param.should eql 'this-is-the-brief'
179
+ end
160
180
  end
161
181
 
162
182
  context "when :as is passed as an argument" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongoid_slug
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.3
5
+ version: 0.6.4
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-02-17 00:00:00 +00:00
13
+ date: 2011-02-27 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -93,6 +93,7 @@ files:
93
93
  - lib/mongoid/slug.rb
94
94
  - LICENSE
95
95
  - README.md
96
+ - spec/models/article.rb
96
97
  - spec/models/author.rb
97
98
  - spec/models/book.rb
98
99
  - spec/models/comic_book.rb
@@ -126,11 +127,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
127
  requirements: []
127
128
 
128
129
  rubyforge_project: mongoid_slug
129
- rubygems_version: 1.5.0
130
+ rubygems_version: 1.5.2
130
131
  signing_key:
131
132
  specification_version: 3
132
133
  summary: Generates a URL slug
133
134
  test_files:
135
+ - spec/models/article.rb
134
136
  - spec/models/author.rb
135
137
  - spec/models/book.rb
136
138
  - spec/models/comic_book.rb