saviour 0.3.1 → 0.4.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.
@@ -27,6 +27,26 @@ describe Saviour::Config do
27
27
  Thread.new { expect(Saviour::Config.storage).to eq("chuck") }.join
28
28
  end
29
29
 
30
+ it "raises correct exception if the main thread is not yet configured" do
31
+ Thread.main["Saviour::Config"] = nil
32
+
33
+ Thread.new {
34
+ expect { Saviour::Config.storage.anything }.to raise_error(RuntimeError)
35
+ }.join
36
+ end
37
+
38
+ it "forwards configuration to main thread if not configured" do
39
+ Thread.main["Saviour::Config"] = nil
40
+
41
+ Thread.new {
42
+ Saviour::Config.storage = 'config_set_on_thread_1'
43
+
44
+ Thread.new {
45
+ expect(Saviour::Config.storage).to eq 'config_set_on_thread_1'
46
+ }.join
47
+ }.join
48
+ end
49
+
30
50
  it "allows per-thread values" do
31
51
  Saviour::Config.storage = 12
32
52
  Thread.new do
@@ -23,9 +23,13 @@ describe Saviour::File do
23
23
  store_dir { "/store/dir" }
24
24
  } }
25
25
 
26
- let(:example_file) { double(read: "some file contents", path: "/my/path") }
26
+ let(:example_file) { double(read: "some file contents", path: "/my/path", rewind: nil) }
27
27
 
28
- let(:dummy_class) { Class.new }
28
+ let(:dummy_class) {
29
+ klass = Class.new
30
+ Saviour::Integrator.new(klass, Saviour::PersistenceLayer).setup!
31
+ klass
32
+ }
29
33
 
30
34
  describe "#assign" do
31
35
  it "returns the assigned object" do
@@ -54,7 +58,7 @@ describe Saviour::File do
54
58
  describe "filename used" do
55
59
  it "is extracted from original_filename if possible" do
56
60
  file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
57
- file.assign(double(read: "contents", original_filename: 'original.jpg', path: "/my/path/my_file.zip"))
61
+ file.assign(double(read: "contents", original_filename: 'original.jpg', path: "/my/path/my_file.zip", rewind: nil))
58
62
  uploader = double
59
63
  allow(file).to receive(:uploader).and_return(uploader)
60
64
  expect(uploader).to receive(:write).with("contents", "original.jpg")
@@ -63,7 +67,7 @@ describe Saviour::File do
63
67
 
64
68
  it "is extracted from path if possible" do
65
69
  file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
66
- file.assign(double(read: "contents", path: "/my/path/my_file.zip"))
70
+ file.assign(double(read: "contents", path: "/my/path/my_file.zip", rewind: nil))
67
71
  uploader = double
68
72
  allow(file).to receive(:uploader).and_return(uploader)
69
73
  expect(uploader).to receive(:write).with("contents", "my_file.zip")
@@ -72,7 +76,7 @@ describe Saviour::File do
72
76
 
73
77
  it "is random if cannot be guessed" do
74
78
  file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
75
- file.assign(double(read: "contents"))
79
+ file.assign(double(read: "contents", rewind: nil))
76
80
  allow(SecureRandom).to receive(:hex).and_return("stubbed-random")
77
81
  uploader = double
78
82
  allow(file).to receive(:uploader).and_return(uploader)
@@ -84,7 +88,7 @@ describe Saviour::File do
84
88
  it "returns the path" do
85
89
  object = dummy_class.new
86
90
  file = Saviour::File.new(uploader_klass, object, :file)
87
- file.assign(double(read: "contents", path: "/my/path/my_file.zip"))
91
+ file.assign(double(read: "contents", path: "/my/path/my_file.zip", rewind: nil))
88
92
  uploader = double
89
93
  allow(file).to receive(:uploader).and_return(uploader)
90
94
  expect(uploader).to receive(:write).with("contents", "my_file.zip").and_return("/store/dir/my_file.zip")
@@ -104,7 +108,7 @@ describe Saviour::File do
104
108
 
105
109
  it "is cleared after persisting" do
106
110
  file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
107
- file.assign(double(read: "contents", path: "/my/path/my_file.zip"))
111
+ file.assign(double(read: "contents", path: "/my/path/my_file.zip", rewind: nil))
108
112
  expect(file).to be_changed
109
113
 
110
114
  uploader = double
@@ -9,57 +9,10 @@ describe Saviour do
9
9
  }.to raise_error(Saviour::NoActiveRecordDetected)
