saviour 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +4 -13
  4. data/DECOMPOSE.md +66 -0
  5. data/Gemfile +1 -0
  6. data/README.md +39 -8
  7. data/lib/saviour/attribute_name_calculator.rb +15 -0
  8. data/lib/saviour/base_integrator.rb +53 -0
  9. data/lib/saviour/basic_model.rb +7 -0
  10. data/lib/saviour/config.rb +0 -1
  11. data/lib/saviour/file.rb +13 -34
  12. data/lib/saviour/life_cycle.rb +57 -0
  13. data/lib/saviour/source_filename_extractor.rb +21 -0
  14. data/lib/saviour/url_source.rb +1 -1
  15. data/lib/saviour/utils/class_attribute.rb +26 -0
  16. data/lib/saviour/version.rb +1 -1
  17. data/lib/saviour.rb +7 -155
  18. data/saviour.gemspec +1 -5
  19. data/spec/feature/access_to_model_and_mounted_as_spec.rb +13 -5
  20. data/spec/feature/versions_spec.rb +72 -49
  21. data/spec/models/attribute_name_calculator_spec.rb +11 -0
  22. data/spec/models/basic_model_spec.rb +51 -0
  23. data/spec/models/file_spec.rb +32 -55
  24. data/spec/models/url_source_spec.rb +5 -5
  25. data/spec/spec_helper.rb +2 -30
  26. data/spec/support/models.rb +7 -2
  27. metadata +12 -72
  28. data/Appraisals +0 -19
  29. data/gemfiles/4.0.gemfile +0 -9
  30. data/gemfiles/4.1.gemfile +0 -9
  31. data/gemfiles/4.2.gemfile +0 -9
  32. data/gemfiles/5.0.gemfile +0 -9
  33. data/lib/saviour/processors/digest.rb +0 -16
  34. data/spec/feature/crud_workflows_spec.rb +0 -143
  35. data/spec/feature/persisted_path_spec.rb +0 -34
  36. data/spec/feature/reload_model_spec.rb +0 -24
  37. data/spec/feature/validations_spec.rb +0 -171
  38. data/spec/models/processors/digest_spec.rb +0 -22
  39. data/spec/models/saviour_spec.rb +0 -80
  40. data/spec/support/schema.rb +0 -9
@@ -25,69 +25,35 @@ describe Saviour::File do
25
25
 
26
26
  let(:example_file) { double(read: "some file contents", path: "/my/path") }
27
27
 
28
- describe "initialization" do
29
- describe "derives persisted from the model" do
30
- it do
31
- file = Saviour::File.new(uploader_klass, Test.new, :file)
32
- expect(file).not_to be_persisted
33
- end
34
-
35
- it do
36
- file = Saviour::File.new(uploader_klass, Test.create!, :file)
37
- expect(file).not_to be_persisted
38
- end
39
-
40
- it do
41
- file = Saviour::File.new(uploader_klass, Test.create!(file: "/mocked/path"), :file)
42
- expect(file).to be_persisted
43
- end
44
- end
45
-
46
- describe "is not changed" do
47
- it do
48
- file = Saviour::File.new(uploader_klass, Test.new, :file)
49
- expect(file).not_to be_changed
50
- end
51
-
52
- it do
53
- file = Saviour::File.new(uploader_klass, Test.create!, :file)
54
- expect(file).not_to be_changed
55
- end
56
-
57
- it do
58
- file = Saviour::File.new(uploader_klass, Test.create!(file: "/mocked/path"), :file)
59
- expect(file).not_to be_changed
60
- end
61
- end
62
- end
28
+ let(:dummy_class) { Class.new }
63
29
 
64
30
  describe "#assign" do
65
31
  it "returns the assigned object" do
66
- file = Saviour::File.new(uploader_klass, Test.new, :file)
32
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
67
33
  expect(file.assign(example_file)).to eq example_file
68
34
  end
69
35
 
70
36
  it "allows to reset the internal source" do
71
- file = Saviour::File.new(uploader_klass, Test.new, :file)
37
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
72
38
  file.assign(example_file)
73
39
  expect(file.assign(nil)).to be_nil
