carrierwave-mongoid 0.1.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.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/README.rdoc +53 -0
- data/Rakefile +12 -0
- data/carrierwave-mongoid.gemspec +27 -0
- data/lib/carrierwave/mongoid.rb +33 -0
- data/lib/carrierwave/mongoid/version.rb +5 -0
- data/lib/carrierwave/storage/grid_fs.rb +136 -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 +287 -0
- data/spec/spec_helper.rb +63 -0
- data/spec/storage/grid_fs_spec.rb +149 -0
- metadata +151 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
= CarrierWave for Mongoid
|
2
|
+
|
3
|
+
This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the
|
4
|
+
CarrierWave documentation for more detailed usage instructions.
|
5
|
+
|
6
|
+
Install it like this:
|
7
|
+
|
8
|
+
gem install carrierwave-mongoid
|
9
|
+
|
10
|
+
Use it like this:
|
11
|
+
|
12
|
+
require 'carrierwave/mongoid'
|
13
|
+
|
14
|
+
Make sure to disable auto_validation on the mounted column.
|
15
|
+
|
16
|
+
Using bundler:
|
17
|
+
|
18
|
+
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
|
19
|
+
|
20
|
+
This used to be part of CarrierWave but has been extracted.
|
21
|
+
|
22
|
+
== Using MongoDB's GridFS store
|
23
|
+
|
24
|
+
You'll need to configure the database and host to use:
|
25
|
+
|
26
|
+
CarrierWave.configure do |config|
|
27
|
+
config.grid_fs_database = 'my_mongo_database'
|
28
|
+
config.grid_fs_host = 'mongo.example.com'
|
29
|
+
end
|
30
|
+
|
31
|
+
The defaults are 'carrierwave' and 'localhost'.
|
32
|
+
|
33
|
+
And then in your uploader, set the storage to <code>:grid_fs</code>:
|
34
|
+
|
35
|
+
class AvatarUploader < CarrierWave::Uploader::Base
|
36
|
+
storage :grid_fs
|
37
|
+
end
|
38
|
+
|
39
|
+
Since GridFS doesn't make the files available via HTTP, you'll need to stream
|
40
|
+
them yourself. In Rails for example, you could use the +send_data+ method. You
|
41
|
+
can tell CarrierWave the URL you will serve your images from, allowing it to
|
42
|
+
generate the correct URL, by setting eg:
|
43
|
+
|
44
|
+
CarrierWave.configure do |config|
|
45
|
+
config.grid_fs_access_url = "/image/show"
|
46
|
+
end
|
47
|
+
|
48
|
+
== Known issues/ limitations
|
49
|
+
|
50
|
+
If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
|
51
|
+
You must explicitly call save on embedded documents in order to save their attached files.
|
52
|
+
You can read more about this {here}[https://github.com/jnicklas/carrierwave/issues#issue/81]
|
53
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
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 = "carrierwave-mongoid"
|
7
|
+
s.version = Carrierwave::Mongoid::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jonas Nicklas", "Trevor Turk"]
|
10
|
+
s.email = ["jonas.nicklas@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/jnicklas/carrierwave-mongoid"
|
12
|
+
s.summary = %q{Mongoid support for CarrierWave}
|
13
|
+
s.description = %q{Mongoid support for CarrierWave}
|
14
|
+
|
15
|
+
s.rubyforge_project = "carrierwave-mongoid"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency "carrierwave"
|
23
|
+
s.add_dependency "mongoid"
|
24
|
+
s.add_development_dependency "rspec", ["~> 2.0"]
|
25
|
+
s.add_development_dependency "bson_ext", ["> 1.3.0"]
|
26
|
+
s.add_development_dependency "sqlite3"
|
27
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
require 'carrierwave/validations/active_model'
|
5
|
+
|
6
|
+
module CarrierWave
|
7
|
+
module Mongoid
|
8
|
+
include CarrierWave::Mount
|
9
|
+
##
|
10
|
+
# See +CarrierWave::Mount#mount_uploader+ for documentation
|
11
|
+
#
|
12
|
+
def mount_uploader(column, uploader=nil, options={}, &block)
|
13
|
+
options[:mount_on] ||= "#{column}_filename"
|
14
|
+
field options[:mount_on]
|
15
|
+
|
16
|
+
super
|
17
|
+
|
18
|
+
alias_method :read_uploader, :read_attribute
|
19
|
+
alias_method :write_uploader, :write_attribute
|
20
|
+
|
21
|
+
include CarrierWave::Validations::ActiveModel
|
22
|
+
|
23
|
+
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
|
24
|
+
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
|
25
|
+
|
26
|
+
after_save "store_#{column}!".to_sym
|
27
|
+
before_save "write_#{column}_identifier".to_sym
|
28
|
+
after_destroy "remove_#{column}!".to_sym
|
29
|
+
end
|
30
|
+
end # Mongoid
|
31
|
+
end # CarrierWave
|
32
|
+
|
33
|
+
Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'mongo'
|
3
|
+
|
4
|
+
module CarrierWave
|
5
|
+
module Storage
|
6
|
+
|
7
|
+
##
|
8
|
+
# The GridFS store uses MongoDB's GridStore file storage system to store files
|
9
|
+
#
|
10
|
+
# There are two ways of configuring the GridFS connection. Either you create a
|
11
|
+
# connection or you reuse an existing connection.
|
12
|
+
#
|
13
|
+
# Creating a connection looks something like this:
|
14
|
+
#
|
15
|
+
# CarrierWave.configure do |config|
|
16
|
+
# config.storage = :grid_fs
|
17
|
+
# config.grid_fs_host = "your-host.com"
|
18
|
+
# config.grid_fs_port = "27017"
|
19
|
+
# config.grid_fs_database = "your_dbs_app_name"
|
20
|
+
# config.grid_fs_username = "user"
|
21
|
+
# config.grid_fs_password = "verysecret"
|
22
|
+
# config.grid_fs_access_url = "/images"
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# In the above example your documents url will look like:
|
26
|
+
#
|
27
|
+
# http://your-app.com/images/:document-identifier-here
|
28
|
+
#
|
29
|
+
# When you already have a Mongo connection object (for example through Mongoid)
|
30
|
+
# you can also reuse this connection:
|
31
|
+
#
|
32
|
+
# CarrierWave.configure do |config|
|
33
|
+
# config.storage = :grid_fs
|
34
|
+
# config.grid_fs_connection = Mongoid.database
|
35
|
+
# config.grid_fs_access_url = "/images"
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
class GridFS < Abstract
|
39
|
+
|
40
|
+
class File
|
41
|
+
|
42
|
+
def initialize(uploader, path)
|
43
|
+
@path = path
|
44
|
+
@uploader = uploader
|
45
|
+
end
|
46
|
+
|
47
|
+
def path
|
48
|
+
@path
|
49
|
+
end
|
50
|
+
|
51
|
+
def url
|
52
|
+
unless @uploader.grid_fs_access_url
|
53
|
+
nil
|
54
|
+
else
|
55
|
+
[@uploader.grid_fs_access_url, @path].join("/")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def read
|
60
|
+
grid.open(@path, 'r').data
|
61
|
+
end
|
62
|
+
|
63
|
+
def write(file)
|
64
|
+
grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
|
65
|
+
f.write(file.read)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def delete
|
70
|
+
grid.delete(@path)
|
71
|
+
end
|
72
|
+
|
73
|
+
def content_type
|
74
|
+
grid.open(@path, 'r').content_type
|
75
|
+
end
|
76
|
+
|
77
|
+
def file_length
|
78
|
+
grid.open(@path, 'r').file_length
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
def database
|
84
|
+
@connection ||= @uploader.grid_fs_connection || begin
|
85
|
+
host = @uploader.grid_fs_host
|
86
|
+
port = @uploader.grid_fs_port
|
87
|
+
database = @uploader.grid_fs_database
|
88
|
+
username = @uploader.grid_fs_username
|
89
|
+
password = @uploader.grid_fs_password
|
90
|
+
db = Mongo::Connection.new(host, port).db(database)
|
91
|
+
db.authenticate(username, password) if username && password
|
92
|
+
db
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def grid
|
97
|
+
@grid ||= Mongo::GridFileSystem.new(database)
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Store the file in MongoDB's GridFS GridStore
|
104
|
+
#
|
105
|
+
# === Parameters
|
106
|
+
#
|
107
|
+
# [file (CarrierWave::SanitizedFile)] the file to store
|
108
|
+
#
|
109
|
+
# === Returns
|
110
|
+
#
|
111
|
+
# [CarrierWave::SanitizedFile] a sanitized file
|
112
|
+
#
|
113
|
+
def store!(file)
|
114
|
+
stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
|
115
|
+
stored.write(file)
|
116
|
+
stored
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Retrieve the file from MongoDB's GridFS GridStore
|
121
|
+
#
|
122
|
+
# === Parameters
|
123
|
+
#
|
124
|
+
# [identifier (String)] the filename of the file
|
125
|
+
#
|
126
|
+
# === Returns
|
127
|
+
#
|
128
|
+
# [CarrierWave::Storage::GridFS::File] a sanitized file
|
129
|
+
#
|
130
|
+
def retrieve!(identifier)
|
131
|
+
CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
|
132
|
+
end
|
133
|
+
|
134
|
+
end # File
|
135
|
+
end # Storage
|
136
|
+
end # CarrierWave
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
this is stuff
|
@@ -0,0 +1 @@
|
|
1
|
+
this is stuff
|
@@ -0,0 +1,287 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
connection = Mongo::Connection.new
|
5
|
+
Mongoid.database = connection.db("carrierwave_test")
|
6
|
+
|
7
|
+
|
8
|
+
def reset_mongo_class(uploader = MongoUploader)
|
9
|
+
class_name = 'MongoUser'
|
10
|
+
Object.send(:remove_const, class_name) rescue nil
|
11
|
+
klass = Object.const_set(class_name, Class.new)
|
12
|
+
klass.class_eval do
|
13
|
+
include Mongoid::Document
|
14
|
+
store_in :users
|
15
|
+
mount_uploader :image, uploader
|
16
|
+
end
|
17
|
+
klass
|
18
|
+
end
|
19
|
+
|
20
|
+
class MongoUploader < CarrierWave::Uploader::Base; end
|
21
|
+
|
22
|
+
class WhiteListUploader < CarrierWave::Uploader::Base
|
23
|
+
def extension_white_list
|
24
|
+
%w(txt)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ProcessingErrorUploader < CarrierWave::Uploader::Base
|
29
|
+
process :monkey
|
30
|
+
def monkey
|
31
|
+
raise CarrierWave::ProcessingError, "Ohh noez!"
|
32
|
+
end
|
33
|
+
def extension_white_list
|
34
|
+
%w(jpg)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
describe CarrierWave::Mongoid do
|
40
|
+
|
41
|
+
after do
|
42
|
+
MongoUser.collection.drop
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#image' do
|
46
|
+
|
47
|
+
context "when nothing is assigned" do
|
48
|
+
|
49
|
+
before do
|
50
|
+
mongo_user_klass = reset_mongo_class
|
51
|
+
@document = mongo_user_klass.new
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns a blank uploader" do
|
55
|
+
@document.image.should be_blank
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when an empty string is assigned" do
|
61
|
+
|
62
|
+
before do
|
63
|
+
mongo_user_klass = reset_mongo_class
|
64
|
+
@document = mongo_user_klass.new(:image_filename => "")
|
65
|
+
@document.save
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns a blank uploader" do
|
69
|
+
@saved_doc = MongoUser.first
|
70
|
+
@saved_doc.image.should be_blank
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
context "when a filename is saved in the database" do
|
76
|
+
|
77
|
+
before do
|
78
|
+
mongo_user_klass = reset_mongo_class
|
79
|
+
@document = mongo_user_klass.new(:image_filename => "test.jpg")
|
80
|
+
@document.save
|
81
|
+
@doc = MongoUser.first
|
82
|
+
end
|
83
|
+
|
84
|
+
it "returns an uploader" do
|
85
|
+
@doc.image.should be_an_instance_of(MongoUploader)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "sets the path to the store directory" do
|
89
|
+
@doc.image.current_path.should == public_path('uploads/test.jpg')
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#image=' do
|
97
|
+
|
98
|
+
before do
|
99
|
+
mongo_user_klass = reset_mongo_class
|
100
|
+
@doc = mongo_user_klass.new
|
101
|
+
end
|
102
|
+
|
103
|
+
context "when nil is assigned" do
|
104
|
+
|
105
|
+
it "does not set the value" do
|
106
|
+
@doc.image = nil
|
107
|
+
@doc.image.should be_blank
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
context "when an empty string is assigned" do
|
113
|
+
|
114
|
+
it "does not set the value" do
|
115
|
+
@doc.image = ''
|
116
|
+
@doc.image.should be_blank
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
context "when a file is assigned" do
|
122
|
+
|
123
|
+
it "should cache a file" do
|
124
|
+
@doc.image = stub_file('test.jpeg')
|
125
|
+
@doc.image.should be_an_instance_of(MongoUploader)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
|
129
|
+
@doc.image_filename.should be_nil
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should copy a file into into the cache directory" do
|
133
|
+
@doc.image = stub_file('test.jpeg')
|
134
|
+
@doc.image.current_path.should =~ /^#{public_path('uploads\/tmp')}/
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when validating integrity' do
|
140
|
+
before do
|
141
|
+
mongo_user_klass = reset_mongo_class(WhiteListUploader)
|
142
|
+
@doc = mongo_user_klass.new
|
143
|
+
@doc.image = stub_file('test.jpg')
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should make the document invalid when an integrity error occurs" do
|
147
|
+
@doc.should_not be_valid
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should use I18n for integrity error messages" do
|
151
|
+
@doc.valid?
|
152
|
+
@doc.errors[:image].should == ['is not an allowed file type']
|
153
|
+
|
154
|
+
change_locale_and_store_translations(:pt,
|
155
|
+
:errors => {
|
156
|
+
:messages => {
|
157
|
+
:carrierwave_integrity_error => 'tipo de imagem não permitido.'
|
158
|
+
}
|
159
|
+
}
|
160
|
+
) do
|
161
|
+
@doc.should_not be_valid
|
162
|
+
@doc.errors[:image].should == ['tipo de imagem não permitido.']
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'when validating processing' do
|
168
|
+
before do
|
169
|
+
mongo_user_klass = reset_mongo_class(ProcessingErrorUploader)
|
170
|
+
@doc = mongo_user_klass.new
|
171
|
+
@doc.image = stub_file('test.jpg')
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should make the document invalid when a processing error occurs" do
|
175
|
+
@doc.should_not be_valid
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should use I18n for processing error messages" do
|
179
|
+
@doc.valid?
|
180
|
+
@doc.errors[:image].should == ['failed to be processed']
|
181
|
+
|
182
|
+
change_locale_and_store_translations(:pt,
|
183
|
+
:errors => {
|
184
|
+
:messages => {
|
185
|
+
:carrierwave_processing_error => 'falha ao processar imagem.'
|
186
|
+
}
|
187
|
+
}
|
188
|
+
) do
|
189
|
+
@doc.should_not be_valid
|
190
|
+
@doc.errors[:image].should == ['falha ao processar imagem.']
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "#save" do
|
198
|
+
|
199
|
+
before do
|
200
|
+
mongo_user_klass = reset_mongo_class
|
201
|
+
@doc = mongo_user_klass.new
|
202
|
+
end
|
203
|
+
|
204
|
+
context "when no file is assigned" do
|
205
|
+
|
206
|
+
it "image is blank" do
|
207
|
+
@doc.save
|
208
|
+
@doc.image.should be_blank
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
context "when a file is assigned" do
|
214
|
+
|
215
|
+
it "copies the file to the upload directory" do
|
216
|
+
@doc.image = stub_file('test.jpg')
|
217
|
+
@doc.save
|
218
|
+
@doc.image.should be_an_instance_of(MongoUploader)
|
219
|
+
@doc.image.current_path.should == public_path('uploads/test.jpg')
|
220
|
+
end
|
221
|
+
|
222
|
+
it "saves the filename in the database" do
|
223
|
+
@doc.image = stub_file('test.jpg')
|
224
|
+
@doc.save
|
225
|
+
@doc.image_filename.should == 'test.jpg'
|
226
|
+
end
|
227
|
+
|
228
|
+
context "when remove_image? is true" do
|
229
|
+
|
230
|
+
it "removes the image" do
|
231
|
+
@doc.image = stub_file('test.jpeg')
|
232
|
+
@doc.save
|
233
|
+
@doc.remove_image = true
|
234
|
+
@doc.save
|
235
|
+
@doc.reload
|
236
|
+
@doc.image.should be_blank
|
237
|
+
@doc.image_filename.should == ''
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
|
246
|
+
describe '#destroy' do
|
247
|
+
|
248
|
+
before do
|
249
|
+
mongo_user_klass = reset_mongo_class
|
250
|
+
@doc = mongo_user_klass.new
|
251
|
+
end
|
252
|
+
|
253
|
+
describe "when file assigned" do
|
254
|
+
|
255
|
+
it "removes the file from the filesystem" do
|
256
|
+
@doc.image = stub_file('test.jpeg')
|
257
|
+
@doc.save.should be_true
|
258
|
+
File.exist?(public_path('uploads/test.jpeg')).should be_true
|
259
|
+
@doc.image.should be_an_instance_of(MongoUploader)
|
260
|
+
@doc.image.current_path.should == public_path('uploads/test.jpeg')
|
261
|
+
@doc.destroy
|
262
|
+
File.exist?(public_path('uploads/test.jpeg')).should be_false
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
describe "when file is not assigned" do
|
268
|
+
|
269
|
+
it "deletes the instance of MongoUser after save" do
|
270
|
+
@doc.save
|
271
|
+
MongoUser.count.should eql(1)
|
272
|
+
@doc.destroy
|
273
|
+
end
|
274
|
+
|
275
|
+
it "deletes the instance of MongoUser after save and then re-looking up the instance" do
|
276
|
+
@doc.save
|
277
|
+
MongoUser.count.should eql(1)
|
278
|
+
@doc = MongoUser.first
|
279
|
+
@doc.destroy
|
280
|
+
end
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
|
286
|
+
|
287
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rspec'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
require 'carrierwave'
|
7
|
+
require 'carrierwave/mongoid'
|
8
|
+
|
9
|
+
def file_path( *paths )
|
10
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
|
11
|
+
end
|
12
|
+
|
13
|
+
def public_path( *paths )
|
14
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
|
15
|
+
end
|
16
|
+
|
17
|
+
CarrierWave.root = public_path
|
18
|
+
|
19
|
+
module CarrierWave
|
20
|
+
module Test
|
21
|
+
module MockFiles
|
22
|
+
def stub_file(filename, mime_type=nil, fake_name=nil)
|
23
|
+
f = File.open(file_path(filename))
|
24
|
+
return f
|
25
|
+
end
|
26
|
+
|
27
|
+
def stub_tempfile(filename, mime_type=nil, fake_name=nil)
|
28
|
+
raise "#{path} file does not exist" unless File.exist?(file_path(filename))
|
29
|
+
|
30
|
+
t = Tempfile.new(filename)
|
31
|
+
FileUtils.copy_file(file_path(filename), t.path)
|
32
|
+
|
33
|
+
# This is stupid, but for some reason rspec won't play nice...
|
34
|
+
eval <<-EOF
|
35
|
+
def t.original_filename; '#{fake_name || filename}'; end
|
36
|
+
def t.content_type; '#{mime_type}'; end
|
37
|
+
def t.local_path; path; end
|
38
|
+
EOF
|
39
|
+
|
40
|
+
return t
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module I18nHelpers
|
45
|
+
def change_locale_and_store_translations(locale, translations, &block)
|
46
|
+
current_locale = I18n.locale
|
47
|
+
begin
|
48
|
+
I18n.backend.store_translations locale, translations
|
49
|
+
I18n.locale = locale
|
50
|
+
yield
|
51
|
+
ensure
|
52
|
+
I18n.reload!
|
53
|
+
I18n.locale = current_locale
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
RSpec.configure do |config|
|
61
|
+
config.include CarrierWave::Test::MockFiles
|
62
|
+
config.include CarrierWave::Test::I18nHelpers
|
63
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'mongo'
|
5
|
+
|
6
|
+
shared_examples_for "a GridFS connection" do
|
7
|
+
describe '#store!' do
|
8
|
+
before do
|
9
|
+
@uploader.stub!(:store_path).and_return('uploads/bar.txt')
|
10
|
+
@grid_fs_file = @storage.store!(@file)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should upload the file to gridfs" do
|
14
|
+
@grid.open('uploads/bar.txt', 'r').data.should == 'this is stuff'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should have the same path that it was stored as" do
|
18
|
+
@grid_fs_file.path.should == 'uploads/bar.txt'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should read the contents of the file" do
|
22
|
+
@grid_fs_file.read.should == "this is stuff"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not have a URL" do
|
26
|
+
@grid_fs_file.url.should be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be deletable" do
|
30
|
+
@grid_fs_file.delete
|
31
|
+
lambda {@grid.open('uploads/bar.txt', 'r')}.should raise_error(Mongo::GridFileNotFound)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should store the content type on GridFS" do
|
35
|
+
@grid_fs_file.content_type.should == 'application/xml'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should have a file length" do
|
39
|
+
@grid_fs_file.file_length.should == 13
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#retrieve!' do
|
45
|
+
before do
|
46
|
+
@grid.open('uploads/bar.txt', 'w') { |f| f.write "A test, 1234" }
|
47
|
+
@uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
|
48
|
+
@grid_fs_file = @storage.retrieve!('bar.txt')
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should retrieve the file contents from gridfs" do
|
52
|
+
@grid_fs_file.read.chomp.should == "A test, 1234"
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should have the same path that it was stored as" do
|
56
|
+
@grid_fs_file.path.should == 'uploads/bar.txt'
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should not have a URL unless set" do
|
60
|
+
@grid_fs_file.url.should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return a URL if configured" do
|
64
|
+
@uploader.stub!(:grid_fs_access_url).and_return("/image/show")
|
65
|
+
@grid_fs_file.url.should == "/image/show/uploads/bar.txt"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be deletable" do
|
69
|
+
@grid_fs_file.delete
|
70
|
+
lambda {@grid.open('uploads/bar.txt', 'r')}.should raise_error(Mongo::GridFileNotFound)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
describe CarrierWave::Storage::GridFS do
|
77
|
+
|
78
|
+
before do
|
79
|
+
@database = Mongo::Connection.new('localhost', 27017).db('carrierwave_test')
|
80
|
+
|
81
|
+
@uploader = mock('an uploader')
|
82
|
+
@uploader.stub!(:grid_fs_access_url).and_return(nil)
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when reusing an existing connection manually" do
|
86
|
+
before do
|
87
|
+
@uploader.stub!(:grid_fs_connection).and_return(@database)
|
88
|
+
|
89
|
+
@grid = Mongo::GridFileSystem.new(@database)
|
90
|
+
|
91
|
+
@storage = CarrierWave::Storage::GridFS.new(@uploader)
|
92
|
+
@file = stub_tempfile('test.jpg', 'application/xml')
|
93
|
+
end
|
94
|
+
|
95
|
+
it_should_behave_like "a GridFS connection"
|
96
|
+
|
97
|
+
# Calling #recreate_versions! on uploaders has been known to fail on
|
98
|
+
# remotely hosted files. This is due to a variety of issues, but this test
|
99
|
+
# makes sure that there's no unnecessary errors during the process
|
100
|
+
describe "#recreate_versions!" do
|
101
|
+
before do
|
102
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
103
|
+
@uploader_class.class_eval do
|
104
|
+
storage :grid_fs
|
105
|
+
end
|
106
|
+
|
107
|
+
@versioned = @uploader_class.new
|
108
|
+
@versioned.stub!(:grid_fs_connection).and_return(@database)
|
109
|
+
|
110
|
+
@versioned.store! File.open(file_path('portrait.jpg'))
|
111
|
+
end
|
112
|
+
|
113
|
+
after do
|
114
|
+
FileUtils.rm_rf(public_path)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "recreates versions stored remotely without error" do
|
118
|
+
lambda {
|
119
|
+
@versioned.recreate_versions!
|
120
|
+
}.should_not raise_error
|
121
|
+
|
122
|
+
@versioned.should be_present
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "when setting a connection manually" do
|
128
|
+
before do
|
129
|
+
@uploader.stub!(:grid_fs_database).and_return("carrierwave_test")
|
130
|
+
@uploader.stub!(:grid_fs_host).and_return("localhost")
|
131
|
+
@uploader.stub!(:grid_fs_port).and_return(27017)
|
132
|
+
@uploader.stub!(:grid_fs_username).and_return(nil)
|
133
|
+
@uploader.stub!(:grid_fs_password).and_return(nil)
|
134
|
+
@uploader.stub!(:grid_fs_connection).and_return(nil)
|
135
|
+
|
136
|
+
@grid = Mongo::GridFileSystem.new(@database)
|
137
|
+
|
138
|
+
@storage = CarrierWave::Storage::GridFS.new(@uploader)
|
139
|
+
@file = stub_tempfile('test.jpg', 'application/xml')
|
140
|
+
end
|
141
|
+
|
142
|
+
it_should_behave_like "a GridFS connection"
|
143
|
+
end
|
144
|
+
|
145
|
+
after do
|
146
|
+
@grid.delete('uploads/bar.txt')
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jonas Nicklas
|
13
|
+
- Trevor Turk
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-08-12 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: carrierwave
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: mongoid
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
prerelease: false
|
50
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 2
|
57
|
+
- 0
|
58
|
+
version: "2.0"
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id003
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: bson_ext
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 1
|
71
|
+
- 3
|
72
|
+
- 0
|
73
|
+
version: 1.3.0
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: sqlite3
|
78
|
+
prerelease: false
|
79
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
type: :development
|
88
|
+
version_requirements: *id005
|
89
|
+
description: Mongoid support for CarrierWave
|
90
|
+
email:
|
91
|
+
- jonas.nicklas@gmail.com
|
92
|
+
executables: []
|
93
|
+
|
94
|
+
extensions: []
|
95
|
+
|
96
|
+
extra_rdoc_files: []
|
97
|
+
|
98
|
+
files:
|
99
|
+
- .gitignore
|
100
|
+
- Gemfile
|
101
|
+
- README.rdoc
|
102
|
+
- Rakefile
|
103
|
+
- carrierwave-mongoid.gemspec
|
104
|
+
- lib/carrierwave/mongoid.rb
|
105
|
+
- lib/carrierwave/mongoid/version.rb
|
106
|
+
- lib/carrierwave/storage/grid_fs.rb
|
107
|
+
- spec/fixtures/portrait.jpg
|
108
|
+
- spec/fixtures/test.jpeg
|
109
|
+
- spec/fixtures/test.jpg
|
110
|
+
- spec/mongoid_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/storage/grid_fs_spec.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: https://github.com/jnicklas/carrierwave-mongoid
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
version: "0"
|
138
|
+
requirements: []
|
139
|
+
|
140
|
+
rubyforge_project: carrierwave-mongoid
|
141
|
+
rubygems_version: 1.3.7
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: Mongoid support for CarrierWave
|
145
|
+
test_files:
|
146
|
+
- spec/fixtures/portrait.jpg
|
147
|
+
- spec/fixtures/test.jpeg
|
148
|
+
- spec/fixtures/test.jpg
|
149
|
+
- spec/mongoid_spec.rb
|
150
|
+
- spec/spec_helper.rb
|
151
|
+
- spec/storage/grid_fs_spec.rb
|