mongoid_slug 3.1.2 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae9fab25b8db6880157df5b1fcab5608336cfe9e
4
- data.tar.gz: e8cd36c3abc7acb9b1850e17feff903d4c965743
3
+ metadata.gz: cb9aaa8ca267d57f8a6b9a301cc904096c5b511a
4
+ data.tar.gz: 4ab3f142f2fcb037d5e9b1e0c61b66ea44d8d770
5
5
  SHA512:
6
- metadata.gz: 21ee51edcd378221ba9057a806d4d3c609df8704a1c297145e3c72c8439d0a4266e4947faed5f8b8d487042b73bdd7759cab0618e001f5c2d957123d331509fc
7
- data.tar.gz: 0a11a12f37b1b45a855ba89ca854b16adb71e184db4ca2c1a5799e9c50c511acd700807551a432f8224c4d29c912357e35b5576dccc13a29e9391e30f691f7f2
6
+ metadata.gz: 4432bce036538231bbf2b60704b192f6b600f1129400aa804093226369e01df038a215c7af1a058192b663ed9fcbb046285793040483c3882102a98d6fcacbdf
7
+ data.tar.gz: ae81c2411c4c53542ac761048dac229e2eb6016f11dae322e20b7060dbf575155cbed2d5874c4c1931fbe9cca878dfb63524a1ea884f782d44f7f5d95d5dcd49
data/README.md CHANGED
@@ -257,7 +257,7 @@ Custom Find Strategies
257
257
  --------------
258
258
 
259
259
  By default find will search for the document by the id field if the provided id
260
- looks like a BSON ObjectId, and it will otherwise find by the _slugs field. However,
260
+ looks like a BSON::ObjectId, and it will otherwise find by the _slugs field. However,
261
261
  custom strategies can ovveride the default behavior, like e.g:
262
262
 