74
40
  end
75
41
 
76
42
  it "shows error if assigned object do not respond to :read" do
77
- file = Saviour::File.new(uploader_klass, Test.new, :file)
43
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
78
44
  expect { file.assign(6) }.to raise_error(RuntimeError)
79
45
  end
80
46
  end
81
47
 
82
48
  describe "#write" do
83
49
  it "fails without source" do
84
- file = Saviour::File.new(uploader_klass, Test.new, :file)
50
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
85
51
  expect { file.write }.to raise_error(RuntimeError)
86
52
  end
87
53
 
88
54
  describe "filename used" do
89
55
  it "is extracted from original_filename if possible" do
90
- file = Saviour::File.new(uploader_klass, Test.new, :file)
56
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
91
57
  file.assign(double(read: "contents", original_filename: 'original.jpg', path: "/my/path/my_file.zip"))
92
58
  uploader = double
93
59
  allow(file).to receive(:uploader).and_return(uploader)
@@ -96,7 +62,7 @@ describe Saviour::File do
96
62
  end
97
63
 
98
64
  it "is extracted from path if possible" do
99
- file = Saviour::File.new(uploader_klass, Test.new, :file)
65
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
100
66
  file.assign(double(read: "contents", path: "/my/path/my_file.zip"))
101
67
  uploader = double
102
68
  allow(file).to receive(:uploader).and_return(uploader)
@@ -105,7 +71,7 @@ describe Saviour::File do
105
71
  end
106
72
 
107
73
  it "is random if cannot be guessed" do
108
- file = Saviour::File.new(uploader_klass, Test.new, :file)
74
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
109
75
  file.assign(double(read: "contents"))
110
76
  allow(SecureRandom).to receive(:hex).and_return("stubbed-random")
111
77
  uploader = double
@@ -116,7 +82,7 @@ describe Saviour::File do
116
82
  end
117
83
 
118
84
  it "returns the path" do
119
- object = Test.new
85
+ object = dummy_class.new
120
86
  file = Saviour::File.new(uploader_klass, object, :file)
121
87
  file.assign(double(read: "contents", path: "/my/path/my_file.zip"))
122
88
  uploader = double
@@ -128,7 +94,7 @@ describe Saviour::File do
128
94
 
129
95
  describe "#changed?" do
130
96
  it "is cleared after removing the assignation" do
131
- file = Saviour::File.new(uploader_klass, Test.new, :file)
97
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
132
98
  expect(file).not_to be_changed
133
99
  file.assign(example_file)
134
100
  expect(file).to be_changed
@@ -137,7 +103,7 @@ describe Saviour::File do
137
103
  end
138
104
 
139
105
  it "is cleared after persisting" do
140
- file = Saviour::File.new(uploader_klass, Test.new, :file)
106
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
141
107
  file.assign(double(read: "contents", path: "/my/path/my_file.zip"))
142
108
  expect(file).to be_changed
143
109
 
@@ -152,14 +118,21 @@ describe Saviour::File do
152
118
 
153
119
  describe "#filename" do
154
120
  it "returns the filename of the persisted file" do
155
- file = Saviour::File.new(uploader_klass, Test.create!(file: "/mocked/path/file.rar"), :file)
121
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
122
+ file.set_path! "/mocked/path/file.rar"
156
123
  expect(file.filename).to eq "file.rar"
157
124
  end
125
+
126
+ it "returns nil if the file doesn't exist" do
127
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
128
+ expect(file.filename).to be_nil
129
+ end
158
130
  end
159
131
 
160
132
  describe "#with_copy" do
161
133
  it "provides a copy of the stored file" do
162
- file = Saviour::File.new(uploader_klass, Test.create!(file: "/mocked/path/file.rar"), :file)
134
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
135
+ file.set_path! "/path/file.jpg"
163
136
  allow(file).to receive(:read).and_return("some contents")
164
137
 
165
138
  file.with_copy do |tmpfile|
@@ -168,15 +141,17 @@ describe Saviour::File do
168
141
  end
169
142
 
170
143
  it "deletes the copied file even on exception" do
