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
@@ -1,34 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "persisted path" 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
- context "can change the default_path on the uploader and previous instances are not affected" do
7
- it do
8
- uploader = Class.new(Saviour::BaseUploader) { store_dir { "/store/dir" } }
9
- klass = Class.new(Test) { include Saviour }
10
- klass.attach_file :file, uploader
11
-
12
- with_test_file("example.xml") do |example|
13
- a = klass.create!
14
- expect(a.update_attributes(file: example)).to be_truthy
15
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
16
- expect(File.dirname(a[:file])).to eq "/store/dir"
17
-
18
-
19
- uploader.class_eval { store_dir { "/another/dir" } }
20
-
21
- with_test_file("camaloon.jpg") do |example_2|
22
- b = klass.create!
23
- expect(b.update_attributes(file: example_2)).to be_truthy
24
-
25
- expect(Saviour::Config.storage.exists?(b[:file])).to be_truthy
26
- expect(Saviour::Config.storage.exists?(a[:file])).to be_truthy
27
-
28
- expect(File.dirname(b[:file])).to eq "/another/dir"
29
- expect(File.dirname(a[:file])).to eq "/store/dir"
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "reload model" 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
- context "updates the Saviour::File instance" do
7
- it do
8
- uploader = Class.new(Saviour::BaseUploader) { store_dir { "/store/dir" } }
9
- klass = Class.new(Test) { include Saviour }
10
- klass.attach_file :file, uploader
11
- a = klass.create!
12
- b = klass.find(a.id)
13
-
14
- with_test_file("example.xml") do |example|
15
- a.update_attributes! file: example
16
- expect(a.file.exists?).to be_truthy
17
- expect(b.file.exists?).to be_falsey
18
-
19
- b.reload
20
- expect(b.file.exists?).to be_truthy
21
- end
22
- end
23
- end
24
- end
@@ -1,171 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "validations 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(:base_klass) {
13
- a = Class.new(Test) { include Saviour }
14
- a.attach_file :file, uploader
15
- a
16
- }
17
-
18
- it "fails at block validation" do
19
- klass = Class.new(base_klass) do
20
- attach_validation(:file) do |contents, _|
21
- errors.add(:file, "Cannot start with X") if contents[0] == 'X'
22
- end
23
- end
24
-
25
- with_test_file("example.xml") do |example|
26
- allow(example).to receive(:read).and_return("X-Extra contents for the file")
27
- a = klass.new
28
- a.file = example
29
- expect(a).not_to be_valid
30
- expect(a.errors[:file][0]).to eq "Cannot start with X"
31
- end
32
-
33
- with_test_file("example.xml") do |example|
34
- a = klass.new
35
- a.file = example
36
- expect(a).to be_valid
37
- expect(a.save).to be_truthy
38
- end
39
- end
40
-
41
-
42
- it "fails at method validation" do
43
- klass = Class.new(base_klass) do
44
- attach_validation :file, :check_filesize
45
-
46
- def check_filesize(contents, _)
47
- errors.add(:file, "Filesize must be less than 10 bytes") if contents.bytesize >= 10
48
- end
49
- end
50
-
51
- with_test_file("example.xml") do |example|
52
- allow(example).to receive(:read).and_return("1234567890")
53
- a = klass.new
54
- a.file = example
55
- expect(a).not_to be_valid
56
- expect(a.errors[:file][0]).to eq "Filesize must be less than 10 bytes"
57
- end
58
-
59
- with_test_file("example.xml") do |example|
60
- allow(example).to receive(:read).and_return("123456789")
61
- a = klass.new
62
- a.file = example
63
- expect(a).to be_valid
64
- end
65
- end
66
-
67
- it "combined validatinos" do
68
- klass = Class.new(base_klass) do
69
- attach_validation :file, :check_filesize
70
- attach_validation(:file) do |contents, _|
71
- errors.add(:file, "Cannot start with X") if contents[0] == 'X'
72
- end
73
-
74
- def check_filesize(contents, _)
75
- errors.add(:file, "Filesize must be less than 10 bytes") if contents.bytesize >= 10
76
- end
77
- end
78
-
79
- with_test_file("example.xml") do |example|
80
- allow(example).to receive(:read).and_return("X-Ex")
81
- a = klass.new
82
- a.file = example
83
- expect(a).not_to be_valid
84
- expect(a.errors[:file][0]).to eq "Cannot start with X"
85
- end
86
-
87
- with_test_file("example.xml") do |example|
88
- allow(example).to receive(:read).and_return("Ex too long content")
89
- a = klass.new
90
- a.file = example
91
- expect(a).not_to be_valid
92
- expect(a.errors[:file][0]).to eq "Filesize must be less than 10 bytes"
93
- end
94
-
95
- # Consistent order
96
- with_test_file("example.xml") do |example|
97
- allow(example).to receive(:read).and_return("X-Ex too long content")
98
- a = klass.new
99
- a.file = example
100
- expect(a).not_to be_valid
101
- expect(a.errors[:file][0]).to eq "Filesize must be less than 10 bytes"
102
- expect(a.errors[:file][1]).to eq "Cannot start with X"
103
- end
104
- end
105
-
106
- it "validates by filename" do
107
- klass = Class.new(base_klass) do
108
- attach_validation :file, :check_filename
109
-
110
- def check_filename(_, filename)
111
- errors.add(:file, "Only .jpg files") unless filename =~ /\.jpg/
112
- end
113
- end
114
-
115
- with_test_file("example.xml") do |example|
116
- allow(example).to receive(:read).and_return("X-Ex")
117
- a = klass.new
118
- a.file = example
119
- expect(a).not_to be_valid
120
- expect(a.errors[:file][0]).to eq "Only .jpg files"
121
- end
122
-
123
- with_test_file("camaloon.jpg") do |example|
124
- allow(example).to receive(:read).and_return("X-Ex")
125
- a = klass.new
126
- a.file = example
127
- expect(a).to be_valid
128
- end
129
- end
130
-
131
- it "receives the attached_as information" do
132
- klass = Class.new(base_klass) do
133
- attach_validation :file, :check_filename
134
-
135
- def check_filename(_, _, opts)
136
- errors.add(:file, "Received error in #{opts[:attached_as]}")
137
- end
138
- end
139
-
140
- with_test_file("example.xml") do |example|
141
- a = klass.new file: example
142
- expect(a).not_to be_valid
143
- expect(a.errors[:file][0]).to eq "Received error in file"
144
- end
145
- end
146
-
147
- context "versions are validated" do
148
- let(:uploader) {
149
- Class.new(Saviour::BaseUploader) {
150
- store_dir { "/store/dir" }
151
- version(:thumb)
152
- }
153
- }
154
- let(:klass) {
155
- Class.new(base_klass) do
156
- attach_validation(:file) do |contents, _, opts|
157
- errors.add(:file, "Cannot start with X in version #{opts[:version]}") if contents[0] == 'X'
158
- end
159
- end
160
- }
161
-
162
- it do
163
- a = klass.create!
164
- a.file.assign Saviour::StringSource.new("correct contents")
165
- a.file(:thumb).assign Saviour::StringSource.new("X Incorrect contents")
166
-
167
- expect(a).not_to be_valid
168
- expect(a.errors[:file][0]).to eq "Cannot start with X in version thumb"
169
- end
170
- end
171
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Saviour::Processors::Digest do
4
- describe "#digest_filename" do
5
- subject {
6
- Class.new { include Saviour::Processors::Digest }.new
7
- }
8
-
9
- let(:filename) { "name.jpg" }
10
- let(:contents) { "bynary contents for a file" }
11
-
12
- it do
13
- _, new_name = subject.digest_filename(contents, filename)
14
- expect(new_name).to eq "name-ab54d187b7909ff4bba34777073d4654.jpg"
15
- end
16
-
17
- it do
18
- _, new_name = subject.digest_filename(contents, filename, separator: '/')
19
- expect(new_name).to eq "name/ab54d187b7909ff4bba34777073d4654.jpg"
20
- end
21
- end
22
- end
@@ -1,80 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Saviour do
4
- it "raises error if included in a non active record class" do
5
- expect {
6
- Class.new do
7
- include Saviour
8
- end
9
- }.to raise_error(Saviour::NoActiveRecordDetected)
10
- end
11
-
12
- it "error if column not present" do
13
- expect {
14
- Class.new(Test) do
15
- include Saviour
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
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
50
-
51
- attach_file :not_present, Saviour::BaseUploader
52
- end
53
- }.to_not raise_error
54
- end
55
-
56
- describe ".attached_files" do
57
- it "includes a mapping of the currently attached files and their versions" do
58
- uploader = Class.new(Saviour::BaseUploader) do
59
- store_dir { "/store/dir" }
60
-
61
- version(:thumb)
62
- version(:thumb_2)
63
- end
64
-
65
- klass = Class.new(Test) do
66
- include Saviour
67
- attach_file :file, uploader
68
- end
69
-
70
- expect(klass.attached_files).to eq({file: [:thumb, :thumb_2]})
71
-
72
- klass2 = Class.new(Test) do
73
- include Saviour
74
- attach_file :file, Saviour::BaseUploader
75
- end
76
-
77
- expect(klass2.attached_files).to eq({file: []})
78
- end
79
- end
80
- end
@@ -1,9 +0,0 @@
1
- ActiveRecord::Schema.define do
2
- create_table :tests do |t|
3
- t.string :file
4
- t.string :file_thumb
5
- t.string :file_thumb_2
6
- t.string :name
7
- t.timestamps null: false
8
- end
9
- end