paperclip 3.4.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +17 -0
- data/.github/issue_template.md +3 -0
- data/.gitignore +0 -6
- data/.hound.yml +1055 -0
- data/.rubocop.yml +1 -0
- data/.travis.yml +19 -12
- data/Appraisals +4 -11
- data/CONTRIBUTING.md +29 -13
- data/Gemfile +13 -4
- data/LICENSE +1 -3
- data/MIGRATING-ES.md +317 -0
- data/MIGRATING.md +375 -0
- data/NEWS +390 -71
- data/README.md +607 -152
- data/RELEASING.md +17 -0
- data/Rakefile +6 -8
- data/UPGRADING +12 -9
- data/features/basic_integration.feature +34 -21
- data/features/migration.feature +0 -24
- data/features/rake_tasks.feature +2 -3
- data/features/step_definitions/attachment_steps.rb +44 -36
- data/features/step_definitions/html_steps.rb +2 -2
- data/features/step_definitions/rails_steps.rb +125 -26
- data/features/step_definitions/s3_steps.rb +3 -3
- data/features/step_definitions/web_steps.rb +1 -103
- data/features/support/env.rb +3 -2
- data/features/support/fakeweb.rb +4 -1
- data/features/support/file_helpers.rb +12 -2
- data/features/support/fixtures/gemfile.txt +1 -1
- data/features/support/paths.rb +1 -1
- data/features/support/rails.rb +4 -11
- 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 +215 -82
- data/lib/paperclip/attachment_registry.rb +60 -0
- data/lib/paperclip/callbacks.rb +13 -1
- data/lib/paperclip/content_type_detector.rb +48 -24
- data/lib/paperclip/errors.rb +8 -1
- data/lib/paperclip/file_command_content_type_detector.rb +6 -8
- data/lib/paperclip/filename_cleaner.rb +15 -0
- data/lib/paperclip/geometry_detector_factory.rb +12 -5
- data/lib/paperclip/geometry_parser_factory.rb +1 -1
- data/lib/paperclip/glue.rb +1 -2
- data/lib/paperclip/has_attached_file.rb +115 -0
- data/lib/paperclip/helpers.rb +15 -20
- data/lib/paperclip/interpolations/plural_cache.rb +18 -0
- data/lib/paperclip/interpolations.rb +36 -14
- data/lib/paperclip/io_adapters/abstract_adapter.rb +42 -5
- data/lib/paperclip/io_adapters/attachment_adapter.rb +20 -9
- data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
- data/lib/paperclip/io_adapters/empty_string_adapter.rb +19 -0
- data/lib/paperclip/io_adapters/file_adapter.rb +13 -7
- data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +16 -0
- 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 +11 -7
- data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
- data/lib/paperclip/locales/en.yml +1 -0
- data/lib/paperclip/logger.rb +1 -1
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +3 -6
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +7 -2
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
- data/lib/paperclip/matchers.rb +1 -1
- data/lib/paperclip/media_type_spoof_detector.rb +93 -0
- data/lib/paperclip/missing_attachment_styles.rb +11 -16
- data/lib/paperclip/processor.rb +15 -43
- data/lib/paperclip/processor_helpers.rb +50 -0
- data/lib/paperclip/rails_environment.rb +25 -0
- data/lib/paperclip/schema.rb +10 -8
- data/lib/paperclip/storage/filesystem.rb +20 -5
- data/lib/paperclip/storage/fog.rb +49 -23
- data/lib/paperclip/storage/s3.rb +153 -82
- data/lib/paperclip/style.rb +8 -3
- data/lib/paperclip/tempfile_factory.rb +6 -4
- data/lib/paperclip/thumbnail.rb +35 -19
- data/lib/paperclip/url_generator.rb +26 -14
- data/lib/paperclip/validators/attachment_content_type_validator.rb +15 -2
- 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 +12 -8
- data/lib/paperclip/validators/attachment_size_validator.rb +17 -10
- data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
- data/lib/paperclip/validators.rb +31 -3
- data/lib/paperclip/version.rb +3 -1
- data/lib/paperclip.rb +41 -55
- data/lib/tasks/paperclip.rake +56 -9
- data/paperclip.gemspec +18 -17
- data/shoulda_macros/paperclip.rb +13 -3
- data/spec/paperclip/attachment_definitions_spec.rb +13 -0
- data/spec/paperclip/attachment_processing_spec.rb +79 -0
- data/spec/paperclip/attachment_registry_spec.rb +158 -0
- data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +597 -389
- data/spec/paperclip/content_type_detector_spec.rb +48 -0
- data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
- data/spec/paperclip/filename_cleaner_spec.rb +13 -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} +179 -199
- data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
- data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
- data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +54 -25
- 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/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
- data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -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/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
- data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
- data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -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 +120 -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/spec/paperclip/rake_spec.rb +103 -0
- 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 +254 -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} +186 -141
- data/spec/paperclip/url_generator_spec.rb +221 -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} +87 -59
- 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 +46 -0
- data/spec/support/assertions.rb +82 -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 +395 -346
- data/Gemfile.lock +0 -200
- 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/lib/paperclip/attachment_options.rb +0 -9
- data/lib/paperclip/instance_methods.rb +0 -35
- data/test/attachment_options_test.rb +0 -27
- data/test/attachment_processing_test.rb +0 -29
- data/test/content_type_detector_test.rb +0 -40
- data/test/file_command_content_type_detector_test.rb +0 -25
- data/test/generator_test.rb +0 -80
- data/test/geometry_detector_test.rb +0 -24
- data/test/helper.rb +0 -199
- data/test/io_adapters/abstract_adapter_test.rb +0 -50
- data/test/io_adapters/file_adapter_test.rb +0 -100
- data/test/io_adapters/identity_adapter_test.rb +0 -8
- data/test/io_adapters/stringio_adapter_test.rb +0 -51
- data/test/io_adapters/uploaded_file_adapter_test.rb +0 -123
- data/test/io_adapters/uri_adapter_test.rb +0 -86
- 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 -47
- 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 -94
- data/test/paperclip_test.rb +0 -259
- data/test/schema_test.rb +0 -200
- data/test/storage/fog_test.rb +0 -453
- data/test/storage/s3_live_test.rb +0 -179
- data/test/storage/s3_test.rb +0 -1236
- data/test/style_test.rb +0 -213
- data/test/support/mock_model.rb +0 -2
- data/test/tempfile_factory_test.rb +0 -13
- data/test/url_generator_test.rb +0 -187
- data/test/validators/attachment_content_type_validator_test.rb +0 -292
- data/test/validators_test.rb +0 -25
- /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
data/lib/tasks/paperclip.rake
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'paperclip/attachment_registry'
|
2
|
+
|
1
3
|
module Paperclip
|
2
4
|
module Task
|
3
5
|
def self.obtain_class
|
@@ -9,13 +11,23 @@ module Paperclip
|
|
9
11
|
def self.obtain_attachments(klass)
|
10
12
|
klass = Paperclip.class_for(klass.to_s)
|
11
13
|
name = ENV['ATTACHMENT'] || ENV['attachment']
|
12
|
-
|
13
|
-
|
14
|
+
|
15
|
+
attachment_names = Paperclip::AttachmentRegistry.names_for(klass)
|
16
|
+
|
17
|
+
if attachment_names.empty?
|
18
|
+
raise "Class #{klass.name} has no attachments specified"
|
19
|
+
end
|
20
|
+
|
21
|
+
if name.present? && attachment_names.map(&:to_s).include?(name.to_s)
|
14
22
|
[ name ]
|
15
23
|
else
|
16
|
-
|
24
|
+
attachment_names
|
17
25
|
end
|
18
26
|
end
|
27
|
+
|
28
|
+
def self.log_error(error)
|
29
|
+
$stderr.puts error
|
30
|
+
end
|
19
31
|
end
|
20
32
|
end
|
21
33
|
|
@@ -31,10 +43,16 @@ namespace :paperclip do
|
|
31
43
|
styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym)
|
32
44
|
names.each do |name|
|
33
45
|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
34
|
-
instance.send(name)
|
46
|
+
attachment = instance.send(name)
|
47
|
+
begin
|
48
|
+
attachment.reprocess!(*styles)
|
49
|
+
rescue StandardError => e
|
50
|
+
Paperclip::Task.log_error("exception while processing #{klass} ID #{instance.id}:")
|
51
|
+
Paperclip::Task.log_error(" " + e.message + "\n")
|
52
|
+
end
|
35
53
|
unless instance.errors.blank?
|
36
|
-
|
37
|
-
|
54
|
+
Paperclip::Task.log_error("errors while processing #{klass} ID #{instance.id}:")
|
55
|
+
Paperclip::Task.log_error(" " + instance.errors.full_messages.join("\n ") + "\n")
|
38
56
|
end
|
39
57
|
end
|
40
58
|
end
|
@@ -46,7 +64,8 @@ namespace :paperclip do
|
|
46
64
|
names = Paperclip::Task.obtain_attachments(klass)
|
47
65
|
names.each do |name|
|
48
66
|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
49
|
-
|
67
|
+
attachment = instance.send(name)
|
68
|
+
if file = Paperclip.io_adapters.for(attachment, attachment.options[:adapter_options])
|
50
69
|
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
|
51
70
|
instance.send("#{name}_content_type=", file.content_type.to_s.strip)
|
52
71
|
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
|
@@ -60,8 +79,7 @@ namespace :paperclip do
|
|
60
79
|
|
61
80
|
desc "Regenerates missing thumbnail styles for all classes using Paperclip."
|
62
81
|
task :missing_styles => :environment do
|
63
|
-
|
64
|
-
Dir[Rails.root + 'app/models/**/*.rb'].each { |path| load path }
|
82
|
+
Rails.application.eager_load!
|
65
83
|
Paperclip.missing_attachments_styles.each do |klass, attachment_definitions|
|
66
84
|
attachment_definitions.each do |attachment_name, missing_styles|
|
67
85
|
puts "Regenerating #{klass} -> #{attachment_name} -> #{missing_styles.inspect}"
|
@@ -73,6 +91,19 @@ namespace :paperclip do
|
|
73
91
|
end
|
74
92
|
Paperclip.save_current_attachments_styles!
|
75
93
|
end
|
94
|
+
|
95
|
+
desc "Regenerates fingerprints for a given CLASS (and optional ATTACHMENT). Useful when changing digest."
|
96
|
+
task :fingerprints => :environment do
|
97
|
+
klass = Paperclip::Task.obtain_class
|
98
|
+
names = Paperclip::Task.obtain_attachments(klass)
|
99
|
+
names.each do |name|
|
100
|
+
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
101
|
+
attachment = instance.send(name)
|
102
|
+
attachment.assign(attachment)
|
103
|
+
instance.save(:validate => false)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
76
107
|
end
|
77
108
|
|
78
109
|
desc "Cleans out invalid attachments. Useful after you've added new validations."
|
@@ -91,4 +122,20 @@ namespace :paperclip do
|
|
91
122
|
end
|
92
123
|
end
|
93
124
|
end
|
125
|
+
|
126
|
+
desc "find missing attachments. Useful to know which attachments are broken"
|
127
|
+
task :find_broken_attachments => :environment do
|
128
|
+
klass = Paperclip::Task.obtain_class
|
129
|
+
names = Paperclip::Task.obtain_attachments(klass)
|
130
|
+
names.each do |name|
|
131
|
+
Paperclip.each_instance_with_attachment(klass, name) do |instance|
|
132
|
+
attachment = instance.send(name)
|
133
|
+
if attachment.exists?
|
134
|
+
print "."
|
135
|
+
else
|
136
|
+
Paperclip::Task.log_error("#{instance.class}##{attachment.name}, #{instance.id}, #{attachment.url}")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
94
141
|
end
|
data/paperclip.gemspec
CHANGED
@@ -10,44 +10,45 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.homepage = "https://github.com/thoughtbot/paperclip"
|
11
11
|
s.summary = "File attachments as attributes for ActiveRecord"
|
12
12
|
s.description = "Easy upload management for ActiveRecord"
|
13
|
-
|
14
|
-
s.rubyforge_project = "paperclip"
|
13
|
+
s.license = "MIT"
|
15
14
|
|
16
15
|
s.files = `git ls-files`.split("\n")
|
17
|
-
s.test_files = `git ls-files -- {
|
16
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
18
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
18
|
s.require_paths = ["lib"]
|
20
19
|
|
21
|
-
if File.
|
20
|
+
if File.exist?('UPGRADING')
|
22
21
|
s.post_install_message = File.read("UPGRADING")
|
23
22
|
end
|
24
23
|
|
25
24
|
s.requirements << "ImageMagick"
|
26
|
-
s.required_ruby_version = ">= 1.
|
25
|
+
s.required_ruby_version = ">= 2.1.0"
|
27
26
|
|
28
|
-
s.add_dependency('
|
29
|
-
s.add_dependency('
|
30
|
-
s.add_dependency('
|
31
|
-
s.add_dependency('cocaine', '~> 0.4.0')
|
27
|
+
s.add_dependency('activemodel', '>= 4.2.0')
|
28
|
+
s.add_dependency('activesupport', '>= 4.2.0')
|
29
|
+
s.add_dependency('terrapin', '~> 0.6.0')
|
32
30
|
s.add_dependency('mime-types')
|
31
|
+
s.add_dependency('mimemagic', '~> 0.3.0')
|
33
32
|
|
33
|
+
s.add_development_dependency('activerecord', '>= 4.2.0')
|
34
34
|
s.add_development_dependency('shoulda')
|
35
|
+
s.add_development_dependency('rspec', '~> 3.0')
|
35
36
|
s.add_development_dependency('appraisal')
|
36
37
|
s.add_development_dependency('mocha')
|
37
|
-
s.add_development_dependency('aws-sdk'
|
38
|
+
s.add_development_dependency('aws-sdk-s3')
|
38
39
|
s.add_development_dependency('bourne')
|
39
|
-
s.add_development_dependency('
|
40
|
-
s.add_development_dependency('cucumber', '
|
41
|
-
s.add_development_dependency('aruba')
|
40
|
+
s.add_development_dependency('cucumber-rails')
|
41
|
+
s.add_development_dependency('cucumber-expressions', '4.0.3') # TODO: investigate failures on 4.0.4
|
42
|
+
s.add_development_dependency('aruba', '~> 0.9.0')
|
42
43
|
s.add_development_dependency('nokogiri')
|
43
44
|
s.add_development_dependency('capybara')
|
44
45
|
s.add_development_dependency('bundler')
|
45
|
-
s.add_development_dependency('
|
46
|
-
s.add_development_dependency('fog'
|
47
|
-
s.add_development_dependency('pry')
|
46
|
+
s.add_development_dependency('fog-aws')
|
47
|
+
s.add_development_dependency('fog-local')
|
48
48
|
s.add_development_dependency('launchy')
|
49
49
|
s.add_development_dependency('rake')
|
50
50
|
s.add_development_dependency('fakeweb')
|
51
51
|
s.add_development_dependency('railties')
|
52
|
-
s.add_development_dependency('
|
52
|
+
s.add_development_dependency('generator_spec')
|
53
|
+
s.add_development_dependency('timecop')
|
53
54
|
end
|
data/shoulda_macros/paperclip.rb
CHANGED
@@ -103,7 +103,11 @@ module Paperclip
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
-
if defined?(
|
106
|
+
if defined?(ActionDispatch::Integration::Session)
|
107
|
+
class ActionDispatch::IntegrationTest::Session #:nodoc:
|
108
|
+
include Paperclip::Shoulda
|
109
|
+
end
|
110
|
+
elsif defined?(ActionController::Integration::Session)
|
107
111
|
class ActionController::Integration::Session #:nodoc:
|
108
112
|
include Paperclip::Shoulda
|
109
113
|
end
|
@@ -119,6 +123,12 @@ else
|
|
119
123
|
end
|
120
124
|
end
|
121
125
|
|
122
|
-
|
123
|
-
|
126
|
+
if defined?(Minitest)
|
127
|
+
class Minitest::Unit::TestCase #:nodoc:
|
128
|
+
extend Paperclip::Shoulda
|
129
|
+
end
|
130
|
+
elsif defined?(Test)
|
131
|
+
class Test::Unit::TestCase #:nodoc:
|
132
|
+
extend Paperclip::Shoulda
|
133
|
+
end
|
124
134
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Attachment Definitions" do
|
4
|
+
it 'returns all of the attachments on the class' do
|
5
|
+
reset_class "Dummy"
|
6
|
+
Dummy.has_attached_file :avatar, {path: "abc"}
|
7
|
+
Dummy.has_attached_file :other_attachment, {url: "123"}
|
8
|
+
Dummy.do_not_validate_attachment_file_type :avatar
|
9
|
+
expected = {avatar: {path: "abc"}, other_attachment: {url: "123"}}
|
10
|
+
|
11
|
+
expect(Dummy.attachment_definitions).to eq expected
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Attachment Processing' do
|
4
|
+
before { rebuild_class }
|
5
|
+
|
6
|
+
context 'using validates_attachment_content_type' do
|
7
|
+
it 'processes attachments given a valid assignment' do
|
8
|
+
file = File.new(fixture_file("5k.png"))
|
9
|
+
Dummy.validates_attachment_content_type :avatar, content_type: "image/png"
|
10
|
+
instance = Dummy.new
|
11
|
+
attachment = instance.avatar
|
12
|
+
attachment.expects(:post_process_styles)
|
13
|
+
|
14
|
+
attachment.assign(file)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'does not process attachments given an invalid assignment with :not' do
|
18
|
+
file = File.new(fixture_file("5k.png"))
|
19
|
+
Dummy.validates_attachment_content_type :avatar, not: "image/png"
|
20
|
+
instance = Dummy.new
|
21
|
+
attachment = instance.avatar
|
22
|
+
attachment.expects(:post_process_styles).never
|
23
|
+
|
24
|
+
attachment.assign(file)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'does not process attachments given an invalid assignment with :content_type' do
|
28
|
+
file = File.new(fixture_file("5k.png"))
|
29
|
+
Dummy.validates_attachment_content_type :avatar, content_type: "image/tiff"
|
30
|
+
instance = Dummy.new
|
31
|
+
attachment = instance.avatar
|
32
|
+
attachment.expects(:post_process_styles).never
|
33
|
+
|
34
|
+
attachment.assign(file)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'allows what would be an invalid assignment when validation :if clause returns false' do
|
38
|
+
invalid_assignment = File.new(fixture_file("5k.png"))
|
39
|
+
Dummy.validates_attachment_content_type :avatar, content_type: "image/tiff", if: lambda{false}
|
40
|
+
instance = Dummy.new
|
41
|
+
attachment = instance.avatar
|
42
|
+
attachment.expects(:post_process_styles)
|
43
|
+
|
44
|
+
attachment.assign(invalid_assignment)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'using validates_attachment' do
|
49
|
+
it 'processes attachments given a valid assignment' do
|
50
|
+
file = File.new(fixture_file("5k.png"))
|
51
|
+
Dummy.validates_attachment :avatar, content_type: {content_type: "image/png"}
|
52
|
+
instance = Dummy.new
|
53
|
+
attachment = instance.avatar
|
54
|
+
attachment.expects(:post_process_styles)
|
55
|
+
|
56
|
+
attachment.assign(file)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'does not process attachments given an invalid assignment with :not' do
|
60
|
+
file = File.new(fixture_file("5k.png"))
|
61
|
+
Dummy.validates_attachment :avatar, content_type: {not: "image/png"}
|
62
|
+
instance = Dummy.new
|
63
|
+
attachment = instance.avatar
|
64
|
+
attachment.expects(:post_process_styles).never
|
65
|
+
|
66
|
+
attachment.assign(file)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'does not process attachments given an invalid assignment with :content_type' do
|
70
|
+
file = File.new(fixture_file("5k.png"))
|
71
|
+
Dummy.validates_attachment :avatar, content_type: {content_type: "image/tiff"}
|
72
|
+
instance = Dummy.new
|
73
|
+
attachment = instance.avatar
|
74
|
+
attachment.expects(:post_process_styles).never
|
75
|
+
|
76
|
+
attachment.assign(file)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Attachment Registry' do
|
4
|
+
before do
|
5
|
+
Paperclip::AttachmentRegistry.clear
|
6
|
+
end
|
7
|
+
|
8
|
+
context '.names_for' do
|
9
|
+
it 'includes attachment names for the given class' do
|
10
|
+
foo = Class.new
|
11
|
+
Paperclip::AttachmentRegistry.register(foo, :avatar, {})
|
12
|
+
|
13
|
+
assert_equal [:avatar], Paperclip::AttachmentRegistry.names_for(foo)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'does not include attachment names for other classes' do
|
17
|
+
foo = Class.new
|
18
|
+
bar = Class.new
|
19
|
+
Paperclip::AttachmentRegistry.register(foo, :avatar, {})
|
20
|
+
Paperclip::AttachmentRegistry.register(bar, :lover, {})
|
21
|
+
|
22
|
+
assert_equal [:lover], Paperclip::AttachmentRegistry.names_for(bar)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'produces the empty array for a missing key' do
|
26
|
+
assert_empty Paperclip::AttachmentRegistry.names_for(Class.new)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '.each_definition' do
|
31
|
+
it 'calls the block with the class, attachment name, and options' do
|
32
|
+
foo = Class.new
|
33
|
+
expected_accumulations = [
|
34
|
+
[foo, :avatar, { yo: "greeting" }],
|
35
|
+
[foo, :greeter, { ciao: "greeting" }]
|
36
|
+
]
|
37
|
+
expected_accumulations.each do |args|
|
38
|
+
Paperclip::AttachmentRegistry.register(*args)
|
39
|
+
end
|
40
|
+
accumulations = []
|
41
|
+
|
42
|
+
Paperclip::AttachmentRegistry.each_definition do |*args|
|
43
|
+
accumulations << args
|
44
|
+
end
|
45
|
+
|
46
|
+
assert_equal expected_accumulations, accumulations
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context '.definitions_for' do
|
51
|
+
it 'produces the attachment name and options' do
|
52
|
+
expected_definitions = {
|
53
|
+
avatar: { yo: "greeting" },
|
54
|
+
greeter: { ciao: "greeting" }
|
55
|
+
}
|
56
|
+
foo = Class.new
|
57
|
+
Paperclip::AttachmentRegistry.register(
|
58
|
+
foo,
|
59
|
+
:avatar,
|
60
|
+
yo: "greeting"
|
61
|
+
)
|
62
|
+
Paperclip::AttachmentRegistry.register(
|
63
|
+
foo,
|
64
|
+
:greeter,
|
65
|
+
ciao: "greeting"
|
66
|
+
)
|
67
|
+
|
68
|
+
definitions = Paperclip::AttachmentRegistry.definitions_for(foo)
|
69
|
+
|
70
|
+
assert_equal expected_definitions, definitions
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'produces defintions for subclasses' do
|
74
|
+
expected_definitions = { avatar: { yo: "greeting" } }
|
75
|
+
foo = Class.new
|
76
|
+
bar = Class.new(foo)
|
77
|
+
Paperclip::AttachmentRegistry.register(
|
78
|
+
foo,
|
79
|
+
:avatar,
|
80
|
+
expected_definitions[:avatar]
|
81
|
+
)
|
82
|
+
|
83
|
+
definitions = Paperclip::AttachmentRegistry.definitions_for(bar)
|
84
|
+
|
85
|
+
assert_equal expected_definitions, definitions
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'produces defintions for subclasses but deep merging them' do
|
89
|
+
foo_definitions = { avatar: { yo: "greeting" } }
|
90
|
+
bar_definitions = { avatar: { ciao: "greeting" } }
|
91
|
+
expected_definitions = {
|
92
|
+
avatar: {
|
93
|
+
yo: "greeting",
|
94
|
+
ciao: "greeting"
|
95
|
+
}
|
96
|
+
}
|
97
|
+
foo = Class.new
|
98
|
+
bar = Class.new(foo)
|
99
|
+
Paperclip::AttachmentRegistry.register(
|
100
|
+
foo,
|
101
|
+
:avatar,
|
102
|
+
foo_definitions[:avatar]
|
103
|
+
)
|
104
|
+
Paperclip::AttachmentRegistry.register(
|
105
|
+
bar,
|
106
|
+
:avatar,
|
107
|
+
bar_definitions[:avatar]
|
108
|
+
)
|
109
|
+
|
110
|
+
definitions = Paperclip::AttachmentRegistry.definitions_for(bar)
|
111
|
+
|
112
|
+
assert_equal expected_definitions, definitions
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'allows subclasses to override attachment defitions' do
|
116
|
+
foo_definitions = { avatar: { yo: "greeting" } }
|
117
|
+
bar_definitions = { avatar: { yo: "hello" } }
|
118
|
+
|
119
|
+
expected_definitions = {
|
120
|
+
avatar: {
|
121
|
+
yo: "hello"
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
foo = Class.new
|
126
|
+
bar = Class.new(foo)
|
127
|
+
Paperclip::AttachmentRegistry.register(
|
128
|
+
foo,
|
129
|
+
:avatar,
|
130
|
+
foo_definitions[:avatar]
|
131
|
+
)
|
132
|
+
Paperclip::AttachmentRegistry.register(
|
133
|
+
bar,
|
134
|
+
:avatar,
|
135
|
+
bar_definitions[:avatar]
|
136
|
+
)
|
137
|
+
|
138
|
+
definitions = Paperclip::AttachmentRegistry.definitions_for(bar)
|
139
|
+
|
140
|
+
assert_equal expected_definitions, definitions
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context '.clear' do
|
145
|
+
it 'removes all of the existing attachment definitions' do
|
146
|
+
foo = Class.new
|
147
|
+
Paperclip::AttachmentRegistry.register(
|
148
|
+
foo,
|
149
|
+
:greeter,
|
150
|
+
ciao: "greeting"
|
151
|
+
)
|
152
|
+
|
153
|
+
Paperclip::AttachmentRegistry.clear
|
154
|
+
|
155
|
+
assert_empty Paperclip::AttachmentRegistry.names_for(foo)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|