171
- file = Saviour::File.new(uploader_klass, Test.create!(file: "/mocked/path/file.rar"), :file)
144
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
145
+ file.set_path! "/path/file.jpg"
172
146
  allow(file).to receive(:read).and_return("some contents")
147
+
173
148
  mocked_tmpfile = double(binmode: "", rewind: "", flush: "", write: "")
174
149
  allow(Tempfile).to receive(:open).and_yield(mocked_tmpfile)
175
150
 
176
151
  expect(mocked_tmpfile).to receive(:close)
177
152
  expect(mocked_tmpfile).to receive(:delete)
178
153
 
179
- test_exception = Class.new(Exception)
154
+ test_exception = Class.new(StandardError)
180
155
 
181
156
  begin
182
157
  file.with_copy { |_| raise(test_exception, "some exception within the block") }
@@ -187,23 +162,25 @@ describe Saviour::File do
187
162
 
188
163
  describe "#blank?" do
189
164
  it "it's true when not yet assigned nor persisted" do
190
- file = Saviour::File.new(uploader_klass, Test.new, :file)
165
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
191
166
  expect(file).to be_blank
192
167
  end
193
168
 
194
- it "it's true when not yet assigned but persisted" do
195
- file = Saviour::File.new(uploader_klass, Test.create!, :file)
196
- expect(file).to be_blank
169
+ it "it's false when not yet assigned but persisted" do
170
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
171
+ file.set_path! "/path/dummy.jpg"
172
+ expect(file).not_to be_blank
197
173
  end
198
174
 
199
175
  it "it's false when not persisted but assigned" do
200
- file = Saviour::File.new(uploader_klass, Test.new, :file)
176
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
201
177
  file.assign example_file
202
178
  expect(file).not_to be_blank
203
179
  end
204
180
 
205
181
  it "it's false when persisted and assigned" do
206
- file = Saviour::File.new(uploader_klass, Test.create!(file: "/mocked/path/file.rar"), :file)
182
+ file = Saviour::File.new(uploader_klass, dummy_class.new, :file)
183
+ file.set_path! "/path/dummy.jpg"
207
184
  expect(file).not_to be_blank
208
185
  end
209
186
  end
@@ -36,18 +36,18 @@ describe Saviour::UrlSource do
36
36
  it "retries the request 3 times on error" do
37
37
  expect(Net::HTTP).to receive(:get_response).and_return(Net::HTTPNotFound, Net::HTTPNotFound)
38
38
  expect(Net::HTTP).to receive(:get_response).and_call_original
39
- a = Saviour::UrlSource.new("http://example.com/")
39
+ a = Saviour::UrlSource.new("http://example.org/")
40
40
  expect(a.read.length).to be > 100
41
41
  end
42
42
 
43
43
  it "succeds if the uri is valid" do
44
- a = Saviour::UrlSource.new("http://example.com/")
44
+ a = Saviour::UrlSource.new("http://example.org/")
45
45
  expect(a.read.length).to be > 100
46
46
  end
47
47
 
48
48
  it "follows redirects" do
49
49
  response = Net::HTTPRedirection.new "1.1", "301", "Redirect"
50
- expect(response).to receive(:[]).with("location").and_return("http://example.com")
50
+ expect(response).to receive(:[]).with("location").and_return("http://example.org")
51
51
 
52
52
  expect(Net::HTTP).to receive(:get_response).and_return(response)
53
53
  expect(Net::HTTP).to receive(:get_response).and_call_original
@@ -58,7 +58,7 @@ describe Saviour::UrlSource do
58
58
 
59
59
  it "does not follow more than 10 redirects" do
60
60
  response = Net::HTTPRedirection.new "1.1", "301", "Redirect"
61
- expect(response).to receive(:[]).with("location").exactly(10).times.and_return("http://example.com")
61
+ expect(response).to receive(:[]).with("location").exactly(10).times.and_return("http://example.org")
62
62
  expect(Net::HTTP).to receive(:get_response).exactly(10).times.and_return(response)
63
63
 
64
64
  expect { Saviour::UrlSource.new("http://faked.blabla").read }.to raise_error(RuntimeError).with_message(/Max number of allowed redirects reached \(10\) when resolving/)