10
10
  end
11
11
 
12
- it "error if column not present" do
13
- expect {
14
- Class.new(Test) do
15
- include Saviour::Model
16
-
17
- attach_file :not_present, Saviour::BaseUploader
18
- end
19
- }.to raise_error(RuntimeError)
20
- end
21
-
22
- context do
23
- it "error if column not present on version" do
24
- uploader = Class.new(Saviour::BaseUploader) do
25
- store_dir { "/store/dir" }
26
-
27
- version(:thumb) do
28
- store_dir { "/versions/store/dir" }
29
- end
30
-
31
- version(:not_present)
32
- end
33
-
34
- expect {
35
- Class.new(Test) do
36
- include Saviour::Model
37
-
38
- attach_file :file, uploader
39
- end
40
- }.to raise_error(RuntimeError)
41
- end
42
- end
43
-
44
- it "does not raise error if table is not present" do
45
- allow(Test).to receive(:table_exists?).and_return(false)
46
-
47
- expect {
48
- Class.new(Test) do
49
- include Saviour::Model
50
-
51
- attach_file :not_present, Saviour::BaseUploader
52
- end
53
- }.to_not raise_error
54
- end
55
-
56
12
  describe ".attached_files" do
57
- it "includes a mapping of the currently attached files and their versions" do
13
+ it "includes a mapping of the currently attached files" do
58
14
  uploader = Class.new(Saviour::BaseUploader) do
59
15
  store_dir { "/store/dir" }
60
-
61
- version(:thumb)
62
- version(:thumb_2)
63
16
  end
64
17
 
65
18
  klass = Class.new(Test) do
@@ -67,39 +20,7 @@ describe Saviour do
67
20
  attach_file :file, uploader
68
21
  end
69
22
 
70
- expect(klass.attached_files).to eq({file: [:thumb, :thumb_2]})
71
-
72
- klass2 = Class.new(Test) do
73
- include Saviour::Model
74
- attach_file :file, Saviour::BaseUploader
75
- end
76
-
77
- expect(klass2.attached_files).to eq({file: []})
78
- end
79
- end
80
-
81
- describe ".attached_files" do
82
- it "includes a mapping of the currently attached files and their versions" do
83
- uploader = Class.new(Saviour::BaseUploader) do
84
- store_dir { "/store/dir" }
85
-
86
- version(:thumb)
87
- version(:thumb_2)
88
- end
89
-
90
- klass = Class.new(Test) do
91
- include Saviour::Model
92
- attach_file :file, uploader
93
- end
94
-
95
- expect(klass.attached_files).to eq({file: [:thumb, :thumb_2]})
96
-
97
- klass2 = Class.new(Test) do
98
- include Saviour::Model
99
- attach_file :file, Saviour::BaseUploader
100
- end
101
-
102
- expect(klass2.attached_files).to eq({file: []})
23
+ expect(klass.attached_files).to eq([:file])
103
24
  end
104
25
  end
105
26
 
@@ -111,17 +32,16 @@ describe Saviour do
111
32
  it "shares model definitions with subclasses" do
112
33
  uploader = Class.new(Saviour::BaseUploader) do
113
34
  store_dir { "/store/dir" }
114
- version(:thumb)
115
35
  end
116
36
 
117
37
  klass = Class.new(Test) do
118
38
  include Saviour::Model
119
39
  attach_file :file, uploader
120
40
  end
121
- expect(klass.attached_files).to eq({file: [:thumb]})
41
+ expect(klass.attached_files).to eq([:file])
122
42
 
123
43
  klass2 = Class.new(klass)
124
- expect(klass2.attached_files).to eq({file: [:thumb]})
44
+ expect(klass2.attached_files).to eq([:file])
125
45
 
126
46
  expect(klass2.new.file).to respond_to :exists?
127
47
  end
@@ -18,13 +18,6 @@ describe Saviour::UrlSource do
18
18
  end
19
19
  end
20
20
 
21
- describe "#path" do
22
- it "is extracted from te passed uri" do
23
- a = Saviour::UrlSource.new("http://domain.com/path/file.jpg")
24
- expect(a.path).to eq "/path/file.jpg"
25
- end
26
- end
27
-
28
21
  describe "#read" do
29
22
  it "fails if the uri cannot be accessed" do
30
23
  allow(Net::HTTP).to receive(:get_response).and_return(Net::HTTPNotFound)
