populate-me 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/config.ru +24 -13
- data/lib/populate_me/attachment.rb +5 -1
- data/lib/populate_me/document_mixins/callbacks.rb +3 -1
- data/lib/populate_me/s3_attachment.rb +120 -0
- data/lib/populate_me/version.rb +1 -1
- data/populate-me.gemspec +1 -0
- data/test/test_attachment.rb +6 -0
- data/test/test_grid_fs_attachment.rb +3 -1
- data/test/test_mongo.rb +2 -0
- data/test/test_s3_attachment.rb +273 -0
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10a0d904a55bc88de4e01af6c15816cc3c943a78
|
4
|
+
data.tar.gz: cc36c361bd647861681d153f5bf2e9d9046a7e73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34f55a27c047c8e0204eaf10a9a8c4d62b3ab06e9672812f4522f6b474b48261bf6521a538383770a3be5d1106651d48c5d4502c74d3dcd282bb87bcba7c075b
|
7
|
+
data.tar.gz: 901ce3866a5c98053a4b92f5f53fac015d7b4424f0c32c89b0a4418104b904083c1901f0a5e44606aa8c7677ac24e45aa20921a470c6548774c8be0ca67d1b61
|
data/example/config.ru
CHANGED
@@ -1,23 +1,34 @@
|
|
1
|
+
# We use this example for running real tests on top of unit tests.
|
2
|
+
# This is not meant to be an example to learn how PopulateMe would work.
|
3
|
+
# Real examples will come later and this file will be removed.
|
4
|
+
|
1
5
|
$:.unshift File.expand_path('../../lib', __FILE__)
|
2
6
|
|
3
7
|
# Models ##########
|
4
8
|
|
5
|
-
require 'populate_me/
|
6
|
-
require 'mongo'
|
9
|
+
require 'populate_me/document'
|
7
10
|
|
8
|
-
MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'blog-populate-me-test')
|
9
|
-
DB = MONGO.database
|
10
|
-
PopulateMe::Mongo.set :db, DB
|
11
|
+
# MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'blog-populate-me-test')
|
12
|
+
# DB = MONGO.database
|
13
|
+
# PopulateMe::Mongo.set :db, DB
|
11
14
|
|
12
15
|
# require 'populate_me/attachment'
|
13
16
|
# PopulateMe::Document.set :default_attachment_class, PopulateMe::Attachment
|
14
17
|
# require 'populate_me/file_system_attachment'
|
15
18
|
# PopulateMe::Document.set :default_attachment_class, PopulateMe::FileSystemAttachment
|
16
|
-
|
17
|
-
|
18
|
-
PopulateMe::
|
19
|
+
#
|
20
|
+
# require 'populate_me/grid_fs_attachment'
|
21
|
+
# PopulateMe::Mongo.set :default_attachment_class, PopulateMe::GridFS
|
22
|
+
# PopulateMe::GridFS.set :db, DB
|
23
|
+
|
24
|
+
require 'populate_me/s3_attachment'
|
25
|
+
s3_resource = Aws::S3::Resource.new
|
26
|
+
s3_bucket = s3_resource.bucket(ENV['BUCKET'])
|
27
|
+
PopulateMe::Document.set :default_attachment_class, PopulateMe::S3Attachment
|
28
|
+
PopulateMe::S3Attachment.set :bucket, s3_bucket
|
29
|
+
|
19
30
|
|
20
|
-
class BlogPost < PopulateMe::
|
31
|
+
class BlogPost < PopulateMe::Document
|
21
32
|
field :title, required: true
|
22
33
|
field :thumbnail, type: :attachment, variations: [
|
23
34
|
PopulateMe::Variation.new_image_magick_job(:populate_me_thumb, :jpg, "-resize '400x225^' -gravity center -extent 400x225")
|
@@ -30,14 +41,14 @@ class BlogPost < PopulateMe::Mongo
|
|
30
41
|
error_on(:content,'Cannot be blank') if WebUtils.blank?(self.content)
|
31
42
|
end
|
32
43
|
end
|
33
|
-
class BlogPost::Author < PopulateMe::
|
44
|
+
class BlogPost::Author < PopulateMe::Document
|
34
45
|
# nested
|
35
46
|
field :name
|
36
47
|
def validate
|
37
48
|
error_on(:name, 'Cannot be shit') if self.name=='shit'
|
38
49
|
end
|
39
50
|
end
|
40
|
-
class BlogPost::Comment < PopulateMe::
|
51
|
+
class BlogPost::Comment < PopulateMe::Document
|
41
52
|
# not nested
|
42
53
|
field :author, default: 'Anonymous'
|
43
54
|
field :content, type: :text
|
@@ -45,7 +56,7 @@ class BlogPost::Comment < PopulateMe::Mongo
|
|
45
56
|
position_field scope: :blog_post_id
|
46
57
|
end
|
47
58
|
|
48
|
-
class Article < PopulateMe::
|
59
|
+
class Article < PopulateMe::Document
|
49
60
|
field :title
|
50
61
|
field :content, type: :text
|
51
62
|
position_field
|
@@ -63,6 +74,6 @@ end
|
|
63
74
|
|
64
75
|
# use PopulateMe::Attachment::Middleware
|
65
76
|
# use PopulateMe::FileSystemAttachment::Middleware
|
66
|
-
use PopulateMe::GridFS::Middleware
|
77
|
+
# use PopulateMe::GridFS::Middleware
|
67
78
|
run Admin
|
68
79
|
|
@@ -52,8 +52,12 @@ module PopulateMe
|
|
52
52
|
self.document.__send__(field)
|
53
53
|
end
|
54
54
|
|
55
|
+
def field_options
|
56
|
+
self.document.class.fields[self.field]
|
57
|
+
end
|
58
|
+
|
55
59
|
def variations
|
56
|
-
self.
|
60
|
+
self.field_options[:variations] || []
|
57
61
|
end
|
58
62
|
|
59
63
|
def field_filename variation_name=:original
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
1
3
|
module PopulateMe
|
2
4
|
module DocumentMixins
|
3
5
|
module Callbacks
|
@@ -24,7 +26,7 @@ module PopulateMe
|
|
24
26
|
|
25
27
|
def ensure_id
|
26
28
|
if self.id.nil?
|
27
|
-
self.id =
|
29
|
+
self.id = SecureRandom.hex
|
28
30
|
end
|
29
31
|
self
|
30
32
|
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'populate_me/attachment'
|
2
|
+
require 'aws-sdk'
|
3
|
+
|
4
|
+
module PopulateMe
|
5
|
+
|
6
|
+
class MissingBucketError < StandardError; end
|
7
|
+
|
8
|
+
class S3Attachment < Attachment
|
9
|
+
|
10
|
+
# For S3 this option behaves a bit differently.
|
11
|
+
# Because S3 file are served directly instead of using a middleware,
|
12
|
+
# the url_prefix is just used in the key name before the attachee_prefix.
|
13
|
+
# It helps saving keys under /public for example which is a common idiom
|
14
|
+
# to save file in a path public by default.
|
15
|
+
# This option can be overriden at the field level.
|
16
|
+
set :url_prefix, '/public'
|
17
|
+
|
18
|
+
# Attachee_prefix is moved on field_value for S3
|
19
|
+
# as well as url_prefix
|
20
|
+
def url variation_name=:original
|
21
|
+
return nil if WebUtils.blank?(self.field_filename(variation_name))
|
22
|
+
"#{settings.bucket.url}/#{self.field_filename(variation_name)}"
|
23
|
+
end
|
24
|
+
# Attachee_prefix is moved on field_value for S3
|
25
|
+
# as well as url_prefix
|
26
|
+
def location_root
|
27
|
+
File.join(
|
28
|
+
settings.root,
|
29
|
+
settings.url_prefix
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def local_url_prefix
|
34
|
+
(
|
35
|
+
self.field_options[:url_prefix] ||
|
36
|
+
self.document.class.settings.s3_url_prefix ||
|
37
|
+
settings.url_prefix
|
38
|
+
).gsub(/^\/|\/$/,'')
|
39
|
+
end
|
40
|
+
|
41
|
+
def next_available_filename filename
|
42
|
+
ext = File.extname(filename)
|
43
|
+
base = File.join(
|
44
|
+
local_url_prefix,
|
45
|
+
attachee_prefix,
|
46
|
+
File.basename(filename,ext)
|
47
|
+
).gsub(/^\//,'')
|
48
|
+
i = 0
|
49
|
+
loop do
|
50
|
+
suffix = i==0 ? '' : "-#{i}"
|
51
|
+
potential_filename = [base,suffix,ext].join
|
52
|
+
if settings.bucket.object(potential_filename).exists?
|
53
|
+
i += 1
|
54
|
+
else
|
55
|
+
filename = potential_filename
|
56
|
+
break
|
57
|
+
end
|
58
|
+
end
|
59
|
+
filename
|
60
|
+
end
|
61
|
+
|
62
|
+
def perform_create hash
|
63
|
+
if hash[:variation].nil?
|
64
|
+
fn = next_available_filename(hash[:filename])
|
65
|
+
file = hash[:tempfile]
|
66
|
+
type = hash[:type]
|
67
|
+
else
|
68
|
+
fn = WebUtils.filename_variation hash[:future_field_value], hash[:variation].name, hash[:variation].ext
|
69
|
+
file = File.open(hash[:variation_path])
|
70
|
+
type = Rack::Mime.mime_type ".#{hash[:variation].ext}"
|
71
|
+
end
|
72
|
+
settings.bucket.put_object({
|
73
|
+
acl: self.field_options[:acl] || 'public-read',
|
74
|
+
key: fn,
|
75
|
+
content_type: type,
|
76
|
+
body: file,
|
77
|
+
metadata: {
|
78
|
+
parent_collection: (self.document.class.respond_to?(:collection) ? self.document.class.collection.name : self.attachee_prefix),
|
79
|
+
}
|
80
|
+
})
|
81
|
+
file.close unless hash[:variation].nil?
|
82
|
+
fn
|
83
|
+
end
|
84
|
+
|
85
|
+
def deletable? variation_name=:original
|
86
|
+
!WebUtils.blank? self.field_filename(variation_name)
|
87
|
+
# Fine since deleting a non-existent file does not raise an error in S3
|
88
|
+
end
|
89
|
+
|
90
|
+
def perform_delete variation_name=:original
|
91
|
+
s3file = settings.bucket.object(self.field_filename(variation_name))
|
92
|
+
s3file.delete unless s3file.nil?
|
93
|
+
end
|
94
|
+
|
95
|
+
class << self
|
96
|
+
|
97
|
+
def ensure_bucket
|
98
|
+
raise MissingBucketError, "Attachment class #{self.name} does not have an S3 bucket." if settings.bucket.nil?
|
99
|
+
end
|
100
|
+
|
101
|
+
def middleware
|
102
|
+
nil
|
103
|
+
end
|
104
|
+
|
105
|
+
# def middleware_options
|
106
|
+
# [
|
107
|
+
# {
|
108
|
+
# prefix: settings.url_prefix.dup.gsub(/^\/|\/$/,''),
|
109
|
+
# db: settings.db,
|
110
|
+
# # lookup: :path
|
111
|
+
# }
|
112
|
+
# ]
|
113
|
+
# end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
data/lib/populate_me/version.rb
CHANGED
data/populate-me.gemspec
CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.add_development_dependency 'rack-cerberus', '~> 1.0'
|
28
28
|
s.add_development_dependency 'mongo', '~> 2.0'
|
29
29
|
s.add_development_dependency 'rack-grid-serve', '~> 0.0.8'
|
30
|
+
s.add_development_dependency 'aws-sdk', '~> 3'
|
30
31
|
s.add_development_dependency 'racksh', '~> 1.0'
|
31
32
|
s.add_development_dependency 'rake', '~> 10.1'
|
32
33
|
end
|
data/test/test_attachment.rb
CHANGED
@@ -43,6 +43,12 @@ describe PopulateMe::Attachment do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
describe "#field_options" do
|
47
|
+
it "Get options of the field" do
|
48
|
+
assert_equal :attachment, subject.field_options[:type]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
46
52
|
describe "#variations" do
|
47
53
|
it "Gets variations for the field" do
|
48
54
|
variations = subject.variations
|
@@ -2,6 +2,8 @@ require 'helper'
|
|
2
2
|
require 'populate_me/mongo'
|
3
3
|
require 'populate_me/grid_fs_attachment'
|
4
4
|
|
5
|
+
Mongo::Logger.logger.level = Logger::ERROR
|
6
|
+
|
5
7
|
GRIDMONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'populate-me-grid-test')
|
6
8
|
GRIDDB = GRIDMONGO.database
|
7
9
|
GRIDDB.drop
|
@@ -29,7 +31,7 @@ describe 'PopulateMe::GridFS' do
|
|
29
31
|
|
30
32
|
# Utils
|
31
33
|
|
32
|
-
it 'Returns URL with
|
34
|
+
it 'Returns URL with url_prefix' do
|
33
35
|
book = GridBook.new cover: "candy.jpg"
|
34
36
|
assert_equal '/attachment/candy.jpg', book.attachment(:cover).url
|
35
37
|
assert_equal '/attachment/candy.thumb.gif', book.attachment(:cover).url(:thumb)
|
data/test/test_mongo.rb
CHANGED
@@ -0,0 +1,273 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'populate_me/document'
|
3
|
+
require 'populate_me/s3_attachment'
|
4
|
+
|
5
|
+
s3_resource = Aws::S3::Resource.new
|
6
|
+
s3_bucket = s3_resource.bucket(ENV['BUCKET'])
|
7
|
+
PopulateMe::Document.set :default_attachment_class, PopulateMe::S3Attachment
|
8
|
+
PopulateMe::S3Attachment.set :bucket, s3_bucket
|
9
|
+
|
10
|
+
describe 'PopulateMe::S3Attachment' do
|
11
|
+
# parallelize_me!
|
12
|
+
|
13
|
+
class S3Book < PopulateMe::Document
|
14
|
+
field :cover, type: :attachment, variations: [
|
15
|
+
PopulateMe::Variation.new_image_magick_job(:thumb, :gif, "-resize '300x'")
|
16
|
+
]
|
17
|
+
field :content, type: :attachment, variations: [
|
18
|
+
PopulateMe::Variation.new(:upcase, :txt, lambda{ |src,dst|
|
19
|
+
Kernel.system "cat \"#{src}\" | tr 'a-z' 'A-Z' > \"#{dst}\""
|
20
|
+
})
|
21
|
+
]
|
22
|
+
field :open_content, type: :attachment, url_prefix: 'open', variations: [
|
23
|
+
PopulateMe::Variation.new(:upcase, :txt, lambda{ |src,dst|
|
24
|
+
Kernel.system "cat \"#{src}\" | tr 'a-z' 'A-Z' > \"#{dst}\""
|
25
|
+
})
|
26
|
+
]
|
27
|
+
end
|
28
|
+
|
29
|
+
class S3BookNoPrefix < PopulateMe::Document
|
30
|
+
set :s3_url_prefix, ''
|
31
|
+
field :content, type: :attachment, variations: [
|
32
|
+
PopulateMe::Variation.new(:upcase, :txt, lambda{ |src,dst|
|
33
|
+
Kernel.system "cat \"#{src}\" | tr 'a-z' 'A-Z' > \"#{dst}\""
|
34
|
+
})
|
35
|
+
]
|
36
|
+
end
|
37
|
+
|
38
|
+
before do
|
39
|
+
s3_bucket.clear!
|
40
|
+
end
|
41
|
+
|
42
|
+
# Utils
|
43
|
+
|
44
|
+
it 'Returns URL with bucket url' do
|
45
|
+
book = S3Book.new cover: "candy.jpg"
|
46
|
+
assert_equal "#{s3_bucket.url}/candy.jpg", book.attachment(:cover).url
|
47
|
+
assert_equal "#{s3_bucket.url}/candy.thumb.gif", book.attachment(:cover).url(:thumb)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'Has nil URL when field is blank' do
|
51
|
+
book = S3Book.new
|
52
|
+
assert_nil book.attachment(:cover).url
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'Has location root without attachee prefix' do
|
56
|
+
book = S3Book.new
|
57
|
+
refute_match book.attachment(:cover).attachee_prefix, book.attachment(:cover).location_root
|
58
|
+
end
|
59
|
+
|
60
|
+
# Create
|
61
|
+
|
62
|
+
it "Saves attachments on create with variations" do
|
63
|
+
book = S3Book.new
|
64
|
+
|
65
|
+
file = Tempfile.new('foo')
|
66
|
+
file.write('hello')
|
67
|
+
file.rewind
|
68
|
+
|
69
|
+
field_value = book.attachment(:content).create({
|
70
|
+
tempfile: file,
|
71
|
+
filename: 'story.txt',
|
72
|
+
type: 'text/plain'
|
73
|
+
})
|
74
|
+
assert_equal 'public/s-3-book/story.txt', field_value
|
75
|
+
assert s3_bucket.object('public/s-3-book/story.txt').exists?
|
76
|
+
assert s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
77
|
+
|
78
|
+
s3file = s3_bucket.object('public/s-3-book/story.txt')
|
79
|
+
assert_equal 'text/plain', s3file.content_type
|
80
|
+
assert check_if_public_read(s3file)
|
81
|
+
assert_equal 's-3-book', s3file.metadata['parent_collection']
|
82
|
+
assert_equal 'hello', s3file.get.body.read
|
83
|
+
|
84
|
+
vars3file = s3_bucket.object('public/s-3-book/story.upcase.txt')
|
85
|
+
assert_equal 'text/plain', vars3file.content_type
|
86
|
+
# assert_equal 'public-read', vars3file.acl
|
87
|
+
assert_equal 's-3-book', vars3file.metadata['parent_collection']
|
88
|
+
assert_equal 'HELLO', vars3file.get.body.read
|
89
|
+
|
90
|
+
file.close
|
91
|
+
file.unlink
|
92
|
+
end
|
93
|
+
|
94
|
+
it "Can override the url_prefix at document class level" do
|
95
|
+
file = Tempfile.new('foo')
|
96
|
+
book = S3BookNoPrefix.new
|
97
|
+
|
98
|
+
field_value = book.attachment(:content).create({
|
99
|
+
tempfile: file,
|
100
|
+
filename: 'story.txt',
|
101
|
+
type: 'text/plain'
|
102
|
+
})
|
103
|
+
|
104
|
+
assert_equal 's-3-book-no-prefix/story.txt', field_value
|
105
|
+
assert s3_bucket.object('s-3-book-no-prefix/story.txt').exists?
|
106
|
+
assert s3_bucket.object('s-3-book-no-prefix/story.upcase.txt').exists?
|
107
|
+
end
|
108
|
+
|
109
|
+
it "Can override the url_prefix at field level" do
|
110
|
+
file = Tempfile.new('foo')
|
111
|
+
book = S3Book.new
|
112
|
+
|
113
|
+
field_value = book.attachment(:open_content).create({
|
114
|
+
tempfile: file,
|
115
|
+
filename: 'story.txt',
|
116
|
+
type: 'text/plain'
|
117
|
+
})
|
118
|
+
|
119
|
+
assert_equal 'open/s-3-book/story.txt', field_value
|
120
|
+
assert s3_bucket.object('open/s-3-book/story.txt').exists?
|
121
|
+
assert s3_bucket.object('open/s-3-book/story.upcase.txt').exists?
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'Does not create 2 files with the same name' do
|
125
|
+
file = Tempfile.new('foo')
|
126
|
+
|
127
|
+
book = S3Book.new
|
128
|
+
|
129
|
+
field_value = book.attachment(:content).create({
|
130
|
+
tempfile: file,
|
131
|
+
filename: 'story.txt',
|
132
|
+
type: 'text/plain'
|
133
|
+
})
|
134
|
+
|
135
|
+
assert_equal 'public/s-3-book/story.txt', field_value
|
136
|
+
|
137
|
+
field_value = book.attachment(:content).create({
|
138
|
+
tempfile: file,
|
139
|
+
filename: 'story.txt',
|
140
|
+
type: 'text/plain'
|
141
|
+
})
|
142
|
+
|
143
|
+
assert_equal 'public/s-3-book/story-1.txt', field_value
|
144
|
+
|
145
|
+
field_value = book.attachment(:content).create({
|
146
|
+
tempfile: file,
|
147
|
+
filename: 'story.txt',
|
148
|
+
type: 'text/plain'
|
149
|
+
})
|
150
|
+
|
151
|
+
assert_equal 'public/s-3-book/story-2.txt', field_value
|
152
|
+
|
153
|
+
file.close
|
154
|
+
file.unlink
|
155
|
+
end
|
156
|
+
|
157
|
+
# Delete
|
158
|
+
|
159
|
+
it 'Is deletable when field is not blank' do
|
160
|
+
book = S3Book.new cover: "candy.jpg"
|
161
|
+
assert book.attachment(:cover).deletable?
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'Is not deletable when field is blank' do
|
165
|
+
book = S3Book.new
|
166
|
+
refute book.attachment(:cover).deletable?
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'Deletes all attachments' do
|
170
|
+
file = Tempfile.new('foo')
|
171
|
+
|
172
|
+
book = S3Book.new
|
173
|
+
|
174
|
+
field_value = book.attachment(:content).create({
|
175
|
+
tempfile: file,
|
176
|
+
filename: 'story.txt',
|
177
|
+
type: 'text/plain'
|
178
|
+
})
|
179
|
+
book.content = field_value
|
180
|
+
|
181
|
+
assert s3_bucket.object('public/s-3-book/story.txt').exists?
|
182
|
+
assert s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
183
|
+
|
184
|
+
book.attachment(:content).delete_all
|
185
|
+
|
186
|
+
refute s3_bucket.object('public/s-3-book/story.txt').exists?
|
187
|
+
refute s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
188
|
+
|
189
|
+
file.close
|
190
|
+
file.unlink
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'Deletes one attachment at a time' do
|
194
|
+
file = Tempfile.new('foo')
|
195
|
+
|
196
|
+
book = S3Book.new
|
197
|
+
|
198
|
+
field_value = book.attachment(:content).create({
|
199
|
+
tempfile: file,
|
200
|
+
filename: 'story.txt',
|
201
|
+
type: 'text/plain'
|
202
|
+
})
|
203
|
+
book.content = field_value
|
204
|
+
|
205
|
+
assert s3_bucket.object('public/s-3-book/story.txt').exists?
|
206
|
+
assert s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
207
|
+
|
208
|
+
book.attachment(:content).delete
|
209
|
+
|
210
|
+
refute s3_bucket.object('public/s-3-book/story.txt').exists?
|
211
|
+
assert s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
212
|
+
|
213
|
+
book.attachment(:content).delete :upcase
|
214
|
+
|
215
|
+
refute s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
216
|
+
|
217
|
+
file.close
|
218
|
+
file.unlink
|
219
|
+
end
|
220
|
+
|
221
|
+
# Update
|
222
|
+
|
223
|
+
it 'Deletes previous attachment when saving a new one' do
|
224
|
+
file = Tempfile.new('foo')
|
225
|
+
file.write('hello')
|
226
|
+
file.rewind
|
227
|
+
|
228
|
+
book = S3Book.new
|
229
|
+
|
230
|
+
field_value = book.attachment(:content).create({
|
231
|
+
tempfile: file,
|
232
|
+
filename: 'story.txt',
|
233
|
+
type: 'text/plain'
|
234
|
+
})
|
235
|
+
book.content = field_value
|
236
|
+
|
237
|
+
assert s3_bucket.object('public/s-3-book/story.txt').exists?
|
238
|
+
assert s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
239
|
+
|
240
|
+
file.rewind
|
241
|
+
file.write('world')
|
242
|
+
file.rewind
|
243
|
+
|
244
|
+
field_value = book.attachment(:content).create({
|
245
|
+
tempfile: file,
|
246
|
+
filename: 'history.md',
|
247
|
+
type: 'text/markdown'
|
248
|
+
})
|
249
|
+
book.content = field_value
|
250
|
+
|
251
|
+
refute s3_bucket.object('public/s-3-book/story.txt').exists?
|
252
|
+
refute s3_bucket.object('public/s-3-book/story.upcase.txt').exists?
|
253
|
+
assert s3_bucket.object('public/s-3-book/history.md').exists?
|
254
|
+
assert s3_bucket.object('public/s-3-book/history.upcase.txt').exists?
|
255
|
+
|
256
|
+
s3file = s3_bucket.object('public/s-3-book/history.md')
|
257
|
+
assert_equal 'text/markdown', s3file.content_type
|
258
|
+
assert_equal 's-3-book', s3file.metadata['parent_collection']
|
259
|
+
assert_equal 'world', s3file.get.body.read
|
260
|
+
|
261
|
+
s3file = s3_bucket.object('public/s-3-book/history.upcase.txt')
|
262
|
+
assert_equal 'text/plain', s3file.content_type
|
263
|
+
assert_equal 's-3-book', s3file.metadata['parent_collection']
|
264
|
+
assert_equal 'WORLD', s3file.get.body.read
|
265
|
+
|
266
|
+
file.close
|
267
|
+
file.unlink
|
268
|
+
end
|
269
|
+
|
270
|
+
end
|
271
|
+
|
272
|
+
s3_bucket.clear!
|
273
|
+
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
11
|
+
date: 2017-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: web-utils
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: 0.0.8
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: aws-sdk
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '3'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '3'
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: racksh
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -200,6 +214,7 @@ files:
|
|
200
214
|
- lib/populate_me/file_system_attachment.rb
|
201
215
|
- lib/populate_me/grid_fs_attachment.rb
|
202
216
|
- lib/populate_me/mongo.rb
|
217
|
+
- lib/populate_me/s3_attachment.rb
|
203
218
|
- lib/populate_me/variation.rb
|
204
219
|
- lib/populate_me/version.rb
|
205
220
|
- populate-me.gemspec
|
@@ -215,6 +230,7 @@ files:
|
|
215
230
|
- test/test_document_typecasting.rb
|
216
231
|
- test/test_grid_fs_attachment.rb
|
217
232
|
- test/test_mongo.rb
|
233
|
+
- test/test_s3_attachment.rb
|
218
234
|
- test/test_variation.rb
|
219
235
|
- test/test_version.rb
|
220
236
|
homepage: https://github.com/mig-hub/populate-me
|
@@ -237,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
237
253
|
version: '0'
|
238
254
|
requirements: []
|
239
255
|
rubyforge_project:
|
240
|
-
rubygems_version: 2.6.
|
256
|
+
rubygems_version: 2.6.13
|
241
257
|
signing_key:
|
242
258
|
specification_version: 4
|
243
259
|
summary: PopulateMe is an admin system for web applications.
|
@@ -254,5 +270,6 @@ test_files:
|
|
254
270
|
- test/test_document_typecasting.rb
|
255
271
|
- test/test_grid_fs_attachment.rb
|
256
272
|
- test/test_mongo.rb
|
273
|
+
- test/test_s3_attachment.rb
|
257
274
|
- test/test_variation.rb
|
258
275
|
- test/test_version.rb
|