carrierwave-mongoid 1.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef0e9975652d6d95435a2a400dc95b0ea4d8c8be278ac1109ba9b5d7b1efa70c
4
- data.tar.gz: 02cb852f7ce81a0ac5ddf759ba4eeea3ca41462c6c3a8633493dc43e05667e1c
3
+ metadata.gz: aef966be0bd79244a4ee503f0691586c3c8f1006f40c9b7b722e98bb182dfe25
4
+ data.tar.gz: 86294733bde85ddff896d9d350f4d3247849eef45dcbfbe4b4f2c883cc4eeb3d
5
5
  SHA512:
6
- metadata.gz: e21bad007ebc54dbf66986f4b9b3e5821a3b3122eb646408cabcfde89399ad71580a2e2b5ace415cfb4268cb834fe35e5a6bc22adf1c5a0da5aac2cab973e2c5
7
- data.tar.gz: f7073fbe4035e5e28023df6c19c4d34e348489115e77dd455ec6a0d6ff9f49b8c1a22df312aba1caf4d5a47bec5a3a3c322513f2e360e62823e61a64d7c63630
6
+ metadata.gz: a8a27055b64dd133662986b49b13020fc93f56ad0094bc59664141405b91b8d9224d07960bb874b6ed728dc91fe0fbd8de51d561cd197d7f6144fe83b8e6e328
7
+ data.tar.gz: de0f640f9be1467330211dde77ffd9e108105340eb377201f6c7e6da11183d19880172cc1582364b6d0adaa03ede0501926a7ff5634f81da9fd98828ad43b666
@@ -6,24 +6,14 @@ jobs:
6
6
  strategy:
7
7
  matrix:
8
8
  mongodb: [4.4]
9
- ruby: [2.7, "3.0", 3.1, 3.2, 3.3]
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
  - mongoid-9
