paperclip 3.5.4 → 5.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +17 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +17 -20
- data/Appraisals +4 -16
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +11 -3
- data/LICENSE +1 -3
- data/NEWS +241 -49
- data/README.md +471 -166
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +27 -8
- data/features/migration.feature +0 -24
- data/features/step_definitions/attachment_steps.rb +36 -28
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +68 -37
- data/features/step_definitions/s3_steps.rb +2 -2
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/file_helpers.rb +2 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +2 -25
- data/gemfiles/4.2.gemfile +17 -0
- data/gemfiles/5.0.gemfile +17 -0
- data/lib/generators/paperclip/paperclip_generator.rb +9 -3
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
- data/lib/paperclip/attachment.rb +151 -46
- data/lib/paperclip/attachment_registry.rb +3 -2
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +26 -22
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/geometry_detector_factory.rb +5 -3
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -1
- data/lib/paperclip/has_attached_file.rb +17 -1
- data/lib/paperclip/helpers.rb +15 -11
- data/lib/paperclip/interpolations/plural_cache.rb +6 -5
- data/lib/paperclip/interpolations.rb +26 -13
- data/lib/paperclip/io_adapters/abstract_adapter.rb +32 -4
- data/lib/paperclip/io_adapters/attachment_adapter.rb +13 -8
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
- data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +7 -7
- data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
- data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
- data/lib/paperclip/io_adapters/registry.rb +6 -2
- data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
- data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
- data/lib/paperclip/io_adapters/uri_adapter.rb +41 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/media_type_spoof_detector.rb +90 -0
- data/lib/paperclip/processor.rb +5 -41
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +9 -7
- data/lib/paperclip/storage/filesystem.rb +14 -3
- data/lib/paperclip/storage/fog.rb +37 -19
- data/lib/paperclip/storage/s3.rb +129 -69
- data/lib/paperclip/style.rb +8 -2
- data/lib/paperclip/tempfile_factory.rb +5 -1
- data/lib/paperclip/thumbnail.rb +30 -18
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
- data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
- data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
- data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
- data/lib/paperclip/validators.rb +11 -4
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +30 -11
- data/lib/tasks/paperclip.rake +34 -5
- data/paperclip.gemspec +21 -16
- data/shoulda_macros/paperclip.rb +0 -1
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -20
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +502 -407
- data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +14 -0
- data/spec/paperclip/geometry_detector_spec.rb +39 -0
- data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
- data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
- data/spec/paperclip/glue_spec.rb +44 -0
- data/spec/paperclip/has_attached_file_spec.rb +158 -0
- data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +137 -128
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +70 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +132 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
- data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
- data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
- data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +121 -0
- data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
- data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
- data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
- data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
- data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +172 -0
- data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
- data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
- data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
- data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
- data/spec/paperclip/media_type_spoof_detector_spec.rb +94 -0
- data/spec/paperclip/meta_class_spec.rb +30 -0
- data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
- data/spec/paperclip/paperclip_spec.rb +192 -0
- data/spec/paperclip/plural_cache_spec.rb +37 -0
- data/spec/paperclip/processor_helpers_spec.rb +57 -0
- data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
- data/spec/paperclip/rails_environment_spec.rb +33 -0
- data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
- data/spec/paperclip/schema_spec.rb +248 -0
- data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
- data/spec/paperclip/storage/fog_spec.rb +566 -0
- data/spec/paperclip/storage/s3_live_spec.rb +188 -0
- data/spec/paperclip/storage/s3_spec.rb +1693 -0
- data/spec/paperclip/style_spec.rb +255 -0
- data/spec/paperclip/tempfile_factory_spec.rb +33 -0
- data/spec/paperclip/tempfile_spec.rb +35 -0
- data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +153 -134
- data/spec/paperclip/url_generator_spec.rb +222 -0
- data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
- data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
- data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
- data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
- data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
- data/spec/paperclip/validators_spec.rb +164 -0
- data/spec/spec_helper.rb +47 -0
- data/spec/support/assertions.rb +82 -0
- data/spec/support/conditional_filter_helper.rb +5 -0
- data/spec/support/fake_model.rb +25 -0
- data/spec/support/fake_rails.rb +12 -0
- data/spec/support/fixtures/empty.html +1 -0
- data/spec/support/fixtures/empty.xlsx +0 -0
- data/spec/support/fixtures/spaced file.jpg +0 -0
- data/spec/support/matchers/accept.rb +5 -0
- data/spec/support/matchers/exist.rb +5 -0
- data/spec/support/matchers/have_column.rb +23 -0
- data/{test → spec}/support/mock_attachment.rb +2 -0
- data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
- data/spec/support/model_reconstruction.rb +68 -0
- data/spec/support/reporting.rb +11 -0
- data/spec/support/test_data.rb +13 -0
- data/spec/support/version_helper.rb +9 -0
- metadata +348 -225
- data/RUNNING_TESTS.md +0 -4
- data/cucumber/paperclip_steps.rb +0 -6
- data/gemfiles/3.0.gemfile +0 -11
- data/gemfiles/3.1.gemfile +0 -11
- data/gemfiles/3.2.gemfile +0 -11
- data/gemfiles/4.0.gemfile +0 -11
- data/test/attachment_definitions_test.rb +0 -12
- data/test/attachment_registry_test.rb +0 -88
- data/test/file_command_content_type_detector_test.rb +0 -27
- data/test/filename_cleaner_test.rb +0 -14
- data/test/generator_test.rb +0 -84
- data/test/geometry_detector_test.rb +0 -24
- data/test/has_attached_file_test.rb +0 -125
- data/test/helper.rb +0 -232
- data/test/io_adapters/abstract_adapter_test.rb +0 -58
- data/test/io_adapters/data_uri_adapter_test.rb +0 -74
- data/test/io_adapters/empty_string_adapter_test.rb +0 -18
- data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/uri_adapter_test.rb +0 -102
- data/test/matchers/have_attached_file_matcher_test.rb +0 -24
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
- data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
- data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
- data/test/meta_class_test.rb +0 -32
- data/test/paperclip_missing_attachment_styles_test.rb +0 -90
- data/test/paperclip_test.rb +0 -217
- data/test/plural_cache_test.rb +0 -36
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -473
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1356
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -17
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -324
- data/test/validators_test.rb +0 -61
- /data/{test → spec}/database.yml +0 -0
- /data/{test → spec/support}/fixtures/12k.png +0 -0
- /data/{test → spec/support}/fixtures/50x50.png +0 -0
- /data/{test → spec/support}/fixtures/5k.png +0 -0
- /data/{test → spec/support}/fixtures/animated +0 -0
- /data/{test → spec/support}/fixtures/animated.gif +0 -0
- /data/{test → spec/support}/fixtures/animated.unknown +0 -0
- /data/{test → spec/support}/fixtures/bad.png +0 -0
- /data/{test → spec/support}/fixtures/fog.yml +0 -0
- /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
- /data/{test → spec/support}/fixtures/s3.yml +0 -0
- /data/{test → spec/support}/fixtures/spaced file.png +0 -0
- /data/{test → spec/support}/fixtures/text.txt +0 -0
- /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
- /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
- /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -1,205 +1,204 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require '
|
3
|
-
require 'paperclip/attachment'
|
2
|
+
require 'spec_helper'
|
4
3
|
|
5
|
-
|
4
|
+
describe Paperclip::Attachment do
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
context "when file not set" do
|
16
|
-
should "not be present" do
|
17
|
-
assert @dummy.avatar.blank?
|
18
|
-
refute @dummy.avatar.present?
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context "when file set" do
|
23
|
-
setup { @dummy.avatar = File.new(fixture_file("50x50.png"), "rb") }
|
6
|
+
it "is not present when file not set" do
|
7
|
+
rebuild_class
|
8
|
+
dummy = Dummy.new
|
9
|
+
expect(dummy.avatar).to be_blank
|
10
|
+
expect(dummy.avatar).to_not be_present
|
11
|
+
end
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
13
|
+
it "is present when the file is set" do
|
14
|
+
rebuild_class
|
15
|
+
dummy = Dummy.new
|
16
|
+
dummy.avatar = File.new(fixture_file("50x50.png"), "rb")
|
17
|
+
expect(dummy.avatar).to_not be_blank
|
18
|
+
expect(dummy.avatar).to be_present
|
30
19
|
end
|
31
20
|
|
32
|
-
|
21
|
+
it "processes :original style first" do
|
33
22
|
file = File.new(fixture_file("50x50.png"), 'rb')
|
34
|
-
rebuild_class :
|
23
|
+
rebuild_class styles: { small: '100x>', original: '42x42#' }
|
35
24
|
dummy = Dummy.new
|
36
25
|
dummy.avatar = file
|
37
26
|
dummy.save
|
38
27
|
|
39
28
|
# :small avatar should be 42px wide (processed original), not 50px (preprocessed original)
|
40
|
-
|
29
|
+
expect(`identify -format "%w" "#{dummy.avatar.path(:small)}"`.strip).to eq "42"
|
41
30
|
|
42
31
|
file.close
|
43
32
|
end
|
44
33
|
|
45
|
-
|
34
|
+
it "does not delete styles that don't get reprocessed" do
|
46
35
|
file = File.new(fixture_file("50x50.png"), 'rb')
|
47
|
-
rebuild_class :
|
48
|
-
|
49
|
-
|
36
|
+
rebuild_class styles: {
|
37
|
+
small: "100x>",
|
38
|
+
large: "500x>",
|
39
|
+
original: "42x42#"
|
40
|
+
}
|
41
|
+
|
50
42
|
dummy = Dummy.new
|
51
43
|
dummy.avatar = file
|
52
44
|
dummy.save
|
53
45
|
|
54
|
-
|
55
|
-
|
56
|
-
|
46
|
+
expect(dummy.avatar.path(:small)).to exist
|
47
|
+
expect(dummy.avatar.path(:large)).to exist
|
48
|
+
expect(dummy.avatar.path(:original)).to exist
|
57
49
|
|
58
50
|
dummy.avatar.reprocess!(:small)
|
59
51
|
|
60
|
-
|
61
|
-
|
62
|
-
|
52
|
+
expect(dummy.avatar.path(:small)).to exist
|
53
|
+
expect(dummy.avatar.path(:large)).to exist
|
54
|
+
expect(dummy.avatar.path(:original)).to exist
|
63
55
|
end
|
64
56
|
|
65
57
|
context "having a not empty hash as a default option" do
|
66
|
-
|
58
|
+
before do
|
67
59
|
@old_default_options = Paperclip::Attachment.default_options.dup
|
68
|
-
@new_default_options = { :
|
60
|
+
@new_default_options = { convert_options: { all: "-background white" } }
|
69
61
|
Paperclip::Attachment.default_options.merge!(@new_default_options)
|
70
62
|
end
|
71
63
|
|
72
|
-
|
64
|
+
after do
|
73
65
|
Paperclip::Attachment.default_options.merge!(@old_default_options)
|
74
66
|
end
|
75
67
|
|
76
|
-
|
77
|
-
new_options = { :
|
68
|
+
it "deep merges when it is overridden" do
|
69
|
+
new_options = { convert_options: { thumb: "-thumbnailize" } }
|
78
70
|
attachment = Paperclip::Attachment.new(:name, :instance, new_options)
|
79
71
|
|
80
|
-
|
81
|
-
attachment.instance_variable_get("@options")
|
72
|
+
expect(Paperclip::Attachment.default_options.deep_merge(new_options)).to eq attachment.instance_variable_get("@options")
|
82
73
|
end
|
83
74
|
end
|
84
75
|
|
85
|
-
|
76
|
+
it "handles a boolean second argument to #url" do
|
86
77
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
87
|
-
attachment = Paperclip::Attachment.new(
|
78
|
+
attachment = Paperclip::Attachment.new(
|
79
|
+
:name,
|
80
|
+
FakeModel.new,
|
81
|
+
url_generator: mock_url_generator_builder
|
82
|
+
)
|
88
83
|
|
89
84
|
attachment.url(:style_name, true)
|
90
|
-
|
85
|
+
expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: true, escape: true)).to eq true
|
91
86
|
|
92
87
|
attachment.url(:style_name, false)
|
93
|
-
|
88
|
+
expect(mock_url_generator_builder.has_generated_url_with_options?(timestamp: false, escape: true)).to eq true
|
94
89
|
end
|
95
90
|
|
96
|
-
|
91
|
+
it "passes the style and options through to the URL generator on #url" do
|
97
92
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
98
|
-
attachment = Paperclip::Attachment.new(
|
99
|
-
|
100
|
-
|
101
|
-
|
93
|
+
attachment = Paperclip::Attachment.new(
|
94
|
+
:name,
|
95
|
+
FakeModel.new,
|
96
|
+
url_generator: mock_url_generator_builder
|
97
|
+
)
|
98
|
+
|
99
|
+
attachment.url(:style_name, options: :values)
|
100
|
+
expect(mock_url_generator_builder.has_generated_url_with_options?(options: :values)).to eq true
|
102
101
|
end
|
103
102
|
|
104
|
-
|
103
|
+
it "passes default options through when #url is given one argument" do
|
105
104
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
106
105
|
attachment = Paperclip::Attachment.new(:name,
|
107
|
-
|
108
|
-
:
|
109
|
-
:
|
106
|
+
FakeModel.new,
|
107
|
+
url_generator: mock_url_generator_builder,
|
108
|
+
use_timestamp: true)
|
110
109
|
|
111
110
|
attachment.url(:style_name)
|
112
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
111
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
113
112
|
end
|
114
113
|
|
115
|
-
|
114
|
+
it "passes default style and options through when #url is given no arguments" do
|
116
115
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
117
116
|
attachment = Paperclip::Attachment.new(:name,
|
118
|
-
|
119
|
-
:
|
120
|
-
:
|
121
|
-
:
|
117
|
+
FakeModel.new,
|
118
|
+
default_style: 'default style',
|
119
|
+
url_generator: mock_url_generator_builder,
|
120
|
+
use_timestamp: true)
|
122
121
|
|
123
122
|
attachment.url
|
124
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
123
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
125
124
|
assert mock_url_generator_builder.has_generated_url_with_style_name?('default style')
|
126
125
|
end
|
127
126
|
|
128
|
-
|
127
|
+
it "passes the option timestamp: true if :use_timestamp is true and :timestamp is not passed" do
|
129
128
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
130
129
|
attachment = Paperclip::Attachment.new(:name,
|
131
|
-
|
132
|
-
:
|
133
|
-
:
|
130
|
+
FakeModel.new,
|
131
|
+
url_generator: mock_url_generator_builder,
|
132
|
+
use_timestamp: true)
|
134
133
|
|
135
134
|
attachment.url(:style_name)
|
136
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
135
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
137
136
|
end
|
138
137
|
|
139
|
-
|
138
|
+
it "passes the option timestamp: false if :use_timestamp is false and :timestamp is not passed" do
|
140
139
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
141
140
|
attachment = Paperclip::Attachment.new(:name,
|
142
|
-
|
143
|
-
:
|
144
|
-
:
|
141
|
+
FakeModel.new,
|
142
|
+
url_generator: mock_url_generator_builder,
|
143
|
+
use_timestamp: false)
|
145
144
|
|
146
145
|
attachment.url(:style_name)
|
147
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
146
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: false)
|
148
147
|
end
|
149
148
|
|
150
|
-
|
149
|
+
it "does not change the :timestamp if :timestamp is passed" do
|
151
150
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
152
151
|
attachment = Paperclip::Attachment.new(:name,
|
153
|
-
|
154
|
-
:
|
155
|
-
:
|
152
|
+
FakeModel.new,
|
153
|
+
url_generator: mock_url_generator_builder,
|
154
|
+
use_timestamp: false)
|
156
155
|
|
157
|
-
attachment.url(:style_name, :
|
158
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
156
|
+
attachment.url(:style_name, timestamp: true)
|
157
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true, timestamp: true)
|
159
158
|
end
|
160
159
|
|
161
|
-
|
160
|
+
it "renders JSON as default style" do
|
162
161
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
163
162
|
attachment = Paperclip::Attachment.new(:name,
|
164
|
-
|
165
|
-
:
|
166
|
-
:
|
163
|
+
FakeModel.new,
|
164
|
+
default_style: 'default style',
|
165
|
+
url_generator: mock_url_generator_builder)
|
167
166
|
|
168
|
-
|
167
|
+
attachment.as_json
|
169
168
|
assert mock_url_generator_builder.has_generated_url_with_style_name?('default style')
|
170
169
|
end
|
171
170
|
|
172
|
-
|
171
|
+
it "passes the option escape: true if :escape_url is true and :escape is not passed" do
|
173
172
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
174
173
|
attachment = Paperclip::Attachment.new(:name,
|
175
|
-
|
176
|
-
:
|
177
|
-
:
|
174
|
+
FakeModel.new,
|
175
|
+
url_generator: mock_url_generator_builder,
|
176
|
+
escape_url: true)
|
178
177
|
|
179
178
|
attachment.url(:style_name)
|
180
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
179
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: true)
|
181
180
|
end
|
182
181
|
|
183
|
-
|
182
|
+
it "passes the option escape: false if :escape_url is false and :escape is not passed" do
|
184
183
|
mock_url_generator_builder = MockUrlGeneratorBuilder.new
|
185
184
|
attachment = Paperclip::Attachment.new(:name,
|
186
|
-
|
187
|
-
:
|
188
|
-
:
|
185
|
+
FakeModel.new,
|
186
|
+
url_generator: mock_url_generator_builder,
|
187
|
+
escape_url: false)
|
189
188
|
|
190
189
|
attachment.url(:style_name)
|
191
|
-
assert mock_url_generator_builder.has_generated_url_with_options?(:
|
190
|
+
assert mock_url_generator_builder.has_generated_url_with_options?(escape: false)
|
192
191
|
end
|
193
192
|
|
194
|
-
|
195
|
-
@attachment = attachment :
|
193
|
+
it "returns the path based on the url by default" do
|
194
|
+
@attachment = attachment url: "/:class/:id/:basename"
|
196
195
|
@model = @attachment.instance
|
197
196
|
@model.id = 1234
|
198
197
|
@model.avatar_file_name = "fake.jpg"
|
199
198
|
assert_equal "#{Rails.root}/public/fake_models/1234/fake", @attachment.path
|
200
199
|
end
|
201
200
|
|
202
|
-
|
201
|
+
it "defaults to a path that scales" do
|
203
202
|
avatar_attachment = attachment
|
204
203
|
model = avatar_attachment.instance
|
205
204
|
model.id = 1234
|
@@ -208,7 +207,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
208
207
|
assert_equal expected_path, avatar_attachment.path
|
209
208
|
end
|
210
209
|
|
211
|
-
|
210
|
+
it "renders JSON as the URL to the attachment" do
|
212
211
|
avatar_attachment = attachment
|
213
212
|
model = avatar_attachment.instance
|
214
213
|
model.id = 1234
|
@@ -216,35 +215,33 @@ class AttachmentTest < Test::Unit::TestCase
|
|
216
215
|
assert_equal attachment.url, attachment.as_json
|
217
216
|
end
|
218
217
|
|
219
|
-
|
218
|
+
it "renders JSON from the model when requested by :methods" do
|
220
219
|
rebuild_model
|
221
220
|
dummy = Dummy.new
|
222
221
|
dummy.id = 1234
|
223
222
|
dummy.avatar_file_name = "fake.jpg"
|
223
|
+
dummy.stubs(:new_record?).returns(false)
|
224
224
|
expected_string = '{"avatar":"/system/dummies/avatars/000/001/234/original/fake.jpg"}'
|
225
|
-
if ActiveRecord::Base.include_root_in_json # This is true by default in Rails 3, and false in 4
|
226
|
-
expected_string = %({"dummy":#{expected_string}})
|
227
|
-
end
|
228
225
|
# active_model pre-3.2 checks only by calling any? on it, thus it doesn't work if it is empty
|
229
|
-
assert_equal expected_string, dummy.to_json(:
|
226
|
+
assert_equal expected_string, dummy.to_json(only: [:dummy_key_for_old_active_model], methods: [:avatar])
|
230
227
|
end
|
231
228
|
|
232
229
|
context "Attachment default_options" do
|
233
|
-
|
230
|
+
before do
|
234
231
|
rebuild_model
|
235
232
|
@old_default_options = Paperclip::Attachment.default_options.dup
|
236
233
|
@new_default_options = @old_default_options.merge({
|
237
|
-
:
|
238
|
-
:
|
239
|
-
:
|
234
|
+
path: "argle/bargle",
|
235
|
+
url: "fooferon",
|
236
|
+
default_url: "not here.png"
|
240
237
|
})
|
241
238
|
end
|
242
239
|
|
243
|
-
|
240
|
+
after do
|
244
241
|
Paperclip::Attachment.default_options.merge! @old_default_options
|
245
242
|
end
|
246
243
|
|
247
|
-
|
244
|
+
it "is overrideable" do
|
248
245
|
Paperclip::Attachment.default_options.merge!(@new_default_options)
|
249
246
|
@new_default_options.keys.each do |key|
|
250
247
|
assert_equal @new_default_options[key],
|
@@ -253,41 +250,46 @@ class AttachmentTest < Test::Unit::TestCase
|
|
253
250
|
end
|
254
251
|
|
255
252
|
context "without an Attachment" do
|
256
|
-
|
253
|
+
before do
|
254
|
+
rebuild_model default_url: "default.url"
|
257
255
|
@dummy = Dummy.new
|
258
256
|
end
|
259
257
|
|
260
|
-
|
258
|
+
it "returns false when asked exists?" do
|
261
259
|
assert !@dummy.avatar.exists?
|
262
260
|
end
|
261
|
+
|
262
|
+
it "#url returns the default_url" do
|
263
|
+
expect(@dummy.avatar.url).to eq "default.url"
|
264
|
+
end
|
263
265
|
end
|
264
266
|
|
265
267
|
context "on an Attachment" do
|
266
|
-
|
268
|
+
before do
|
267
269
|
@dummy = Dummy.new
|
268
270
|
@attachment = @dummy.avatar
|
269
271
|
end
|
270
272
|
|
271
273
|
Paperclip::Attachment.default_options.keys.each do |key|
|
272
|
-
|
274
|
+
it "is the default_options for #{key}" do
|
273
275
|
assert_equal @old_default_options[key],
|
274
276
|
@attachment.instance_variable_get("@options")[key],
|
275
|
-
key
|
277
|
+
key.to_s
|
276
278
|
end
|
277
279
|
end
|
278
280
|
|
279
281
|
context "when redefined" do
|
280
|
-
|
282
|
+
before do
|
281
283
|
Paperclip::Attachment.default_options.merge!(@new_default_options)
|
282
284
|
@dummy = Dummy.new
|
283
285
|
@attachment = @dummy.avatar
|
284
286
|
end
|
285
287
|
|
286
288
|
Paperclip::Attachment.default_options.keys.each do |key|
|
287
|
-
|
289
|
+
it "is the new default_options for #{key}" do
|
288
290
|
assert_equal @new_default_options[key],
|
289
291
|
@attachment.instance_variable_get("@options")[key],
|
290
|
-
key
|
292
|
+
key.to_s
|
291
293
|
end
|
292
294
|
end
|
293
295
|
end
|
@@ -295,23 +297,23 @@ class AttachmentTest < Test::Unit::TestCase
|
|
295
297
|
end
|
296
298
|
|
297
299
|
context "An attachment with similarly named interpolations" do
|
298
|
-
|
299
|
-
rebuild_model :
|
300
|
+
before do
|
301
|
+
rebuild_model path: ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
|
300
302
|
@dummy = Dummy.new
|
301
303
|
@dummy.stubs(:id).returns(1024)
|
302
304
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
303
305
|
@dummy.avatar = @file
|
304
306
|
end
|
305
307
|
|
306
|
-
|
308
|
+
after { @file.close }
|
307
309
|
|
308
|
-
|
310
|
+
it "makes sure that they are interpolated correctly" do
|
309
311
|
assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
|
310
312
|
end
|
311
313
|
end
|
312
314
|
|
313
315
|
context "An attachment with :timestamp interpolations" do
|
314
|
-
|
316
|
+
before do
|
315
317
|
@file = StringIO.new("...")
|
316
318
|
@zone = 'UTC'
|
317
319
|
Time.stubs(:zone).returns(@zone)
|
@@ -320,78 +322,77 @@ class AttachmentTest < Test::Unit::TestCase
|
|
320
322
|
end
|
321
323
|
|
322
324
|
context "using default time zone" do
|
323
|
-
|
324
|
-
rebuild_model :
|
325
|
+
before do
|
326
|
+
rebuild_model path: ":timestamp", use_default_time_zone: true
|
325
327
|
@dummy = Dummy.new
|
326
328
|
@dummy.avatar = @file
|
327
329
|
end
|
328
330
|
|
329
|
-
|
331
|
+
it "returns a time in the default zone" do
|
330
332
|
assert_equal @dummy.avatar_updated_at.in_time_zone(@zone_default).to_s, @dummy.avatar.path
|
331
333
|
end
|
332
334
|
end
|
333
335
|
|
334
336
|
context "using per-thread time zone" do
|
335
|
-
|
336
|
-
rebuild_model :
|
337
|
+
before do
|
338
|
+
rebuild_model path: ":timestamp", use_default_time_zone: false
|
337
339
|
@dummy = Dummy.new
|
338
340
|
@dummy.avatar = @file
|
339
341
|
end
|
340
342
|
|
341
|
-
|
343
|
+
it "returns a time in the per-thread zone" do
|
342
344
|
assert_equal @dummy.avatar_updated_at.in_time_zone(@zone).to_s, @dummy.avatar.path
|
343
345
|
end
|
344
346
|
end
|
345
347
|
end
|
346
348
|
|
347
349
|
context "An attachment with :hash interpolations" do
|
348
|
-
|
349
|
-
@file =
|
350
|
+
before do
|
351
|
+
@file = File.open(fixture_file("5k.png"))
|
352
|
+
end
|
353
|
+
|
354
|
+
after do
|
355
|
+
@file.close
|
350
356
|
end
|
351
357
|
|
352
|
-
|
353
|
-
|
358
|
+
it "raises if no secret is provided" do
|
359
|
+
rebuild_model path: ":hash"
|
360
|
+
@attachment = Dummy.new.avatar
|
354
361
|
@attachment.assign @file
|
355
362
|
|
356
|
-
|
363
|
+
assert_raises ArgumentError do
|
357
364
|
@attachment.path
|
358
365
|
end
|
359
366
|
end
|
360
367
|
|
361
368
|
context "when secret is set" do
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
@attachment
|
369
|
+
before do
|
370
|
+
rebuild_model path: ":hash",
|
371
|
+
hash_secret: "w00t",
|
372
|
+
hash_data: ":class/:attachment/:style/:filename"
|
373
|
+
@attachment = Dummy.new.avatar
|
367
374
|
@attachment.assign @file
|
368
375
|
end
|
369
376
|
|
370
|
-
|
371
|
-
|
372
|
-
|
377
|
+
it "results in the correct interpolation" do
|
378
|
+
assert_equal "dummies/avatars/original/5k.png",
|
379
|
+
@attachment.send(:interpolate, @attachment.options[:hash_data])
|
380
|
+
assert_equal "dummies/avatars/thumb/5k.png",
|
381
|
+
@attachment.send(:interpolate, @attachment.options[:hash_data], :thumb)
|
373
382
|
end
|
374
383
|
|
375
|
-
|
376
|
-
assert_equal "
|
377
|
-
|
378
|
-
|
379
|
-
should "result in a correct hash" do
|
380
|
-
assert_equal "d22b617d1bf10016aa7d046d16427ae203f39fce", @attachment.path
|
381
|
-
end
|
382
|
-
|
383
|
-
should "generate a hash digest with the correct style" do
|
384
|
-
OpenSSL::HMAC.expects(:hexdigest).with(anything, anything, "fake_models/avatars/1234/medium/1234567890")
|
385
|
-
@attachment.path("medium")
|
384
|
+
it "results in a correct hash" do
|
385
|
+
assert_equal "0a59e9142bba11576de1d353d8747b1acad5ad34", @attachment.path
|
386
|
+
assert_equal "b39a062c1e62e85a6c785ed00cf3bebf5f850e2b", @attachment.path(:thumb)
|
386
387
|
end
|
387
388
|
end
|
388
389
|
end
|
389
390
|
|
390
391
|
context "An attachment with a :rails_env interpolation" do
|
391
|
-
|
392
|
+
before do
|
392
393
|
@rails_env = "blah"
|
393
394
|
@id = 1024
|
394
|
-
rebuild_model :
|
395
|
+
rebuild_model path: ":rails_env/:id.png"
|
395
396
|
@dummy = Dummy.new
|
396
397
|
@dummy.stubs(:id).returns(@id)
|
397
398
|
@file = StringIO.new(".")
|
@@ -399,83 +400,84 @@ class AttachmentTest < Test::Unit::TestCase
|
|
399
400
|
Rails.stubs(:env).returns(@rails_env)
|
400
401
|
end
|
401
402
|
|
402
|
-
|
403
|
+
it "returns the proper path" do
|
403
404
|
assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
|
404
405
|
end
|
405
406
|
end
|
406
407
|
|
407
408
|
context "An attachment with a default style and an extension interpolation" do
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
@
|
413
|
-
@file.
|
409
|
+
before do
|
410
|
+
rebuild_model path: ":basename.:extension",
|
411
|
+
styles: { default: ["100x100", :jpg] },
|
412
|
+
default_style: :default
|
413
|
+
@attachment = Dummy.new.avatar
|
414
|
+
@file = File.open(fixture_file("5k.png"))
|
415
|
+
@file.stubs(:original_filename).returns("file.png")
|
414
416
|
end
|
415
|
-
|
417
|
+
it "returns the right extension for the path" do
|
416
418
|
@attachment.assign(@file)
|
417
|
-
assert_equal "file.
|
419
|
+
assert_equal "file.jpg", @attachment.path
|
418
420
|
end
|
419
421
|
end
|
420
422
|
|
421
423
|
context "An attachment with :convert_options" do
|
422
|
-
|
423
|
-
rebuild_model :
|
424
|
-
:
|
425
|
-
:
|
424
|
+
before do
|
425
|
+
rebuild_model styles: {
|
426
|
+
thumb: "100x100",
|
427
|
+
large: "400x400"
|
426
428
|
},
|
427
|
-
:
|
428
|
-
:
|
429
|
-
:
|
429
|
+
convert_options: {
|
430
|
+
all: "-do_stuff",
|
431
|
+
thumb: "-thumbnailize"
|
430
432
|
}
|
431
433
|
@dummy = Dummy.new
|
432
434
|
@dummy.avatar
|
433
435
|
end
|
434
436
|
|
435
|
-
|
437
|
+
it "reports the correct options when sent #extra_options_for(:thumb)" do
|
436
438
|
assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
|
437
439
|
end
|
438
440
|
|
439
|
-
|
441
|
+
it "reports the correct options when sent #extra_options_for(:large)" do
|
440
442
|
assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
|
441
443
|
end
|
442
444
|
end
|
443
445
|
|
444
446
|
context "An attachment with :source_file_options" do
|
445
|
-
|
446
|
-
rebuild_model :
|
447
|
-
:
|
448
|
-
:
|
447
|
+
before do
|
448
|
+
rebuild_model styles: {
|
449
|
+
thumb: "100x100",
|
450
|
+
large: "400x400"
|
449
451
|
},
|
450
|
-
:
|
451
|
-
:
|
452
|
-
:
|
452
|
+
source_file_options: {
|
453
|
+
all: "-density 400",
|
454
|
+
thumb: "-depth 8"
|
453
455
|
}
|
454
456
|
@dummy = Dummy.new
|
455
457
|
@dummy.avatar
|
456
458
|
end
|
457
459
|
|
458
|
-
|
460
|
+
it "reports the correct options when sent #extra_source_file_options_for(:thumb)" do
|
459
461
|
assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.source_file_options.inspect
|
460
462
|
end
|
461
463
|
|
462
|
-
|
464
|
+
it "reports the correct options when sent #extra_source_file_options_for(:large)" do
|
463
465
|
assert_equal "-density 400", @dummy.avatar.send(:extra_source_file_options_for, :large)
|
464
466
|
end
|
465
467
|
end
|
466
468
|
|
467
469
|
context "An attachment with :only_process" do
|
468
|
-
|
469
|
-
rebuild_model :
|
470
|
-
:
|
471
|
-
:
|
470
|
+
before do
|
471
|
+
rebuild_model styles: {
|
472
|
+
thumb: "100x100",
|
473
|
+
large: "400x400"
|
472
474
|
},
|
473
|
-
:
|
475
|
+
only_process: [:thumb]
|
474
476
|
@file = StringIO.new("...")
|
475
477
|
@attachment = Dummy.new.avatar
|
476
478
|
end
|
477
479
|
|
478
|
-
|
480
|
+
it "only processes the provided style" do
|
479
481
|
@attachment.expects(:post_process).with(:thumb)
|
480
482
|
@attachment.expects(:post_process).with(:large).never
|
481
483
|
@attachment.assign(@file)
|
@@ -483,33 +485,34 @@ class AttachmentTest < Test::Unit::TestCase
|
|
483
485
|
end
|
484
486
|
|
485
487
|
context "An attachment with :only_process that is a proc" do
|
486
|
-
|
487
|
-
rebuild_model :
|
488
|
-
:
|
489
|
-
:
|
488
|
+
before do
|
489
|
+
rebuild_model styles: {
|
490
|
+
thumb: "100x100",
|
491
|
+
large: "400x400"
|
490
492
|
},
|
491
|
-
:
|
493
|
+
only_process: lambda { |attachment| [:thumb] }
|
492
494
|
|
493
495
|
@file = StringIO.new("...")
|
494
496
|
@attachment = Dummy.new.avatar
|
495
497
|
end
|
496
498
|
|
497
|
-
|
499
|
+
it "only processes the provided style" do
|
498
500
|
@attachment.expects(:post_process).with(:thumb)
|
499
501
|
@attachment.expects(:post_process).with(:large).never
|
500
502
|
@attachment.assign(@file)
|
503
|
+
@attachment.save
|
501
504
|
end
|
502
505
|
end
|
503
506
|
|
504
507
|
context "An attachment with :convert_options that is a proc" do
|
505
|
-
|
506
|
-
rebuild_model :
|
507
|
-
:
|
508
|
-
:
|
508
|
+
before do
|
509
|
+
rebuild_model styles: {
|
510
|
+
thumb: "100x100",
|
511
|
+
large: "400x400"
|
509
512
|
},
|
510
|
-
:
|
511
|
-
:
|
512
|
-
:
|
513
|
+
convert_options: {
|
514
|
+
all: lambda{|i| i.all },
|
515
|
+
thumb: lambda{|i| i.thumb }
|
513
516
|
}
|
514
517
|
Dummy.class_eval do
|
515
518
|
def all; "-all"; end
|
@@ -519,54 +522,54 @@ class AttachmentTest < Test::Unit::TestCase
|
|
519
522
|
@dummy.avatar
|
520
523
|
end
|
521
524
|
|
522
|
-
|
525
|
+
it "reports the correct options when sent #extra_options_for(:thumb)" do
|
523
526
|
assert_equal "-thumb -all", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
|
524
527
|
end
|
525
528
|
|
526
|
-
|
529
|
+
it "reports the correct options when sent #extra_options_for(:large)" do
|
527
530
|
assert_equal "-all", @dummy.avatar.send(:extra_options_for, :large)
|
528
531
|
end
|
529
532
|
end
|
530
533
|
|
531
534
|
context "An attachment with :path that is a proc" do
|
532
|
-
|
533
|
-
rebuild_model :
|
535
|
+
before do
|
536
|
+
rebuild_model path: lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
|
534
537
|
|
535
538
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
536
|
-
@dummyA = Dummy.new(:
|
539
|
+
@dummyA = Dummy.new(other: 'a')
|
537
540
|
@dummyA.avatar = @file
|
538
|
-
@dummyB = Dummy.new(:
|
541
|
+
@dummyB = Dummy.new(other: 'b')
|
539
542
|
@dummyB.avatar = @file
|
540
543
|
end
|
541
544
|
|
542
|
-
|
545
|
+
after { @file.close }
|
543
546
|
|
544
|
-
|
547
|
+
it "returns correct path" do
|
545
548
|
assert_equal "path/a.png", @dummyA.avatar.path
|
546
549
|
assert_equal "path/b.png", @dummyB.avatar.path
|
547
550
|
end
|
548
551
|
end
|
549
552
|
|
550
553
|
context "An attachment with :styles that is a proc" do
|
551
|
-
|
552
|
-
rebuild_model :
|
554
|
+
before do
|
555
|
+
rebuild_model styles: lambda{ |attachment| {thumb: "50x50#", large: "400x400"} }
|
553
556
|
|
554
557
|
@attachment = Dummy.new.avatar
|
555
558
|
end
|
556
559
|
|
557
|
-
|
560
|
+
it "has the correct geometry" do
|
558
561
|
assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
|
559
562
|
end
|
560
563
|
end
|
561
564
|
|
562
565
|
context "An attachment with conditional :styles that is a proc" do
|
563
|
-
|
564
|
-
rebuild_model :
|
566
|
+
before do
|
567
|
+
rebuild_model styles: lambda{ |attachment| attachment.instance.other == 'a' ? {thumb: "50x50#"} : {large: "400x400"} }
|
565
568
|
|
566
|
-
@dummy = Dummy.new(:
|
569
|
+
@dummy = Dummy.new(other: 'a')
|
567
570
|
end
|
568
571
|
|
569
|
-
|
572
|
+
it "has the correct styles for the assigned instance values" do
|
570
573
|
assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
|
571
574
|
assert_nil @dummy.avatar.styles[:large]
|
572
575
|
|
@@ -580,22 +583,22 @@ class AttachmentTest < Test::Unit::TestCase
|
|
580
583
|
geometry_specs = [
|
581
584
|
[ lambda{|z| "50x50#" }, :png ],
|
582
585
|
lambda{|z| "50x50#" },
|
583
|
-
{ :
|
586
|
+
{ geometry: lambda{|z| "50x50#" } }
|
584
587
|
]
|
585
588
|
geometry_specs.each do |geometry_spec|
|
586
589
|
context "An attachment geometry like #{geometry_spec}" do
|
587
|
-
|
588
|
-
rebuild_model :
|
590
|
+
before do
|
591
|
+
rebuild_model styles: { normal: geometry_spec }
|
589
592
|
@attachment = Dummy.new.avatar
|
590
593
|
end
|
591
594
|
|
592
595
|
context "when assigned" do
|
593
|
-
|
596
|
+
before do
|
594
597
|
@file = StringIO.new(".")
|
595
598
|
@attachment.assign(@file)
|
596
599
|
end
|
597
600
|
|
598
|
-
|
601
|
+
it "has the correct geometry" do
|
599
602
|
assert_equal "50x50#", @attachment.styles[:normal][:geometry]
|
600
603
|
end
|
601
604
|
end
|
@@ -603,64 +606,89 @@ class AttachmentTest < Test::Unit::TestCase
|
|
603
606
|
end
|
604
607
|
|
605
608
|
context "An attachment with both 'normal' and hash-style styles" do
|
606
|
-
|
607
|
-
rebuild_model :
|
608
|
-
:
|
609
|
-
:
|
609
|
+
before do
|
610
|
+
rebuild_model styles: {
|
611
|
+
normal: ["50x50#", :png],
|
612
|
+
hash: { geometry: "50x50#", format: :png }
|
610
613
|
}
|
611
614
|
@dummy = Dummy.new
|
612
615
|
@attachment = @dummy.avatar
|
613
616
|
end
|
614
617
|
|
615
618
|
[:processors, :whiny, :convert_options, :geometry, :format].each do |field|
|
616
|
-
|
619
|
+
it "has the same #{field} field" do
|
617
620
|
assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
|
618
621
|
end
|
619
622
|
end
|
620
623
|
end
|
621
624
|
|
622
625
|
context "An attachment with :processors that is a proc" do
|
623
|
-
|
626
|
+
before do
|
624
627
|
class Paperclip::Test < Paperclip::Processor; end
|
625
628
|
@file = StringIO.new("...")
|
626
629
|
Paperclip::Test.stubs(:make).returns(@file)
|
627
630
|
|
628
|
-
rebuild_model :
|
631
|
+
rebuild_model styles: { normal: '' }, processors: lambda { |a| [ :test ] }
|
629
632
|
@attachment = Dummy.new.avatar
|
630
633
|
end
|
631
634
|
|
632
635
|
context "when assigned" do
|
633
|
-
|
636
|
+
before do
|
634
637
|
@attachment.assign(StringIO.new("."))
|
635
638
|
end
|
636
639
|
|
637
|
-
|
640
|
+
it "has the correct processors" do
|
638
641
|
assert_equal [ :test ], @attachment.styles[:normal][:processors]
|
639
642
|
end
|
640
643
|
end
|
641
644
|
end
|
642
645
|
|
643
646
|
context "An attachment with erroring processor" do
|
644
|
-
|
645
|
-
rebuild_model :
|
647
|
+
before do
|
648
|
+
rebuild_model processor: [:thumbnail], styles: { small: '' }, whiny_thumbnails: true
|
646
649
|
@dummy = Dummy.new
|
647
|
-
Paperclip::Thumbnail.expects(:make).raises(Paperclip::Error, "cannot be processed.")
|
648
650
|
@file = StringIO.new("...")
|
649
651
|
@file.stubs(:to_tempfile).returns(@file)
|
650
|
-
@dummy.avatar = @file
|
651
652
|
end
|
652
653
|
|
653
|
-
|
654
|
-
|
655
|
-
|
654
|
+
context "when error is meaningful for the end user" do
|
655
|
+
before do
|
656
|
+
Paperclip::Thumbnail.expects(:make).raises(
|
657
|
+
Paperclip::Errors::NotIdentifiedByImageMagickError,
|
658
|
+
"cannot be processed."
|
659
|
+
)
|
660
|
+
end
|
661
|
+
|
662
|
+
it "correctly forwards processing error message to the instance" do
|
663
|
+
@dummy.avatar = @file
|
664
|
+
@dummy.valid?
|
665
|
+
assert_contains(
|
666
|
+
@dummy.errors.full_messages,
|
667
|
+
"Avatar cannot be processed."
|
668
|
+
)
|
669
|
+
end
|
670
|
+
end
|
671
|
+
|
672
|
+
context "when error is intended for the developer" do
|
673
|
+
before do
|
674
|
+
Paperclip::Thumbnail.expects(:make).raises(
|
675
|
+
Paperclip::Errors::CommandNotFoundError
|
676
|
+
)
|
677
|
+
end
|
678
|
+
|
679
|
+
it "propagates the error" do
|
680
|
+
assert_raises(Paperclip::Errors::CommandNotFoundError) do
|
681
|
+
@dummy.avatar = @file
|
682
|
+
end
|
683
|
+
end
|
656
684
|
end
|
657
685
|
end
|
658
686
|
|
659
687
|
context "An attachment with multiple processors" do
|
660
|
-
|
688
|
+
before do
|
661
689
|
class Paperclip::Test < Paperclip::Processor; end
|
662
|
-
@style_params = { :
|
663
|
-
rebuild_model :
|
690
|
+
@style_params = { once: {one: 1, two: 2} }
|
691
|
+
rebuild_model processors: [:thumbnail, :test], styles: @style_params
|
664
692
|
@dummy = Dummy.new
|
665
693
|
@file = StringIO.new("...")
|
666
694
|
@file.stubs(:close)
|
@@ -669,56 +697,87 @@ class AttachmentTest < Test::Unit::TestCase
|
|
669
697
|
end
|
670
698
|
|
671
699
|
context "when assigned" do
|
672
|
-
|
700
|
+
it "calls #make on all specified processors" do
|
701
|
+
@dummy.avatar = @file
|
673
702
|
|
674
|
-
|
675
|
-
Paperclip::
|
676
|
-
Paperclip::Test.expects(:make).with(any_parameters).returns(@file)
|
703
|
+
expect(Paperclip::Thumbnail).to have_received(:make)
|
704
|
+
expect(Paperclip::Test).to have_received(:make)
|
677
705
|
end
|
678
706
|
|
679
|
-
|
707
|
+
it "calls #make with the right parameters passed as second argument" do
|
680
708
|
expected_params = @style_params[:once].merge({
|
681
|
-
:
|
682
|
-
:
|
683
|
-
:
|
684
|
-
:
|
709
|
+
style: :once,
|
710
|
+
processors: [:thumbnail, :test],
|
711
|
+
whiny: true,
|
712
|
+
convert_options: "",
|
713
|
+
source_file_options: ""
|
685
714
|
})
|
686
|
-
|
715
|
+
|
716
|
+
@dummy.avatar = @file
|
717
|
+
|
718
|
+
expect(Paperclip::Thumbnail).to have_received(:make).with(anything, expected_params, anything)
|
687
719
|
end
|
688
720
|
|
689
|
-
|
690
|
-
|
721
|
+
it "calls #make with attachment passed as third argument" do
|
722
|
+
@dummy.avatar = @file
|
723
|
+
|
724
|
+
expect(Paperclip::Test).to have_received(:make).with(anything, anything, @dummy.avatar)
|
725
|
+
end
|
726
|
+
|
727
|
+
it "calls #make and unlinks intermediary files afterward" do
|
728
|
+
@dummy.avatar.expects(:unlink_files).with([@file, @file])
|
729
|
+
|
730
|
+
@dummy.avatar = @file
|
731
|
+
end
|
732
|
+
end
|
733
|
+
end
|
734
|
+
|
735
|
+
context "An attachment with a processor that returns original file" do
|
736
|
+
before do
|
737
|
+
class Paperclip::Test < Paperclip::Processor
|
738
|
+
def make; @file; end
|
739
|
+
end
|
740
|
+
rebuild_model processors: [:test], styles: { once: "100x100" }
|
741
|
+
@file = StringIO.new("...")
|
742
|
+
@file.stubs(:close)
|
743
|
+
@dummy = Dummy.new
|
744
|
+
end
|
745
|
+
|
746
|
+
context "when assigned" do
|
747
|
+
it "#calls #make and doesn't unlink the original file" do
|
748
|
+
@dummy.avatar.expects(:unlink_files).with([])
|
749
|
+
|
750
|
+
@dummy.avatar = @file
|
691
751
|
end
|
692
752
|
end
|
693
753
|
end
|
694
754
|
|
695
|
-
|
696
|
-
rebuild_model :
|
755
|
+
it "includes the filesystem module when loading the filesystem storage" do
|
756
|
+
rebuild_model storage: :filesystem
|
697
757
|
@dummy = Dummy.new
|
698
758
|
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
699
759
|
end
|
700
760
|
|
701
|
-
|
702
|
-
rebuild_model :
|
761
|
+
it "includes the filesystem module even if capitalization is wrong" do
|
762
|
+
rebuild_model storage: :FileSystem
|
703
763
|
@dummy = Dummy.new
|
704
764
|
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
705
765
|
|
706
|
-
rebuild_model :
|
766
|
+
rebuild_model storage: :Filesystem
|
707
767
|
@dummy = Dummy.new
|
708
768
|
assert @dummy.avatar.is_a?(Paperclip::Storage::Filesystem)
|
709
769
|
end
|
710
770
|
|
711
|
-
|
712
|
-
rebuild_model :
|
771
|
+
it "converts underscored storage name to camelcase" do
|
772
|
+
rebuild_model storage: :not_here
|
713
773
|
@dummy = Dummy.new
|
714
|
-
exception = assert_raises(Paperclip::Errors::StorageMethodNotFound) do
|
774
|
+
exception = assert_raises(Paperclip::Errors::StorageMethodNotFound, /NotHere/) do
|
715
775
|
@dummy.avatar
|
716
776
|
end
|
717
|
-
assert exception.message.include?("NotHere")
|
718
777
|
end
|
719
778
|
|
720
|
-
|
721
|
-
rebuild_model :
|
779
|
+
it "raises an error if you try to include a storage module that doesn't exist" do
|
780
|
+
rebuild_model storage: :not_here
|
722
781
|
@dummy = Dummy.new
|
723
782
|
assert_raises(Paperclip::Errors::StorageMethodNotFound) do
|
724
783
|
@dummy.avatar
|
@@ -726,30 +785,30 @@ class AttachmentTest < Test::Unit::TestCase
|
|
726
785
|
end
|
727
786
|
|
728
787
|
context "An attachment with styles but no processors defined" do
|
729
|
-
|
730
|
-
rebuild_model :
|
788
|
+
before do
|
789
|
+
rebuild_model processors: [], styles: {something: '1'}
|
731
790
|
@dummy = Dummy.new
|
732
791
|
@file = StringIO.new("...")
|
733
792
|
end
|
734
|
-
|
793
|
+
it "raises when assigned to" do
|
735
794
|
assert_raises(RuntimeError){ @dummy.avatar = @file }
|
736
795
|
end
|
737
796
|
end
|
738
797
|
|
739
798
|
context "An attachment without styles and with no processors defined" do
|
740
|
-
|
741
|
-
rebuild_model :
|
799
|
+
before do
|
800
|
+
rebuild_model processors: [], styles: {}
|
742
801
|
@dummy = Dummy.new
|
743
802
|
@file = StringIO.new("...")
|
744
803
|
end
|
745
|
-
|
804
|
+
it "does not raise when assigned to" do
|
746
805
|
@dummy.avatar = @file
|
747
806
|
end
|
748
807
|
end
|
749
808
|
|
750
809
|
context "Assigning an attachment with post_process hooks" do
|
751
|
-
|
752
|
-
rebuild_class :
|
810
|
+
before do
|
811
|
+
rebuild_class styles: { something: "100x100#" }
|
753
812
|
Dummy.class_eval do
|
754
813
|
before_avatar_post_process :do_before_avatar
|
755
814
|
after_avatar_post_process :do_after_avatar
|
@@ -767,7 +826,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
767
826
|
@attachment = @dummy.avatar
|
768
827
|
end
|
769
828
|
|
770
|
-
|
829
|
+
it "calls the defined callbacks when assigned" do
|
771
830
|
@dummy.expects(:do_before_avatar).with()
|
772
831
|
@dummy.expects(:do_after_avatar).with()
|
773
832
|
@dummy.expects(:do_before_all).with()
|
@@ -776,7 +835,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
776
835
|
@dummy.avatar = @file
|
777
836
|
end
|
778
837
|
|
779
|
-
|
838
|
+
it "does not cancel the processing if a before_post_process returns nil" do
|
780
839
|
@dummy.expects(:do_before_avatar).with().returns(nil)
|
781
840
|
@dummy.expects(:do_after_avatar).with()
|
782
841
|
@dummy.expects(:do_before_all).with().returns(nil)
|
@@ -785,7 +844,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
785
844
|
@dummy.avatar = @file
|
786
845
|
end
|
787
846
|
|
788
|
-
|
847
|
+
it "cancels the processing if a before_post_process returns false" do
|
789
848
|
@dummy.expects(:do_before_avatar).never
|
790
849
|
@dummy.expects(:do_after_avatar).never
|
791
850
|
@dummy.expects(:do_before_all).with().returns(false)
|
@@ -794,7 +853,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
794
853
|
@dummy.avatar = @file
|
795
854
|
end
|
796
855
|
|
797
|
-
|
856
|
+
it "cancels the processing if a before_avatar_post_process returns false" do
|
798
857
|
@dummy.expects(:do_before_avatar).with().returns(false)
|
799
858
|
@dummy.expects(:do_after_avatar)
|
800
859
|
@dummy.expects(:do_before_all).with().returns(true)
|
@@ -805,37 +864,37 @@ class AttachmentTest < Test::Unit::TestCase
|
|
805
864
|
end
|
806
865
|
|
807
866
|
context "Assigning an attachment" do
|
808
|
-
|
809
|
-
rebuild_model :
|
867
|
+
before do
|
868
|
+
rebuild_model styles: { something: "100x100#" }
|
810
869
|
@file = File.new(fixture_file("5k.png"), "rb")
|
811
870
|
@dummy = Dummy.new
|
812
871
|
@dummy.avatar = @file
|
813
872
|
end
|
814
873
|
|
815
|
-
|
874
|
+
it "strips whitespace from original_filename field" do
|
816
875
|
assert_equal "5k.png", @dummy.avatar.original_filename
|
817
876
|
end
|
818
877
|
|
819
|
-
|
878
|
+
it "strips whitespace from content_type field" do
|
820
879
|
assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
|
821
880
|
end
|
822
881
|
end
|
823
882
|
|
824
883
|
context "Assigning an attachment" do
|
825
|
-
|
826
|
-
rebuild_model :
|
884
|
+
before do
|
885
|
+
rebuild_model styles: { something: "100x100#" }
|
827
886
|
@file = File.new(fixture_file("5k.png"), "rb")
|
828
887
|
@dummy = Dummy.new
|
829
888
|
@dummy.avatar = @file
|
830
889
|
end
|
831
890
|
|
832
|
-
|
891
|
+
it "makes sure the content_type is a string" do
|
833
892
|
assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
|
834
893
|
end
|
835
894
|
end
|
836
895
|
|
837
896
|
context "Attachment with strange letters" do
|
838
|
-
|
897
|
+
before do
|
839
898
|
rebuild_model
|
840
899
|
@file = File.new(fixture_file("5k.png"), "rb")
|
841
900
|
@file.stubs(:original_filename).returns("sheep_say_bæ.png")
|
@@ -843,18 +902,18 @@ class AttachmentTest < Test::Unit::TestCase
|
|
843
902
|
@dummy.avatar = @file
|
844
903
|
end
|
845
904
|
|
846
|
-
|
905
|
+
it "does not remove strange letters" do
|
847
906
|
assert_equal "sheep_say_bæ.png", @dummy.avatar.original_filename
|
848
907
|
end
|
849
908
|
end
|
850
909
|
|
851
910
|
context "Attachment with reserved filename" do
|
852
|
-
|
911
|
+
before do
|
853
912
|
rebuild_model
|
854
913
|
@file = Tempfile.new(["filename","png"])
|
855
914
|
end
|
856
915
|
|
857
|
-
|
916
|
+
after do
|
858
917
|
@file.unlink
|
859
918
|
end
|
860
919
|
|
@@ -863,37 +922,37 @@ class AttachmentTest < Test::Unit::TestCase
|
|
863
922
|
context "with character #{character}" do
|
864
923
|
|
865
924
|
context "at beginning of filename" do
|
866
|
-
|
925
|
+
before do
|
867
926
|
@file.stubs(:original_filename).returns("#{character}filename.png")
|
868
927
|
@dummy = Dummy.new
|
869
928
|
@dummy.avatar = @file
|
870
929
|
end
|
871
930
|
|
872
|
-
|
931
|
+
it "converts special character into underscore" do
|
873
932
|
assert_equal "_filename.png", @dummy.avatar.original_filename
|
874
933
|
end
|
875
934
|
end
|
876
935
|
|
877
936
|
context "at end of filename" do
|
878
|
-
|
937
|
+
before do
|
879
938
|
@file.stubs(:original_filename).returns("filename.png#{character}")
|
880
939
|
@dummy = Dummy.new
|
881
940
|
@dummy.avatar = @file
|
882
941
|
end
|
883
942
|
|
884
|
-
|
943
|
+
it "converts special character into underscore" do
|
885
944
|
assert_equal "filename.png_", @dummy.avatar.original_filename
|
886
945
|
end
|
887
946
|
end
|
888
947
|
|
889
948
|
context "in the middle of filename" do
|
890
|
-
|
949
|
+
before do
|
891
950
|
@file.stubs(:original_filename).returns("file#{character}name.png")
|
892
951
|
@dummy = Dummy.new
|
893
952
|
@dummy.avatar = @file
|
894
953
|
end
|
895
954
|
|
896
|
-
|
955
|
+
it "converts special character into underscore" do
|
897
956
|
assert_equal "file_name.png", @dummy.avatar.original_filename
|
898
957
|
end
|
899
958
|
end
|
@@ -903,53 +962,53 @@ class AttachmentTest < Test::Unit::TestCase
|
|
903
962
|
end
|
904
963
|
|
905
964
|
context "with specified regexp replacement" do
|
906
|
-
|
965
|
+
before do
|
907
966
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
908
967
|
end
|
909
968
|
|
910
|
-
|
969
|
+
after do
|
911
970
|
Paperclip::Attachment.default_options.merge! @old_defaults
|
912
971
|
end
|
913
972
|
|
914
973
|
context 'as another regexp' do
|
915
|
-
|
916
|
-
Paperclip::Attachment.default_options.merge! :
|
974
|
+
before do
|
975
|
+
Paperclip::Attachment.default_options.merge! restricted_characters: /o/
|
917
976
|
|
918
977
|
@file.stubs(:original_filename).returns("goood.png")
|
919
978
|
@dummy = Dummy.new
|
920
979
|
@dummy.avatar = @file
|
921
980
|
end
|
922
981
|
|
923
|
-
|
982
|
+
it "matches and converts that character" do
|
924
983
|
assert_equal "g___d.png", @dummy.avatar.original_filename
|
925
984
|
end
|
926
985
|
end
|
927
986
|
|
928
987
|
context 'as nil' do
|
929
|
-
|
930
|
-
Paperclip::Attachment.default_options.merge! :
|
988
|
+
before do
|
989
|
+
Paperclip::Attachment.default_options.merge! restricted_characters: nil
|
931
990
|
|
932
991
|
@file.stubs(:original_filename).returns("goood.png")
|
933
992
|
@dummy = Dummy.new
|
934
993
|
@dummy.avatar = @file
|
935
994
|
end
|
936
995
|
|
937
|
-
|
996
|
+
it "ignores and returns the original file name" do
|
938
997
|
assert_equal "goood.png", @dummy.avatar.original_filename
|
939
998
|
end
|
940
999
|
end
|
941
1000
|
end
|
942
1001
|
|
943
1002
|
context 'with specified cleaner' do
|
944
|
-
|
1003
|
+
before do
|
945
1004
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
946
1005
|
end
|
947
1006
|
|
948
|
-
|
1007
|
+
after do
|
949
1008
|
Paperclip::Attachment.default_options.merge! @old_defaults
|
950
1009
|
end
|
951
1010
|
|
952
|
-
|
1011
|
+
it 'calls the given proc and take the result as cleaned filename' do
|
953
1012
|
Paperclip::Attachment.default_options[:filename_cleaner] = lambda do |str|
|
954
1013
|
"from_proc_#{str}"
|
955
1014
|
end
|
@@ -960,7 +1019,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
960
1019
|
assert_equal "from_proc_goood.png", @dummy.avatar.original_filename
|
961
1020
|
end
|
962
1021
|
|
963
|
-
|
1022
|
+
it 'calls the given object and take the result as the cleaned filename' do
|
964
1023
|
class MyCleaner
|
965
1024
|
def call(filename)
|
966
1025
|
"foo"
|
@@ -977,102 +1036,100 @@ class AttachmentTest < Test::Unit::TestCase
|
|
977
1036
|
end
|
978
1037
|
|
979
1038
|
context "Attachment with uppercase extension and a default style" do
|
980
|
-
|
1039
|
+
before do
|
981
1040
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
982
1041
|
Paperclip::Attachment.default_options.merge!({
|
983
|
-
:
|
1042
|
+
path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
|
984
1043
|
})
|
985
1044
|
FileUtils.rm_rf("tmp")
|
986
|
-
rebuild_model
|
1045
|
+
rebuild_model styles: { large: ["400x400", :jpg],
|
1046
|
+
medium: ["100x100", :jpg],
|
1047
|
+
small: ["32x32#", :jpg]},
|
1048
|
+
default_style: :small
|
987
1049
|
@instance = Dummy.new
|
988
1050
|
@instance.stubs(:id).returns 123
|
989
|
-
|
990
1051
|
@file = File.new(fixture_file("uppercase.PNG"), 'rb')
|
991
1052
|
|
992
|
-
|
993
|
-
|
994
|
-
:small => ["32x32#", :jpg]},
|
995
|
-
:default_style => :small}
|
996
|
-
@attachment = Paperclip::Attachment.new(:avatar,
|
997
|
-
@instance,
|
998
|
-
styles)
|
1053
|
+
@attachment = @instance.avatar
|
1054
|
+
|
999
1055
|
now = Time.now
|
1000
1056
|
Time.stubs(:now).returns(now)
|
1001
1057
|
@attachment.assign(@file)
|
1002
1058
|
@attachment.save
|
1003
1059
|
end
|
1004
1060
|
|
1005
|
-
|
1061
|
+
after do
|
1006
1062
|
@file.close
|
1007
1063
|
Paperclip::Attachment.default_options.merge!(@old_defaults)
|
1008
1064
|
end
|
1009
1065
|
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1066
|
+
it "has matching to_s and url methods" do
|
1067
|
+
assert_equal @attachment.to_s, @attachment.url
|
1068
|
+
assert_equal @attachment.to_s(:small), @attachment.url(:small)
|
1013
1069
|
end
|
1014
1070
|
|
1015
|
-
|
1016
|
-
|
1071
|
+
it "has matching expiring_url and url methods when using the filesystem storage" do
|
1072
|
+
assert_equal @attachment.expiring_url, @attachment.url
|
1017
1073
|
end
|
1018
1074
|
end
|
1019
1075
|
|
1020
1076
|
context "An attachment" do
|
1021
|
-
|
1077
|
+
before do
|
1022
1078
|
@old_defaults = Paperclip::Attachment.default_options.dup
|
1023
1079
|
Paperclip::Attachment.default_options.merge!({
|
1024
|
-
:
|
1080
|
+
path: ":rails_root/:attachment/:class/:style/:id/:basename.:extension"
|
1025
1081
|
})
|
1026
1082
|
FileUtils.rm_rf("tmp")
|
1027
1083
|
rebuild_model
|
1028
1084
|
@instance = Dummy.new
|
1029
1085
|
@instance.stubs(:id).returns 123
|
1030
|
-
@attachment = Paperclip::Attachment.new(:avatar, @instance)
|
1086
|
+
# @attachment = Paperclip::Attachment.new(:avatar, @instance)
|
1087
|
+
@attachment = @instance.avatar
|
1031
1088
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
1032
1089
|
end
|
1033
1090
|
|
1034
|
-
|
1091
|
+
after do
|
1035
1092
|
@file.close
|
1036
1093
|
Paperclip::Attachment.default_options.merge!(@old_defaults)
|
1037
1094
|
end
|
1038
1095
|
|
1039
|
-
|
1096
|
+
it "raises if there are not the correct columns when you try to assign" do
|
1040
1097
|
@other_attachment = Paperclip::Attachment.new(:not_here, @instance)
|
1041
1098
|
assert_raises(Paperclip::Error) do
|
1042
1099
|
@other_attachment.assign(@file)
|
1043
1100
|
end
|
1044
1101
|
end
|
1045
1102
|
|
1046
|
-
|
1103
|
+
it 'clears out the previous assignment when assigned nil' do
|
1047
1104
|
@attachment.assign(@file)
|
1048
|
-
|
1105
|
+
@attachment.queued_for_write[:original]
|
1049
1106
|
@attachment.assign(nil)
|
1050
1107
|
assert_nil @attachment.queued_for_write[:original]
|
1051
1108
|
end
|
1052
1109
|
|
1053
|
-
|
1110
|
+
it 'does not do anything when it is assigned an empty string' do
|
1054
1111
|
@attachment.assign(@file)
|
1055
1112
|
original_file = @attachment.queued_for_write[:original]
|
1056
1113
|
@attachment.assign("")
|
1057
1114
|
assert_equal original_file, @attachment.queued_for_write[:original]
|
1058
1115
|
end
|
1059
1116
|
|
1060
|
-
|
1117
|
+
it "returns nil as path when no file assigned" do
|
1061
1118
|
assert_equal nil, @attachment.path
|
1062
1119
|
assert_equal nil, @attachment.path(:blah)
|
1063
1120
|
end
|
1064
1121
|
|
1065
1122
|
context "with a file assigned but not saved yet" do
|
1066
|
-
|
1123
|
+
it "clears out any attached files" do
|
1067
1124
|
@attachment.assign(@file)
|
1068
|
-
assert
|
1125
|
+
assert @attachment.queued_for_write.present?
|
1069
1126
|
@attachment.clear
|
1070
1127
|
assert @attachment.queued_for_write.blank?
|
1071
1128
|
end
|
1072
1129
|
end
|
1073
1130
|
|
1074
1131
|
context "with a file assigned in the database" do
|
1075
|
-
|
1132
|
+
before do
|
1076
1133
|
@attachment.stubs(:instance_read).with(:file_name).returns("5k.png")
|
1077
1134
|
@attachment.stubs(:instance_read).with(:content_type).returns("image/png")
|
1078
1135
|
@attachment.stubs(:instance_read).with(:file_size).returns(12345)
|
@@ -1082,54 +1139,57 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1082
1139
|
@attachment.stubs(:instance_read).with(:updated_at).returns(dtnow)
|
1083
1140
|
end
|
1084
1141
|
|
1085
|
-
|
1142
|
+
it "returns the proper path when filename has a single .'s" do
|
1086
1143
|
assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.png"), File.expand_path(@attachment.path)
|
1087
1144
|
end
|
1088
1145
|
|
1089
|
-
|
1146
|
+
it "returns the proper path when filename has multiple .'s" do
|
1090
1147
|
@attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png")
|
1091
1148
|
assert_equal File.expand_path("tmp/avatars/dummies/original/#{@instance.id}/5k.old.png"), File.expand_path(@attachment.path)
|
1092
1149
|
end
|
1093
1150
|
|
1094
1151
|
context "when expecting three styles" do
|
1095
|
-
|
1096
|
-
styles
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1152
|
+
before do
|
1153
|
+
rebuild_class styles: {
|
1154
|
+
large: ["400x400", :png],
|
1155
|
+
medium: ["100x100", :gif],
|
1156
|
+
small: ["32x32#", :jpg]
|
1157
|
+
}
|
1158
|
+
@instance = Dummy.new
|
1159
|
+
@instance.stubs(:id).returns 123
|
1160
|
+
@file = File.new(fixture_file("5k.png"), 'rb')
|
1161
|
+
@attachment = @instance.avatar
|
1102
1162
|
end
|
1103
1163
|
|
1104
1164
|
context "and assigned a file" do
|
1105
|
-
|
1165
|
+
before do
|
1106
1166
|
now = Time.now
|
1107
1167
|
Time.stubs(:now).returns(now)
|
1108
1168
|
@attachment.assign(@file)
|
1109
1169
|
end
|
1110
1170
|
|
1111
|
-
|
1171
|
+
it "is dirty" do
|
1112
1172
|
assert @attachment.dirty?
|
1113
1173
|
end
|
1114
1174
|
|
1115
1175
|
context "and saved" do
|
1116
|
-
|
1176
|
+
before do
|
1117
1177
|
@attachment.save
|
1118
1178
|
end
|
1119
1179
|
|
1120
|
-
|
1180
|
+
it "commits the files to disk" do
|
1121
1181
|
[:large, :medium, :small].each do |style|
|
1122
|
-
|
1182
|
+
expect(@attachment.path(style)).to exist
|
1123
1183
|
end
|
1124
1184
|
end
|
1125
1185
|
|
1126
|
-
|
1186
|
+
it "saves the files as the right formats and sizes" do
|
1127
1187
|
[[:large, 400, 61, "PNG"],
|
1128
1188
|
[:medium, 100, 15, "GIF"],
|
1129
1189
|
[:small, 32, 32, "JPEG"]].each do |style|
|
1130
1190
|
cmd = %Q[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"]
|
1131
1191
|
out = `#{cmd}`
|
1132
|
-
width, height,
|
1192
|
+
width, height, _size, format = out.split(" ")
|
1133
1193
|
assert_equal style[1].to_s, width.to_s
|
1134
1194
|
assert_equal style[2].to_s, height.to_s
|
1135
1195
|
assert_equal style[3].to_s, format.to_s
|
@@ -1137,13 +1197,13 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1137
1197
|
end
|
1138
1198
|
|
1139
1199
|
context "and trying to delete" do
|
1140
|
-
|
1200
|
+
before do
|
1141
1201
|
@existing_names = @attachment.styles.keys.collect do |style|
|
1142
1202
|
@attachment.path(style)
|
1143
1203
|
end
|
1144
1204
|
end
|
1145
1205
|
|
1146
|
-
|
1206
|
+
it "deletes the files after assigning nil" do
|
1147
1207
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1148
1208
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1149
1209
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1154,7 +1214,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1154
1214
|
@existing_names.each{|f| assert_file_not_exists(f) }
|
1155
1215
|
end
|
1156
1216
|
|
1157
|
-
|
1217
|
+
it "deletes the files when you call #clear and #save" do
|
1158
1218
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1159
1219
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1160
1220
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1165,7 +1225,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1165
1225
|
@existing_names.each{|f| assert_file_not_exists(f) }
|
1166
1226
|
end
|
1167
1227
|
|
1168
|
-
|
1228
|
+
it "deletes the files when you call #delete" do
|
1169
1229
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1170
1230
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1171
1231
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1176,11 +1236,11 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1176
1236
|
end
|
1177
1237
|
|
1178
1238
|
context "when keeping old files" do
|
1179
|
-
|
1239
|
+
before do
|
1180
1240
|
@attachment.options[:keep_old_files] = true
|
1181
1241
|
end
|
1182
1242
|
|
1183
|
-
|
1243
|
+
it "keeps the files after assigning nil" do
|
1184
1244
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1185
1245
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1186
1246
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1191,7 +1251,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1191
1251
|
@existing_names.each{|f| assert_file_exists(f) }
|
1192
1252
|
end
|
1193
1253
|
|
1194
|
-
|
1254
|
+
it "keeps the files when you call #clear and #save" do
|
1195
1255
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1196
1256
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1197
1257
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1202,7 +1262,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1202
1262
|
@existing_names.each{|f| assert_file_exists(f) }
|
1203
1263
|
end
|
1204
1264
|
|
1205
|
-
|
1265
|
+
it "keeps the files when you call #delete" do
|
1206
1266
|
@attachment.expects(:instance_write).with(:file_name, nil)
|
1207
1267
|
@attachment.expects(:instance_write).with(:content_type, nil)
|
1208
1268
|
@attachment.expects(:instance_write).with(:file_size, nil)
|
@@ -1219,19 +1279,19 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1219
1279
|
end
|
1220
1280
|
|
1221
1281
|
context "when trying a nonexistant storage type" do
|
1222
|
-
|
1223
|
-
rebuild_model :
|
1282
|
+
before do
|
1283
|
+
rebuild_model storage: :not_here
|
1224
1284
|
end
|
1225
1285
|
|
1226
|
-
|
1227
|
-
|
1286
|
+
it "is not able to find the module" do
|
1287
|
+
assert_raises(Paperclip::Errors::StorageMethodNotFound){ Dummy.new.avatar }
|
1228
1288
|
end
|
1229
1289
|
end
|
1230
1290
|
end
|
1231
1291
|
|
1232
1292
|
context "An attachment with only a avatar_file_name column" do
|
1233
|
-
|
1234
|
-
ActiveRecord::Base.connection.create_table :dummies, :
|
1293
|
+
before do
|
1294
|
+
ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
|
1235
1295
|
table.column :avatar_file_name, :string
|
1236
1296
|
end
|
1237
1297
|
rebuild_class
|
@@ -1239,41 +1299,41 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1239
1299
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
1240
1300
|
end
|
1241
1301
|
|
1242
|
-
|
1302
|
+
after { @file.close }
|
1243
1303
|
|
1244
|
-
|
1304
|
+
it "does not error when assigned an attachment" do
|
1245
1305
|
assert_nothing_raised { @dummy.avatar = @file }
|
1246
1306
|
end
|
1247
1307
|
|
1248
|
-
|
1308
|
+
it "does not return the time when sent #avatar_updated_at" do
|
1249
1309
|
@dummy.avatar = @file
|
1250
1310
|
assert_nil @dummy.avatar.updated_at
|
1251
1311
|
end
|
1252
1312
|
|
1253
|
-
|
1313
|
+
it "returns the right value when sent #avatar_file_size" do
|
1254
1314
|
@dummy.avatar = @file
|
1255
1315
|
assert_equal File.size(@file), @dummy.avatar.size
|
1256
1316
|
end
|
1257
1317
|
|
1258
1318
|
context "and avatar_created_at column" do
|
1259
|
-
|
1319
|
+
before do
|
1260
1320
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_created_at, :timestamp
|
1261
1321
|
rebuild_class
|
1262
1322
|
@dummy = Dummy.new
|
1263
1323
|
end
|
1264
1324
|
|
1265
|
-
|
1325
|
+
it "does not error when assigned an attachment" do
|
1266
1326
|
assert_nothing_raised { @dummy.avatar = @file }
|
1267
1327
|
end
|
1268
1328
|
|
1269
|
-
|
1329
|
+
it "returns the creation time when sent #avatar_created_at" do
|
1270
1330
|
now = Time.now
|
1271
1331
|
Time.stubs(:now).returns(now)
|
1272
1332
|
@dummy.avatar = @file
|
1273
1333
|
assert_equal now.to_i, @dummy.avatar.created_at
|
1274
1334
|
end
|
1275
1335
|
|
1276
|
-
|
1336
|
+
it "returns the creation time when sent #avatar_created_at and the entry has been updated" do
|
1277
1337
|
creation = 2.hours.ago
|
1278
1338
|
now = Time.now
|
1279
1339
|
Time.stubs(:now).returns(creation)
|
@@ -1284,7 +1344,7 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1284
1344
|
assert_not_equal now.to_i, @dummy.avatar.created_at
|
1285
1345
|
end
|
1286
1346
|
|
1287
|
-
|
1347
|
+
it "sets changed? to true on attachment assignment" do
|
1288
1348
|
@dummy.avatar = @file
|
1289
1349
|
@dummy.save!
|
1290
1350
|
@dummy.avatar = @file
|
@@ -1293,17 +1353,17 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1293
1353
|
end
|
1294
1354
|
|
1295
1355
|
context "and avatar_updated_at column" do
|
1296
|
-
|
1356
|
+
before do
|
1297
1357
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
|
1298
1358
|
rebuild_class
|
1299
1359
|
@dummy = Dummy.new
|
1300
1360
|
end
|
1301
1361
|
|
1302
|
-
|
1362
|
+
it "does not error when assigned an attachment" do
|
1303
1363
|
assert_nothing_raised { @dummy.avatar = @file }
|
1304
1364
|
end
|
1305
1365
|
|
1306
|
-
|
1366
|
+
it "returns the right value when sent #avatar_updated_at" do
|
1307
1367
|
now = Time.now
|
1308
1368
|
Time.stubs(:now).returns(now)
|
1309
1369
|
@dummy.avatar = @file
|
@@ -1311,45 +1371,51 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1311
1371
|
end
|
1312
1372
|
end
|
1313
1373
|
|
1314
|
-
|
1374
|
+
it "does not calculate fingerprint" do
|
1375
|
+
Digest::MD5.stubs(:file)
|
1376
|
+
@dummy.avatar = @file
|
1377
|
+
expect(Digest::MD5).to have_received(:file).never
|
1378
|
+
end
|
1379
|
+
|
1380
|
+
it "does not assign fingerprint" do
|
1315
1381
|
@dummy.avatar = @file
|
1316
1382
|
assert_nil @dummy.avatar.fingerprint
|
1317
1383
|
end
|
1318
1384
|
|
1319
1385
|
context "and avatar_content_type column" do
|
1320
|
-
|
1386
|
+
before do
|
1321
1387
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
|
1322
1388
|
rebuild_class
|
1323
1389
|
@dummy = Dummy.new
|
1324
1390
|
end
|
1325
1391
|
|
1326
|
-
|
1392
|
+
it "does not error when assigned an attachment" do
|
1327
1393
|
assert_nothing_raised { @dummy.avatar = @file }
|
1328
1394
|
end
|
1329
1395
|
|
1330
|
-
|
1396
|
+
it "returns the right value when sent #avatar_content_type" do
|
1331
1397
|
@dummy.avatar = @file
|
1332
1398
|
assert_equal "image/png", @dummy.avatar.content_type
|
1333
1399
|
end
|
1334
1400
|
end
|
1335
1401
|
|
1336
1402
|
context "and avatar_file_size column" do
|
1337
|
-
|
1403
|
+
before do
|
1338
1404
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer
|
1339
1405
|
rebuild_class
|
1340
1406
|
@dummy = Dummy.new
|
1341
1407
|
end
|
1342
1408
|
|
1343
|
-
|
1409
|
+
it "does not error when assigned an attachment" do
|
1344
1410
|
assert_nothing_raised { @dummy.avatar = @file }
|
1345
1411
|
end
|
1346
1412
|
|
1347
|
-
|
1413
|
+
it "returns the right value when sent #avatar_file_size" do
|
1348
1414
|
@dummy.avatar = @file
|
1349
1415
|
assert_equal File.size(@file), @dummy.avatar.size
|
1350
1416
|
end
|
1351
1417
|
|
1352
|
-
|
1418
|
+
it "returns the right value when saved, reloaded, and sent #avatar_file_size" do
|
1353
1419
|
@dummy.avatar = @file
|
1354
1420
|
@dummy.save
|
1355
1421
|
@dummy = Dummy.find(@dummy.id)
|
@@ -1358,33 +1424,63 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1358
1424
|
end
|
1359
1425
|
|
1360
1426
|
context "and avatar_fingerprint column" do
|
1361
|
-
|
1427
|
+
before do
|
1362
1428
|
ActiveRecord::Base.connection.add_column :dummies, :avatar_fingerprint, :string
|
1363
1429
|
rebuild_class
|
1364
1430
|
@dummy = Dummy.new
|
1365
1431
|
end
|
1366
1432
|
|
1367
|
-
|
1433
|
+
it "does not error when assigned an attachment" do
|
1368
1434
|
assert_nothing_raised { @dummy.avatar = @file }
|
1369
1435
|
end
|
1370
1436
|
|
1371
|
-
|
1372
|
-
|
1373
|
-
|
1437
|
+
context "with explicitly set digest" do
|
1438
|
+
before do
|
1439
|
+
rebuild_class adapter_options: { hash_digest: Digest::SHA256 }
|
1440
|
+
@dummy = Dummy.new
|
1441
|
+
end
|
1442
|
+
|
1443
|
+
it "returns the right value when sent #avatar_fingerprint" do
|
1444
|
+
@dummy.avatar = @file
|
1445
|
+
assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
|
1446
|
+
@dummy.avatar_fingerprint
|
1447
|
+
end
|
1448
|
+
|
1449
|
+
it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
|
1450
|
+
@dummy.avatar = @file
|
1451
|
+
@dummy.save
|
1452
|
+
@dummy = Dummy.find(@dummy.id)
|
1453
|
+
assert_equal "734016d801a497f5579cdd4ef2ae1d020088c1db754dc434482d76dd5486520a",
|
1454
|
+
@dummy.avatar_fingerprint
|
1455
|
+
end
|
1374
1456
|
end
|
1375
1457
|
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1458
|
+
context "with the default digest" do
|
1459
|
+
before do
|
1460
|
+
rebuild_class # MD5 is the default
|
1461
|
+
@dummy = Dummy.new
|
1462
|
+
end
|
1463
|
+
|
1464
|
+
it "returns the right value when sent #avatar_fingerprint" do
|
1465
|
+
@dummy.avatar = @file
|
1466
|
+
assert_equal "aec488126c3b33c08a10c3fa303acf27",
|
1467
|
+
@dummy.avatar_fingerprint
|
1468
|
+
end
|
1469
|
+
|
1470
|
+
it "returns the right value when saved, reloaded, and sent #avatar_fingerprint" do
|
1471
|
+
@dummy.avatar = @file
|
1472
|
+
@dummy.save
|
1473
|
+
@dummy = Dummy.find(@dummy.id)
|
1474
|
+
assert_equal "aec488126c3b33c08a10c3fa303acf27",
|
1475
|
+
@dummy.avatar_fingerprint
|
1476
|
+
end
|
1381
1477
|
end
|
1382
1478
|
end
|
1383
1479
|
end
|
1384
1480
|
|
1385
1481
|
context "an attachment with delete_file option set to false" do
|
1386
|
-
|
1387
|
-
rebuild_model :
|
1482
|
+
before do
|
1483
|
+
rebuild_model preserve_files: true
|
1388
1484
|
@dummy = Dummy.new
|
1389
1485
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
1390
1486
|
@dummy.avatar = @file
|
@@ -1393,27 +1489,27 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1393
1489
|
@path = @attachment.path
|
1394
1490
|
end
|
1395
1491
|
|
1396
|
-
|
1492
|
+
after { @file.close }
|
1397
1493
|
|
1398
|
-
|
1494
|
+
it "does not delete the files from storage when attachment is destroyed" do
|
1399
1495
|
@attachment.destroy
|
1400
1496
|
assert_file_exists(@path)
|
1401
1497
|
end
|
1402
1498
|
|
1403
|
-
|
1499
|
+
it "clears out attachment data when attachment is destroyed" do
|
1404
1500
|
@attachment.destroy
|
1405
1501
|
assert !@attachment.exists?
|
1406
1502
|
assert_nil @dummy.avatar_file_name
|
1407
1503
|
end
|
1408
1504
|
|
1409
|
-
|
1505
|
+
it "does not delete the file when model is destroyed" do
|
1410
1506
|
@dummy.destroy
|
1411
1507
|
assert_file_exists(@path)
|
1412
1508
|
end
|
1413
1509
|
end
|
1414
1510
|
|
1415
1511
|
context "An attached file" do
|
1416
|
-
|
1512
|
+
before do
|
1417
1513
|
rebuild_model
|
1418
1514
|
@dummy = Dummy.new
|
1419
1515
|
@file = File.new(fixture_file("5k.png"), 'rb')
|
@@ -1423,24 +1519,24 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1423
1519
|
@path = @attachment.path
|
1424
1520
|
end
|
1425
1521
|
|
1426
|
-
|
1522
|
+
after { @file.close }
|
1427
1523
|
|
1428
|
-
|
1524
|
+
it "is not deleted when the model fails to destroy" do
|
1429
1525
|
@dummy.stubs(:destroy).raises(Exception)
|
1430
1526
|
|
1431
|
-
|
1527
|
+
assert_raises Exception do
|
1432
1528
|
@dummy.destroy
|
1433
1529
|
end
|
1434
1530
|
|
1435
1531
|
assert_file_exists(@path)
|
1436
1532
|
end
|
1437
1533
|
|
1438
|
-
|
1534
|
+
it "is deleted when the model is destroyed" do
|
1439
1535
|
@dummy.destroy
|
1440
1536
|
assert_file_not_exists(@path)
|
1441
1537
|
end
|
1442
1538
|
|
1443
|
-
|
1539
|
+
it "is not deleted when transaction rollbacks after model is destroyed" do
|
1444
1540
|
ActiveRecord::Base.transaction do
|
1445
1541
|
@dummy.destroy
|
1446
1542
|
raise ActiveRecord::Rollback
|
@@ -1449,5 +1545,4 @@ class AttachmentTest < Test::Unit::TestCase
|
|
1449
1545
|
assert_file_exists(@path)
|
1450
1546
|
end
|
1451
1547
|
end
|
1452
|
-
|
1453
1548
|
end
|