gnuside-carrierwave-mongoid 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +19 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +20 -0
- data/README.md +219 -0
- data/Rakefile +12 -0
- data/gemfiles/carrierwave-0.8.gemfile +5 -0
- data/gemfiles/carrierwave-master.gemfile +5 -0
- data/gemfiles/mongoid-4.0.gemfile +6 -0
- data/gnuside-carrierwave-mongoid.gemspec +29 -0
- data/lib/carrierwave/mongoid.rb +118 -0
- data/lib/carrierwave/mongoid/version.rb +5 -0
- data/lib/carrierwave/storage/grid_fs.rb +114 -0
- data/lib/glebtv-carrierwave-mongoid.rb +1 -0
- data/spec/fixtures/new.jpeg +1 -0
- data/spec/fixtures/new.txt +1 -0
- data/spec/fixtures/old.jpeg +1 -0
- data/spec/fixtures/old.txt +1 -0
- data/spec/fixtures/portrait.jpg +0 -0
- data/spec/fixtures/test.jpeg +1 -0
- data/spec/fixtures/test.jpg +1 -0
- data/spec/mongoid_spec.rb +879 -0
- data/spec/spec_helper.rb +74 -0
- data/spec/storage/grid_fs_spec.rb +184 -0
- metadata +210 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca43efedbc77776f18febcb4221aaa33c3282ce3
|
4
|
+
data.tar.gz: 3bf91aabd888b0cc5a3d64843d9c1a7ce313654e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3b041ee18c6af1ead1bbaa2a5067427602dd78cd6aef24b191db4d96c54f14ca6d003c114b1199d40a920b16107a8cb6934040f54aabc13572ffb726903551e
|
7
|
+
data.tar.gz: 35fdd5f94ff329d1ea3d703e433a522b5b0dad7680cad1981a63e5cce2ab321a539e64998ea66e6260bc32ba62573e831493ef4184153263a66f83264c618485
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
glebtv-carrierwave-mongoid
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p247
|
data/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
services: mongodb
|
2
|
+
|
3
|
+
notifications:
|
4
|
+
email: false
|
5
|
+
|
6
|
+
language: ruby
|
7
|
+
rvm:
|
8
|
+
- 1.9.3
|
9
|
+
- 2.0.0
|
10
|
+
- jruby-19mode
|
11
|
+
- rbx-19mode
|
12
|
+
|
13
|
+
gemfile:
|
14
|
+
- Gemfile
|
15
|
+
- gemfiles/carrierwave-0.8.gemfile
|
16
|
+
- gemfiles/carrierwave-master.gemfile
|
17
|
+
- gemfiles/mongoid-3.0.gemfile
|
18
|
+
- gemfiles/mongoid-3.1.gemfile
|
19
|
+
- gemfiles/mongoid-4.0.gemfile
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
glebtv-carrierwave-mongoid (0.6.5)
|
5
|
+
carrierwave (>= 0.8.0, < 0.10.0)
|
6
|
+
mongoid (>= 3.0, < 5.0)
|
7
|
+
mongoid (>= 3.0, < 5.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activemodel (3.2.15)
|
13
|
+
activesupport (= 3.2.15)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
activesupport (3.2.15)
|
16
|
+
i18n (~> 0.6, >= 0.6.4)
|
17
|
+
multi_json (~> 1.0)
|
18
|
+
builder (3.0.4)
|
19
|
+
carrierwave (0.9.0)
|
20
|
+
activemodel (>= 3.2.0)
|
21
|
+
activesupport (>= 3.2.0)
|
22
|
+
json (>= 1.7)
|
23
|
+
coderay (1.0.9)
|
24
|
+
diff-lcs (1.2.4)
|
25
|
+
i18n (0.6.5)
|
26
|
+
json (1.8.1)
|
27
|
+
method_source (0.8.2)
|
28
|
+
mini_magick (3.6.0)
|
29
|
+
subexec (~> 0.2.1)
|
30
|
+
mongoid (3.1.5)
|
31
|
+
activemodel (~> 3.2)
|
32
|
+
moped (~> 1.4)
|
33
|
+
origin (~> 1.0)
|
34
|
+
tzinfo (~> 0.3.29)
|
35
|
+
moped (1.5.1)
|
36
|
+
multi_json (1.8.2)
|
37
|
+
origin (1.1.0)
|
38
|
+
pry (0.9.12.2)
|
39
|
+
coderay (~> 1.0.5)
|
40
|
+
method_source (~> 0.8)
|
41
|
+
slop (~> 3.4)
|
42
|
+
rake (10.1.0)
|
43
|
+
rspec (2.14.1)
|
44
|
+
rspec-core (~> 2.14.0)
|
45
|
+
rspec-expectations (~> 2.14.0)
|
46
|
+
rspec-mocks (~> 2.14.0)
|
47
|
+
rspec-core (2.14.6)
|
48
|
+
rspec-expectations (2.14.3)
|
49
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
50
|
+
rspec-mocks (2.14.4)
|
51
|
+
slop (3.4.6)
|
52
|
+
subexec (0.2.3)
|
53
|
+
tzinfo (0.3.38)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
glebtv-carrierwave-mongoid!
|
60
|
+
mini_magick
|
61
|
+
pry
|
62
|
+
rake (~> 10.0)
|
63
|
+
rspec (~> 2.14)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2012 Jonas Nicklas
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
# Fork with OPTIONAL gridfs
|
2
|
+
|
3
|
+
# CarrierWave for Mongoid
|
4
|
+
|
5
|
+
See upstream for details: https://github.com/carrierwaveuploader/carrierwave-mongoid
|
6
|
+
|
7
|
+
|
8
|
+
This functionality used to be part of CarrierWave but has since been extracted
|
9
|
+
into this gem.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Install the latest release:
|
14
|
+
|
15
|
+
gem install carrierwave-mongoid
|
16
|
+
|
17
|
+
Require it in your code:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'carrierwave/mongoid'
|
21
|
+
```
|
22
|
+
|
23
|
+
Or, in Rails you can add it to your Gemfile:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
|
27
|
+
```
|
28
|
+
|
29
|
+
Note: If using Rails 4, you'll need to make sure `mongoid-grid_fs` is `>= 1.9.0`.
|
30
|
+
If in doubt, run `bundle update mongoid-grid_fs`
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'mongoid-grid_fs', github: 'ahoward/mongoid-grid_fs'
|
34
|
+
```
|
35
|
+
|
36
|
+
## Getting Started
|
37
|
+
|
38
|
+
Follow the "Getting Started" directions in the main
|
39
|
+
[Carrierwave repository](https://raw.github.com/carrierwaveuploader/carrierwave/).
|
40
|
+
|
41
|
+
[Suggested] Add the field to your attr_accessor list for mass assignment
|
42
|
+
protection:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
attr_accessible :avatar, :avatar_cache
|
46
|
+
```
|
47
|
+
|
48
|
+
Now you can cache files by assigning them to the attribute; they will
|
49
|
+
automatically be stored when the record is saved. Ex:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
u = User.new
|
53
|
+
u.avatar = File.open('somewhere')
|
54
|
+
u.save!
|
55
|
+
```
|
56
|
+
|
57
|
+
## Using MongoDB's GridFS store
|
58
|
+
|
59
|
+
In your uploader, set the storage to `:grid_fs`:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
63
|
+
storage :grid_fs
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
Bringing it all together, you can also configure Carrierwave to use Mongoid's
|
68
|
+
database connection and default all storage to GridFS. That might look something
|
69
|
+
like this:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
CarrierWave.configure do |config|
|
73
|
+
config.storage = :grid_fs
|
74
|
+
config.root = Rails.root.join('tmp')
|
75
|
+
config.cache_dir = "uploads"
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## Serving uploading files
|
80
|
+
|
81
|
+
Since GridFS doesn't make the files available via HTTP, you'll need to stream
|
82
|
+
them yourself. For example, in Rails, you could use the `send_data` method:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class UsersController < ApplicationController
|
86
|
+
def avatar
|
87
|
+
content = @user.avatar.read
|
88
|
+
if stale?(etag: content, last_modified: @user.updated_at.utc, public: true)
|
89
|
+
send_data content, type: @user.avatar.file.content_type, disposition: "inline"
|
90
|
+
expires_in 0, public: true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# and in routes.rb
|
96
|
+
resources :users do
|
97
|
+
get :avatar, on: :member
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
You can optionally tell CarrierWave the URL you will serve your images from,
|
102
|
+
allowing it to generate the correct URL, by setting `grid_fs_access_url`:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
CarrierWave.configure do |config|
|
106
|
+
config.grid_fs_access_url = "/systems/uploads"
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
## Route configuration
|
111
|
+
|
112
|
+
If you follow the instruction to this point, the uploaded images will be
|
113
|
+
stored to GridFS, and you are responsible for serving the images a public
|
114
|
+
endpoint. If you would like to use the `#url` method on the uploaded file, you
|
115
|
+
will need to take some additional steps.
|
116
|
+
|
117
|
+
The `grid_fs_access_url` configuration option is the prefix for the path of
|
118
|
+
the stored file in carrierwave.
|
119
|
+
|
120
|
+
Let's assume that we have a mounted `avatar` uploader on a `User` model and a
|
121
|
+
`GridfsController`. Let's also assume that your uploader definition
|
122
|
+
(i.e. `app/uploaders/avatar_uploader.rb`) defines `store_dir` like this:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
def store_dir
|
126
|
+
"#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
If `grid_fs_access_url` (in `config/initializers/carrierwave.rb`) were:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
config.grid_fs_access_url = '/uploads/grid'
|
134
|
+
```
|
135
|
+
|
136
|
+
You would need to define a route in your `config/routes.rb` like so:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#avatar'
|
140
|
+
```
|
141
|
+
|
142
|
+
Now, `user.avatar.url` should return an appropriate url path to use in your
|
143
|
+
views.
|
144
|
+
|
145
|
+
### Different uploaded versions
|
146
|
+
|
147
|
+
If you need to include different versions (e.g. thumbnails), additional routes
|
148
|
+
will help:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
match '/uploads/grid/user/avatar/:id/:filename' => 'gridfs#thumb_avatar', constraints: { filename: /thumb.*/ }
|
152
|
+
```
|
153
|
+
|
154
|
+
## Version differences
|
155
|
+
|
156
|
+
| Version | Notes |
|
157
|
+
|----------|---------------------------------------------------------------------------------|
|
158
|
+
| ~> 0.6.0 | ([compare][compare-0.6], [dependencies][deps-0.6]) Mongoid 3 & 4, bug fixes |
|
159
|
+
| ~> 0.5.0 | ([compare][compare-0.5], [dependencies][deps-0.5]) Mongoid::Paranoia support |
|
160
|
+
| ~> 0.4.0 | ([compare][compare-0.4], [dependencies][deps-0.4]) Carrierwave bump |
|
161
|
+
| ~> 0.3.0 | ([compare][compare-0.3], [dependencies][deps-0.3]) Mongoid >= 3.0 |
|
162
|
+
| ~> 0.2.0 | ([compare][compare-0.2], [dependencies][deps-0.2]) Rails >= 3.2, Mongoid ~> 2.0 |
|
163
|
+
| ~> 0.1.0 | ([compare][compare-0.1], [dependencies][deps-0.1]) Rails <= 3.1 |
|
164
|
+
|
165
|
+
[compare-0.6]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.5.0...v0.6.3
|
166
|
+
[compare-0.5]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.4.0...v0.5.0
|
167
|
+
[compare-0.4]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.3.1...v0.4.0
|
168
|
+
[compare-0.3]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.2.1...v0.3.1
|
169
|
+
[compare-0.2]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.1.7...v0.2.2
|
170
|
+
[compare-0.1]: https://github.com/carrierwaveuploader/carrierwave-mongoid/compare/v0.1.1...v0.1.7
|
171
|
+
|
172
|
+
[deps-0.6]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.6.3
|
173
|
+
[deps-0.5]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.5.0
|
174
|
+
[deps-0.4]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.4.0
|
175
|
+
[deps-0.3]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.3.1
|
176
|
+
[deps-0.2]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.2.2
|
177
|
+
[deps-0.1]: https://rubygems.org/gems/carrierwave-mongoid/versions/0.1.7
|
178
|
+
|
179
|
+
### Changes from earlier versions of CarrierWave <= 0.5.6
|
180
|
+
|
181
|
+
CarrierWave used to have built-in Mongoid support. This gem replaces that
|
182
|
+
support and only supports Mongoid ~> 2.1
|
183
|
+
|
184
|
+
You can use `upload_identifier` to retrieve the original name of the uploaded file.
|
185
|
+
|
186
|
+
In the earlier version, the mount_uploader-method for mongoid had been defined
|
187
|
+
in lib/carrierwave/orm/mongoid. This code has been moved to
|
188
|
+
carrierwave/mongoid. If you update from earlier versions, don't forget to adjust
|
189
|
+
your require accordingly in your carrierwave-initializer.
|
190
|
+
|
191
|
+
The default mount column used to be the name of the upload column plus
|
192
|
+
`_filename`. Now it is simply the name of the column. Most of the time, the
|
193
|
+
column was called `upload`, so it would have been mounted to `upload_filename`.
|
194
|
+
If you'd like to avoid a database migration, simply use the `:mount_on` option
|
195
|
+
to specify the field name explicitly. Therefore, you only have to add a
|
196
|
+
`_filename` to your column name. For example, if your column is called
|
197
|
+
`:upload`:
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
class Dokument
|
201
|
+
mount_uploader :upload, DokumentUploader, mount_on: :upload_filename
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
## Known issues and limitations
|
206
|
+
|
207
|
+
Note that files mounted in embedded documents aren't saved when parent documents
|
208
|
+
are saved. By default, mongoid does not cascade callbacks on embedded
|
209
|
+
documents. In order to save the attached files on embedded documents, you must
|
210
|
+
either explicitly call save on the embedded documents or you must configure the
|
211
|
+
embedded association to cascade the callbacks automatically. For example:
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
class User
|
215
|
+
embeds_many :pictures, cascade_callbacks: true
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
You can read more about this [here](https://github.com/carrierwaveuploader/carrierwave/issues#issue/81)
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc "Run all examples"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
8
|
+
#t.rspec_path = 'bin/rspec'
|
9
|
+
t.rspec_opts = %w[--color]
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :spec
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "carrierwave/mongoid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "gnuside-carrierwave-mongoid"
|
7
|
+
s.version = Carrierwave::Mongoid::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["GlebTv", "Jonas Nicklas", "Trevor Turk", "Roland Laurès"]
|
10
|
+
s.email = ["jonas.nicklas@gmail.com", "roland.laures@gnuside.com"]
|
11
|
+
s.homepage = "https://github.com/Gnuside/carrierwave-mongoid"
|
12
|
+
s.summary = "This fork is made to be compatible with rails 4"
|
13
|
+
s.description = "This fork is made to be compatible with rails 4"
|
14
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "mongoid", [">= 3.0", "< 5.0"]
|
22
|
+
s.add_dependency "carrierwave", [">= 0.8.0", "< 0.10.0"]
|
23
|
+
s.add_dependency "mongoid", [">= 4.0.0"]
|
24
|
+
s.add_dependency "mongoid-grid_fs", [">= 1.9"]
|
25
|
+
s.add_development_dependency "rspec", ["~> 2.14"]
|
26
|
+
s.add_development_dependency "rake", ["~> 10.0"]
|
27
|
+
s.add_development_dependency "mini_magick"
|
28
|
+
s.add_development_dependency "pry"
|
29
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
require 'carrierwave'
|
5
|
+
require 'carrierwave/validations/active_model'
|
6
|
+
|
7
|
+
module CarrierWave
|
8
|
+
module Mongoid
|
9
|
+
include CarrierWave::Mount
|
10
|
+
##
|
11
|
+
# See +CarrierWave::Mount#mount_uploader+ for documentation
|
12
|
+
#
|
13
|
+
def mount_uploader(column, uploader=nil, options={}, &block)
|
14
|
+
field_name = options[:mount_on] || column
|
15
|
+
unless fields.keys.include?(field_name.to_s) || fields.values.map { |f| f.options[:as].to_s }.include?(field_name.to_s)
|
16
|
+
field field_name
|
17
|
+
end
|
18
|
+
|
19
|
+
super
|
20
|
+
|
21
|
+
alias_method :read_uploader, :read_attribute
|
22
|
+
alias_method :write_uploader, :write_attribute
|
23
|
+
public :read_uploader
|
24
|
+
public :write_uploader
|
25
|
+
|
26
|
+
include CarrierWave::Validations::ActiveModel
|
27
|
+
|
28
|
+
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
29
|
+
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
30
|
+
|
31
|
+
after_save :"store_#{column}!"
|
32
|
+
before_save :"write_#{column}_identifier"
|
33
|
+
after_destroy :"remove_#{column}!"
|
34
|
+
before_update :"store_previous_model_for_#{column}"
|
35
|
+
after_save :"remove_previously_stored_#{column}"
|
36
|
+
|
37
|
+
class_eval <<-RUBY, __FILE__, __LINE__+1
|
38
|
+
def #{column}=(new_file)
|
39
|
+
column = _mounter(:#{column}).serialization_column
|
40
|
+
|
41
|
+
# mongoid won't upload a new file if there was no file previously.
|
42
|
+
write_uploader(column, '_old_') if self.persisted? && read_uploader(column).nil?
|
43
|
+
|
44
|
+
send(:"\#{column}_will_change!")
|
45
|
+
super
|
46
|
+
end
|
47
|
+
|
48
|
+
def remove_#{column}=(arg)
|
49
|
+
column = _mounter(:#{column}).serialization_column
|
50
|
+
send(:"\#{column}_will_change!")
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
def remove_#{column}!
|
55
|
+
super unless respond_to?(:paranoid?) && paranoid? && flagged_for_destroy?
|
56
|
+
end
|
57
|
+
|
58
|
+
# Overrides Mongoid's default dirty behavior to instead work more like
|
59
|
+
# ActiveRecord's. Mongoid doesn't deem an attribute as changed unless
|
60
|
+
# the new value is different than the original. Given that CarrierWave
|
61
|
+
# caches files before save, it's necessary to know that there's a
|
62
|
+
# pending change even though the attribute value itself might not
|
63
|
+
# reflect that yet.
|
64
|
+
def #{column}_changed?
|
65
|
+
changed_attributes.has_key?("#{column}")
|
66
|
+
end
|
67
|
+
|
68
|
+
# The default Mongoid attribute_will_change! method is not enough
|
69
|
+
# when we want to upload a new file in an existing embedded document.
|
70
|
+
# The custom version of that method forces the callbacks to be
|
71
|
+
# ran and so does the upload.
|
72
|
+
def #{column}_will_change!
|
73
|
+
changed_attributes["#{column}"] = '_new_'
|
74
|
+
end
|
75
|
+
|
76
|
+
def find_previous_model_for_#{column}
|
77
|
+
if self.embedded?
|
78
|
+
ancestors = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
|
79
|
+
first_parent = ancestors.first.last
|
80
|
+
reloaded_parent = first_parent.class.unscoped.find(first_parent.to_key.first)
|
81
|
+
association = ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }
|
82
|
+
association.is_a?(Array) ? association.find(to_key.first) : association
|
83
|
+
else
|
84
|
+
self.class.unscoped.for_ids(to_key).first
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def serializable_hash(options=nil)
|
89
|
+
hash = {}
|
90
|
+
|
91
|
+
except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s)
|
92
|
+
only = options && options[:only] && Array.wrap(options[:only]).map(&:to_s)
|
93
|
+
|
94
|
+
self.class.uploaders.each do |column, uploader|
|
95
|
+
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s))
|
96
|
+
hash[column.to_s] = _mounter(column.to_sym).uploader.serializable_hash
|
97
|
+
end
|
98
|
+
end
|
99
|
+
super(options).merge(hash)
|
100
|
+
end
|
101
|
+
RUBY
|
102
|
+
end
|
103
|
+
end # Mongoid
|
104
|
+
end # CarrierWave
|
105
|
+
|
106
|
+
if defined?(Mongoid::GridFs)
|
107
|
+
CarrierWave::Storage.autoload :GridFS, 'carrierwave/storage/grid_fs'
|
108
|
+
|
109
|
+
class CarrierWave::Uploader::Base
|
110
|
+
add_config :grid_fs_access_url
|
111
|
+
|
112
|
+
configure do |config|
|
113
|
+
config.storage_engines[:grid_fs] = "CarrierWave::Storage::GridFS"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
|