@@ -66,7 +66,7 @@ describe Saviour::UrlSource do
66
66
 
67
67
  it "fails if the redirected location is not a valid URI" do
68
68
  response = Net::HTTPRedirection.new "1.1", "301", "Redirect"
69
- expect(response).to receive(:[]).with("location").and_return("http://example.com/%@(&#<<<<")
69
+ expect(response).to receive(:[]).with("location").and_return("http://example.org/%@(&#<<<<")
70
70
 
71
71
  expect(Net::HTTP).to receive(:get_response).and_return(response)
72
72
 
data/spec/spec_helper.rb CHANGED
@@ -1,39 +1,11 @@
1
- require "codeclimate-test-reporter"
2
- CodeClimate::TestReporter.start
1
+ require 'simplecov'
2
+ SimpleCov.start
3
3
 
4
4
  require 'bundler/setup'
5
5
  require 'rspec'
6
- require 'active_record'
7
- require 'sqlite3'
8
- require 'logger'
9
6
 
10
7
  require File.expand_path("../../lib/saviour", __FILE__)
11
8
 
12
- connection_opts = case ENV.fetch('DB', "sqlite")
13
- when "sqlite"
14
- {adapter: "sqlite3", database: ":memory:"}
15
- when "mysql"
16
- {adapter: "mysql2", database: "saviour", username: "root", encoding: "utf8"}
17
- when "postgres"
18
- {adapter: "postgresql", database: "saviour", username: "postgres"}
19
- end
20
-
21
- ActiveRecord::Base.establish_connection(connection_opts)
22
-
23
- ActiveRecord::Base.logger = Logger.new(STDOUT) if ENV['DEBUG']
24
-
25
- def silence_stream(stream)
26
- old_stream = stream.dup
27
- stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
28
- stream.sync = true
29
- yield
30
- ensure
31
- stream.reopen(old_stream)
32
- old_stream.close
33
- end
34
-
35
- silence_stream(STDOUT) { require 'support/schema' }
36
-
37
9
  require 'support/models'
38
10
 
39
11
  RSpec.configure do |config|
@@ -1,3 +1,8 @@
1
- class Test < ActiveRecord::Base
1
+ # Constant lookup in ruby works by lexical scope, so we can't create classes dynamically to test this.
2
+ class TestForSaviourFileResolution
3
+ include Saviour::BasicModel
2
4
 
3
- end
5
+ def foo
6
+ File.file?("/tasdasdasdmp/blabla.txt")
7
+ end
8
+ end
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saviour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.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: 2016-07-24 00:00:00.000000000 Z
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: activerecord
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '4.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '4.0'
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '4.0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '4.0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: fog-aws
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -108,34 +80,6 @@ dependencies:
108
80
  - - ">="
109
81
  - !ruby/object:Gem::Version
110
82
  version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: sqlite3
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: appraisal
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
83
  description: File storage handler following active record model lifecycle
140
84
  email:
141
85
  - roger@rogercampos.com
@@ -145,49 +89,45 @@ extra_rdoc_files: []
145
89
  files:
146
90
  - ".gitignore"
147
91
  - ".travis.yml"
148
- - Appraisals
92
+ - DECOMPOSE.md
149
93
  - Gemfile
150
94
  - LICENSE.txt
151
95
  - README.md
152
96
  - Rakefile
153
- - gemfiles/4.0.gemfile
154
- - gemfiles/4.1.gemfile
155
- - gemfiles/4.2.gemfile
156
- - gemfiles/5.0.gemfile
157
97
  - lib/saviour.rb
98
+ - lib/saviour/attribute_name_calculator.rb
99
+ - lib/saviour/base_integrator.rb
158
100
  - lib/saviour/base_uploader.rb
101
+ - lib/saviour/basic_model.rb
159
102
  - lib/saviour/config.rb
160
103
  - lib/saviour/file.rb
104
+ - lib/saviour/life_cycle.rb
161
105
  - lib/saviour/local_storage.rb
162
- - lib/saviour/processors/digest.rb
163
106
  - lib/saviour/s3_storage.rb
