populate-me 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5046f2abc8d311d2dedd3c31fac2ac90f6ae0f36
4
- data.tar.gz: 2df84aed8c576a8a2908977fde658f2ad85ae2e7
3
+ metadata.gz: 95deb6a0aa3778df73490932b2db67e8bb9fddc1
4
+ data.tar.gz: e441c164c3e24d703ec2acd0567a8f3715393915
5
5
  SHA512:
6
- metadata.gz: 0f4d57c7dfc72de5c4dc08e027c498e7cee4bd4de3fa2b3cc5facb74c28aaa006e8270d5f79baa1fc0af2b48aec0b297bb0dd9ecc211a28018d37b1ca415c109
7
- data.tar.gz: 71dd5a2221caa8d1e9d3761c2651e2542f1182ea30f00d50f4b0bb9d5e010d1d8f46df5695b941544aac4af5f83a306cd27243ba03e28a6a2cb4018619446081
6
+ metadata.gz: 80488dcf9765e65444649d32f3c92a03d49258cef876a41a97377b65c42f1aa5a6078acedcae69459c031963efe3994e4737d7b44ac49b4ea707c56a9e3493c1
7
+ data.tar.gz: eb032cdedacdc1dcdcf7d04d7a0e8572d4eea30f9e918c55767c7ae0fe148310fb26730642e76b5e3f24efc7db083bbfd7cfdd19a694ed3c4177d050461a1430
@@ -13,7 +13,7 @@ module PopulateMe
13
13
  end
14
14
 
15
15
  def attachment f
16
- attacher = Utils.resolve_class_name self.class.fields[f][:class_name]
16
+ attacher = WebUtils.resolve_class_name self.class.fields[f][:class_name]
17
17
  attacher.new self, f
18
18
  end
19
19
 
@@ -1,6 +1,6 @@
1
1
  require 'populate_me/attachment'
2
2
  require 'mongo'
3
- require 'rack/gridfs'
3
+ require 'rack/grid_serve'
4
4
 
5
5
  module PopulateMe
6
6
 
@@ -10,14 +10,6 @@ module PopulateMe
10
10
 
11
11
  # set :url_prefix, '/attachment'
12
12
 
13
- def grid
14
- self.class.grid
15
- end
16
-
17
- def gridfs
18
- self.class.gridfs
19
- end
20
-
21
13
  # Attachee_prefix is moved on field_value for gridfs
22
14
  def url variation_name=:original
23
15
  return nil if WebUtils.blank?(self.field_filename(variation_name))
@@ -31,16 +23,6 @@ module PopulateMe
31
23
  )
32
24
  end
33
25
 
34
- def deletable? variation_name=:original
35
- !WebUtils.blank? self.field_filename(variation_name)
36
- # Fine since deleting a non-existent file does not raise an error in mongo
37
- end
38
-
39
- def perform_delete variation_name=:original
40
- # gridfs works with names instead of IDs
41
- gridfs.delete self.field_filename(variation_name)
42
- end
43
-
44
26
  def next_available_filename filename
45
27
  ext = File.extname(filename)
46
28
  base = "#{attachee_prefix}/#{File.basename(filename,ext)}"
@@ -48,7 +30,7 @@ module PopulateMe
48
30
  loop do
49
31
  suffix = i==0 ? '' : "-#{i}"
50
32
  potential_filename = [base,suffix,ext].join
51
- if grid.exist?(filename: potential_filename)
33
+ if settings.db.fs.find(filename: potential_filename).count>0
52
34
  i += 1
53
35
  else
54
36
  filename = potential_filename
@@ -68,9 +50,9 @@ module PopulateMe
68
50
  file = File.open(hash[:variation_path])
69
51
  type = Rack::Mime.mime_type ".#{hash[:variation].ext}"
70
52
  end