metadata CHANGED
@@ -1,73 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saviour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
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-04-10 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: fog-aws
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: mime-types
28
+ name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '4.0'
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: '0'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activerecord
42
+ name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '4.0'
48
- type: :runtime
47
+ version: '0'
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '4.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: activesupport
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '4.0'
62
- type: :runtime
61
+ version: '0'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '4.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: bundler
70
+ name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rspec
84
+ name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rake
98
+ name: appraisal
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: sqlite3
112
+ name: fog-aws
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: appraisal
126
+ name: mime-types
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -155,7 +155,6 @@ files:
155
155
  - gemfiles/4.2.gemfile
156
156
  - gemfiles/5.0.gemfile
157
157
  - lib/saviour.rb
158
- - lib/saviour/attribute_name_calculator.rb
159
158
  - lib/saviour/base_uploader.rb
160
159
  - lib/saviour/config.rb
161
160
  - lib/saviour/file.rb
@@ -167,7 +166,6 @@ files:
167
166
  - lib/saviour/s3_storage.rb
168
167
  - lib/saviour/source_filename_extractor.rb
169
168
  - lib/saviour/string_source.rb
170
- - lib/saviour/uploader/element.rb
171
169
  - lib/saviour/uploader/processors_runner.rb
172
170
  - lib/saviour/uploader/store_dir_extractor.rb
173
171
  - lib/saviour/url_source.rb
@@ -175,12 +173,14 @@ files:
175
173
  - lib/saviour/version.rb
176
174
  - saviour.gemspec
177
175
  - spec/feature/access_to_model_and_mounted_as_spec.rb
176
+ - spec/feature/allow_overriding_attached_as_method.rb
178
177
  - spec/feature/crud_workflows_spec.rb
178
+ - spec/feature/follow_file_spec.rb
179
179
  - spec/feature/persisted_path_spec.rb
180
180
  - spec/feature/reload_model_spec.rb
181
+ - spec/feature/rewind_source_before_read.rb
182
+ - spec/feature/uploader_declaration.rb
181
183
  - spec/feature/validations_spec.rb
182
- - spec/feature/versions_spec.rb
183
- - spec/models/attribute_name_calculator_spec.rb
184
184
  - spec/models/base_uploader_spec.rb
185
185
  - spec/models/config_spec.rb
186
186
  - spec/models/file_spec.rb
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  version: '0'
215
215
  requirements: []
216
216
  rubyforge_project:
217
- rubygems_version: 2.4.5.1
217
+ rubygems_version: 2.5.1
218
218
  signing_key:
219
219
  specification_version: 4
220
220
  summary: File storage handler following active record model lifecycle