107
+ - lib/saviour/source_filename_extractor.rb
164
108
  - lib/saviour/string_source.rb
165
109
  - lib/saviour/uploader/element.rb
166
110
  - lib/saviour/uploader/processors_runner.rb
167
111
  - lib/saviour/uploader/store_dir_extractor.rb
168
112
  - lib/saviour/url_source.rb
113
+ - lib/saviour/utils/class_attribute.rb
169
114
  - lib/saviour/version.rb
170
115
  - saviour.gemspec
171
116
  - spec/feature/access_to_model_and_mounted_as_spec.rb
172
- - spec/feature/crud_workflows_spec.rb
173
- - spec/feature/persisted_path_spec.rb
174
- - spec/feature/reload_model_spec.rb
175
- - spec/feature/validations_spec.rb
176
117
  - spec/feature/versions_spec.rb
118
+ - spec/models/attribute_name_calculator_spec.rb
177
119
  - spec/models/base_uploader_spec.rb
120
+ - spec/models/basic_model_spec.rb
178
121
  - spec/models/config_spec.rb
179
122
  - spec/models/file_spec.rb
180
123
  - spec/models/local_storage_spec.rb
181
- - spec/models/processors/digest_spec.rb
182
124
  - spec/models/s3_storage_spec.rb
183
- - spec/models/saviour_spec.rb
184
125
  - spec/models/url_source_spec.rb
185
126
  - spec/spec_helper.rb
186
127
  - spec/support/data/camaloon.jpg
187
128
  - spec/support/data/example.xml
188
129
  - spec/support/data/text.txt
189
130
  - spec/support/models.rb
190
- - spec/support/schema.rb
191
131
  homepage: https://github.com/rogercampos/saviour
192
132
  licenses:
193
133
  - MIT
@@ -200,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
140
  requirements:
201
141
  - - ">="
202
142
  - !ruby/object:Gem::Version
203
- version: 2.0.0
143
+ version: 2.1.0
204
144
  required_rubygems_version: !ruby/object:Gem::Requirement
205
145
  requirements:
206
146
  - - ">="