71
- attachment_id = grid.put(
53
+ settings.db.fs.upload_from_stream(
54
+ fn,
72
55
  file, {
73
- filename: fn,
74
56
  content_type: type,
75
57
  metadata: {
76
58
  parent_collection: (self.document.class.respond_to?(:collection) ? self.document.class.collection.name : self.attachee_prefix),
@@ -81,30 +63,24 @@ module PopulateMe
81
63
  fn
82
64
  end
83
65
 
66
+ def deletable? variation_name=:original
67
+ !WebUtils.blank? self.field_filename(variation_name)
68
+ # Fine since deleting a non-existent file does not raise an error in mongo
69
+ end
70
+
71
+ def perform_delete variation_name=:original
72
+ gridfile = settings.db.fs.find(filename: self.field_filename(variation_name)).first
73
+ settings.db.fs.delete(gridfile['_id']) unless gridfile.nil?
74
+ end
75
+
84
76
  class << self
85
77
 
86
78
  def ensure_db
87
79
  raise MissingMongoDBError, "Attachment class #{self.name} does not have a Mongo database." if settings.db.nil?
88
80
  end
89
81
 
90
- def grid
91
- ensure_db
92
- unless settings.grid.is_a?(::Mongo::Grid)
93
- set :grid, ::Mongo::Grid.new(settings.db)
94
- end
95
- settings.grid
96
- end
97
-
98
- def gridfs
99
- ensure_db
100
- unless settings.gridfs.is_a?(::Mongo::GridFileSystem)
101
- set :gridfs, ::Mongo::GridFileSystem.new(settings.db)
102
- end
103
- settings.gridfs
104
- end
105
-
106
82
  def middleware
107
- Rack::GridFS
83
+ Rack::GridServe
108
84
  end
109
85
 
110
86
  def middleware_options
@@ -1,4 +1,4 @@
1
1
  module PopulateMe
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
4
4
 
data/populate-me.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ['lib']
19
19
 
20
20
  s.add_dependency 'web-utils', '~> 0'
21
- s.add_dependency 'sinatra', '~> 1.0'
21
+ s.add_dependency 'sinatra', '~> 1.4'
22
22
  s.add_dependency 'json', '~> 2.1'
23
23
 
24
24
  s.add_development_dependency 'bundler', '~> 1.13'
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency 'rack-test', '~> 0.6'
27
27
  s.add_development_dependency 'rack-cerberus', '~> 1.0'
28
28
  s.add_development_dependency 'mongo', '~> 2.0'
29
+ s.add_development_dependency 'rack-grid-serve', '~> 0.0.5'
29
30
  s.add_development_dependency 'racksh', '~> 1.0'
30
31
  s.add_development_dependency 'rake', '~> 10.1'
31
32
  end
@@ -0,0 +1,237 @@
1
+ require 'helper'
2
+ require 'populate_me/mongo'
3
+ require 'populate_me/grid_fs_attachment'
4
+
5
+ GRIDMONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'populate-me-grid-test')
6
+ GRIDDB = GRIDMONGO.database
7
+ GRIDDB.drop
8
+ PopulateMe::Mongo.set :db, GRIDDB
9
+ PopulateMe::Mongo.set :default_attachment_class, PopulateMe::GridFS
10
+ PopulateMe::GridFS.set :db, GRIDDB
11
+
12
+ describe 'PopulateMe::GridFS' do
13
+ # parallelize_me!
14
+
15
+ class GridBook < PopulateMe::Mongo
16
+ field :cover, type: :attachment, variations: [
17
+ PopulateMe::Variation.new_image_magick_job(:thumb, :gif, "-resize '300x'")
18
+ ]
19
+ field :content, type: :attachment, variations: [
20
+ PopulateMe::Variation.new(:upcase, :txt, lambda{ |src,dst|
21
+ Kernel.system "cat \"#{src}\" | tr 'a-z' 'A-Z' > \"#{dst}\""
22
+ })
23
+ ]
24
+ end
25
+
26
+ before do
27
+ GRIDDB.drop
28
+ end
29
+
30
+ # Utils
31
+
32
+ it 'Returns URL with attachee_prefix' do
33
+ book = GridBook.new cover: "candy.jpg"
34
+ assert_equal '/attachment/candy.jpg', book.attachment(:cover).url
35
+ assert_equal '/attachment/candy.thumb.gif', book.attachment(:cover).url(:thumb)
36
+ end
37
+
38
+ it 'Has nil URL when field is blank' do
39
+ book = GridBook.new
40
+ assert_nil book.attachment(:cover).url
41
+ end
42
+
43
+ it 'Has location root without attachee prefix' do
44
+ book = GridBook.new
45
+ refute_match book.attachment(:cover).attachee_prefix, book.attachment(:cover).location_root
46
+ end
47
+
48
+ # Create
49
+
50
+ it "Saves attachments on create with variations" do
51
+ book = GridBook.new
52
+
53
+ file = Tempfile.new('foo')
54
+ file.write('hello')
55
+ file.rewind
56
+
57
+ assert_equal 0, GRIDDB['fs.files'].count
58
+ field_value = book.attachment(:content).create({
59
+ tempfile: file,
60
+ filename: 'story.txt',
61
+ type: 'text/plain'
62
+ })
63
+ assert_equal 2, GRIDDB['fs.files'].count
64
+ assert_equal 'grid-book/story.txt', field_value
65
+
66
+ gridfile = GRIDDB.fs.find(filename: 'grid-book/story.txt').first
67
+ assert_equal 'text/plain', gridfile['contentType']
68
+ assert_equal 'grid-book', gridfile['metadata']['parent_collection']
69
+ GRIDDB.fs.open_download_stream(gridfile['_id']) do |stream|
70
+ assert_equal 'hello', stream.read
71
+ end
72
+
73
+ vargridfile = GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
74
+ assert_equal 'text/plain', vargridfile['contentType']
75
+ assert_equal 'grid-book', vargridfile['metadata']['parent_collection']
76
+ GRIDDB.fs.open_download_stream(vargridfile['_id']) do |stream|
77
+ assert_equal 'HELLO', stream.read
78
+ end
79
+
80
+ file.close
81
+ file.unlink
82
+ end
83
+
84
+ it 'Does not create 2 files with the same name' do
85
+ file = Tempfile.new('foo')
86
+
87
+ book = GridBook.new
88
+
89
+ field_value = book.attachment(:content).create({
90
+ tempfile: file,
91
+ filename: 'story.txt',
92
+ type: 'text/plain'
93
+ })
94
+
95
+ assert_equal 'grid-book/story.txt', field_value
96
+
97
+ field_value = book.attachment(:content).create({
98
+ tempfile: file,
99
+ filename: 'story.txt',
100
+ type: 'text/plain'
101
+ })
102
+
103
+ assert_equal 'grid-book/story-1.txt', field_value
104
+
105
+ field_value = book.attachment(:content).create({
106
+ tempfile: file,
107
+ filename: 'story.txt',
108
+ type: 'text/plain'
109
+ })
110
+
111
+ assert_equal 'grid-book/story-2.txt', field_value
112
+
113
+ file.close
114
+ file.unlink
115
+ end
116
+
117
+ # Delete
118
+
119
+ it 'Is deletable when field is not blank' do
120
+ book = GridBook.new cover: "candy.jpg"
121
+ assert book.attachment(:cover).deletable?
122
+ end
123
+
124
+ it 'Is not deletable when field is blank' do
125
+ book = GridBook.new
126
+ refute book.attachment(:cover).deletable?
127
+ end
128
+
129
+ it 'Deletes all attachments' do
130
+ file = Tempfile.new('foo')
131
+
132
+ book = GridBook.new
133
+
134
+ field_value = book.attachment(:content).create({
135
+ tempfile: file,
136
+ filename: 'story.txt',
137
+ type: 'text/plain'
138
+ })
139
+ book.content = field_value
140
+
141
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.txt').first
142
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
143
+
144
+ book.attachment(:content).delete_all
145
+
146
+ assert_nil GRIDDB.fs.find(filename: 'grid-book/story.txt').first
147
+ assert_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
148
+
149
+ file.close
150
+ file.unlink
151
+ end
152
+
153
+ it 'Deletes one attachment at a time' do
154
+ file = Tempfile.new('foo')
155
+
156
+ book = GridBook.new
157
+
158
+ field_value = book.attachment(:content).create({
159
+ tempfile: file,
160
+ filename: 'story.txt',
161
+ type: 'text/plain'
162
+ })
163
+ book.content = field_value
164
+
165
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.txt').first
166
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
167
+
168
+ book.attachment(:content).delete
169
+
170
+ assert_nil GRIDDB.fs.find(filename: 'grid-book/story.txt').first
171
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
172
+
173
+ book.attachment(:content).delete :upcase
174
+
175
+ assert_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
176
+
177
+ file.close
178
+ file.unlink
179
+ end
180
+
181
+ # Update
182
+
183
+ it 'Deletes previous attachment when saving a new one' do
184
+ file = Tempfile.new('foo')
185
+ file.write('hello')
186
+ file.rewind
187
+
188
+ book = GridBook.new
189
+
190
+ field_value = book.attachment(:content).create({
191
+ tempfile: file,
192
+ filename: 'story.txt',
193
+ type: 'text/plain'
194
+ })
195
+ book.content = field_value
196
+
197
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.txt').first
198
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
199
+
200
+ file.rewind
201
+ file.write('world')
202
+ file.rewind
203
+
204
+ field_value = book.attachment(:content).create({
205
+ tempfile: file,
206
+ filename: 'history.md',
207
+ type: 'text/markdown'
208
+ })
209
+ book.content = field_value
210
+
211
+ assert_nil GRIDDB.fs.find(filename: 'grid-book/story.txt').first
212
+ assert_nil GRIDDB.fs.find(filename: 'grid-book/story.upcase.txt').first
213
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/history.md').first
214
+ refute_nil GRIDDB.fs.find(filename: 'grid-book/history.upcase.txt').first
215
+
216
+ gridfile = GRIDDB.fs.find(filename: 'grid-book/history.md').first
217
+ assert_equal 'text/markdown', gridfile['contentType']
218
+ assert_equal 'grid-book', gridfile['metadata']['parent_collection']
219
+ GRIDDB.fs.open_download_stream(gridfile['_id']) do |stream|
220
+ assert_equal 'world', stream.read
221
+ end
222
+
223
+ gridfile = GRIDDB.fs.find(filename: 'grid-book/history.upcase.txt').first
224
+ assert_equal 'text/plain', gridfile['contentType']
225
+ assert_equal 'grid-book', gridfile['metadata']['parent_collection']
226
+ GRIDDB.fs.open_download_stream(gridfile['_id']) do |stream|
227
+ assert_equal 'WORLD', stream.read
228
+ end
229
+
230
+ file.close
231
+ file.unlink
232
+ end
233
+
234
+ end
235
+
236
+ GRIDDB.drop
237
+
data/test/test_mongo.rb CHANGED
@@ -8,7 +8,6 @@ DB.drop
8
8
  OTHER_MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'populate-me-test-other')
9
9
  OTHER_DB = OTHER_MONGO.database
10
10
  OTHER_DB.drop
11
- class NoMongoDB < PopulateMe::Mongo; end
12
11
  PopulateMe::Mongo.set :db, DB
13
12
 
14
13
 
@@ -44,6 +43,8 @@ describe 'PopulateMe::Mongo' do
44
43
 
45
44
  describe 'Database connection' do
46
45
 
46
+ class NoMongoDB < PopulateMe::Mongo; set :db, nil; end
47
+
47
48
  it 'Should raise if db is not set' do
48
49
  assert_raises(PopulateMe::MissingMongoDBError) do
49
50
  NoMongoDB.collection
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: populate-me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mickael Riga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-04 00:00:00.000000000 Z
11
+ date: 2017-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: web-utils
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.0'
33
+ version: '1.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.0'
40
+ version: '1.4'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '2.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rack-grid-serve
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.0.5
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.0.5
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: racksh
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -198,6 +212,7 @@ files:
198
212
  - test/test_document_callbacks.rb
199
213
  - test/test_document_persistence.rb
200
214
  - test/test_document_typecasting.rb
215
+ - test/test_grid_fs_attachment.rb
201
216
  - test/test_mongo.rb
202
217
  - test/test_variation.rb
203
218
  - test/test_version.rb
@@ -235,6 +250,7 @@ test_files:
235
250
  - test/test_document_callbacks.rb
236
251
  - test/test_document_persistence.rb
237
252
  - test/test_document_typecasting.rb
253
+ - test/test_grid_fs_attachment.rb
238
254
  - test/test_mongo.rb
239
255
  - test/test_variation.rb
240
256
  - test/test_version.rb