263
263
  ```ruby
@@ -133,8 +133,6 @@ module Mongoid
133
133
  if localized?
134
134
  begin
135
135
  orig_locale = I18n.locale
136
- all_locales = self.slugged_attributes
137
- .map{|attr| self.send("#{attr}_translations").keys}.flatten.uniq
138
136
  all_locales.each do |target_locale|
139
137
  I18n.locale = target_locale
140
138
  set_slug
@@ -206,7 +204,9 @@ module Mongoid
206
204
  _cur_slug || pre_slug_string
207
205
  end
208
206
 
209
-
207
+ def self.mongoid3?
208
+ ::Mongoid.const_defined? :Observer
209
+ end
210
210
 
211
211
  private
212
212
 
@@ -253,6 +253,18 @@ module Mongoid
253
253
  self.fields['_slugs'].options[:localize] rescue false
254
254
  end
255
255
 
256
+ # Return all possible locales for model
257
+ # Avoiding usage of I18n.available_locales in case the user hasn't set it properly, or is
258
+ # doing something crazy, but at the same time we need a fallback in case the model doesn't
259
+ # have any localized attributes at all (extreme edge case).
260
+ def all_locales
261
+ locales = self.slugged_attributes
262
+ .map{|attr| self.send("#{attr}_translations").keys if self.respond_to?("#{attr}_translations")}
263
+ .flatten.compact.uniq
264
+ locales = I18n.available_locales if locales.empty?
265
+ locales
266
+ end
267
+
256
268
  def pre_slug_string
257
269
  self.slugged_attributes.map { |f| self.send f }.join ' '
258
270
  end
@@ -1,12 +1,10 @@
1
- # require 'moped/bson/object_id'
2
-
3
1
  module Mongoid
4
2
  module Slug
5
3
  class Criteria < Mongoid::Criteria
6
4
  # Find the matchind document(s) in the criteria for the provided ids or slugs.
7
5
  #
8
- # If the document _ids are of the type Moped::BSON::ObjectId, and all the supplied parameters are
9
- # convertible to Moped::BSON::ObjectId (via Moped::BSON::ObjectId#from_string), finding will be
6
+ # If the document _ids are of the type BSON::ObjectId, and all the supplied parameters are
7
+ # convertible to BSON::ObjectId (via BSON::ObjectId#from_string), finding will be
10
8
  # performed via _ids.
11
9
  #
12
10
  # If the document has any other type of _id field, and all the supplied parameters are of the same
@@ -15,10 +13,10 @@ module Mongoid
15
13
  # Otherwise finding will be performed via slugs.
16
14
  #
17
15
  # @example Find by an id.
18
- # criteria.find(Moped::BSON::ObjectId.new)
16
+ # criteria.find(BSON::ObjectId.new)
19
17
  #
20
18
  # @example Find by multiple ids.
21
- # criteria.find([ Moped::BSON::ObjectId.new, Moped::BSON::ObjectId.new ])
19
+ # criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
22
20
  #
23
21
  # @example Find by a slug.
24
22
  # criteria.find('some-slug')
@@ -67,9 +65,13 @@ module Mongoid
67
65
  self.respond_to?(type_method, true) ? method(type_method) : lambda {|id| false}
68
66
  end
69
67
 
70
- # a string will not look like a slug if it looks like a legal ObjectId
68
+ # a string will not look like a slug if it looks like a legal BSON::ObjectId
71
69
  def objectid_slug_strategy id
72
- Moped::BSON::ObjectId.legal?(id)
70
+ if Mongoid::Slug.mongoid3?
71
+ Moped::BSON::ObjectId.legal? id
72
+ else
73
+ BSON::ObjectId.legal?(id)
74
+ end
73
75
  end
74
76
 
75
77
  # a string will always look like a slug
@@ -1,5 +1,5 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Slug
3
- VERSION = '3.1.2'
3
+ VERSION = '3.2.0'
4
4
  end
5
5
  end
@@ -0,0 +1,11 @@
1
+ class PageSlugLocalizedCustom
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ attr_accessor :title
6
+
7
+ slug :title, localize: true do |obj|
8
+ obj.title.to_url
9
+ end
10
+
11
+ end
@@ -187,4 +187,4 @@ describe Mongoid::Slug::Criteria do
187
187
  end
188
188
  end
189
189
  end
190
- end
190
+ end
@@ -101,8 +101,8 @@ module Mongoid
101
101
  }
102
102
  end
103
103
 
104
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
105
- bson_id = Moped::BSON::ObjectId.new.to_s
104
+ it "does not allow a BSON::ObjectId as use for a slug" do
105
+ bson_id = Mongoid::Slug.mongoid3? ? Moped::BSON::ObjectId.new.to_s : BSON::ObjectId.new.to_s
106
106
  bad = Book.create(:title => bson_id)
107
107
  bad.slugs.should_not include(bson_id)
108
108
  end
@@ -127,11 +127,11 @@ module Mongoid
127
127
  Book.find([book.id.to_s]).should eql [book]
128
128
  end
129
129
 
130
- it "finds by id as Moped::BSON::ObjectId" do
130
+ it "finds by id as BSON::ObjectId" do
131
131
  Book.find(book.id).should eql book
132
132
  end
133
133
 
134
- it "finds by id as an array of Moped::BSON::ObjectIds" do
134
+ it "finds by id as an array of BSON::ObjectIds" do
135
135
  Book.find([book.id]).should eql [book]
136
136
  end
137
137
 
@@ -161,7 +161,7 @@ module Mongoid
161
161
  dup.to_param.should eql "psychoanalysis-1"
162
162
  end
163
163
 
164
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
164
+ it "does not allow a BSON::ObjectId as use for a slug" do
165
165
  bad = book.subjects.create(:name => "4ea0389f0364313d79104fb3")
166
166
  bad.slugs.should_not eql "4ea0389f0364313d79104fb3"
167
167
  end
@@ -185,11 +185,11 @@ module Mongoid
185
185
  book.subjects.find([subject.id.to_s]).should eql [subject]
186
186
  end
187
187
 
188
- it "finds by id as Moped::BSON::ObjectId" do
188
+ it "finds by id as BSON::ObjectId" do
189
189
  book.subjects.find(subject.id).should eql subject
190
190
  end
191
191
 
192
- it "finds by id as an array of Moped::BSON::ObjectIds" do
192
+ it "finds by id as an array of BSON::ObjectIds" do
193
193
  book.subjects.find([subject.id]).should eql [subject]
194
194
  end
195
195
 
@@ -228,7 +228,7 @@ module Mongoid
228
228
  dup.to_param.should eql "jane-smith-1"
229
229
  end
230
230
 
231
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
231
+ it "does not allow a BSON::ObjectId as use for a slug" do
232
232
  bad = relationship.partners.create(:name => "4ea0389f0364313d79104fb3")
233
233
  bad.slugs.should_not eql "4ea0389f0364313d79104fb3"
234
234
  end
@@ -258,11 +258,11 @@ module Mongoid
258
258
  relationship.partners.find([partner.id.to_s]).should eql [partner]
259
259
  end
260
260
 
261
- it "finds by id as Moped::BSON::ObjectId" do
261
+ it "finds by id as BSON::ObjectId" do
262
262
  relationship.partners.find(partner.id).should eql partner
263
263
  end
264
264
 
265
- it "finds by id as an array of Moped::BSON::ObjectIds" do
265
+ it "finds by id as an array of BSON::ObjectIds" do
266
266
  relationship.partners.find([partner.id]).should eql [partner]
267
267
  end
268
268
 
@@ -305,7 +305,7 @@ module Mongoid
305
305
  dup2.to_param.should eql "gilles-deleuze-2"
306
306
  end
307
307
 
308
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
308
+ it "does not allow a BSON::ObjectId as use for a slug" do
309
309
  bad = Author.create(:first_name => "4ea0389f0364",
310
310
  :last_name => "313d79104fb3")
311
311
  bad.to_param.should_not eql "4ea0389f0364313d79104fb3"
@@ -372,7 +372,7 @@ module Mongoid
372
372
  dup.to_param.should eql "book-title-1"
373
373
  end
374
374
 
375
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
375
+ it "does not allow a BSON::ObjectId as use for a slug" do
376
376
  bad = Book.create(:title => "4ea0389f0364313d79104fb3")
377
377
  bad.to_param.should_not eql "4ea0389f0364313d79104fb3"
378
378
  end
@@ -405,7 +405,7 @@ module Mongoid
405
405
  dup.to_param.should eql "gilles-deleuze-1"
406
406
  end
407
407
 
408
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
408
+ it "does not allow a BSON::ObjectId as use for a slug" do
409
409
  bad = book.authors.create(:first_name => "4ea0389f0364",
410
410
  :last_name => "313d79104fb3")
411
411
  bad.to_param.should_not eql "4ea0389f0364313d79104fb3"
@@ -447,7 +447,7 @@ module Mongoid
447
447
  dup.to_param.should eql "big-weekly-1"
448
448
  end
449
449
 
450
- it "does not allow a Moped::BSON::ObjectId as use for a slug" do
450
+ it "does not allow a BSON::ObjectId as use for a slug" do
451
451
  bad = Magazine.create(:title => "4ea0389f0364313d79104fb3", :publisher_id => "abc123")
452
452
  bad.to_param.should_not eql "4ea0389f0364313d79104fb3"
453
453
  end
@@ -522,36 +522,24 @@ module Mongoid
522
522
  end
523
523
 
524
524
  context "when slug is not scoped by a reference association" do
525
- it "defines an index on the slug" do
526
- Book.index_options.should have_key( :_slugs => 1 )
527
- end
528
-
529
- it "defines a unique index" do
530
- Book.index_options[ :_slugs => 1 ][:unique].should be_true
531
- end
525
+ subject { Book }
526
+ it_should_behave_like "has an index", { _slugs: 1 }, { unique: true }
532
527
  end
533
528
 
534
529
  context "when slug is scoped by a reference association" do
535
- it "does not define an index on the slug" do
536
- Author.index_options.should_not have_key(:_slugs => 1 )
537
- end
530
+ subject { Author }
531
+ it_should_behave_like "does not have an index", { _slugs: 1 }
538
532
  end
539
533
 
540
534
  context "for subclass scope" do
541
535
  context "when slug is not scoped by a reference association" do
542
- it "defines an index on the slug" do
543
- BookPolymorphic.index_options.should have_key( :_type => 1, :_slugs => 1 )
544
- end
545
-
546
- it "defines a unique index" do
547
- BookPolymorphic.index_options[ :_type => 1, :_slugs => 1 ][:unique].should_not be_true
548
- end
536
+ subject { BookPolymorphic }
537
+ it_should_behave_like "has an index", { _type: 1, _slugs: 1 }, { unique: nil }
549
538
  end
550
539
 
551
540
  context "when slug is scoped by a reference association" do
552
- it "does not define an index on the slug" do
553
- AuthorPolymorphic.index_options.should_not have_key(:_type => 1, :_slugs => 1 )
554
- end
541
+ subject { AuthorPolymorphic }
542
+ it_should_behave_like "does not have an index", { _type: 1, _slugs: 1 }
555
543
  end
556
544
 
557
545
  context "when the object has STI" do
@@ -879,6 +867,13 @@ module Mongoid
879
867
  page["_slugs"].should == {"en" => ["modified-title-on-english"],
880
868
  "nl" => ["title-on-netherlands"]}
881
869
  end
870
+
871
+ it "works with a custom slug strategy" do
872
+ page = PageSlugLocalizedCustom.new
873
+ page.title = "a title for the slug"
874
+ page.save
875
+ page["_slugs"].should == {"en" => ["a-title-for-the-slug"]}
876
+ end
882
877
  end
883
878
 
884
879
  context "slug can be localized when using history" do
@@ -0,0 +1,27 @@
1
+ shared_examples "has an index" do |key, options|
2
+ it "has a #{key} index" do
3
+ index = if Mongoid::Slug.mongoid3?
4
+ subject.index_options[key]
5
+ else
6
+ subject.index_specifications.detect { |spec| spec.key == key }
7
+ end
8
+ index.should_not be_nil
9
+ options.each_pair { |name, value|
10
+ if Mongoid::Slug.mongoid3?
11
+ index[name].should == value
12
+ else
13
+ index.options[name].should == value
14
+ end
15
+ } if options
16
+ end
17
+ end
18
+
19
+ shared_examples "does not have an index" do |key, option|
20
+ it "does not have the #{key} index" do
21
+ if Mongoid::Slug.mongoid3?
22
+ subject.index_options[key].should be_nil
23
+ else
24
+ subject.index_specifications.detect { |spec| spec.key == key }.should be_nil
25
+ end
26
+ end
27
+ end
@@ -23,7 +23,9 @@ Mongoid.configure do |config|
23
23
  config.connect_to database_id
24
24
  end
25
25
 
26
- Dir['./spec/models/*.rb'].each { |f| require f }
26
+ [ 'models', 'shared' ].each do |dir|
27
+ Dir["./spec/#{dir}/*.rb"].each { |f| require f }
28
+ end
27
29
 
28
30
  RSpec.configure do |c|
29
31
  c.before(:each) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Saebjoernsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-02 00:00:00.000000000 Z
11
+ date: 2013-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '1.4'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '1.4'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: guard-rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -152,6 +152,7 @@ files:
152
152
  - spec/models/page.rb
153
153
  - spec/models/page_localize.rb
154
154
  - spec/models/page_slug_localized.rb
155
+ - spec/models/page_slug_localized_custom.rb
155
156
  - spec/models/page_slug_localized_history.rb
156
157
  - spec/models/paranoid_document.rb
157
158
  - spec/models/partner.rb
@@ -163,9 +164,11 @@ files:
163
164
  - spec/mongoid/criteria_spec.rb
164
165
  - spec/mongoid/slug_spec.rb
165
166
  - spec/mongoid/slug_spec.rb.b00
167
+ - spec/shared/indexes.rb
166
168
  - spec/spec_helper.rb
167
169
  homepage: https://github.com/digitalplaywright/mongoid-slug
168
- licenses: []
170
+ licenses:
171
+ - MIT
169
172
  metadata: {}
170
173
  post_install_message:
171
174
  rdoc_options: []
@@ -183,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
186
  version: '0'
184
187
  requirements: []
185
188
  rubyforge_project: mongoid_slug
186
- rubygems_version: 2.0.3
189
+ rubygems_version: 2.1.10
187
190
  signing_key:
188
191
  specification_version: 4
189
192
  summary: Mongoid URL slugs
@@ -203,6 +206,7 @@ test_files:
203
206
  - spec/models/page.rb
204
207
  - spec/models/page_localize.rb
205
208
  - spec/models/page_slug_localized.rb
209
+ - spec/models/page_slug_localized_custom.rb
206
210
  - spec/models/page_slug_localized_history.rb
207
211
  - spec/models/paranoid_document.rb
208
212
  - spec/models/partner.rb
@@ -214,5 +218,5 @@ test_files:
214
218
  - spec/mongoid/criteria_spec.rb
215
219
  - spec/mongoid/slug_spec.rb
216
220
  - spec/mongoid/slug_spec.rb.b00
221
+ - spec/shared/indexes.rb
217
222
  - spec/spec_helper.rb
218
- has_rdoc: