carrierwave-mongoid 1.2.0 → 1.4.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: e3d352163a5884a2d049f679726257062179c252456e3ca79bd51d74f9eddc5c
4
- data.tar.gz: ae0662582c29b0f1b1dc1d8ef66f8d055019289a1af50496ea3f95aa611ec987
3
+ metadata.gz: 695a0d096fa1477e3034c1d1e265f43064edd398e33a683ef664e16af964f4f6
4
+ data.tar.gz: 72b97b4845445ef2378dfc565f3f70fd7d151b027fcd42d0e932c35a1027bd46
5
5
  SHA512:
6
- metadata.gz: 1bab73849cf2ee4b2fae2eca787b1511dbc421ad1b0bc9c46c8b0463e74c42d897a9868fe754bdf4198057a21cf8403a408be5acff676518affe371a5df36659
7
- data.tar.gz: 07c1ba5af2b570b0f9d210c4002d56a8089f3c5f1d5f194cf0a54d60af84f6f54860bf8deea4b9c640de23b7d3940b9c72fee05573f1fc7bbb3bfe0066780c59
6
+ metadata.gz: e0c6bc4f88cfe2f136c6fa3e2dc99fb94c530ef8c1b85ba9932b36bf82d268a480892f76e063be6163ed98b94764f622161025f13df70a3743bb7ee720e79600
7
+ data.tar.gz: 7087b901685c9f7b09b933aa0ccbc7154d7611ab4b1b0e95042d24c23c1b0f358b71f9b888b27f0c3e3d17c3938ed3ff65db517558cd9b63bd0cb72616b06fd9
@@ -0,0 +1,45 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ mongodb: [4.4]
9
+ ruby: [2.7, 3.0, 3.1]
10
+ gemfile:
11
+ - carrierwave-2.1
12
+ - carrierwave-2.2
13
+ - mongoid-7
14
+ - mongoid-8
15
+ include:
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" }
26
+ name: Ruby ${{ matrix.ruby }} with ${{ matrix.gemfile }}
27
+ env:
28
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
29
+ steps:
30
+ - name: Set up MongoDB ${{ matrix.mongodb }}
31
+ uses: supercharge/mongodb-github-action@1.3.0
32
+ with:
33
+ mongodb-version: ${{ matrix.mongodb }}
34
+ - uses: actions/checkout@v2
35
+ - name: Set up Ruby
36
+ uses: ruby/setup-ruby@v1
37
+ with:
38
+ ruby-version: ${{ matrix.ruby }}
39
+ bundler-cache: true
40
+ - run: cat /home/runner/work/carrierwave-mongoid/carrierwave-mongoid/vendor/bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/mimemagic-0.4.3/gem_make.out
41
+ if: ${{ failure() }}
42
+ - run: cat /home/runner/work/carrierwave-mongoid/carrierwave-mongoid/vendor/bundle/ruby/3.0.0/gems/mimemagic-0.4.3/ext/mimemagic/Rakefile
43
+ if: ${{ failure() }}
44
+ - name: Run tests
45
+ run: bundle exec rake
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # CarrierWave for Mongoid
2
2
 
