glebtv-carrierwave-mongoid 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.ruby-gemset +1 -0
- data/.travis.yml +18 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +5 -0
- data/Rakefile +12 -0
- data/gemfiles/carrierwave-master.gemfile +5 -0
- data/gemfiles/mongoid-3.0.gemfile +5 -0
- data/gemfiles/mongoid-3.1.gemfile +5 -0
- data/gemfiles/mongoid-4.0.gemfile +6 -0
- data/glebtv-carrierwave-mongoid.gemspec +27 -0
- data/lib/carrierwave/mongoid.rb +101 -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 +834 -0
- data/spec/spec_helper.rb +74 -0
- data/spec/storage/grid_fs_spec.rb +184 -0
- metadata +173 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rspec'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'stringio'
|
6
|
+
|
7
|
+
require 'carrierwave'
|
8
|
+
require 'carrierwave/mongoid'
|
9
|
+
|
10
|
+
Mongoid.configure do |config|
|
11
|
+
config.connect_to('carrierwave_test')
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_path( *paths )
|
15
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
|
16
|
+
end
|
17
|
+
|
18
|
+
def public_path( *paths )
|
19
|
+
File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
|
20
|
+
end
|
21
|
+
|
22
|
+
CarrierWave.root = public_path
|
23
|
+
|
24
|
+
module CarrierWave
|
25
|
+
module Test
|
26
|
+
module MockFiles
|
27
|
+
def stub_file(filename, mime_type=nil, fake_name=nil)
|
28
|
+
f = File.open(file_path(filename))
|
29
|
+
return f
|
30
|
+
end
|
31
|
+
|
32
|
+
def stub_tempfile(filename, mime_type=nil, fake_name=nil)
|
33
|
+
raise "#{path} file does not exist" unless File.exist?(file_path(filename))
|
34
|
+
|
35
|
+
t = Tempfile.new(filename)
|
36
|
+
FileUtils.copy_file(file_path(filename), t.path)
|
37
|
+
|
38
|
+
t.stub!(:local_path => "",
|
39
|
+
:original_filename => filename || fake_name,
|
40
|
+
:content_type => mime_type)
|
41
|
+
|
42
|
+
return t
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module I18nHelpers
|
47
|
+
def change_locale_and_store_translations(locale, translations, &block)
|
48
|
+
current_locale = I18n.locale
|
49
|
+
begin
|
50
|
+
I18n.backend.store_translations locale, translations
|
51
|
+
I18n.locale = locale
|
52
|
+
yield
|
53
|
+
ensure
|
54
|
+
I18n.reload!
|
55
|
+
I18n.locale = current_locale
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class SIO < StringIO
|
63
|
+
attr_accessor :filename
|
64
|
+
|
65
|
+
def initialize(filename, *args, &block)
|
66
|
+
@filename = filename
|
67
|
+
super(*args, &block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
RSpec.configure do |config|
|
72
|
+
config.include CarrierWave::Test::MockFiles
|
73
|
+
config.include CarrierWave::Test::I18nHelpers
|
74
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
|
7
|
+
shared_examples_for "a GridFS connection" do
|
8
|
+
describe '#store!' do
|
9
|
+
before do
|
10
|
+
@uploader.stub!(:store_path).and_return('uploads/bar.txt')
|
11
|
+
@grid_fs_file = @storage.store!(@file)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should upload the file to gridfs" do
|
15
|
+
@grid['uploads/bar.txt'].data.should == 'this is stuff'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should upload the file to gridfs" do
|
19
|
+
@grid['uploads/bar.txt'].data.should == 'this is stuff'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should have the same path that it was stored as" do
|
23
|
+
@grid_fs_file.path.should == 'uploads/bar.txt'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should read the contents of the file" do
|
27
|
+
@grid_fs_file.read.should == "this is stuff"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not have a URL" do
|
31
|
+
@grid_fs_file.url.should be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be deletable" do
|
35
|
+
@grid_fs_file.delete
|
36
|
+
@grid['uploads/bar.txt'].should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should store the content type on GridFS" do
|
40
|
+
@grid_fs_file.content_type.should == 'text/plain'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should have a file length" do
|
44
|
+
@grid_fs_file.file_length.should == 13
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#retrieve!' do
|
49
|
+
before do
|
50
|
+
@grid.clear
|
51
|
+
@grid['uploads/bar.txt'] = StringIO.new('A test, 1234')
|
52
|
+
@uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
|
53
|
+
@grid_fs_file = @storage.retrieve!('bar.txt')
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should retrieve the file contents from gridfs" do
|
57
|
+
@grid_fs_file.read.chomp.should == "A test, 1234"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should have the same path that it was stored as" do
|
61
|
+
@grid_fs_file.path.should == 'uploads/bar.txt'
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should not have a URL unless access_url is set" do
|
65
|
+
@grid_fs_file.url.should be_nil
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return a relative URL path if access_url is set to the root path" do
|
69
|
+
@uploader.stub!(:grid_fs_access_url).and_return("/")
|
70
|
+
@grid_fs_file.url.should == "/uploads/bar.txt"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return a URL path if access_url is set to a file path" do
|
74
|
+
@uploader.stub!(:grid_fs_access_url).and_return("/image/show")
|
75
|
+
@grid_fs_file.url.should == "/image/show/uploads/bar.txt"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return an absolute URL if access_url is set to an absolute URL" do
|
79
|
+
@uploader.stub!(:grid_fs_access_url).and_return("http://example.com/images/")
|
80
|
+
@grid_fs_file.url.should == "http://example.com/images/uploads/bar.txt"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should be deletable" do
|
84
|
+
@grid_fs_file.delete
|
85
|
+
@grid['uploads/bar.txt'].should be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#retrieve! on a store_dir with leading slash' do
|
90
|
+
before do
|
91
|
+
@uploader.stub!(:store_path).with('bar.txt').and_return('/uploads/bar.txt')
|
92
|
+
@grid_fs_file = @storage.retrieve!('bar.txt')
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should return a relative URL path if access_url is set to the root path" do
|
96
|
+
@uploader.stub!(:grid_fs_access_url).and_return("/")
|
97
|
+
@grid_fs_file.url.should == "/uploads/bar.txt"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
if defined?(Mongoid::GridFs)
|
104
|
+
describe CarrierWave::Storage::GridFS do
|
105
|
+
|
106
|
+
before do
|
107
|
+
@uploader = mock('an uploader')
|
108
|
+
@uploader.stub!(:grid_fs_access_url).and_return(nil)
|
109
|
+
end
|
110
|
+
|
111
|
+
context "when reusing an existing connection manually" do
|
112
|
+
before do
|
113
|
+
@uploader.stub!(:grid_fs_connection).and_return(@database)
|
114
|
+
|
115
|
+
@grid = ::Mongoid::GridFs
|
116
|
+
|
117
|
+
@storage = CarrierWave::Storage::GridFS.new(@uploader)
|
118
|
+
@file = stub_tempfile('test.jpg', 'application/xml')
|
119
|
+
end
|
120
|
+
|
121
|
+
it_should_behave_like "a GridFS connection"
|
122
|
+
|
123
|
+
# Calling #recreate_versions! on uploaders has been known to fail on
|
124
|
+
# remotely hosted files. This is due to a variety of issues, but this test
|
125
|
+
# makes sure that there's no unnecessary errors during the process
|
126
|
+
describe "#recreate_versions!" do
|
127
|
+
before do
|
128
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
129
|
+
@uploader_class.class_eval{
|
130
|
+
include CarrierWave::MiniMagick
|
131
|
+
storage :grid_fs
|
132
|
+
|
133
|
+
process :resize_to_fit => [10, 10]
|
134
|
+
}
|
135
|
+
|
136
|
+
@versioned = @uploader_class.new
|
137
|
+
|
138
|
+
@versioned.store! File.open(file_path('portrait.jpg'))
|
139
|
+
end
|
140
|
+
|
141
|
+
after do
|
142
|
+
FileUtils.rm_rf(public_path)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "recreates versions stored remotely without error" do
|
146
|
+
lambda {
|
147
|
+
@versioned.recreate_versions!
|
148
|
+
}.should_not raise_error
|
149
|
+
|
150
|
+
@versioned.should be_present
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "resize_to_fill" do
|
155
|
+
before do
|
156
|
+
@uploader_class = Class.new(CarrierWave::Uploader::Base)
|
157
|
+
@uploader_class.class_eval{
|
158
|
+
include CarrierWave::MiniMagick
|
159
|
+
storage :grid_fs
|
160
|
+
}
|
161
|
+
|
162
|
+
@versioned = @uploader_class.new
|
163
|
+
|
164
|
+
@versioned.store! File.open(file_path('portrait.jpg'))
|
165
|
+
end
|
166
|
+
|
167
|
+
after do
|
168
|
+
FileUtils.rm_rf(public_path)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "resizes the file with out error" do
|
172
|
+
lambda {
|
173
|
+
@versioned.resize_to_fill(200, 200)
|
174
|
+
}.should_not raise_error
|
175
|
+
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
after do
|
181
|
+
@grid.clear
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
metadata
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glebtv-carrierwave-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- GlebTv
|
8
|
+
- Jonas Nicklas
|
9
|
+
- Trevor Turk
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-06-14 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: carrierwave
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 0.8.0
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: mongoid
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '3.0'
|
36
|
+
- - <
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '5.0'
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.0'
|
46
|
+
- - <
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.0'
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
requirement: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '2.6'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '2.6'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rake
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '10.0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '10.0'
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: mini_magick
|
79
|
+
requirement: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
type: :development
|
85
|
+
prerelease: false
|
86
|
+
version_requirements: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: pry
|
93
|
+
requirement: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
type: :development
|
99
|
+
prerelease: false
|
100
|
+
version_requirements: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
description: This fork makes GridFS optional
|
106
|
+
email:
|
107
|
+
- jonas.nicklas@gmail.com
|
108
|
+
executables: []
|
109
|
+
extensions: []
|
110
|
+
extra_rdoc_files: []
|
111
|
+
files:
|
112
|
+
- .gitignore
|
113
|
+
- .ruby-gemset
|
114
|
+
- .travis.yml
|
115
|
+
- Gemfile
|
116
|
+
- LICENSE
|
117
|
+
- README.md
|
118
|
+
- Rakefile
|
119
|
+
- gemfiles/carrierwave-master.gemfile
|
120
|
+
- gemfiles/mongoid-3.0.gemfile
|
121
|
+
- gemfiles/mongoid-3.1.gemfile
|
122
|
+
- gemfiles/mongoid-4.0.gemfile
|
123
|
+
- glebtv-carrierwave-mongoid.gemspec
|
124
|
+
- lib/carrierwave/mongoid.rb
|
125
|
+
- lib/carrierwave/mongoid/version.rb
|
126
|
+
- lib/carrierwave/storage/grid_fs.rb
|
127
|
+
- lib/glebtv-carrierwave-mongoid.rb
|
128
|
+
- log/.gitkeep
|
129
|
+
- spec/fixtures/new.jpeg
|
130
|
+
- spec/fixtures/new.txt
|
131
|
+
- spec/fixtures/old.jpeg
|
132
|
+
- spec/fixtures/old.txt
|
133
|
+
- spec/fixtures/portrait.jpg
|
134
|
+
- spec/fixtures/test.jpeg
|
135
|
+
- spec/fixtures/test.jpg
|
136
|
+
- spec/mongoid_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
- spec/storage/grid_fs_spec.rb
|
139
|
+
homepage: https://github.com/crowdtask/carrierwave-mongoid
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
metadata: {}
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - '>='
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - '>='
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 2.0.3
|
160
|
+
signing_key:
|
161
|
+
specification_version: 4
|
162
|
+
summary: This fork makes GridFS optional
|
163
|
+
test_files:
|
164
|
+
- spec/fixtures/new.jpeg
|
165
|
+
- spec/fixtures/new.txt
|
166
|
+
- spec/fixtures/old.jpeg
|
167
|
+
- spec/fixtures/old.txt
|
168
|
+
- spec/fixtures/portrait.jpg
|
169
|
+
- spec/fixtures/test.jpeg
|
170
|
+
- spec/fixtures/test.jpg
|
171
|
+
- spec/mongoid_spec.rb
|
172
|
+
- spec/spec_helper.rb
|
173
|
+
- spec/storage/grid_fs_spec.rb
|