16
- include:
17
- - { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-0.10" }
18
- - { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-0.11" }
19
- - { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.1" }
20
- - { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.2" }
21
- - { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.3" }
22
- - { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-2.0" }
23
- - { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-3" }
24
- - { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-4" }
25
- - { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-5" }
26
- - { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-6" }
27
17
  name: Ruby ${{ matrix.ruby }} with ${{ matrix.gemfile }}
28
18
  env:
29
19
  BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
@@ -32,6 +22,8 @@ jobs:
32
22
  uses: supercharge/mongodb-github-action@1.3.0
33
23
  with:
34
24
  mongodb-version: ${{ matrix.mongodb }}
25
+ - name: Install ImageMagick
26
+ run: sudo apt-get -y install imagemagick
35
27
  - uses: actions/checkout@v4
36
28
  - name: Set up Ruby
37
29
  uses: ruby/setup-ruby@v1
data/.gitignore CHANGED
@@ -3,6 +3,7 @@ pkg/*
3
3
  *.sw*
4
4
  .bundle
5
5
  spec/public
6
+ spec/tmp
6
7
  .rvmrc
7
8
  .ruby-version
8
9
  Gemfile.lock
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
@@ -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", [">= 0.8", "< 3"]
24
- s.add_dependency "mongoid", [">= 3.0", "< 10.0"]
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
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
@@ -1,5 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "mongoid", "~> 3.1"
3
+ gem "carrierwave", "~> 3.0.0"
4
4
 
5
5
  gemspec path: "../"
@@ -1,6 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "mongoid", "~> 6.0"
4
- gem "carrierwave", "~> 1.3"
3
+ gem "carrierwave", "~> 3.1.0"
5
4
 
6
5
  gemspec path: "../"
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Mongoid
3
- VERSION = "1.5.0"
3
+ VERSION = "1.6.0"
4
4
  end
5
5
  end
@@ -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
- super
12
+ module CarrierWave3
13
+ def mount_uploader(column, uploader=nil, options={}, &block)
14
+ field options[:mount_on] || column
18
15
 
19
- alias_method :read_uploader, :read_attribute
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
- class_eval <<-RUBY, __FILE__, __LINE__+1
40
- def #{column}=(new_file)
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
- def remove_#{column}=(arg)
58
- if ['1', true].include?(arg)
59
- column = _mounter(:#{column}).serialization_column
60
- send(:"\#{column}_will_change!")
61
- end
62
- super
63
- end
22
+ super
23
+ end
64
24
 
65
- def remove_#{column}!
66
- super unless respond_to?(:paranoid?) && paranoid? && flagged_for_destroy?
67
- end
25
+ private
68
26
 
69
- # Overrides Mongoid's default dirty behavior to instead work more like
70
- # ActiveRecord's. Mongoid doesn't deem an attribute as changed unless
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
- # The default Mongoid attribute_will_change! method is not enough
80
- # when we want to upload a new file in an existing embedded document.
81
- # The custom version of that method forces the callbacks to be
82
- # ran and so does the upload.
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
- # Since we had to use tricks with _old_ and _new_ values to properly
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
- # CarrierWave 1.1 references ::ActiveRecord constant directly which
103
- # will fail in projects without ActiveRecord. We need to overwrite this
104
- # method to avoid it.
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
- def find_previous_model_for_#{column}
111
- if self.embedded?
112
- ancestors =
113
- if self.respond_to?(:_association) # Mongoid >= 7.0.0.beta
114
- [[ self._association.key, self._parent ]].tap { |x| x.unshift([ x.first.last._association.key, x.first.last._parent ]) while x.first.last.embedded? }
115
- elsif self.respond_to?(:__metadata) # Mongoid >= 4.0.0.beta1 < 7.0.0.beta
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 Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
138
- next if _mounter(column.to_sym).uploaders.blank?
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 mongoid reload
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
- if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new('1.0.beta')
157
- def mount_uploaders(column, uploader = nil, options = {}, &block)
158
- field (options[:mount_on] || column), type: Array, default: []
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 column if uploader_option(column.to_sym, :validate_integrity)
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
- after_save :"remove_previously_stored_#{column}"
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__, (__LINE__ + 1)
179
- def #{column}=(new_files)
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
- super
190
- end
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
- def #{column}_changed?
193
- changed_attributes.has_key?("#{column}")
133
+ send(:"\#{column}_will_change!")
134
+ super
194
135
  end
195
136
 
196
- def remove_#{column}=(value)
197
- if ['1', true].include?(value)
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}"] = ['_new_']
164
+ changed_attributes["#{column}"] = '_new_'
212
165
  end
213
166
 
214
- def remove_previously_stored_#{column}
215
- before, after = @_previous_changes_for_#{column}
216
- # Don't delete if the files had the same name
217
- return if before.nil? && after.nil?
218
- # Proceed to remove the file, use the original name instead of '_new_'
219
- before = @_previous_uploader_value_for_#{column} || before
220
- _mounter(:#{column}).remove_previous(Array.wrap(before), Array.wrap(after))
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
- def serializable_hash(options = nil)
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 = options && options[:only] && Array.wrap(options[:only]).map(&:to_s)
228
-
229
- self.class.uploaders.each do |column, _uploader|
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
- next if _mounter(column.to_sym).uploaders.blank?
232
- hash[column.to_s] = _mounter(column.to_sym).uploaders.map(&:serializable_hash)
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
- def store_previous_changes_for_#{column}
240
- @_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
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
- expect(record[:images]).to match_array(['test.jpeg'])
404
- expect(record.images_identifiers).to match_array(['test.jpeg'])
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 old file had the same path' do
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(public_path('uploads/old.jpeg'))
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 old file had the same dynamic path' do
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(public_path('uploads/Mike.jpeg'))
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 'does not remove file if old file had the same path' do
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(public_path('uploads/old.jpeg'))
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 'does not remove file if old file had the same path' do
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(public_path('uploads/old.jpeg'))
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 old file had the same path' do
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(public_path('uploads/old.jpeg'))
888
- expect(File).to exist(public_path('uploads/thumb_old.jpeg'))
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(public_path('uploads/old.txt'))
937
+ expect(File).to exist(model.textfiles[0].path)
938
938
  end
939
939
 
940
- it 'does not remove file1 or file2 if file1 and file2 have the same paths' do
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(public_path('uploads/old.jpeg'))
947
- expect(File).to exist(public_path('uploads/old.txt'))
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 old file had the same path' do
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(public_path('uploads/old.jpeg'))
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 write nothing to the database, to prevent overridden filenames to fail because of unassigned attributes" do
172
- expect(@doc[:image]).to be_nil
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
- expect(@doc[:image]).to eq 'test.jpeg'
351
- expect(@doc.image_identifier).to eq 'test.jpeg'
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 "should not remove file if old file had the same path" do
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?(public_path('uploads/old.jpeg'))).to be_truthy
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 "should not remove file if old file had the same dynamic path" do
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?(public_path('uploads/test.jpeg'))).to be_truthy
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 "should not remove file if old file had the same path" do
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?(public_path('uploads/old.jpeg'))).to be_truthy
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 "should not remove file if old file had the same path" do
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?(public_path('uploads/old.jpeg'))).to be_truthy
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
@@ -782,11 +794,11 @@ describe CarrierWave::Mongoid do
782
794
  expect(File.exist?(public_path('uploads/thumb_old.jpeg'))).to be_falsey
783
795
  end
784
796
 
785
- it "should not remove file if old file had the same path" do
797
+ it 'should not remove new file if both of files had the same path' do
786
798
  @doc.image = stub_file('old.jpeg')
787
799
  expect(@doc.save).to be_truthy
788
- expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
789
- expect(File.exist?(public_path('uploads/thumb_old.jpeg'))).to be_truthy
800
+ expect(File.exist?(@doc.image.path)).to be_truthy
801
+ expect(File.exist?(@doc.image.thumb.path)).to be_truthy
790
802
  end
791
803
  end
792
804
 
@@ -819,21 +831,21 @@ describe CarrierWave::Mongoid do
819
831
  expect(File.exist?(public_path('uploads/old.txt'))).to be_falsey
820
832
  end
821
833
 
822
- 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
823
835
  @doc.image = stub_file('new.jpeg')
824
836
  @doc.textfile = stub_file('old.txt')
825
837
  expect(@doc.save).to be_truthy
826
838
  expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
827
839
  expect(File.exist?(public_path('uploads/old.jpeg'))).to be_falsey
828
- expect(File.exist?(public_path('uploads/old.txt'))).to be_truthy
840
+ expect(File.exist?(@doc.textfile.path)).to be_truthy
829
841
  end
830
842
 
831
- it "should not remove file1 or file2 if file1 and file2 have the same paths" do
843
+ it "should not remove new files if each pair of files has the same paths" do
832
844
  @doc.image = stub_file('old.jpeg')
833
845
  @doc.textfile = stub_file('old.txt')
834
846
  expect(@doc.save).to be_truthy
835
- expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
836
- expect(File.exist?(public_path('uploads/old.txt'))).to be_truthy
847
+ expect(File.exist?(@doc.image.path)).to be_truthy
848
+ expect(File.exist?(@doc.textfile.path)).to be_truthy
837
849
  end
838
850
  end
839
851
 
@@ -860,10 +872,10 @@ describe CarrierWave::Mongoid do
860
872
  expect(File.exist?(public_path('uploads/old.jpeg'))).to be_falsey
861
873
  end
862
874
 
863
- it "should not remove file if old file had the same path" do
875
+ it 'should not remove new file if both of files had the same path' do
864
876
  @doc.avatar = stub_file('old.jpeg')
865
877
  expect(@doc.save).to be_truthy
866
- expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
878
+ expect(File).to exist(@doc.avatar.path)
867
879
  end
868
880
  end
869
881
 
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'bundler/setup'
3
3
  require 'rspec'
4
4
  require 'tempfile'
5
5
  require 'stringio'
6
+ require 'logger'
6
7
 
7
8
  require 'carrierwave'
8
9
  require 'carrierwave/mongoid'
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.5.0
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: 2024-11-22 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: carrierwave
@@ -17,27 +16,27 @@ dependencies:
17
16
  requirements:
18
17
  - - ">="
19
18
  - !ruby/object:Gem::Version
20
- version: '0.8'
19
+ version: '2.2'
21
20
  - - "<"
22
21
  - !ruby/object:Gem::Version
23
- version: '3'
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: '0.8'
29
+ version: '2.2'
31
30
  - - "<"
32
31
  - !ruby/object:Gem::Version
33
- version: '3'
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: '3.0'
39
+ version: '7.0'
41
40
  - - "<"
42
41
  - !ruby/object:Gem::Version
43
42
  version: '10.0'
@@ -47,7 +46,7 @@ dependencies:
47
46
  requirements:
48
47
  - - ">="
49
48
  - !ruby/object:Gem::Version
50
- version: '3.0'
49
+ version: '7.0'
51
50
  - - "<"
52
51
  - !ruby/object:Gem::Version
53
52
  version: '10.0'
@@ -141,18 +140,9 @@ 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/mongoid-3.gemfile
153
- - gemfiles/mongoid-4.gemfile
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
158
148
  - gemfiles/mongoid-9.gemfile
@@ -175,7 +165,6 @@ homepage: https://github.com/carrierwaveuploader/carrierwave-mongoid
175
165
  licenses:
176
166
  - MIT
177
167
  metadata: {}
178
- post_install_message:
179
168
  rdoc_options: []
180
169
  require_paths:
181
170
  - lib
@@ -183,15 +172,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
172
  requirements:
184
173
  - - ">="
185
174
  - !ruby/object:Gem::Version
186
- version: '0'
175
+ version: 3.2.0
187
176
  required_rubygems_version: !ruby/object:Gem::Requirement
188
177
  requirements:
189
178
  - - ">="
190
179
  - !ruby/object:Gem::Version
191
180
  version: '0'
192
181
  requirements: []
193
- rubygems_version: 3.5.22
194
- signing_key:
182
+ rubygems_version: 4.0.23
195
183
  specification_version: 4
196
184
  summary: Mongoid support for CarrierWave
197
185
  test_files:
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 0.10.0"
4
- gem "mongoid", "~> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 0.11.0"
4
- gem "mongoid", "~> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 1.1.0"
4
- gem "mongoid", "> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 1.2.0"
4
- gem "mongoid", "> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 1.3.0"
4
- gem "mongoid", "> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 2.0.0"
4
- gem "mongoid", "> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "carrierwave", "~> 2.1.0"
4
- gem "mongoid", "> 5.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "mongoid", "~> 4.0"
4
- gem "carrierwave", "~> 0.11.0"
5
-
6
- gemspec path: "../"
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "mongoid", "~> 5.0"
4
- gem "carrierwave", "~> 0.11.0"
5
-
6
- gemspec path: "../"