data/Appraisals DELETED
@@ -1,19 +0,0 @@
1
- appraise "4.0" do
2
- gem "activesupport", "~> 4.0.0"
3
- gem "activerecord", "~> 4.0.0"
4
- end
5
-
6
- appraise "4.1" do
7
- gem "activesupport", "~> 4.1.0"
8
- gem "activerecord", "~> 4.1.0"
9
- end
10
-
11
- appraise "4.2" do
12
- gem "activesupport", "~> 4.2.0"
13
- gem "activerecord", "~> 4.2.0"
14
- end
15
-
16
- appraise "5.0" do
17
- gem "activesupport", "~> 5.0.0"
18
- gem "activerecord", "~> 5.0.0"
19
- end
data/gemfiles/4.0.gemfile DELETED
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "codeclimate-test-reporter", :group => :test, :require => nil
6
- gem "activesupport", "~> 4.0.0"
7
- gem "activerecord", "~> 4.0.0"
8
-
9
- gemspec :path => "../"
data/gemfiles/4.1.gemfile DELETED
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "codeclimate-test-reporter", :group => :test, :require => nil
6
- gem "activesupport", "~> 4.1.0"
7
- gem "activerecord", "~> 4.1.0"
8
-
9
- gemspec :path => "../"
data/gemfiles/4.2.gemfile DELETED
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "codeclimate-test-reporter", :group => :test, :require => nil
6
- gem "activesupport", "~> 4.2.0"
7
- gem "activerecord", "~> 4.2.0"
8
-
9
- gemspec :path => "../"
data/gemfiles/5.0.gemfile DELETED
@@ -1,9 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "codeclimate-test-reporter", :group => :test, :require => nil
6
- gem "activesupport", "~> 5.0.0"
7
- gem "activerecord", "~> 5.0.0"
8
-
9
- gemspec :path => "../"
@@ -1,16 +0,0 @@
1
- module Saviour
2
- module Processors
3
- module Digest
4
- def digest_filename(contents, filename, opts = {})
5
- separator = opts.fetch(:separator, "-")
6
-
7
- digest = ::Digest::MD5.hexdigest(contents)
8
- extension = ::File.extname(filename)
9
-
10
- new_filename = "#{[::File.basename(filename, ".*"), digest].join(separator)}#{extension}"
11
-
12
- [contents, new_filename]
13
- end
14
- end
15
- end
16
- end
@@ -1,143 +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) {
8
- store_dir { "/store/dir" }
9
- }
10
- }
11
-
12
- let(:klass) {
13
- a = Class.new(Test) { include Saviour }
14
- a.attach_file :file, uploader
15
- a
16
- }
17
-
18
- describe "creation" do
19
- it do
20
- with_test_file("example.xml") do |example|
21
- a = klass.create!
22
- expect(a.update_attributes(file: example)).to be_truthy
23
- end
24
- end
25
-
26
- it do
27
- with_test_file("example.xml") do |example|
28
- a = klass.create!
29
- a.update_attributes(file: example)
30
-
31
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
32
- end
33
- end
34
-
35
- it do
36
- with_test_file("example.xml") do |example, real_filename|
37
- a = klass.create!
38
- a.update_attributes(file: example)
39
- expect(a[:file]).to eq "/store/dir/#{real_filename}"
40
- end
41
- end
42
-
43
- it do
44
- with_test_file("example.xml") do |example|
45
- a = klass.create!
46
- a.update_attributes(file: example)
47
-
48
- example.rewind
49
- expect(a.file.read).to eq example.read
50
- end
51
- end
52
-
53
- it do
54
- with_test_file("example.xml") do |example|
55
- a = klass.create!
56
- a.update_attributes(file: example)
57
-
58
- expect(a.file.exists?).to be_truthy
59
- end
60
- end
61
-
62
- it do
63
- with_test_file("example.xml") do |example, real_filename|
64
- a = klass.create!
65
- a.update_attributes(file: example)
66
-
67
- expect(a.file.filename).to eq real_filename
68
- end
69
- end
70
-
71
- it do
72
- with_test_file("example.xml") do |example, real_filename|
73
- a = klass.create!
74
- a.update_attributes(file: example)
75
-
76
- expect(a.file.url).to eq "http://domain.com/store/dir/#{real_filename}"
77
- expect(a.file.public_url).to eq a.file.url
78
- end
79
- end
80
-
81
- it "don't create anything if save do not completes (halt during before_save)" do
82
- klass = Class.new(Test) do
83
- attr_accessor :fail_at_save
84
- before_save {
85
- if ActiveRecord.version >= Gem::Version.new("5.0")
86
- throw(:abort) if fail_at_save
87
- else
88
- !fail_at_save
89
- end
90
- }
91
- include Saviour
92
- end
93
- klass.attach_file :file, uploader
94
-
95
- expect {
96
- a = klass.new
97
- a.fail_at_save = true
98
- a.save!
99
- }.to raise_error(ActiveRecord::RecordNotSaved)
100
-
101
- with_test_file("example.xml") do |example, _|
102
- a = klass.new
103
- a.fail_at_save = true
104
- a.file = example
105
-
106
- expect(Saviour::Config.storage).not_to receive(:write)
107
- a.save
108
- end
109
- end
110
- end
111
-
112
- describe "deletion" do
113
- it do
114
- with_test_file("example.xml") do |example|
115
- a = klass.create!
116
- a.update_attributes(file: example)
117
- expect(a.file.exists?).to be_truthy
118
- expect(a.destroy).to be_truthy
119
-
120
- expect(Saviour::Config.storage.exists?(a[:file])).to be_falsey
121
- end
122
- end
123
- end
124
-
125
- describe "updating" do
126
- it do
127
- with_test_file("example.xml") do |example|
128
- a = klass.create!
129
- a.update_attributes(file: example)
130
-
131
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
132
- previous_location = a[:file]
133
-
134
- with_test_file("camaloon.jpg") do |example_2|
135
- a.update_attributes(file: example_2)
136
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
137
-
138
- expect(Saviour::Config.storage.exists?(previous_location)).to be_falsey
139
- end
140
- end
141
- end
142
- end
143
- end