3
3
  [![Gem Version](http://img.shields.io/gem/v/carrierwave-mongoid.svg)](https://rubygems.org/gems/carrierwave-mongoid)
4
- [![Build Status](https://travis-ci.org/carrierwaveuploader/carrierwave-mongoid.svg)](http://travis-ci.org/carrierwaveuploader/carrierwave-mongoid)
5
- [![Code Climate](http://img.shields.io/codeclimate/github/carrierwaveuploader/carrierwave-mongoid.svg)](https://codeclimate.com/github/carrierwaveuploader/carrierwave-mongoid)
4
+ [![Build Status](https://github.com/carrierwaveuploader/carrierwave-mongoid/workflows/CI/badge.svg?branch=master)](https://github.com/carrierwaveuploader/carrierwave-mongoid/actions?query=workflow%3ACI)
6
5
  [![Gem Downloads](https://img.shields.io/gem/dt/carrierwave-mongoid.svg)](https://rubygems.org/gems/carrierwave-mongoid)
7
6
 
8
7
  This gem adds support for Mongoid and MongoDB's GridFS to
@@ -230,4 +229,4 @@ class User
230
229
  end
231
230
  ```
232
231
 
233
- You can read more about this [here](https://github.com/carrierwaveuploader/carrierwave/issues#issue/81)
232
+ You can read more about this [here](https://github.com/carrierwaveuploader/carrierwave/issues/81)
@@ -20,12 +20,12 @@ 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", "< 1.4"]
24
- s.add_dependency "mongoid", [">= 3.0", "< 8.0"]
23
+ s.add_dependency "carrierwave", [">= 0.8", "< 3"]
24
+ s.add_dependency "mongoid", [">= 3.0", "< 9.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
- s.add_development_dependency "rspec", "~>3.4.0"
28
- s.add_development_dependency "rake", "~>11.1.2"
27
+ s.add_development_dependency "rspec", "~> 3.4"
28
+ s.add_development_dependency "rake", ">= 12.3.3"
29
29
  s.add_development_dependency "mini_magick"
30
30
  s.add_development_dependency "pry"
31
31
  end
@@ -1,5 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem "carrierwave", "~> 1.1.0"
4
+ gem "mongoid", "> 5.0"
4
5
 
5
6
  gemspec path: "../"
@@ -1,5 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem "carrierwave", "~> 1.2.0"
4
+ gem "mongoid", "> 5.0"
4
5
 
5
6
  gemspec path: "../"
@@ -1,5 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem "carrierwave", "~> 1.3.0"
4
+ gem "mongoid", "> 5.0"
4
5
 
5
6
  gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "carrierwave", "~> 2.0.0"
4
+ gem "mongoid", "> 5.0"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "carrierwave", "~> 2.1.0"
4
+ gem "mongoid", "> 5.0"
5
+
6
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "carrierwave", "~> 2.2.0"
4
+ gem "mongoid", "> 5.0"
5
+
6
+ gemspec path: "../"
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem "mongoid", "~> 7.0"
4
- gem "carrierwave", "~> 1.3"
4
+ gem "carrierwave", "~> 2.2"
5
5
 
6
6
  gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid", "~> 8.0"
4
+ gem "carrierwave", "~> 2.2"
5
+
6
+ gemspec path: "../"
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Mongoid
3
- VERSION = "1.2.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
@@ -109,11 +109,14 @@ module CarrierWave
109
109
 
110
110
  def find_previous_model_for_#{column}
111
111
  if self.embedded?
112
- if self.respond_to?(:__metadata) # Mongoid >= 4.0.0.beta1
113
- ancestors = [[ self.__metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.__metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
114
- else # Mongoid < 4.0.0.beta1
115
- ancestors = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
116
- end
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
117
120
  first_parent = ancestors.first.last
118
121
  reloaded_parent = first_parent.class.unscoped.find(first_parent.to_key.first)
119
122
  association = ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }
@@ -141,8 +144,104 @@ module CarrierWave
141
144
  end
142
145
  super(options).merge(hash)
143
146
  end
147
+
148
+ # Reset cached mounter on mongoid reload
149
+ def reload
150
+ @_mounters = nil
151
+ super
152
+ end
144
153
  RUBY
145
154
  end
155
+
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: []
159
+
160
+ super
161
+
162
+ alias_method :read_uploader, :read_attribute
163
+ alias_method :write_uploader, :write_attribute
164
+ public :read_uploader
165
+ public :write_uploader
166
+
167
+ include CarrierWave::Validations::ActiveModel
168
+
169
+ validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
170
+ validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
171
+
172
+ before_update :"store_previous_changes_for_#{column}"
173
+ before_save :"write_#{column}_identifier"
174
+ after_save :"store_#{column}!"
175
+ after_save :"remove_previously_stored_#{column}"
176
+ after_destroy :"remove_#{column}!"
177
+
178
+ class_eval <<-RUBY, __FILE__, (__LINE__ + 1)
179
+ def #{column}=(new_files)
180
+ column = _mounter(:#{column}).serialization_column
181
+
182
+ previous_uploader_value = read_uploader(column)
183
+ @_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
+
189
+ super
190
+ end
191
+
192
+ def #{column}_changed?
193
+ changed_attributes.has_key?("#{column}")
194
+ end
195
+
196
+ def remove_#{column}=(value)
197
+ if ['1', true].include?(value)
198
+ column = _mounter(:#{column}).serialization_column
199
+
200
+ send(:"\#{column}_will_change!")
201
+ end
202
+
203
+ super
204
+ end
205
+
206
+ # The default Mongoid attribute_will_change! method is not enough
207
+ # when we want to upload a new file in an existing embedded document.
208
+ # The custom version of that method forces the callbacks to be
209
+ # ran and so does the upload.
210
+ def #{column}_will_change!
211
+ changed_attributes["#{column}"] = ['_new_']
212
+ end
213
+
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))
221
+ end
222
+
223
+ def serializable_hash(options = nil)
224
+ hash = {}
225
+
226
+ 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|
230
+ 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)
233
+ end
234
+ end
235
+
236
+ super(options).merge(hash)
237
+ end
238
+
239
+ def store_previous_changes_for_#{column}
240
+ @_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
241
+ end
242
+ RUBY
243
+ end
244
+ end
146
245
  end # Mongoid
147
246
  end # CarrierWave
148
247
 
@@ -44,7 +44,7 @@ module CarrierWave
44
44
  end
45
45
 
46
46
  def write(file)
47
- grid[@uploader.store_path] = file
47
+ grid[path] = file
48
48
  ensure
49
49
  @grid_file = nil
50
50
  end
@@ -109,6 +109,55 @@ module CarrierWave
109
109
  CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
110
110
  end
111
111
 
112
+ ##
113
+ # Cache the file in MongoDB's GridFS GridStore
114
+ #
115
+ # === Parameters
116
+ #
117
+ # [file (CarrierWave::SanitizedFile)] the file to store
118
+ #
119
+ # === Returns
120
+ #
121
+ # [CarrierWave::SanitizedFile] a sanitized file
122
+ #
123
+ def cache!(file)
124
+ stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.cache_path)
125
+ stored.write(file)
126
+ stored
127
+ end
128
+
129
+ ##
130
+ # Retrieve the cached file from MongoDB's GridFS GridStore
131
+ #
132
+ # === Parameters
133
+ #
134
+ # [identifier (String)] uniquely identifies a cache file
135
+ #
136
+ # === Returns
137
+ #
138
+ # [CarrierWave::Storage::GridFS::File] a sanitized file
139
+ #
140
+ def retrieve_from_cache!(identifier)
141
+ CarrierWave::Storage::GridFS::File.new(uploader, uploader.cache_path(identifier))
142
+ end
143
+
144
+ def delete_dir!(path)
145
+ # do nothing, because there's no such things as 'empty directory'
146
+ end
147
+
148
+ ##
149
+ # Clean old caches
150
+ #
151
+ # === Parameters
152
+ #
153
+ # [seconds (Integer)] duration in seconds, caches older than this will be deleted
154
+ #
155
+ def clean_cache!(seconds)
156
+ File.grid.namespace.
157
+ where(filename: /\d+-\d+-\d+(?:-\d+)?\/.+/).
158
+ and(:filename.lt => (Time.now.utc - seconds).to_i.to_s).
159
+ delete
160
+ end
112
161
  end # GridFS
113
162
  end # Storage
114
163
  end # CarrierWave