@@ -1,15 +0,0 @@
1
- module Saviour
2
- class AttributeNameCalculator
3
- def initialize(attached_as, version = nil)
4
- @attached_as, @version = attached_as, version
5
- end
6
-
7
- def name
8
- if @version
9
- "#{@attached_as}_#{@version}"
10
- else
11
- @attached_as.to_s
12
- end
13
- end
14
- end
15
- end
@@ -1,19 +0,0 @@
1
- module Saviour
2
- module Uploader
3
- class Element
4
- attr_reader :version, :method_or_block
5
-
6
- def initialize(version, method_or_block)
7
- @version, @method_or_block = version, method_or_block
8
- end
9
-
10
- def versioned?
11
- !!@version
12
- end
13
-
14
- def block?
15
- @method_or_block.respond_to?(:call)
16
- end
17
- end
18
- end
19
- end
@@ -1,185 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "saving a new file" 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
-
10
- version(:thumb) do
11
- store_dir { "/versions/store/dir" }
12
- end
13
- end
14
- }
15
-
16
- let(:klass) {
17
- a = Class.new(Test) { include Saviour::Model }
18
- a.attach_file :file, uploader
19
- a
20
- }
21
-
22
- describe "creation following main file" do
23
- it do
24
- with_test_file("example.xml") do |example|
25
- a = klass.create!
26
- expect(a.update_attributes(file: example)).to be_truthy
27
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
28
- end
29
- end
30
- end
31
-
32
- describe "deletion" do
33
- it do
34
- with_test_file("example.xml") do |example|
35
- a = klass.create!
36
- a.update_attributes(file: example)
37
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
38
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
39
-
40
- a.destroy
41
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_falsey
42
- expect(Saviour::Config.storage.exists?(a[:file])).to be_falsey
43
- end
44
- end
45
- end
46
-
47
- describe "changes following main file" do
48
- it do
49
- with_test_file("example.xml") do |example|
50
- a = klass.create!
51
- expect(a.update_attributes(file: example)).to be_truthy
52
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
53
-
54
- with_test_file("camaloon.jpg") do |file|
55
- a.update_attributes(file: file)
56
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
57
- file.rewind
58
- expect(a.file(:thumb).read).to eq file.read
59
- end
60
- end
61
- end
62
- end
63
-
64
- describe "accessing file features directly" do
65
- let(:uploader) {
66
- Class.new(Saviour::BaseUploader) do
67
- store_dir { "/store/dir" }
68
-
69
- version(:thumb) do
70
- store_dir { "/versions/store/dir" }
71
- process { |contents, name| ["#{contents}_for_version_thumb", name] }
72
- end
73
- end
74
- }
75
-
76
- it "#url" do
77
- with_test_file("example.xml") do |example, name|
78
- a = klass.create!
79
- expect(a.update_attributes(file: example)).to be_truthy
80
-
81
- versioned_name = "#{File.basename(name, ".*")}_thumb#{File.extname(name)}"
82
- expect(a.file(:thumb).url).to eq "http://domain.com/versions/store/dir/#{versioned_name}"
83
- end
84
- end
85
-
86
- it "#read" do
87
- with_test_file("text.txt") do |example|
88
- a = klass.create!
89
- a.update_attributes(file: example)
90
-
91
- expect(a.file(:thumb).read).to eq "Hello world\n_for_version_thumb"
92
- end
93
- end
94
-
95
- it "#delete" do
96
- with_test_file("example.xml") do |example|
97
- a = klass.create!
98
- expect(a.update_attributes(file: example)).to be_truthy
99
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
100
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
101
-
102
- a.file(:thumb).delete
103
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_falsey
104
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
105
- end
106
- end
107
-
108
- it "#exists?" do
109
- with_test_file("example.xml") do |example|
110
- a = klass.create!
111
- expect(a.update_attributes(file: example)).to be_truthy
112
- expect(a.file(:thumb).exists?).to be_truthy
113
- end
114
- end
115
- end
116
-
117
- describe "assign specific version after first creation" do
118
- it do
119
- with_test_file("example.xml") do |example|
120
- a = klass.create!
121
- expect(a.update_attributes(file: example)).to be_truthy
122
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
123
- expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(example, ".*")}_thumb.xml"
124
-
125
- with_test_file("camaloon.jpg") do |ex2, filename|
126
- a.file(:thumb).assign(ex2)
127
-
128
- expect(a.save!).to be_truthy
129
-
130
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
131
- expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(filename, ".*")}.jpg"
132
- end
133
- end
134
- end
135
-
136
- context do
137
- let(:uploader) {
138
- Class.new(Saviour::BaseUploader) do
139
- store_dir { "/store/dir" }
140
-
141
- version(:thumb) do
142
- store_dir { "/versions/store/dir" }
143
- process { |_, filename| ["modified_content", filename] }
144
- end
145
- end
146
- }
147
-
148
- it "runs the processors for that version only" do
149
- with_test_file("example.xml") do |example|
150
- a = klass.create!
151
- expect(a.update_attributes(file: example)).to be_truthy
152
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
153
- expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(example, ".*")}_thumb.xml"
154
-
155
- with_test_file("camaloon.jpg") do |ex2, filename|
156
- a.file(:thumb).assign(ex2)
157
-
158
- expect(a.save!).to be_truthy
159
-
160
- expect(Saviour::Config.storage.exists?(a[:file_thumb])).to be_truthy
161
- expect(a[:file_thumb]).to eq "/versions/store/dir/#{File.basename(filename, ".*")}.jpg"
162
- expect(Saviour::Config.storage.read(a[:file_thumb])).to eq "modified_content"
163
- end
164
- end
165
- end
166
- end
167
- end
168
-
169
- describe "respects version assignation vs main file assignation on conflict" do
170
- it do
171
- a = klass.create!
172
-
173
- with_test_file("example.xml") do |file1, fname1|
174
- with_test_file("camaloon.jpg") do |file2, fname2|
175
- a.file.assign(file1)
176
- a.file(:thumb).assign(file2)
177
- a.save!
178
-
179
- expect(a[:file]).to eq "/store/dir/#{fname1}"
180
- expect(a[:file_thumb]).to eq "/versions/store/dir/#{fname2}"
181
- end
182
- end
183
- end
184
- end
185
- end