saviour 0.4.4 → 0.4.5
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.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/lib/saviour/base_uploader.rb +12 -5
- data/lib/saviour/file.rb +5 -2
- data/lib/saviour/life_cycle.rb +1 -1
- data/lib/saviour/version.rb +1 -1
- data/spec/feature/halt_processor_spec.rb +45 -0
- data/spec/models/base_uploader_spec.rb +19 -7
- data/spec/models/file_spec.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6a8dde206061328305a93bbcbcf03ef6e15d9659
|
|
4
|
+
data.tar.gz: 828b58ec76d1974a88bf595ea2306cded359f752
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0a13bc0dced7f96b5306a6ef7dbb72f01a66d8b9f9f962b4de99697eea8212db71f706abb9f7d12ef1cb29ae5a879d329c408937ddffd9f225d1c35ae4bb4e76
|
|
7
|
+
data.tar.gz: dee3ef8dab84f49536c9c311d23e97cbe66ee6e1e5278fe48f02267720dfa7178a424b814746ba0dd1fdf67d4cd8e213c11f37d17e26144fa1bd281e40a801ea
|
data/README.md
CHANGED
|
@@ -410,6 +410,20 @@ some File instance different than the one you received pointing to a file.
|
|
|
410
410
|
|
|
411
411
|
From inside a process you can also access the current store dir with `store_dir`.
|
|
412
412
|
|
|
413
|
+
From inside a process, you can also call `halt_process` to abort the current processing and upload of the file.
|
|
414
|
+
This can be useful for example for an "image_thumb" attachment that can be generic. If you're able to generate a
|
|
415
|
+
thumbnail image for the given file, then it works normally, otherwise halt:
|
|
416
|
+
|
|
417
|
+
```ruby
|
|
418
|
+
process_with_file do |file, filename|
|
|
419
|
+
if can_generate_thumbnail?(file) # Only if jpg, png or pdf file
|
|
420
|
+
create_thumbnail(file, filename)
|
|
421
|
+
else
|
|
422
|
+
halt_process
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
```
|
|
426
|
+
|
|
413
427
|
Finally, processors can be disabled entirely via a configuration parameter. Example:
|
|
414
428
|
|
|
415
429
|
```
|
|
@@ -22,13 +22,20 @@ module Saviour
|
|
|
22
22
|
def write(contents, filename)
|
|
23
23
|
raise RuntimeError, "Please use `store_dir` before trying to write" unless store_dir
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
catch(:halt_process) do
|
|
26
|
+
if Config.processing_enabled
|
|
27
|
+
contents, filename = Uploader::ProcessorsRunner.new(self).run!(contents, filename)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
path = ::File.join(store_dir, filename)
|
|
31
|
+
Config.storage.write(contents, path)
|
|
32
|
+
|
|
33
|
+
path
|
|
27
34
|
end
|
|
35
|
+
end
|
|
28
36
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
path
|
|
37
|
+
def halt_process
|
|
38
|
+
throw(:halt_process)
|
|
32
39
|
end
|
|
33
40
|
|
|
34
41
|
def store_dir
|
data/lib/saviour/file.rb
CHANGED
data/lib/saviour/life_cycle.rb
CHANGED
|
@@ -32,7 +32,7 @@ module Saviour
|
|
|
32
32
|
Config.storage.delete(current_path) if current_path
|
|
33
33
|
|
|
34
34
|
new_path = @model.send(column).write
|
|
35
|
-
persistence_layer.write(column, new_path) if persistence_layer
|
|
35
|
+
persistence_layer.write(column, new_path) if persistence_layer && new_path
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def attached_files
|
data/lib/saviour/version.rb
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "halt processor behavior" do
|
|
4
|
+
before { allow(Saviour::Config).to receive(:storage).and_return(Saviour::LocalStorage.new(local_prefix: @tmpdir, public_url_prefix: "http://domain.com")) }
|
|
5
|
+
|
|
6
|
+
let(:uploader) {
|
|
7
|
+
Class.new(Saviour::BaseUploader) do
|
|
8
|
+
store_dir { "/store/dir" }
|
|
9
|
+
process { |_contents, _name| halt_process }
|
|
10
|
+
end
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let(:klass) {
|
|
14
|
+
klass = Class.new(Test) {
|
|
15
|
+
include Saviour::Model
|
|
16
|
+
}
|
|
17
|
+
klass.attach_file :file, uploader
|
|
18
|
+
klass
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
it "does not write the file" do
|
|
22
|
+
a = klass.create!
|
|
23
|
+
|
|
24
|
+
expect(Saviour::Config.storage).to_not receive(:write)
|
|
25
|
+
|
|
26
|
+
a.update_attributes! file: StringIO.new("contents")
|
|
27
|
+
expect(a.reload.read_attribute(:file)).to be_nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "is considered as not persisted after save" do
|
|
31
|
+
a = klass.new
|
|
32
|
+
a.file = StringIO.new("contents")
|
|
33
|
+
expect(a.file.persisted?).to be_falsey
|
|
34
|
+
a.save
|
|
35
|
+
expect(a.file.persisted?).to be_falsey
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "is considered as not dirty after save" do
|
|
39
|
+
a = klass.new
|
|
40
|
+
a.file = StringIO.new("contents")
|
|
41
|
+
expect(a.file.changed?).to be_truthy
|
|
42
|
+
a.save
|
|
43
|
+
expect(a.file.changed?).to be_falsey
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -28,7 +28,7 @@ describe Saviour::BaseUploader do
|
|
|
28
28
|
expect(subject.processors[0][:opts]).to eq({})
|
|
29
29
|
|
|
30
30
|
expect(subject.processors[1][:method_or_block]).to eq :resize
|
|
31
|
-
expect(subject.processors[1][:opts]).to eq({width: 50})
|
|
31
|
+
expect(subject.processors[1][:opts]).to eq({ width: 50 })
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it do
|
|
@@ -66,23 +66,23 @@ describe Saviour::BaseUploader do
|
|
|
66
66
|
|
|
67
67
|
describe "initialization with data" do
|
|
68
68
|
it "can declare wathever" do
|
|
69
|
-
uploader = Class.new(Saviour::BaseUploader).new(data: {a: "2", data: "my file"})
|
|
69
|
+
uploader = Class.new(Saviour::BaseUploader).new(data: { a: "2", data: "my file" })
|
|
70
70
|
expect(uploader).to respond_to :a
|
|
71
71
|
expect(uploader).to respond_to :data
|
|
72
72
|
expect(uploader.a).to eq "2"
|
|
73
73
|
end
|
|
74
74
|
|
|
75
75
|
it do
|
|
76
|
-
uploader = Class.new(Saviour::BaseUploader).new(data: {a: "2", data: "my file"})
|
|
76
|
+
uploader = Class.new(Saviour::BaseUploader).new(data: { a: "2", data: "my file" })
|
|
77
77
|
expect(uploader).to respond_to :a
|
|
78
78
|
|
|
79
|
-
uploader = Class.new(Saviour::BaseUploader).new(data: {name: "johny"})
|
|
79
|
+
uploader = Class.new(Saviour::BaseUploader).new(data: { name: "johny" })
|
|
80
80
|
expect(uploader).not_to respond_to :a
|
|
81
81
|
end
|
|
82
82
|
end
|
|
83
83
|
|
|
84
84
|
describe "#write" do
|
|
85
|
-
subject { uploader.new(data: {model: "model", attached_as: "attached_as"}) }
|
|
85
|
+
subject { uploader.new(data: { model: "model", attached_as: "attached_as" }) }
|
|
86
86
|
|
|
87
87
|
context do
|
|
88
88
|
let(:uploader) { Class.new(Saviour::BaseUploader) }
|
|
@@ -173,17 +173,29 @@ describe Saviour::BaseUploader do
|
|
|
173
173
|
} }
|
|
174
174
|
|
|
175
175
|
let(:model) { double(id: 8, name: "Robert") }
|
|
176
|
-
subject { uploader.new(data: {model: model, attached_as: "attached_as"}) }
|
|
176
|
+
subject { uploader.new(data: { model: model, attached_as: "attached_as" }) }
|
|
177
177
|
|
|
178
178
|
it "can access model from processors" do
|
|
179
179
|
expect(Saviour::Config.storage).to receive(:write).with("content", "/store/dir/Robert_8_output.png")
|
|
180
180
|
subject.write("content", "output.png")
|
|
181
181
|
end
|
|
182
182
|
end
|
|
183
|
+
|
|
184
|
+
describe "returns nil if halted" do
|
|
185
|
+
let(:uploader) { Class.new(Saviour::BaseUploader) {
|
|
186
|
+
store_dir { "/store/dir" }
|
|
187
|
+
process { halt_process }
|
|
188
|
+
} }
|
|
189
|
+
|
|
190
|
+
it do
|
|
191
|
+
expect(Saviour::Config.storage).to_not receive(:write)
|
|
192
|
+
expect(subject.write("contents", "file.jpg")).to be_nil
|
|
193
|
+
end
|
|
194
|
+
end
|
|
183
195
|
end
|
|
184
196
|
|
|
185
197
|
describe "#process_with_file" do
|
|
186
|
-
subject { uploader.new(data: {model: "model", attached_as: "attached_as"}) }
|
|
198
|
+
subject { uploader.new(data: { model: "model", attached_as: "attached_as" }) }
|
|
187
199
|
|
|
188
200
|
context do
|
|
189
201
|
let(:uploader) { Class.new(Saviour::BaseUploader) {
|
data/spec/models/file_spec.rb
CHANGED
|
@@ -119,7 +119,7 @@ describe Saviour::File do
|
|
|
119
119
|
|
|
120
120
|
uploader = double
|
|
121
121
|
allow(file).to receive(:uploader).and_return(uploader)
|
|
122
|
-
expect(uploader).to receive(:write)
|
|
122
|
+
expect(uploader).to receive(:write).and_return("/some/path")
|
|
123
123
|
file.write
|
|
124
124
|
|
|
125
125
|
expect(file).not_to be_changed
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: saviour
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Roger Campos
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-12-
|
|
11
|
+
date: 2017-12-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -177,6 +177,7 @@ files:
|
|
|
177
177
|
- spec/feature/crud_workflows_spec.rb
|
|
178
178
|
- spec/feature/dirty_spec.rb
|
|
179
179
|
- spec/feature/follow_file_spec.rb
|
|
180
|
+
- spec/feature/halt_processor_spec.rb
|
|
180
181
|
- spec/feature/persisted_path_spec.rb
|
|
181
182
|
- spec/feature/processors_api.rb
|
|
182
183
|
- spec/feature/reload_model_spec.rb
|