carrierwave-mongoid 1.4.0 → 1.6.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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +9 -16
- data/.gitignore +1 -0
- data/README.md +12 -0
- data/carrierwave-mongoid.gemspec +5 -3
- data/gemfiles/{mongoid-3.gemfile → carrierwave-3.0.gemfile} +1 -1
- data/gemfiles/{mongoid-6.gemfile → carrierwave-3.1.gemfile} +1 -2
- data/gemfiles/mongoid-9.gemfile +6 -0
- data/lib/carrierwave/mongoid/version.rb +1 -1
- data/lib/carrierwave/mongoid.rb +261 -154
- data/spec/carrierwave/mongoid/mount_uploaders_spec.rb +37 -37
- data/spec/mongoid_spec.rb +45 -31
- data/spec/spec_helper.rb +1 -0
- metadata +19 -30
- data/gemfiles/carrierwave-0.10.gemfile +0 -6
- data/gemfiles/carrierwave-0.11.gemfile +0 -6
- data/gemfiles/carrierwave-1.1.gemfile +0 -6
- data/gemfiles/carrierwave-1.2.gemfile +0 -6
- data/gemfiles/carrierwave-1.3.gemfile +0 -6
- data/gemfiles/carrierwave-2.0.gemfile +0 -6
- data/gemfiles/carrierwave-2.1.gemfile +0 -6
- data/gemfiles/mongoid-4.gemfile +0 -6
- data/gemfiles/mongoid-5.gemfile +0 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: aef966be0bd79244a4ee503f0691586c3c8f1006f40c9b7b722e98bb182dfe25
|
|
4
|
+
data.tar.gz: 86294733bde85ddff896d9d350f4d3247849eef45dcbfbe4b4f2c883cc4eeb3d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a8a27055b64dd133662986b49b13020fc93f56ad0094bc59664141405b91b8d9224d07960bb874b6ed728dc91fe0fbd8de51d561cd197d7f6144fe83b8e6e328
|
|
7
|
+
data.tar.gz: de0f640f9be1467330211dde77ffd9e108105340eb377201f6c7e6da11183d19880172cc1582364b6d0adaa03ede0501926a7ff5634f81da9fd98828ad43b666
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -6,23 +6,14 @@ jobs:
|
|
|
6
6
|
strategy:
|
|
7
7
|
matrix:
|
|
8
8
|
mongodb: [4.4]
|
|
9
|
-
ruby: [2
|
|
9
|
+
ruby: ["3.2", "3.3", "3.4"]
|
|
10
10
|
gemfile:
|
|
11
|
-
- carrierwave-2.1
|
|
12
11
|
- carrierwave-2.2
|
|
12
|
+
- carrierwave-3.0
|
|
13
|
+
- carrierwave-3.1
|
|
13
14
|
- mongoid-7
|
|
14
15
|
- mongoid-8
|
|
15
|
-
|
|
16
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-0.10" }
|
|
17
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-0.11" }
|
|
18
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.1" }
|
|
19
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.2" }
|
|
20
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.3" }
|
|
21
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-2.0" }
|
|
22
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-3" }
|
|
23
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-4" }
|
|
24
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-5" }
|
|
25
|
-
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-6" }
|
|
16
|
+
- mongoid-9
|
|
26
17
|
name: Ruby ${{ matrix.ruby }} with ${{ matrix.gemfile }}
|
|
27
18
|
env:
|
|
28
19
|
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
|
@@ -31,15 +22,17 @@ jobs:
|
|
|
31
22
|
uses: supercharge/mongodb-github-action@1.3.0
|
|
32
23
|
with:
|
|
33
24
|
mongodb-version: ${{ matrix.mongodb }}
|
|
34
|
-
-
|
|
25
|
+
- name: Install ImageMagick
|
|
26
|
+
run: sudo apt-get -y install imagemagick
|
|
27
|
+
- uses: actions/checkout@v4
|
|
35
28
|
- name: Set up Ruby
|
|
36
29
|
uses: ruby/setup-ruby@v1
|
|
37
30
|
with:
|
|
38
31
|
ruby-version: ${{ matrix.ruby }}
|
|
39
32
|
bundler-cache: true
|
|
40
|
-
- run: cat /home/runner/work/carrierwave-mongoid/carrierwave-mongoid/vendor/bundle/ruby
|
|
33
|
+
- run: cat /home/runner/work/carrierwave-mongoid/carrierwave-mongoid/vendor/bundle/ruby/${{ matrix.ruby }}/extensions/x86_64-linux/${{ matrix.ruby }}/mimemagic-0.4.3/gem_make.out
|
|
41
34
|
if: ${{ failure() }}
|
|
42
|
-
- run: cat /home/runner/work/carrierwave-mongoid/carrierwave-mongoid/vendor/bundle/ruby
|
|
35
|
+
- run: cat /home/runner/work/carrierwave-mongoid/carrierwave-mongoid/vendor/bundle/ruby/${{ matrix.ruby }}/gems/mimemagic-0.4.3/ext/mimemagic/Rakefile
|
|
43
36
|
if: ${{ failure() }}
|
|
44
37
|
- name: Run tests
|
|
45
38
|
run: bundle exec rake
|
data/.gitignore
CHANGED
data/README.md
CHANGED
|
@@ -150,6 +150,10 @@ match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#thumb_avatar', constr
|
|
|
150
150
|
|
|
151
151
|
| Version | Notes |
|
|
152
152
|
|-----------|---------------------------------------------------------------------------------|
|
|
153
|
+
| ~> 1.5.0 | ([compare][compare-1.5], [dependencies][deps-1.5]) Mongoid 9.0 support |
|
|
154
|
+
| ~> 1.4.0 | ([compare][compare-1.4], [dependencies][deps-1.4]) Mongoid 8.0 support |
|
|
155
|
+
| ~> 1.3.0 | ([compare][compare-1.3], [dependencies][deps-1.3]) CarrierWave 2.0 support |
|
|
156
|
+
| ~> 1.2.0 | ([compare][compare-1.2], [dependencies][deps-1.2]) Carrierwave 1.3 support |
|
|
153
157
|
| ~> 1.1.0 | ([compare][compare-1.1], [dependencies][deps-1.1]) Mongoid 7.0 support |
|
|
154
158
|
| ~> 1.0.0 | ([compare][compare-1.0], [dependencies][deps-1.0]) Carrierwave 1.x support |
|
|
155
159
|
| ~> 0.10.0 | ([compare][compare-0.10], [dependencies][deps-0.10]) Mongoid 6.0 support |
|
|
@@ -163,6 +167,10 @@ match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#thumb_avatar', constr
|
|
|
163
167
|
| ~> 0.2.0 | ([compare][compare-0.2], [dependencies][deps-0.2]) Rails >= 3.2, Mongoid ~> 2.0 |
|
|
164
168
|
| ~> 0.1.0 | ([compare][compare-0.1], [dependencies][deps-0.1]) Rails <= 3.1 |
|
|
165
169
|
|
|
170
|
+
[compare-1.5]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v1.4.0...v1.5.0
|
|
171
|
+
[compare-1.4]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v1.3.0...v1.4.0
|
|
172
|
+
[compare-1.3]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v1.2.0...v1.3.0
|
|
173
|
+
[compare-1.2]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v1.1.0...v1.2.0
|
|
166
174
|
[compare-1.1]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v1.0.0...v1.1.0
|
|
167
175
|
[compare-1.0]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.10.0...v1.0.0
|
|
168
176
|
[compare-0.10]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.9.0...v0.10.0
|
|
@@ -176,6 +184,10 @@ match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#thumb_avatar', constr
|
|
|
176
184
|
[compare-0.2]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.1.7...v0.2.2
|
|
177
185
|
[compare-0.1]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.1.1...v0.1.7
|
|
178
186
|
|
|
187
|
+
[deps-1.5]: https://rubygems.org/gems/carrierwave-mongoid/versions/1.5.0
|
|
188
|
+
[deps-1.4]: https://rubygems.org/gems/carrierwave-mongoid/versions/1.4.0
|
|
189
|
+
[deps-1.3]: https://rubygems.org/gems/carrierwave-mongoid/versions/1.3.0
|
|
190
|
+
[deps-1.2]: https://rubygems.org/gems/carrierwave-mongoid/versions/1.2.0
|
|
179
191
|
[deps-1.1]: https://rubygems.org/gems/carrierwave-mongoid/versions/1.1.0
|
|
180
192
|
[deps-1.0]: https://rubygems.org/gems/carrierwave-mongoid/versions/1.0.0
|
|
181
193
|
[deps-0.10]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.10.0
|
data/carrierwave-mongoid.gemspec
CHANGED
|
@@ -20,12 +20,14 @@ Gem::Specification.new do |s|
|
|
|
20
20
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
21
21
|
s.require_paths = ["lib"]
|
|
22
22
|
|
|
23
|
-
s.add_dependency "carrierwave", [">=
|
|
24
|
-
s.add_dependency "mongoid", [">=
|
|
23
|
+
s.add_dependency "carrierwave", [">= 2.2", "< 4"]
|
|
24
|
+
s.add_dependency "mongoid", [">= 7.0", "< 10.0"]
|
|
25
25
|
s.add_dependency "mongoid-grid_fs", [">= 1.3", "< 3.0"]
|
|
26
26
|
s.add_dependency "mime-types", "< 3" if RUBY_VERSION < "2.0" # mime-types 3+ doesn't support ruby 1.9
|
|
27
27
|
s.add_development_dependency "rspec", "~> 3.4"
|
|
28
28
|
s.add_development_dependency "rake", ">= 12.3.3"
|
|
29
|
-
s.add_development_dependency "mini_magick"
|
|
29
|
+
s.add_development_dependency "mini_magick", "< 5"
|
|
30
30
|
s.add_development_dependency "pry"
|
|
31
|
+
|
|
32
|
+
s.required_ruby_version = ">= 3.2.0"
|
|
31
33
|
end
|
data/lib/carrierwave/mongoid.rb
CHANGED
|
@@ -8,124 +8,46 @@ require 'carrierwave/validations/active_model'
|
|
|
8
8
|
module CarrierWave
|
|
9
9
|
module Mongoid
|
|
10
10
|
include CarrierWave::Mount
|
|
11
|
-
##
|
|
12
|
-
# See +CarrierWave::Mount#mount_uploader+ for documentation
|
|
13
|
-
#
|
|
14
|
-
def mount_uploader(column, uploader=nil, options={}, &block)
|
|
15
|
-
field options[:mount_on] || column
|
|
16
11
|
|
|
17
|
-
|
|
12
|
+
module CarrierWave3
|
|
13
|
+
def mount_uploader(column, uploader=nil, options={}, &block)
|
|
14
|
+
field options[:mount_on] || column
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
alias_method :write_uploader, :write_attribute
|
|
21
|
-
public :read_uploader
|
|
22
|
-
public :write_uploader
|
|
23
|
-
|
|
24
|
-
include CarrierWave::Validations::ActiveModel
|
|
25
|
-
|
|
26
|
-
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
|
27
|
-
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
|
28
|
-
|
|
29
|
-
after_save :"store_#{column}!"
|
|
30
|
-
before_save :"write_#{column}_identifier"
|
|
31
|
-
after_destroy :"remove_#{column}!"
|
|
32
|
-
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
|
|
33
|
-
before_update :"store_previous_changes_for_#{column}"
|
|
34
|
-
else
|
|
35
|
-
before_update :"store_previous_model_for_#{column}"
|
|
16
|
+
super
|
|
36
17
|
end
|
|
37
|
-
after_save :"remove_previously_stored_#{column}"
|
|
38
18
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
column = _mounter(:#{column}).serialization_column
|
|
42
|
-
|
|
43
|
-
# We're using _new_ and _old_ placeholder values to force Mongoid to
|
|
44
|
-
# recognize changes in embedded documents. Before we assign these
|
|
45
|
-
# values, we need to store the original file name in case we need to
|
|
46
|
-
# delete it when document is saved.
|
|
47
|
-
previous_uploader_value = read_uploader(column)
|
|
48
|
-
@_previous_uploader_value_for_#{column} = previous_uploader_value
|
|
49
|
-
|
|
50
|
-
# mongoid won't upload a new file if there was no file previously.
|
|
51
|
-
write_uploader(column, '_old_') if self.persisted? && read_uploader(column).nil?
|
|
52
|
-
|
|
53
|
-
send(:"\#{column}_will_change!")
|
|
54
|
-
super
|
|
55
|
-
end
|
|
19
|
+
def mount_uploaders(column, uploader = nil, options = {}, &block)
|
|
20
|
+
field (options[:mount_on] || column), type: Array, default: []
|
|
56
21
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
column = _mounter(:#{column}).serialization_column
|
|
60
|
-
send(:"\#{column}_will_change!")
|
|
61
|
-
end
|
|
62
|
-
super
|
|
63
|
-
end
|
|
22
|
+
super
|
|
23
|
+
end
|
|
64
24
|
|
|
65
|
-
|
|
66
|
-
super unless respond_to?(:paranoid?) && paranoid? && flagged_for_destroy?
|
|
67
|
-
end
|
|
25
|
+
private
|
|
68
26
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
# the new value is different than the original. Given that CarrierWave
|
|
72
|
-
# caches files before save, it's necessary to know that there's a
|
|
73
|
-
# pending change even though the attribute value itself might not
|
|
74
|
-
# reflect that yet.
|
|
75
|
-
def #{column}_changed?
|
|
76
|
-
changed_attributes.has_key?("#{column}")
|
|
77
|
-
end
|
|
27
|
+
def mount_base(column, uploader=nil, options={}, &block)
|
|
28
|
+
super
|
|
78
29
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def #{column}_will_change!
|
|
84
|
-
changed_attributes["#{column}"] = '_new_'
|
|
85
|
-
end
|
|
30
|
+
alias_method :read_uploader, :read_attribute
|
|
31
|
+
alias_method :write_uploader, :write_attribute
|
|
32
|
+
public :read_uploader
|
|
33
|
+
public :write_uploader
|
|
86
34
|
|
|
87
|
-
|
|
88
|
-
# track changes in embedded documents, we need to overwrite this method
|
|
89
|
-
# to remove the original file if it was replaced with a new one that
|
|
90
|
-
# had a different name.
|
|
91
|
-
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
|
|
92
|
-
def remove_previously_stored_#{column}
|
|
93
|
-
before, after = @_previous_changes_for_#{column}
|
|
94
|
-
# Don't delete if the files had the same name
|
|
95
|
-
return if before.nil? && after.nil?
|
|
96
|
-
# Proceed to remove the file, use the original name instead of '_new_'
|
|
97
|
-
before = @_previous_uploader_value_for_#{column} || before
|
|
98
|
-
_mounter(:#{column}).remove_previous([before], [after])
|
|
99
|
-
end
|
|
100
|
-
end
|
|
35
|
+
include CarrierWave::Validations::ActiveModel
|
|
101
36
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
# See https://github.com/carrierwaveuploader/carrierwave/blob/07dc4d7bd7806ab4b963cf8acbad73d97cdfe74e/lib/carrierwave/mount.rb#L189
|
|
106
|
-
def store_previous_changes_for_#{column}
|
|
107
|
-
@_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
|
|
108
|
-
end
|
|
37
|
+
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
|
38
|
+
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
|
39
|
+
validates_download_of column if uploader_option(column.to_sym, :validate_download)
|
|
109
40
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
[[ self.__metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.__metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
|
|
117
|
-
else # Mongoid < 4.0.0.beta1
|
|
118
|
-
[[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
|
|
119
|
-
end
|
|
120
|
-
first_parent = ancestors.first.last
|
|
121
|
-
reloaded_parent = first_parent.class.unscoped.find(first_parent.to_key.first)
|
|
122
|
-
association = ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }
|
|
123
|
-
association.is_a?(Array) ? association.find(to_key.first) : association
|
|
124
|
-
else
|
|
125
|
-
self.class.unscoped.for_ids(to_key).first
|
|
126
|
-
end
|
|
127
|
-
end
|
|
41
|
+
after_save :"store_#{column}!"
|
|
42
|
+
before_save :"write_#{column}_identifier"
|
|
43
|
+
after_update :"remove_previously_stored_#{column}"
|
|
44
|
+
after_save :"reset_previous_changes_for_#{column}"
|
|
45
|
+
after_update :"mark_remove_#{column}_false"
|
|
46
|
+
after_destroy :"remove_#{column}!"
|
|
128
47
|
|
|
48
|
+
mod = Module.new
|
|
49
|
+
prepend mod
|
|
50
|
+
mod.class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
129
51
|
def serializable_hash(options=nil)
|
|
130
52
|
hash = {}
|
|
131
53
|
|
|
@@ -134,28 +56,43 @@ module CarrierWave
|
|
|
134
56
|
|
|
135
57
|
self.class.uploaders.each do |column, uploader|
|
|
136
58
|
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s))
|
|
137
|
-
if
|
|
138
|
-
|
|
139
|
-
hash[column.to_s] = _mounter(column.to_sym).uploaders[0].serializable_hash
|
|
140
|
-
else
|
|
141
|
-
hash[column.to_s] = _mounter(column.to_sym).uploader.serializable_hash
|
|
142
|
-
end
|
|
59
|
+
next if _mounter(column.to_sym).uploaders.blank?
|
|
60
|
+
hash[column.to_s] = _mounter(column.to_sym).#{options[:multiple] ? "uploaders.map(&:serializable_hash)" : "uploaders[0].serializable_hash"}
|
|
143
61
|
end
|
|
144
62
|
end
|
|
145
63
|
super(options).merge(hash)
|
|
146
64
|
end
|
|
147
65
|
|
|
148
|
-
# Reset cached mounter on
|
|
149
|
-
def reload
|
|
66
|
+
# Reset cached mounter on record reload
|
|
67
|
+
def reload(*)
|
|
150
68
|
@_mounters = nil
|
|
151
69
|
super
|
|
152
70
|
end
|
|
71
|
+
|
|
72
|
+
# Reset cached mounter on record dup
|
|
73
|
+
def initialize_dup(other)
|
|
74
|
+
old_uploaders = _mounter(:"#{column}").uploaders
|
|
75
|
+
super
|
|
76
|
+
@_mounters[:"#{column}"] = nil
|
|
77
|
+
# The attribute needs to be cleared to prevent it from picked up as identifier
|
|
78
|
+
write_attribute(_mounter(:#{column}).serialization_column, nil)
|
|
79
|
+
_mounter(:"#{column}").cache(old_uploaders)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def write_#{column}_identifier
|
|
83
|
+
return unless has_attribute?(_mounter(:#{column}).serialization_column)
|
|
84
|
+
super
|
|
85
|
+
end
|
|
153
86
|
RUBY
|
|
87
|
+
end
|
|
154
88
|
end
|
|
155
89
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
90
|
+
module CarrierWavePre3
|
|
91
|
+
##
|
|
92
|
+
# See +CarrierWave::Mount#mount_uploader+ for documentation
|
|
93
|
+
#
|
|
94
|
+
def mount_uploader(column, uploader=nil, options={}, &block)
|
|
95
|
+
field options[:mount_on] || column
|
|
159
96
|
|
|
160
97
|
super
|
|
161
98
|
|
|
@@ -166,81 +103,231 @@ module CarrierWave
|
|
|
166
103
|
|
|
167
104
|
include CarrierWave::Validations::ActiveModel
|
|
168
105
|
|
|
169
|
-
validates_integrity_of
|
|
106
|
+
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
|
170
107
|
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
|
171
108
|
|
|
172
|
-
before_update :"store_previous_changes_for_#{column}"
|
|
173
|
-
before_save :"write_#{column}_identifier"
|
|
174
109
|
after_save :"store_#{column}!"
|
|
175
|
-
|
|
110
|
+
before_save :"write_#{column}_identifier"
|
|
176
111
|
after_destroy :"remove_#{column}!"
|
|
112
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
|
|
113
|
+
before_update :"store_previous_changes_for_#{column}"
|
|
114
|
+
else
|
|
115
|
+
before_update :"store_previous_model_for_#{column}"
|
|
116
|
+
end
|
|
117
|
+
after_save :"remove_previously_stored_#{column}"
|
|
177
118
|
|
|
178
|
-
class_eval <<-RUBY, __FILE__,
|
|
179
|
-
def #{column}=(
|
|
119
|
+
class_eval <<-RUBY, __FILE__, __LINE__+1
|
|
120
|
+
def #{column}=(new_file)
|
|
180
121
|
column = _mounter(:#{column}).serialization_column
|
|
181
|
-
|
|
122
|
+
|
|
123
|
+
# We're using _new_ and _old_ placeholder values to force Mongoid to
|
|
124
|
+
# recognize changes in embedded documents. Before we assign these
|
|
125
|
+
# values, we need to store the original file name in case we need to
|
|
126
|
+
# delete it when document is saved.
|
|
182
127
|
previous_uploader_value = read_uploader(column)
|
|
183
128
|
@_previous_uploader_value_for_#{column} = previous_uploader_value
|
|
184
|
-
|
|
185
|
-
write_uploader(column, []) if self.persisted? && read_uploader(column).nil?
|
|
186
|
-
|
|
187
|
-
send(:"\#{column}_will_change!")
|
|
188
129
|
|
|
189
|
-
|
|
190
|
-
|
|
130
|
+
# mongoid won't upload a new file if there was no file previously.
|
|
131
|
+
write_uploader(column, '_old_') if self.persisted? && read_uploader(column).nil?
|
|
191
132
|
|
|
192
|
-
|
|
193
|
-
|
|
133
|
+
send(:"\#{column}_will_change!")
|
|
134
|
+
super
|
|
194
135
|
end
|
|
195
136
|
|
|
196
|
-
def remove_#{column}=(
|
|
197
|
-
if ['1', true].include?(
|
|
137
|
+
def remove_#{column}=(arg)
|
|
138
|
+
if ['1', true].include?(arg)
|
|
198
139
|
column = _mounter(:#{column}).serialization_column
|
|
199
|
-
|
|
200
140
|
send(:"\#{column}_will_change!")
|
|
201
141
|
end
|
|
202
|
-
|
|
203
142
|
super
|
|
204
143
|
end
|
|
205
144
|
|
|
145
|
+
def remove_#{column}!
|
|
146
|
+
super unless respond_to?(:paranoid?) && paranoid? && flagged_for_destroy?
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Overrides Mongoid's default dirty behavior to instead work more like
|
|
150
|
+
# ActiveRecord's. Mongoid doesn't deem an attribute as changed unless
|
|
151
|
+
# the new value is different than the original. Given that CarrierWave
|
|
152
|
+
# caches files before save, it's necessary to know that there's a
|
|
153
|
+
# pending change even though the attribute value itself might not
|
|
154
|
+
# reflect that yet.
|
|
155
|
+
def #{column}_changed?
|
|
156
|
+
changed_attributes.has_key?("#{column}")
|
|
157
|
+
end
|
|
158
|
+
|
|
206
159
|
# The default Mongoid attribute_will_change! method is not enough
|
|
207
160
|
# when we want to upload a new file in an existing embedded document.
|
|
208
161
|
# The custom version of that method forces the callbacks to be
|
|
209
162
|
# ran and so does the upload.
|
|
210
163
|
def #{column}_will_change!
|
|
211
|
-
changed_attributes["#{column}"] =
|
|
164
|
+
changed_attributes["#{column}"] = '_new_'
|
|
212
165
|
end
|
|
213
166
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
167
|
+
# Since we had to use tricks with _old_ and _new_ values to properly
|
|
168
|
+
# track changes in embedded documents, we need to overwrite this method
|
|
169
|
+
# to remove the original file if it was replaced with a new one that
|
|
170
|
+
# had a different name.
|
|
171
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
|
|
172
|
+
def remove_previously_stored_#{column}
|
|
173
|
+
before, after = @_previous_changes_for_#{column}
|
|
174
|
+
# Don't delete if the files had the same name
|
|
175
|
+
return if before.nil? && after.nil?
|
|
176
|
+
# Proceed to remove the file, use the original name instead of '_new_'
|
|
177
|
+
before = @_previous_uploader_value_for_#{column} || before
|
|
178
|
+
_mounter(:#{column}).remove_previous([before], [after])
|
|
179
|
+
end
|
|
221
180
|
end
|
|
222
181
|
|
|
223
|
-
|
|
182
|
+
# CarrierWave 1.1 references ::ActiveRecord constant directly which
|
|
183
|
+
# will fail in projects without ActiveRecord. We need to overwrite this
|
|
184
|
+
# method to avoid it.
|
|
185
|
+
# See https://github.com/carrierwaveuploader/carrierwave/blob/07dc4d7bd7806ab4b963cf8acbad73d97cdfe74e/lib/carrierwave/mount.rb#L189
|
|
186
|
+
def store_previous_changes_for_#{column}
|
|
187
|
+
@_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def find_previous_model_for_#{column}
|
|
191
|
+
if self.embedded?
|
|
192
|
+
ancestors =
|
|
193
|
+
if self.respond_to?(:_association) # Mongoid >= 7.0.0.beta
|
|
194
|
+
[[ self._association.key, self._parent ]].tap { |x| x.unshift([ x.first.last._association.key, x.first.last._parent ]) while x.first.last.embedded? }
|
|
195
|
+
elsif self.respond_to?(:__metadata) # Mongoid >= 4.0.0.beta1 < 7.0.0.beta
|
|
196
|
+
[[ self.__metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.__metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
|
|
197
|
+
else # Mongoid < 4.0.0.beta1
|
|
198
|
+
[[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
|
|
199
|
+
end
|
|
200
|
+
first_parent = ancestors.first.last
|
|
201
|
+
reloaded_parent = first_parent.class.unscoped.find(first_parent.to_key.first)
|
|
202
|
+
association = ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }
|
|
203
|
+
association.is_a?(Array) ? association.find(to_key.first) : association
|
|
204
|
+
else
|
|
205
|
+
self.class.unscoped.for_ids(to_key).first
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def serializable_hash(options=nil)
|
|
224
210
|
hash = {}
|
|
225
|
-
|
|
211
|
+
|
|
226
212
|
except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s)
|
|
227
|
-
only
|
|
228
|
-
|
|
229
|
-
self.class.uploaders.each do |column,
|
|
213
|
+
only = options && options[:only] && Array.wrap(options[:only]).map(&:to_s)
|
|
214
|
+
|
|
215
|
+
self.class.uploaders.each do |column, uploader|
|
|
230
216
|
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s))
|
|
231
|
-
|
|
232
|
-
|
|
217
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
|
|
218
|
+
next if _mounter(column.to_sym).uploaders.blank?
|
|
219
|
+
hash[column.to_s] = _mounter(column.to_sym).uploaders[0].serializable_hash
|
|
220
|
+
else
|
|
221
|
+
hash[column.to_s] = _mounter(column.to_sym).uploader.serializable_hash
|
|
222
|
+
end
|
|
233
223
|
end
|
|
234
224
|
end
|
|
235
|
-
|
|
236
225
|
super(options).merge(hash)
|
|
237
226
|
end
|
|
238
227
|
|
|
239
|
-
|
|
240
|
-
|
|
228
|
+
# Reset cached mounter on mongoid reload
|
|
229
|
+
def reload
|
|
230
|
+
@_mounters = nil
|
|
231
|
+
super
|
|
241
232
|
end
|
|
242
233
|
RUBY
|
|
243
234
|
end
|
|
235
|
+
|
|
236
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new('1.0.beta')
|
|
237
|
+
def mount_uploaders(column, uploader = nil, options = {}, &block)
|
|
238
|
+
field (options[:mount_on] || column), type: Array, default: []
|
|
239
|
+
|
|
240
|
+
super
|
|
241
|
+
|
|
242
|
+
alias_method :read_uploader, :read_attribute
|
|
243
|
+
alias_method :write_uploader, :write_attribute
|
|
244
|
+
public :read_uploader
|
|
245
|
+
public :write_uploader
|
|
246
|
+
|
|
247
|
+
include CarrierWave::Validations::ActiveModel
|
|
248
|
+
|
|
249
|
+
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
|
250
|
+
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
|
251
|
+
|
|
252
|
+
before_update :"store_previous_changes_for_#{column}"
|
|
253
|
+
before_save :"write_#{column}_identifier"
|
|
254
|
+
after_save :"store_#{column}!"
|
|
255
|
+
after_save :"remove_previously_stored_#{column}"
|
|
256
|
+
after_destroy :"remove_#{column}!"
|
|
257
|
+
|
|
258
|
+
class_eval <<-RUBY, __FILE__, (__LINE__ + 1)
|
|
259
|
+
def #{column}=(new_files)
|
|
260
|
+
column = _mounter(:#{column}).serialization_column
|
|
261
|
+
|
|
262
|
+
previous_uploader_value = read_uploader(column)
|
|
263
|
+
@_previous_uploader_value_for_#{column} = previous_uploader_value
|
|
264
|
+
|
|
265
|
+
write_uploader(column, []) if self.persisted? && read_uploader(column).nil?
|
|
266
|
+
|
|
267
|
+
send(:"\#{column}_will_change!")
|
|
268
|
+
|
|
269
|
+
super
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def #{column}_changed?
|
|
273
|
+
changed_attributes.has_key?("#{column}")
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def remove_#{column}=(value)
|
|
277
|
+
if ['1', true].include?(value)
|
|
278
|
+
column = _mounter(:#{column}).serialization_column
|
|
279
|
+
|
|
280
|
+
send(:"\#{column}_will_change!")
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
super
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# The default Mongoid attribute_will_change! method is not enough
|
|
287
|
+
# when we want to upload a new file in an existing embedded document.
|
|
288
|
+
# The custom version of that method forces the callbacks to be
|
|
289
|
+
# ran and so does the upload.
|
|
290
|
+
def #{column}_will_change!
|
|
291
|
+
changed_attributes["#{column}"] = ['_new_']
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def remove_previously_stored_#{column}
|
|
295
|
+
before, after = @_previous_changes_for_#{column}
|
|
296
|
+
# Don't delete if the files had the same name
|
|
297
|
+
return if before.nil? && after.nil?
|
|
298
|
+
# Proceed to remove the file, use the original name instead of '_new_'
|
|
299
|
+
before = @_previous_uploader_value_for_#{column} || before
|
|
300
|
+
_mounter(:#{column}).remove_previous(Array.wrap(before), Array.wrap(after))
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
def serializable_hash(options = nil)
|
|
304
|
+
hash = {}
|
|
305
|
+
|
|
306
|
+
except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s)
|
|
307
|
+
only = options && options[:only] && Array.wrap(options[:only]).map(&:to_s)
|
|
308
|
+
|
|
309
|
+
self.class.uploaders.each do |column, _uploader|
|
|
310
|
+
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s))
|
|
311
|
+
next if _mounter(column.to_sym).uploaders.blank?
|
|
312
|
+
hash[column.to_s] = _mounter(column.to_sym).uploaders.map(&:serializable_hash)
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
super(options).merge(hash)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def store_previous_changes_for_#{column}
|
|
320
|
+
@_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
|
|
321
|
+
end
|
|
322
|
+
RUBY
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("3.0.beta")
|
|
328
|
+
include CarrierWave3
|
|
329
|
+
else
|
|
330
|
+
include CarrierWavePre3
|
|
244
331
|
end
|
|
245
332
|
end # Mongoid
|
|
246
333
|
end # CarrierWave
|
|
@@ -255,4 +342,24 @@ class CarrierWave::Uploader::Base
|
|
|
255
342
|
end
|
|
256
343
|
end
|
|
257
344
|
|
|
345
|
+
if Gem::Version.new("3.0.beta") <= Gem::Version.new(CarrierWave::VERSION) &&
|
|
346
|
+
Gem::Version.new(CarrierWave::VERSION) < Gem::Version.new("3.1.1")
|
|
347
|
+
# Monkey patch to work around https://github.com/carrierwaveuploader/carrierwave/issues/2770
|
|
348
|
+
CarrierWave::SanitizedFile.prepend Module.new {
|
|
349
|
+
def read(*args)
|
|
350
|
+
if args.empty?
|
|
351
|
+
super
|
|
352
|
+
else
|
|
353
|
+
if is_path?
|
|
354
|
+
@file = File.open(path, "rb")
|
|
355
|
+
elsif @file.is_a?(CarrierWave::Uploader::Base)
|
|
356
|
+
@file = StringIO.new(@file.read)
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
@file.read(*args)
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
}
|
|
363
|
+
end
|
|
364
|
+
|
|
258
365
|
Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
|
|
@@ -140,6 +140,7 @@ describe CarrierWave::Mongoid do
|
|
|
140
140
|
|
|
141
141
|
describe 'model#remove_uploaders=' do
|
|
142
142
|
before do
|
|
143
|
+
model.images = [stub_file('test.jpg')]
|
|
143
144
|
model.save
|
|
144
145
|
end
|
|
145
146
|
|
|
@@ -208,7 +209,7 @@ describe CarrierWave::Mongoid do
|
|
|
208
209
|
|
|
209
210
|
before do
|
|
210
211
|
model.save!
|
|
211
|
-
model.collection.update_one({ _id: model.id }, { images: identifiers })
|
|
212
|
+
model.collection.update_one({ _id: model.id }, '$set' => { images: identifiers })
|
|
212
213
|
end
|
|
213
214
|
|
|
214
215
|
it 'returns an array of uploaders' do
|
|
@@ -239,10 +240,6 @@ describe CarrierWave::Mongoid do
|
|
|
239
240
|
expect(model.images.map(&:identifier)).to match_array(identifiers)
|
|
240
241
|
end
|
|
241
242
|
|
|
242
|
-
it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
|
|
243
|
-
expect(model[:images]).to match_array([])
|
|
244
|
-
end
|
|
245
|
-
|
|
246
243
|
it 'copies a file into into the cache directory' do
|
|
247
244
|
expect(model.images.first.current_path).to match(/^#{Regexp.escape(public_path('uploads/tmp'))}/)
|
|
248
245
|
end
|
|
@@ -260,10 +257,6 @@ describe CarrierWave::Mongoid do
|
|
|
260
257
|
expect(model.images.map(&:identifier)).to match_array(['portrait.jpg', 'test.jpeg'])
|
|
261
258
|
end
|
|
262
259
|
|
|
263
|
-
it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
|
|
264
|
-
expect(model[:images]).to match_array(['portrait.jpg'])
|
|
265
|
-
end
|
|
266
|
-
|
|
267
260
|
it 'copies a file into into the cache directory' do
|
|
268
261
|
expect(model.images.map(&:current_path)).to all(match(/^#{Regexp.escape(public_path('uploads/tmp'))}/))
|
|
269
262
|
end
|
|
@@ -301,10 +294,6 @@ describe CarrierWave::Mongoid do
|
|
|
301
294
|
expect(model.images).to all(be_an_instance_of(uploader_class))
|
|
302
295
|
end
|
|
303
296
|
|
|
304
|
-
it 'does not write to the database' do
|
|
305
|
-
expect(model[:images]).to be_empty
|
|
306
|
-
end
|
|
307
|
-
|
|
308
297
|
it 'copies a file into into the cache directory' do
|
|
309
298
|
expect(model.images.map(&:current_path)).to all(start_with(public_path('uploads/tmp')))
|
|
310
299
|
end
|
|
@@ -393,15 +382,20 @@ describe CarrierWave::Mongoid do
|
|
|
393
382
|
|
|
394
383
|
before do
|
|
395
384
|
model_class.create!(images: files)
|
|
396
|
-
end
|
|
397
|
-
|
|
398
|
-
it 'replaced it by a file with the same name' do
|
|
399
385
|
record.update!(images: [stub_file('test.jpeg')])
|
|
400
|
-
|
|
401
386
|
record.reload
|
|
387
|
+
end
|
|
402
388
|
|
|
403
|
-
|
|
404
|
-
|
|
389
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("3.0.beta")
|
|
390
|
+
it "performs deduplication" do
|
|
391
|
+
expect(record[:images]).to match_array(['test(2).jpeg'])
|
|
392
|
+
expect(record.images_identifiers).to match_array(['test(2).jpeg'])
|
|
393
|
+
end
|
|
394
|
+
else
|
|
395
|
+
it 'replaced it by a file with the same name' do
|
|
396
|
+
expect(record[:images]).to match_array(['test.jpeg'])
|
|
397
|
+
expect(record.images_identifiers).to match_array(['test.jpeg'])
|
|
398
|
+
end
|
|
405
399
|
end
|
|
406
400
|
end
|
|
407
401
|
|
|
@@ -492,12 +486,14 @@ describe CarrierWave::Mongoid do
|
|
|
492
486
|
expect(model.save).to be_truthy
|
|
493
487
|
expect(File).to exist(public_path('uploads/new.jpeg'))
|
|
494
488
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
489
|
+
ensure
|
|
490
|
+
uploader_class.remove_previously_stored_files_after_update = true
|
|
495
491
|
end
|
|
496
492
|
|
|
497
|
-
it 'does not remove file if
|
|
493
|
+
it 'does not remove new file if both of files had the same path' do
|
|
498
494
|
model.images = [stub_file('old.jpeg')]
|
|
499
495
|
expect(model.save).to be_truthy
|
|
500
|
-
expect(File).to exist(
|
|
496
|
+
expect(File).to exist(model.images[0].path)
|
|
501
497
|
end
|
|
502
498
|
|
|
503
499
|
it 'does not remove file if validations fail on save' do
|
|
@@ -519,13 +515,13 @@ describe CarrierWave::Mongoid do
|
|
|
519
515
|
|
|
520
516
|
let!(:model) { model_class.create!(name: 'Mike', images: [stub_file('old.jpeg')]) }
|
|
521
517
|
|
|
522
|
-
it 'does not remove file if
|
|
518
|
+
it 'does not remove new file if both of files had the same path' do
|
|
523
519
|
expect(File).to exist(public_path('uploads/Mike.jpeg'))
|
|
524
520
|
expect(model.images.first.read).to eq 'this is stuff'
|
|
525
521
|
|
|
526
522
|
model.update!(images: [stub_file('test.jpeg')])
|
|
527
523
|
|
|
528
|
-
expect(File).to exist(
|
|
524
|
+
expect(File).to exist(model.images[0].path)
|
|
529
525
|
end
|
|
530
526
|
|
|
531
527
|
it 'removes old file if old file had a different dynamic path' do
|
|
@@ -553,12 +549,14 @@ describe CarrierWave::Mongoid do
|
|
|
553
549
|
expect(embedded_model.save).to be_truthy
|
|
554
550
|
expect(File).to exist(public_path('uploads/new.jpeg'))
|
|
555
551
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
552
|
+
ensure
|
|
553
|
+
uploader_class.remove_previously_stored_files_after_update = true
|
|
556
554
|
end
|
|
557
555
|
|
|
558
|
-
it '
|
|
556
|
+
it 'should not remove new file if both of files had the same path' do
|
|
559
557
|
embedded_model.images = [stub_file('old.jpeg')]
|
|
560
558
|
expect(embedded_model.save).to be_truthy
|
|
561
|
-
expect(File).to exist(
|
|
559
|
+
expect(File).to exist(embedded_model.images[0].path)
|
|
562
560
|
end
|
|
563
561
|
|
|
564
562
|
it 'does not remove file if validations fail on save' do
|
|
@@ -592,12 +590,14 @@ describe CarrierWave::Mongoid do
|
|
|
592
590
|
expect(double_embedded_model.save).to be_truthy
|
|
593
591
|
expect(File).to exist(public_path('uploads/new.jpeg'))
|
|
594
592
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
593
|
+
ensure
|
|
594
|
+
uploader_class.remove_previously_stored_files_after_update = true
|
|
595
595
|
end
|
|
596
596
|
|
|
597
|
-
it '
|
|
597
|
+
it 'should not remove new file if both of files had the same path' do
|
|
598
598
|
double_embedded_model.images = [stub_file('old.jpeg')]
|
|
599
599
|
expect(double_embedded_model.save).to be_truthy
|
|
600
|
-
expect(File).to exist(
|
|
600
|
+
expect(File).to exist(double_embedded_model.images[0].path)
|
|
601
601
|
end
|
|
602
602
|
|
|
603
603
|
it 'does not remove file if validations fail on save' do
|
|
@@ -878,14 +878,14 @@ describe CarrierWave::Mongoid do
|
|
|
878
878
|
expect(File).not_to exist(public_path('uploads/thumb_old.jpeg'))
|
|
879
879
|
end
|
|
880
880
|
|
|
881
|
-
it 'does not remove file if
|
|
881
|
+
it 'does not remove new file if both of files had the same path' do
|
|
882
882
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
883
883
|
expect(File).to exist(public_path('uploads/thumb_old.jpeg'))
|
|
884
884
|
|
|
885
885
|
model.update!(images: [stub_file('old.jpeg')])
|
|
886
886
|
|
|
887
|
-
expect(File).to exist(
|
|
888
|
-
expect(File).to exist(
|
|
887
|
+
expect(File).to exist(model.images[0].path)
|
|
888
|
+
expect(File).to exist(model.images[0].thumb.path)
|
|
889
889
|
end
|
|
890
890
|
end
|
|
891
891
|
|
|
@@ -926,7 +926,7 @@ describe CarrierWave::Mongoid do
|
|
|
926
926
|
expect(File).not_to exist(public_path('uploads/old.txt'))
|
|
927
927
|
end
|
|
928
928
|
|
|
929
|
-
it 'removes old file1 but not file2 if old file1 had a different path but old file2 has the same path' do
|
|
929
|
+
it 'removes old file1 but not new file2 if old file1 had a different path but old file2 has the same path' do
|
|
930
930
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
931
931
|
expect(File).to exist(public_path('uploads/old.txt'))
|
|
932
932
|
|
|
@@ -934,17 +934,17 @@ describe CarrierWave::Mongoid do
|
|
|
934
934
|
|
|
935
935
|
expect(File).to exist(public_path('uploads/new.jpeg'))
|
|
936
936
|
expect(File).not_to exist(public_path('uploads/old.jpeg'))
|
|
937
|
-
expect(File).to exist(
|
|
937
|
+
expect(File).to exist(model.textfiles[0].path)
|
|
938
938
|
end
|
|
939
939
|
|
|
940
|
-
it 'does not remove
|
|
940
|
+
it 'does not remove new files if each pair of files has the same paths' do
|
|
941
941
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
942
942
|
expect(File).to exist(public_path('uploads/old.txt'))
|
|
943
943
|
|
|
944
944
|
model.update!(images: [stub_file('old.jpeg')], textfiles: [stub_file('old.txt')])
|
|
945
945
|
|
|
946
|
-
expect(File).to exist(
|
|
947
|
-
expect(File).to exist(
|
|
946
|
+
expect(File).to exist(model.images[0].path)
|
|
947
|
+
expect(File).to exist(model.textfiles[0].path)
|
|
948
948
|
end
|
|
949
949
|
end
|
|
950
950
|
|
|
@@ -981,12 +981,12 @@ describe CarrierWave::Mongoid do
|
|
|
981
981
|
expect(File).not_to exist(public_path('uploads/old.jpeg'))
|
|
982
982
|
end
|
|
983
983
|
|
|
984
|
-
it 'does not remove file if
|
|
984
|
+
it 'does not remove new file if both of files had the same path' do
|
|
985
985
|
expect(File).to exist(public_path('uploads/old.jpeg'))
|
|
986
986
|
|
|
987
987
|
model.update!(avatars: [stub_file('old.jpeg')])
|
|
988
988
|
|
|
989
|
-
expect(File).to exist(
|
|
989
|
+
expect(File).to exist(model.avatars[0].path)
|
|
990
990
|
end
|
|
991
991
|
end
|
|
992
992
|
end
|
data/spec/mongoid_spec.rb
CHANGED
|
@@ -162,18 +162,15 @@ describe CarrierWave::Mongoid do
|
|
|
162
162
|
end
|
|
163
163
|
|
|
164
164
|
context "when a file is assigned" do
|
|
165
|
-
|
|
166
|
-
it "should cache a file" do
|
|
165
|
+
before do
|
|
167
166
|
@doc.image = stub_file('test.jpeg')
|
|
168
|
-
expect(@doc.image).to be_an_instance_of(MongoUploader)
|
|
169
167
|
end
|
|
170
168
|
|
|
171
|
-
it "should
|
|
172
|
-
expect(@doc
|
|
169
|
+
it "should cache a file" do
|
|
170
|
+
expect(@doc.image).to be_an_instance_of(MongoUploader)
|
|
173
171
|
end
|
|
174
172
|
|
|
175
173
|
it "should copy a file into into the cache directory" do
|
|
176
|
-
@doc.image = stub_file('test.jpeg')
|
|
177
174
|
expect(@doc.image.current_path).to match /^#{public_path('uploads\/tmp')}/
|
|
178
175
|
end
|
|
179
176
|
|
|
@@ -237,6 +234,8 @@ describe CarrierWave::Mongoid do
|
|
|
237
234
|
before do
|
|
238
235
|
mongo_user_klass = reset_mongo_class
|
|
239
236
|
@doc = mongo_user_klass.new
|
|
237
|
+
@doc.image = stub_file('test.jpg')
|
|
238
|
+
@doc.save!
|
|
240
239
|
end
|
|
241
240
|
|
|
242
241
|
it "treats true argument such that attribute is marked as changed" do
|
|
@@ -341,14 +340,21 @@ describe CarrierWave::Mongoid do
|
|
|
341
340
|
@doc.image = stub_file('test.jpeg')
|
|
342
341
|
@doc.save
|
|
343
342
|
@doc.reload
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
it "replaced it by a file with the same name" do
|
|
347
343
|
@doc.image = stub_file('test.jpeg')
|
|
348
344
|
@doc.save
|
|
349
345
|
@doc.reload
|
|
350
|
-
|
|
351
|
-
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("3.0.beta")
|
|
349
|
+
it "performs deduplication" do
|
|
350
|
+
expect(@doc[:image]).to eq 'test(2).jpeg'
|
|
351
|
+
expect(@doc.image_identifier).to eq 'test(2).jpeg'
|
|
352
|
+
end
|
|
353
|
+
else
|
|
354
|
+
it "replaced it by a file with the same name" do
|
|
355
|
+
expect(@doc[:image]).to eq 'test.jpeg'
|
|
356
|
+
expect(@doc.image_identifier).to eq 'test.jpeg'
|
|
357
|
+
end
|
|
352
358
|
end
|
|
353
359
|
|
|
354
360
|
end
|
|
@@ -442,12 +448,14 @@ describe CarrierWave::Mongoid do
|
|
|
442
448
|
expect(@doc.save).to be_truthy
|
|
443
449
|
expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
|
|
444
450
|
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
|
|
451
|
+
ensure
|
|
452
|
+
@doc.image.class.remove_previously_stored_files_after_update = true
|
|
445
453
|
end
|
|
446
454
|
|
|
447
|
-
it
|
|
455
|
+
it 'should not remove new file if both of files had the same path' do
|
|
448
456
|
@doc.image = stub_file('old.jpeg')
|
|
449
457
|
expect(@doc.save).to be_truthy
|
|
450
|
-
expect(File.exist?(
|
|
458
|
+
expect(File.exist?(@doc.image.path)).to be_truthy
|
|
451
459
|
end
|
|
452
460
|
|
|
453
461
|
it "should not remove file if validations fail on save" do
|
|
@@ -473,10 +481,10 @@ describe CarrierWave::Mongoid do
|
|
|
473
481
|
expect(@doc.image.read).to eq "this is stuff"
|
|
474
482
|
end
|
|
475
483
|
|
|
476
|
-
it
|
|
484
|
+
it 'should not remove new file if both of files had the same path' do
|
|
477
485
|
@doc.image = stub_file('test.jpeg')
|
|
478
486
|
expect(@doc.save).to be_truthy
|
|
479
|
-
expect(File.exist?(
|
|
487
|
+
expect(File.exist?(@doc.image.path)).to be_truthy
|
|
480
488
|
end
|
|
481
489
|
|
|
482
490
|
it "should remove old file if old file had a different dynamic path" do
|
|
@@ -502,12 +510,14 @@ describe CarrierWave::Mongoid do
|
|
|
502
510
|
expect(@embedded_doc.save).to be_truthy
|
|
503
511
|
expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
|
|
504
512
|
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
|
|
513
|
+
ensure
|
|
514
|
+
@embedded_doc.image.class.remove_previously_stored_files_after_update = true
|
|
505
515
|
end
|
|
506
516
|
|
|
507
|
-
it
|
|
517
|
+
it 'should not remove new file if both of files had the same path' do
|
|
508
518
|
@embedded_doc.image = stub_file('old.jpeg')
|
|
509
519
|
expect(@embedded_doc.save).to be_truthy
|
|
510
|
-
expect(File.exist?(
|
|
520
|
+
expect(File.exist?(@embedded_doc.image.path)).to be_truthy
|
|
511
521
|
end
|
|
512
522
|
|
|
513
523
|
it "should not remove file if validations fail on save" do
|
|
@@ -540,12 +550,14 @@ describe CarrierWave::Mongoid do
|
|
|
540
550
|
expect(@double_embedded_doc.save).to be_truthy
|
|
541
551
|
expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
|
|
542
552
|
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
|
|
553
|
+
ensure
|
|
554
|
+
@double_embedded_doc.image.class.remove_previously_stored_files_after_update = true
|
|
543
555
|
end
|
|
544
556
|
|
|
545
|
-
it
|
|
557
|
+
it 'should not remove new file if both of files had the same path' do
|
|
546
558
|
@double_embedded_doc.image = stub_file('old.jpeg')
|
|
547
559
|
expect(@double_embedded_doc.save).to be_truthy
|
|
548
|
-
expect(File.exist?(
|
|
560
|
+
expect(File.exist?(@double_embedded_doc.image.path)).to be_truthy
|
|
549
561
|
end
|
|
550
562
|
|
|
551
563
|
it "should not remove file if validations fail on save" do
|
|
@@ -623,11 +635,13 @@ describe CarrierWave::Mongoid do
|
|
|
623
635
|
|
|
624
636
|
it "attaches a new file to an existing document that had no file at first" do
|
|
625
637
|
doc = @class.new
|
|
626
|
-
doc.mongo_locations.build
|
|
638
|
+
new_file = doc.mongo_locations.build
|
|
639
|
+
expect(new_file.save).to be_truthy
|
|
627
640
|
expect(doc.save).to be_truthy
|
|
628
641
|
doc.reload
|
|
629
642
|
|
|
630
|
-
|
|
643
|
+
new_file.image = stub_file('test.jpeg')
|
|
644
|
+
expect(new_file.save).to be_truthy
|
|
631
645
|
expect(doc.save).to be_truthy
|
|
632
646
|
doc.reload
|
|
633
647
|
|
|
@@ -780,11 +794,11 @@ describe CarrierWave::Mongoid do
|
|
|
780
794
|
expect(File.exist?(public_path('uploads/thumb_old.jpeg'))).to be_falsey
|
|
781
795
|
end
|
|
782
796
|
|
|
783
|
-
it
|
|
797
|
+
it 'should not remove new file if both of files had the same path' do
|
|
784
798
|
@doc.image = stub_file('old.jpeg')
|
|
785
799
|
expect(@doc.save).to be_truthy
|
|
786
|
-
expect(File.exist?(
|
|
787
|
-
expect(File.exist?(
|
|
800
|
+
expect(File.exist?(@doc.image.path)).to be_truthy
|
|
801
|
+
expect(File.exist?(@doc.image.thumb.path)).to be_truthy
|
|
788
802
|
end
|
|
789
803
|
end
|
|
790
804
|
|
|
@@ -817,21 +831,21 @@ describe CarrierWave::Mongoid do
|
|
|
817
831
|
expect(File.exist?(public_path('uploads/old.txt'))).to be_falsey
|
|
818
832
|
end
|
|
819
833
|
|
|
820
|
-
it "should remove old file1 but not file2 if old file1 had a different path but old file2 has the same path" do
|
|
834
|
+
it "should remove old file1 but not new file2 if old file1 had a different path but old file2 has the same path" do
|
|
821
835
|
@doc.image = stub_file('new.jpeg')
|
|
822
836
|
@doc.textfile = stub_file('old.txt')
|
|
823
837
|
expect(@doc.save).to be_truthy
|
|
824
838
|
expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
|
|
825
839
|
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_falsey
|
|
826
|
-
expect(File.exist?(
|
|
840
|
+
expect(File.exist?(@doc.textfile.path)).to be_truthy
|
|
827
841
|
end
|
|
828
842
|
|
|
829
|
-
it "should not remove
|
|
843
|
+
it "should not remove new files if each pair of files has the same paths" do
|
|
830
844
|
@doc.image = stub_file('old.jpeg')
|
|
831
845
|
@doc.textfile = stub_file('old.txt')
|
|
832
846
|
expect(@doc.save).to be_truthy
|
|
833
|
-
expect(File.exist?(
|
|
834
|
-
expect(File.exist?(
|
|
847
|
+
expect(File.exist?(@doc.image.path)).to be_truthy
|
|
848
|
+
expect(File.exist?(@doc.textfile.path)).to be_truthy
|
|
835
849
|
end
|
|
836
850
|
end
|
|
837
851
|
|
|
@@ -858,10 +872,10 @@ describe CarrierWave::Mongoid do
|
|
|
858
872
|
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_falsey
|
|
859
873
|
end
|
|
860
874
|
|
|
861
|
-
it
|
|
875
|
+
it 'should not remove new file if both of files had the same path' do
|
|
862
876
|
@doc.avatar = stub_file('old.jpeg')
|
|
863
877
|
expect(@doc.save).to be_truthy
|
|
864
|
-
expect(File.exist
|
|
878
|
+
expect(File).to exist(@doc.avatar.path)
|
|
865
879
|
end
|
|
866
880
|
end
|
|
867
881
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: carrierwave-mongoid
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jonas Nicklas
|
|
8
8
|
- Trevor Turk
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: carrierwave
|
|
@@ -17,40 +16,40 @@ dependencies:
|
|
|
17
16
|
requirements:
|
|
18
17
|
- - ">="
|
|
19
18
|
- !ruby/object:Gem::Version
|
|
20
|
-
version: '
|
|
19
|
+
version: '2.2'
|
|
21
20
|
- - "<"
|
|
22
21
|
- !ruby/object:Gem::Version
|
|
23
|
-
version: '
|
|
22
|
+
version: '4'
|
|
24
23
|
type: :runtime
|
|
25
24
|
prerelease: false
|
|
26
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
27
26
|
requirements:
|
|
28
27
|
- - ">="
|
|
29
28
|
- !ruby/object:Gem::Version
|
|
30
|
-
version: '
|
|
29
|
+
version: '2.2'
|
|
31
30
|
- - "<"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '4'
|
|
34
33
|
- !ruby/object:Gem::Dependency
|
|
35
34
|
name: mongoid
|
|
36
35
|
requirement: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
37
|
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
39
|
+
version: '7.0'
|
|
41
40
|
- - "<"
|
|
42
41
|
- !ruby/object:Gem::Version
|
|
43
|
-
version: '
|
|
42
|
+
version: '10.0'
|
|
44
43
|
type: :runtime
|
|
45
44
|
prerelease: false
|
|
46
45
|
version_requirements: !ruby/object:Gem::Requirement
|
|
47
46
|
requirements:
|
|
48
47
|
- - ">="
|
|
49
48
|
- !ruby/object:Gem::Version
|
|
50
|
-
version: '
|
|
49
|
+
version: '7.0'
|
|
51
50
|
- - "<"
|
|
52
51
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '
|
|
52
|
+
version: '10.0'
|
|
54
53
|
- !ruby/object:Gem::Dependency
|
|
55
54
|
name: mongoid-grid_fs
|
|
56
55
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -103,16 +102,16 @@ dependencies:
|
|
|
103
102
|
name: mini_magick
|
|
104
103
|
requirement: !ruby/object:Gem::Requirement
|
|
105
104
|
requirements:
|
|
106
|
-
- - "
|
|
105
|
+
- - "<"
|
|
107
106
|
- !ruby/object:Gem::Version
|
|
108
|
-
version: '
|
|
107
|
+
version: '5'
|
|
109
108
|
type: :development
|
|
110
109
|
prerelease: false
|
|
111
110
|
version_requirements: !ruby/object:Gem::Requirement
|
|
112
111
|
requirements:
|
|
113
|
-
- - "
|
|
112
|
+
- - "<"
|
|
114
113
|
- !ruby/object:Gem::Version
|
|
115
|
-
version: '
|
|
114
|
+
version: '5'
|
|
116
115
|
- !ruby/object:Gem::Dependency
|
|
117
116
|
name: pry
|
|
118
117
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -141,20 +140,12 @@ files:
|
|
|
141
140
|
- README.md
|
|
142
141
|
- Rakefile
|
|
143
142
|
- carrierwave-mongoid.gemspec
|
|
144
|
-
- gemfiles/carrierwave-0.10.gemfile
|
|
145
|
-
- gemfiles/carrierwave-0.11.gemfile
|
|
146
|
-
- gemfiles/carrierwave-1.1.gemfile
|
|
147
|
-
- gemfiles/carrierwave-1.2.gemfile
|
|
148
|
-
- gemfiles/carrierwave-1.3.gemfile
|
|
149
|
-
- gemfiles/carrierwave-2.0.gemfile
|
|
150
|
-
- gemfiles/carrierwave-2.1.gemfile
|
|
151
143
|
- gemfiles/carrierwave-2.2.gemfile
|
|
152
|
-
- gemfiles/
|
|
153
|
-
- gemfiles/
|
|
154
|
-
- gemfiles/mongoid-5.gemfile
|
|
155
|
-
- gemfiles/mongoid-6.gemfile
|
|
144
|
+
- gemfiles/carrierwave-3.0.gemfile
|
|
145
|
+
- gemfiles/carrierwave-3.1.gemfile
|
|
156
146
|
- gemfiles/mongoid-7.gemfile
|
|
157
147
|
- gemfiles/mongoid-8.gemfile
|
|
148
|
+
- gemfiles/mongoid-9.gemfile
|
|
158
149
|
- lib/carrierwave/mongoid.rb
|
|
159
150
|
- lib/carrierwave/mongoid/version.rb
|
|
160
151
|
- lib/carrierwave/storage/grid_fs.rb
|
|
@@ -174,7 +165,6 @@ homepage: https://github.com/carrierwaveuploader/carrierwave-mongoid
|
|
|
174
165
|
licenses:
|
|
175
166
|
- MIT
|
|
176
167
|
metadata: {}
|
|
177
|
-
post_install_message:
|
|
178
168
|
rdoc_options: []
|
|
179
169
|
require_paths:
|
|
180
170
|
- lib
|
|
@@ -182,15 +172,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
182
172
|
requirements:
|
|
183
173
|
- - ">="
|
|
184
174
|
- !ruby/object:Gem::Version
|
|
185
|
-
version:
|
|
175
|
+
version: 3.2.0
|
|
186
176
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
187
177
|
requirements:
|
|
188
178
|
- - ">="
|
|
189
179
|
- !ruby/object:Gem::Version
|
|
190
180
|
version: '0'
|
|
191
181
|
requirements: []
|
|
192
|
-
rubygems_version:
|
|
193
|
-
signing_key:
|
|
182
|
+
rubygems_version: 4.0.23
|
|
194
183
|
specification_version: 4
|
|
195
184
|
summary: Mongoid support for CarrierWave
|
|
196
185
|
test_files:
|
data/gemfiles/mongoid-4.gemfile
DELETED