carrierwave-mongoid 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/carrierwave/mongoid.rb +2 -2
- data/lib/carrierwave/mongoid/version.rb +1 -1
- data/spec/mongoid_spec.rb +93 -21
- metadata +16 -16
data/lib/carrierwave/mongoid.rb
CHANGED
@@ -52,11 +52,11 @@ module CarrierWave
|
|
52
52
|
if self.embedded?
|
53
53
|
ancestors = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
|
54
54
|
first_parent = ancestors.first.last
|
55
|
-
reloaded_parent = first_parent.class.find(first_parent.to_key.first)
|
55
|
+
reloaded_parent = first_parent.class.unscoped.find(first_parent.to_key.first)
|
56
56
|
association = ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }
|
57
57
|
association.is_a?(Array) ? association.find(to_key.first) : association
|
58
58
|
else
|
59
|
-
self.class.find(to_key.first)
|
59
|
+
self.class.unscoped.find(to_key.first)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
data/spec/mongoid_spec.rb
CHANGED
@@ -470,6 +470,36 @@ describe CarrierWave::Mongoid do
|
|
470
470
|
end
|
471
471
|
end
|
472
472
|
|
473
|
+
shared_examples "double embedded documents" do
|
474
|
+
it "should remove old file if old file had a different path" do
|
475
|
+
@double_embedded_doc.image = stub_file('new.jpeg')
|
476
|
+
@double_embedded_doc.save.should be_true
|
477
|
+
File.exists?(public_path('uploads/new.jpeg')).should be_true
|
478
|
+
File.exists?(public_path('uploads/old.jpeg')).should be_false
|
479
|
+
end
|
480
|
+
|
481
|
+
it "should not remove old file if old file had a different path but config is false" do
|
482
|
+
@double_embedded_doc.image.stub!(:remove_previously_stored_files_after_update).and_return(false)
|
483
|
+
@double_embedded_doc.image = stub_file('new.jpeg')
|
484
|
+
@double_embedded_doc.save.should be_true
|
485
|
+
File.exists?(public_path('uploads/new.jpeg')).should be_true
|
486
|
+
File.exists?(public_path('uploads/old.jpeg')).should be_true
|
487
|
+
end
|
488
|
+
|
489
|
+
it "should not remove file if old file had the same path" do
|
490
|
+
@double_embedded_doc.image = stub_file('old.jpeg')
|
491
|
+
@double_embedded_doc.save.should be_true
|
492
|
+
File.exists?(public_path('uploads/old.jpeg')).should be_true
|
493
|
+
end
|
494
|
+
|
495
|
+
it "should not remove file if validations fail on save" do
|
496
|
+
@double_embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
|
497
|
+
@double_embedded_doc.image = stub_file('new.jpeg')
|
498
|
+
@double_embedded_doc.save.should be_false
|
499
|
+
File.exists?(public_path('uploads/old.jpeg')).should be_true
|
500
|
+
end
|
501
|
+
|
502
|
+
end
|
473
503
|
|
474
504
|
describe 'with document embedded as embeds_one' do
|
475
505
|
before do
|
@@ -492,6 +522,28 @@ describe CarrierWave::Mongoid do
|
|
492
522
|
include_examples "embedded documents"
|
493
523
|
end
|
494
524
|
|
525
|
+
describe 'with document embedded as embeds_one and parent document not matched the default scope' do
|
526
|
+
before do
|
527
|
+
@embedded_doc_class = define_mongo_class('MongoLocation') do
|
528
|
+
include Mongoid::Document
|
529
|
+
mount_uploader :image, @uploader
|
530
|
+
embedded_in :mongo_user
|
531
|
+
end
|
532
|
+
|
533
|
+
@class.class_eval do
|
534
|
+
embeds_one :mongo_location
|
535
|
+
default_scope where(:always_false => false)
|
536
|
+
end
|
537
|
+
|
538
|
+
@doc = @class.new
|
539
|
+
@embedded_doc = @doc.build_mongo_location
|
540
|
+
@embedded_doc.image = stub_file('old.jpeg')
|
541
|
+
@embedded_doc.save.should be_true
|
542
|
+
end
|
543
|
+
|
544
|
+
include_examples "embedded documents"
|
545
|
+
end
|
546
|
+
|
495
547
|
describe 'with embedded documents' do
|
496
548
|
before do
|
497
549
|
@embedded_doc_class = define_mongo_class('MongoLocation') do
|
@@ -535,36 +587,56 @@ describe CarrierWave::Mongoid do
|
|
535
587
|
@double_embedded_doc.save.should be_true
|
536
588
|
end
|
537
589
|
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
590
|
+
include_examples "double embedded documents"
|
591
|
+
end
|
592
|
+
end
|
593
|
+
|
594
|
+
describe 'with embedded documents and parent document not matched the default scope' do
|
595
|
+
before do
|
596
|
+
@embedded_doc_class = define_mongo_class('MongoLocation') do
|
597
|
+
include Mongoid::Document
|
598
|
+
mount_uploader :image, @uploader
|
599
|
+
embedded_in :mongo_user
|
543
600
|
end
|
544
601
|
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
@double_embedded_doc.save.should be_true
|
549
|
-
File.exists?(public_path('uploads/new.jpeg')).should be_true
|
550
|
-
File.exists?(public_path('uploads/old.jpeg')).should be_true
|
602
|
+
@class.class_eval do
|
603
|
+
embeds_many :mongo_locations
|
604
|
+
default_scope where(:always_false => false)
|
551
605
|
end
|
552
606
|
|
553
|
-
|
607
|
+
@doc = @class.new
|
608
|
+
@embedded_doc = @doc.mongo_locations.build
|
609
|
+
@embedded_doc.image = stub_file('old.jpeg')
|
610
|
+
@embedded_doc.save.should be_true
|
611
|
+
end
|
612
|
+
|
613
|
+
include_examples "embedded documents"
|
614
|
+
|
615
|
+
describe 'with double embedded documents' do
|
616
|
+
|
617
|
+
before do
|
618
|
+
@double_embedded_doc_class = define_mongo_class('MongoItem') do
|
619
|
+
include Mongoid::Document
|
620
|
+
mount_uploader :image, @uploader
|
621
|
+
embedded_in :mongo_location
|
622
|
+
end
|
623
|
+
|
624
|
+
@embedded_doc_class.class_eval do
|
625
|
+
embeds_many :mongo_items
|
626
|
+
end
|
627
|
+
|
628
|
+
@doc = @class.new
|
629
|
+
@embedded_doc = @doc.mongo_locations.build
|
630
|
+
@embedded_doc.image = stub_file('old.jpeg')
|
631
|
+
@embedded_doc.save.should be_true
|
632
|
+
|
633
|
+
@double_embedded_doc = @embedded_doc.mongo_items.build
|
554
634
|
@double_embedded_doc.image = stub_file('old.jpeg')
|
555
635
|
@double_embedded_doc.save.should be_true
|
556
|
-
File.exists?(public_path('uploads/old.jpeg')).should be_true
|
557
|
-
end
|
558
|
-
|
559
|
-
it "should not remove file if validations fail on save" do
|
560
|
-
@double_embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
|
561
|
-
@double_embedded_doc.image = stub_file('new.jpeg')
|
562
|
-
@double_embedded_doc.save.should be_false
|
563
|
-
File.exists?(public_path('uploads/old.jpeg')).should be_true
|
564
636
|
end
|
565
637
|
|
638
|
+
include_examples "double embedded documents"
|
566
639
|
end
|
567
|
-
|
568
640
|
end
|
569
641
|
end
|
570
642
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave-mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-05-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: carrierwave
|
17
|
-
requirement: &
|
17
|
+
requirement: &70153292588880 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 0.6.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70153292588880
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: mongoid
|
28
|
-
requirement: &
|
28
|
+
requirement: &70153292588280 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '2.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70153292588280
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: rspec
|
39
|
-
requirement: &
|
39
|
+
requirement: &70153292587700 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '2.6'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70153292587700
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: bson_ext
|
50
|
-
requirement: &
|
50
|
+
requirement: &70153292587120 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ~>
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '1.3'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *70153292587120
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: rake
|
61
|
-
requirement: &
|
61
|
+
requirement: &70153292586540 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ~>
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: '0.9'
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *70153292586540
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: mini_magick
|
72
|
-
requirement: &
|
72
|
+
requirement: &70153292585880 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,7 +77,7 @@ dependencies:
|
|
77
77
|
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *70153292585880
|
81
81
|
description: Mongoid support for CarrierWave
|
82
82
|
email:
|
83
83
|
- jonas.nicklas@gmail.com
|
@@ -119,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 3527585187429890130
|
123
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
124
|
none: false
|
125
125
|
requirements:
|
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
segments:
|
130
130
|
- 0
|
131
|
-
hash:
|
131
|
+
hash: 3527585187429890130
|
132
132
|
requirements: []
|
133
133
|
rubyforge_project: carrierwave-mongoid
|
134
134
|
rubygems_version: 1.8.11
|