paperclip 3.5.4 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (216) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +3 -0
  4. data/.gitignore +0 -6
  5. data/.hound.yml +1055 -0
  6. data/.rubocop.yml +1 -0
  7. data/.travis.yml +17 -20
  8. data/Appraisals +4 -16
  9. data/CONTRIBUTING.md +29 -13
  10. data/Gemfile +11 -3
  11. data/LICENSE +1 -3
  12. data/MIGRATING-ES.md +317 -0
  13. data/MIGRATING.md +375 -0
  14. data/NEWS +262 -49
  15. data/README.md +496 -169
  16. data/RELEASING.md +17 -0
  17. data/Rakefile +6 -8
  18. data/UPGRADING +12 -9
  19. data/features/basic_integration.feature +27 -8
  20. data/features/migration.feature +0 -24
  21. data/features/step_definitions/attachment_steps.rb +44 -36
  22. data/features/step_definitions/html_steps.rb +2 -2
  23. data/features/step_definitions/rails_steps.rb +68 -37
  24. data/features/step_definitions/s3_steps.rb +2 -2
  25. data/features/step_definitions/web_steps.rb +1 -103
  26. data/features/support/env.rb +3 -2
  27. data/features/support/file_helpers.rb +2 -2
  28. data/features/support/fixtures/gemfile.txt +1 -1
  29. data/features/support/paths.rb +1 -1
  30. data/features/support/rails.rb +2 -25
  31. data/gemfiles/4.2.gemfile +17 -0
  32. data/gemfiles/5.0.gemfile +17 -0
  33. data/lib/generators/paperclip/paperclip_generator.rb +9 -3
  34. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +2 -2
  35. data/lib/paperclip/attachment.rb +170 -52
  36. data/lib/paperclip/attachment_registry.rb +3 -2
  37. data/lib/paperclip/callbacks.rb +13 -1
  38. data/lib/paperclip/content_type_detector.rb +26 -22
  39. data/lib/paperclip/errors.rb +8 -1
  40. data/lib/paperclip/file_command_content_type_detector.rb +6 -8
  41. data/lib/paperclip/filename_cleaner.rb +0 -1
  42. data/lib/paperclip/geometry_detector_factory.rb +6 -4
  43. data/lib/paperclip/geometry_parser_factory.rb +1 -1
  44. data/lib/paperclip/glue.rb +1 -1
  45. data/lib/paperclip/has_attached_file.rb +17 -1
  46. data/lib/paperclip/helpers.rb +15 -11
  47. data/lib/paperclip/interpolations/plural_cache.rb +6 -5
  48. data/lib/paperclip/interpolations.rb +31 -13
  49. data/lib/paperclip/io_adapters/abstract_adapter.rb +34 -5
  50. data/lib/paperclip/io_adapters/attachment_adapter.rb +19 -8
  51. data/lib/paperclip/io_adapters/data_uri_adapter.rb +11 -16
  52. data/lib/paperclip/io_adapters/empty_string_adapter.rb +5 -4
  53. data/lib/paperclip/io_adapters/file_adapter.rb +12 -6
  54. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +8 -7
  55. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -6
  56. data/lib/paperclip/io_adapters/nil_adapter.rb +8 -5
  57. data/lib/paperclip/io_adapters/registry.rb +6 -2
  58. data/lib/paperclip/io_adapters/stringio_adapter.rb +15 -16
  59. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +10 -6
  60. data/lib/paperclip/io_adapters/uri_adapter.rb +43 -19
  61. data/lib/paperclip/locales/en.yml +1 -0
  62. data/lib/paperclip/logger.rb +1 -1
  63. data/lib/paperclip/matchers/have_attached_file_matcher.rb +2 -1
  64. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +4 -4
  65. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +2 -1
  66. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +2 -1
  67. data/lib/paperclip/media_type_spoof_detector.rb +93 -0
  68. data/lib/paperclip/processor.rb +15 -43
  69. data/lib/paperclip/processor_helpers.rb +50 -0
  70. data/lib/paperclip/rails_environment.rb +25 -0
  71. data/lib/paperclip/schema.rb +10 -8
  72. data/lib/paperclip/storage/filesystem.rb +14 -3
  73. data/lib/paperclip/storage/fog.rb +38 -20
  74. data/lib/paperclip/storage/s3.rb +124 -73
  75. data/lib/paperclip/style.rb +8 -3
  76. data/lib/paperclip/tempfile_factory.rb +5 -1
  77. data/lib/paperclip/thumbnail.rb +34 -19
  78. data/lib/paperclip/url_generator.rb +26 -14
  79. data/lib/paperclip/validators/attachment_content_type_validator.rb +4 -0
  80. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  81. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  82. data/lib/paperclip/validators/attachment_presence_validator.rb +4 -0
  83. data/lib/paperclip/validators/attachment_size_validator.rb +5 -3
  84. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +31 -0
  85. data/lib/paperclip/validators.rb +11 -4
  86. data/lib/paperclip/version.rb +3 -1
  87. data/lib/paperclip.rb +31 -11
  88. data/lib/tasks/paperclip.rake +34 -5
  89. data/paperclip.gemspec +21 -16
  90. data/shoulda_macros/paperclip.rb +0 -1
  91. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  92. data/{test/attachment_processing_test.rb → spec/paperclip/attachment_processing_spec.rb} +17 -21
  93. data/spec/paperclip/attachment_registry_spec.rb +158 -0
  94. data/{test/attachment_test.rb → spec/paperclip/attachment_spec.rb} +519 -409
  95. data/{test/content_type_detector_test.rb → spec/paperclip/content_type_detector_spec.rb} +17 -20
  96. data/spec/paperclip/file_command_content_type_detector_spec.rb +40 -0
  97. data/spec/paperclip/filename_cleaner_spec.rb +13 -0
  98. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  99. data/{test/geometry_parser_test.rb → spec/paperclip/geometry_parser_spec.rb} +27 -27
  100. data/{test/geometry_test.rb → spec/paperclip/geometry_spec.rb} +50 -52
  101. data/spec/paperclip/glue_spec.rb +44 -0
  102. data/spec/paperclip/has_attached_file_spec.rb +158 -0
  103. data/{test/integration_test.rb → spec/paperclip/integration_spec.rb} +174 -129
  104. data/{test/interpolations_test.rb → spec/paperclip/interpolations_spec.rb} +79 -46
  105. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +160 -0
  106. data/{test/io_adapters/attachment_adapter_test.rb → spec/paperclip/io_adapters/attachment_adapter_spec.rb} +33 -32
  107. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +89 -0
  108. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  109. data/{test/io_adapters/file_adapter_test.rb → spec/paperclip/io_adapters/file_adapter_spec.rb} +38 -42
  110. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +138 -0
  111. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  112. data/{test/io_adapters/nil_adapter_test.rb → spec/paperclip/io_adapters/nil_adapter_spec.rb} +7 -7
  113. data/{test/io_adapters/registry_test.rb → spec/paperclip/io_adapters/registry_spec.rb} +12 -9
  114. data/{test/io_adapters/stringio_adapter_test.rb → spec/paperclip/io_adapters/stringio_adapter_spec.rb} +21 -18
  115. data/{test/io_adapters/uploaded_file_adapter_test.rb → spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb} +46 -46
  116. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +220 -0
  117. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  118. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +109 -0
  119. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  120. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  121. data/spec/paperclip/media_type_spoof_detector_spec.rb +120 -0
  122. data/spec/paperclip/meta_class_spec.rb +30 -0
  123. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  124. data/spec/paperclip/paperclip_spec.rb +192 -0
  125. data/spec/paperclip/plural_cache_spec.rb +37 -0
  126. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  127. data/{test/processor_test.rb → spec/paperclip/processor_spec.rb} +7 -7
  128. data/spec/paperclip/rails_environment_spec.rb +33 -0
  129. data/{test/rake_test.rb → spec/paperclip/rake_spec.rb} +15 -15
  130. data/spec/paperclip/schema_spec.rb +248 -0
  131. data/{test/storage/filesystem_test.rb → spec/paperclip/storage/filesystem_spec.rb} +18 -18
  132. data/spec/paperclip/storage/fog_spec.rb +566 -0
  133. data/spec/paperclip/storage/s3_live_spec.rb +188 -0
  134. data/spec/paperclip/storage/s3_spec.rb +1693 -0
  135. data/spec/paperclip/style_spec.rb +254 -0
  136. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  137. data/spec/paperclip/tempfile_spec.rb +35 -0
  138. data/{test/thumbnail_test.rb → spec/paperclip/thumbnail_spec.rb} +158 -137
  139. data/spec/paperclip/url_generator_spec.rb +221 -0
  140. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  141. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  142. data/{test/validators/attachment_presence_validator_test.rb → spec/paperclip/validators/attachment_presence_validator_spec.rb} +20 -20
  143. data/{test/validators/attachment_size_validator_test.rb → spec/paperclip/validators/attachment_size_validator_spec.rb} +77 -64
  144. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  145. data/spec/paperclip/validators_spec.rb +164 -0
  146. data/spec/spec_helper.rb +46 -0
  147. data/spec/support/assertions.rb +82 -0
  148. data/spec/support/fake_model.rb +25 -0
  149. data/spec/support/fake_rails.rb +12 -0
  150. data/spec/support/fixtures/empty.html +1 -0
  151. data/spec/support/fixtures/empty.xlsx +0 -0
  152. data/spec/support/fixtures/spaced file.jpg +0 -0
  153. data/spec/support/matchers/accept.rb +5 -0
  154. data/spec/support/matchers/exist.rb +5 -0
  155. data/spec/support/matchers/have_column.rb +23 -0
  156. data/{test → spec}/support/mock_attachment.rb +2 -0
  157. data/{test → spec}/support/mock_url_generator_builder.rb +2 -2
  158. data/spec/support/model_reconstruction.rb +68 -0
  159. data/spec/support/reporting.rb +11 -0
  160. data/spec/support/test_data.rb +13 -0
  161. data/spec/support/version_helper.rb +9 -0
  162. metadata +344 -226
  163. data/RUNNING_TESTS.md +0 -4
  164. data/cucumber/paperclip_steps.rb +0 -6
  165. data/gemfiles/3.0.gemfile +0 -11
  166. data/gemfiles/3.1.gemfile +0 -11
  167. data/gemfiles/3.2.gemfile +0 -11
  168. data/gemfiles/4.0.gemfile +0 -11
  169. data/test/attachment_definitions_test.rb +0 -12
  170. data/test/attachment_registry_test.rb +0 -88
  171. data/test/file_command_content_type_detector_test.rb +0 -27
  172. data/test/filename_cleaner_test.rb +0 -14
  173. data/test/generator_test.rb +0 -84
  174. data/test/geometry_detector_test.rb +0 -24
  175. data/test/has_attached_file_test.rb +0 -125
  176. data/test/helper.rb +0 -232
  177. data/test/io_adapters/abstract_adapter_test.rb +0 -58
  178. data/test/io_adapters/data_uri_adapter_test.rb +0 -74
  179. data/test/io_adapters/empty_string_adapter_test.rb +0 -18
  180. data/test/io_adapters/http_url_proxy_adapter_test.rb +0 -102
  181. data/test/io_adapters/identity_adapter_test.rb +0 -8
  182. data/test/io_adapters/uri_adapter_test.rb +0 -102
  183. data/test/matchers/have_attached_file_matcher_test.rb +0 -24
  184. data/test/matchers/validate_attachment_content_type_matcher_test.rb +0 -110
  185. data/test/matchers/validate_attachment_presence_matcher_test.rb +0 -69
  186. data/test/matchers/validate_attachment_size_matcher_test.rb +0 -86
  187. data/test/meta_class_test.rb +0 -32
  188. data/test/paperclip_missing_attachment_styles_test.rb +0 -90
  189. data/test/paperclip_test.rb +0 -217
  190. data/test/plural_cache_test.rb +0 -36
  191. data/test/schema_test.rb +0 -200
  192. data/test/storage/fog_test.rb +0 -473
  193. data/test/storage/s3_live_test.rb +0 -179
  194. data/test/storage/s3_test.rb +0 -1356
  195. data/test/style_test.rb +0 -213
  196. data/test/support/mock_model.rb +0 -2
  197. data/test/tempfile_factory_test.rb +0 -17
  198. data/test/url_generator_test.rb +0 -187
  199. data/test/validators/attachment_content_type_validator_test.rb +0 -324
  200. data/test/validators_test.rb +0 -61
  201. /data/{test → spec}/database.yml +0 -0
  202. /data/{test → spec/support}/fixtures/12k.png +0 -0
  203. /data/{test → spec/support}/fixtures/50x50.png +0 -0
  204. /data/{test → spec/support}/fixtures/5k.png +0 -0
  205. /data/{test → spec/support}/fixtures/animated +0 -0
  206. /data/{test → spec/support}/fixtures/animated.gif +0 -0
  207. /data/{test → spec/support}/fixtures/animated.unknown +0 -0
  208. /data/{test → spec/support}/fixtures/bad.png +0 -0
  209. /data/{test → spec/support}/fixtures/fog.yml +0 -0
  210. /data/{test → spec/support}/fixtures/rotated.jpg +0 -0
  211. /data/{test → spec/support}/fixtures/s3.yml +0 -0
  212. /data/{test → spec/support}/fixtures/spaced file.png +0 -0
  213. /data/{test → spec/support}/fixtures/text.txt +0 -0
  214. /data/{test → spec/support}/fixtures/twopage.pdf +0 -0
  215. /data/{test → spec/support}/fixtures/uppercase.PNG +0 -0
  216. /data/{test → spec}/support/mock_interpolator.rb +0 -0
@@ -103,107 +103,5 @@ When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field|
103
103
  end
104
104
 
105
105
  Then /^(?:|I )should see "([^"]*)"$/ do |text|
106
- if page.respond_to? :should
107
- page.should have_content(text)
108
- else
109
- assert page.has_content?(text)
110
- end
111
- end
112
-
113
- Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
114
- regexp = Regexp.new(regexp)
115
-
116
- if page.respond_to? :should
117
- page.should have_xpath('//*', :text => regexp)
118
- else
119
- assert page.has_xpath?('//*', :text => regexp)
120
- end
121
- end
122
-
123
- Then /^(?:|I )should not see "([^"]*)"$/ do |text|
124
- if page.respond_to? :should
125
- page.should have_no_content(text)
126
- else
127
- assert page.has_no_content?(text)
128
- end
129
- end
130
-
131
- Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
132
- regexp = Regexp.new(regexp)
133
-
134
- if page.respond_to? :should
135
- page.should have_no_xpath('//*', :text => regexp)
136
- else
137
- assert page.has_no_xpath?('//*', :text => regexp)
138
- end
139
- end
140
-
141
- Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
142
- with_scope(parent) do
143
- field = find_field(field)
144
- if field.value.respond_to? :should
145
- field.value.should =~ /#{value}/
146
- else
147
- assert_match(/#{value}/, field.value)
148
- end
149
- end
150
- end
151
-
152
- Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value|
153
- with_scope(parent) do
154
- field = find_field(field)
155
- if field.value.respond_to? :should_not
156
- field.value.should_not =~ /#{value}/
157
- else
158
- assert_no_match(/#{value}/, field.value)
159
- end
160
- end
161
- end
162
-
163
- Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent|
164
- with_scope(parent) do
165
- field_checked = find_field(label)['checked']
166
- if field_checked.respond_to? :should
167
- field_checked.should be_true
168
- else
169
- assert field_checked
170
- end
171
- end
172
- end
173
-
174
- Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent|
175
- with_scope(parent) do
176
- field_checked = find_field(label)['checked']
177
- if field_checked.respond_to? :should
178
- field_checked.should be_false
179
- else
180
- assert !field_checked
181
- end
182
- end
183
- end
184
-
185
- Then /^(?:|I )should be on (.+)$/ do |page_name|
186
- current_path = URI.parse(current_url).path
187
- if current_path.respond_to? :should
188
- current_path.should == path_to(page_name)
189
- else
190
- assert_equal path_to(page_name), current_path
191
- end
192
- end
193
-
194
- Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
195
- query = URI.parse(current_url).query
196
- actual_params = query ? CGI.parse(query) : {}
197
- expected_params = {}
198
- expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
199
-
200
- if actual_params.respond_to? :should
201
- actual_params.should == expected_params
202
- else
203
- assert_equal expected_params, actual_params
204
- end
205
- end
206
-
207
- Then /^show me the page$/ do
208
- save_and_open_page
106
+ expect(page).to have_content(text)
209
107
  end
@@ -1,11 +1,12 @@
1
1
  require 'aruba/cucumber'
2
2
  require 'capybara/cucumber'
3
- require 'test/unit/assertions'
3
+ require 'rspec/matchers'
4
4
 
5
5
  $CUCUMBER=1
6
6
 
7
- World(Test::Unit::Assertions)
7
+ World(RSpec::Matchers)
8
8
 
9
9
  Before do
10
+ aruba.config.command_launcher = ENV.fetch("DEBUG", nil) ? :debug : :spawn
10
11
  @aruba_timeout_seconds = 120
11
12
  end
@@ -1,6 +1,6 @@
1
1
  module FileHelpers
2
2
  def append_to(path, contents)
3
- in_current_dir do
3
+ cd(".") do
4
4
  File.open(path, "a") do |file|
5
5
  file.puts
6
6
  file.puts contents
@@ -13,7 +13,7 @@ module FileHelpers
13
13
  end
14
14
 
15
15
  def comment_out_gem_in_gemfile(gemname)
16
- in_current_dir do
16
+ cd(".") do
17
17
  gemfile = File.read("Gemfile")
18
18
  gemfile.sub!(/^(\s*)(gem\s*['"]#{gemname})/, "\\1# \\2")
19
19
  File.open("Gemfile", 'w'){ |file| file.write(gemfile) }
@@ -2,4 +2,4 @@ source "http://rubygems.org"
2
2
 
3
3
  gem "rails", "RAILS_VERSION"
4
4
  gem "rdoc"
5
- gem "sqlite3"
5
+ gem "sqlite3", "1.3.8"
@@ -17,7 +17,7 @@ module NavigationHelpers
17
17
  page_name =~ /the (.*) page/
18
18
  path_components = $1.split(/\s+/)
19
19
  self.send(path_components.push('path').join('_').to_sym)
20
- rescue Object => e
20
+ rescue Object
21
21
  raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
22
22
  "Now, go and add a mapping in #{__FILE__}"
23
23
  end
@@ -6,7 +6,8 @@ ORIGINAL_BUNDLE_VARS = Hash[ENV.select{ |key,value| BUNDLE_ENV_VARS.include?(key
6
6
  ENV['RAILS_ENV'] = 'test'
7
7
 
8
8
  Before do
9
- ENV['BUNDLE_GEMFILE'] = File.join(Dir.pwd, ENV['BUNDLE_GEMFILE']) unless ENV['BUNDLE_GEMFILE'].start_with?(Dir.pwd)
9
+ gemfile = ENV['BUNDLE_GEMFILE'].to_s
10
+ ENV['BUNDLE_GEMFILE'] = File.join(Dir.pwd, gemfile) unless gemfile.start_with?(Dir.pwd)
10
11
  @framework_version = nil
11
12
  end
12
13
 
@@ -34,29 +35,5 @@ module RailsCommandHelpers
34
35
  def framework_major_version
35
36
  framework_version.split(".").first.to_i
36
37
  end
37
-
38
- def using_protected_attributes?
39
- framework_major_version < 4
40
- end
41
-
42
- def new_application_command
43
- "rails new"
44
- end
45
-
46
- def generator_command
47
- if framework_major_version >= 4
48
- "rails generate"
49
- else
50
- "script/rails generate"
51
- end
52
- end
53
-
54
- def runner_command
55
- if framework_major_version >= 4
56
- "rails runner"
57
- else
58
- "script/rails runner"
59
- end
60
- end
61
38
  end
62
39
  World(RailsCommandHelpers)
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", "~> 1.3.8", :platforms => :ruby
6
+ gem "pry"
7
+ gem "rails", "~> 4.2.0"
8
+
9
+ group :development, :test do
10
+ gem "activerecord-import"
11
+ gem "mime-types"
12
+ gem "builder"
13
+ gem "rubocop", :require => false
14
+ gem "rspec"
15
+ end
16
+
17
+ gemspec :path => "../"
@@ -0,0 +1,17 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", "~> 1.3.8", :platforms => :ruby
6
+ gem "pry"
7
+ gem "rails", "~> 5.0.0"
8
+
9
+ group :development, :test do
10
+ gem "activerecord-import"
11
+ gem "mime-types"
12
+ gem "builder"
13
+ gem "rubocop", :require => false
14
+ gem "rspec"
15
+ end
16
+
17
+ gemspec :path => "../"
@@ -13,11 +13,11 @@ class PaperclipGenerator < ActiveRecord::Generators::Base
13
13
  end
14
14
 
15
15
  def generate_migration
16
- migration_template "paperclip_migration.rb.erb", "db/migrate/#{migration_file_name}"
16
+ migration_template("paperclip_migration.rb.erb",
17
+ "db/migrate/#{migration_file_name}",
18
+ migration_version: migration_version)
17
19
  end
18
20
 
19
- protected
20
-
21
21
  def migration_name
22
22
  "add_attachment_#{attachment_names.join("_")}_to_#{name.underscore.pluralize}"
23
23
  end
@@ -29,4 +29,10 @@ class PaperclipGenerator < ActiveRecord::Generators::Base
29
29
  def migration_class_name
30
30
  migration_name.camelize
31
31
  end
32
+
33
+ def migration_version
34
+ if Rails.version.start_with? "5"
35
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
36
+ end
37
+ end
32
38
  end
@@ -1,4 +1,4 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
2
  def self.up
3
3
  change_table :<%= table_name %> do |t|
4
4
  <% attachment_names.each do |attachment| -%>
@@ -9,7 +9,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration
9
9
 
10
10
  def self.down
11
11
  <% attachment_names.each do |attachment| -%>
12
- drop_attached_file :<%= table_name %>, :<%= attachment %>
12
+ remove_attachment :<%= table_name %>, :<%= attachment %>
13
13
  <% end -%>
14
14
  end
15
15
  end
@@ -1,6 +1,7 @@
1
- # encoding: utf-8
2
1
  require 'uri'
3
2
  require 'paperclip/url_generator'
3
+ require 'active_support/deprecation'
4
+ require 'active_support/core_ext/string/inflections'
4
5
 
5
6
  module Paperclip
6
7
  # The Attachment class manages the files for a given attachment. It saves
@@ -30,12 +31,14 @@ module Paperclip
30
31
  :use_default_time_zone => true,
31
32
  :use_timestamp => true,
32
33
  :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
34
+ :validate_media_type => true,
35
+ :adapter_options => { hash_digest: Digest::MD5 },
33
36
  :check_validity_before_processing => true
34
37
  }
35
38
  end
36
39
 
37
40
  attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny,
38
- :options, :interpolator, :source_file_options, :whiny
41
+ :options, :interpolator, :source_file_options
39
42
  attr_accessor :post_processing
40
43
 
41
44
  # Creates an Attachment object. +name+ is the name of the attachment,
@@ -47,7 +50,8 @@ module Paperclip
47
50
  # +url+ - a relative URL of the attachment. This is interpolated using +interpolator+
48
51
  # +path+ - where on the filesystem to store the attachment. This is interpolated using +interpolator+
49
52
  # +styles+ - a hash of options for processing the attachment. See +has_attached_file+ for the details
50
- # +only_process+ - style args to be run through the post-processor. This defaults to the empty list
53
+ # +only_process+ - style args to be run through the post-processor. This defaults to the empty list (which is
54
+ # a special case that indicates all styles should be processed)
51
55
  # +default_url+ - a URL for the missing image
52
56
  # +default_style+ - the style to use when an argument is not specified e.g. #url, #path
53
57
  # +storage+ - the storage mechanism. Defaults to :filesystem
@@ -66,7 +70,8 @@ module Paperclip
66
70
  # +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator
67
71
  # +escape_url+ - Perform URI escaping to URLs. Defaults to true
68
72
  def initialize(name, instance, options = {})
69
- @name = name
73
+ @name = name.to_sym
74
+ @name_string = name.to_s
70
75
  @instance = instance
71
76
 
72
77
  options = self.class.default_options.deep_merge(options)
@@ -78,7 +83,7 @@ module Paperclip
78
83
  @errors = {}
79
84
  @dirty = false
80
85
  @interpolator = options[:interpolator]
81
- @url_generator = options[:url_generator].new(self, @options)
86
+ @url_generator = options[:url_generator].new(self)
82
87
  @source_file_options = options[:source_file_options]
83
88
  @whiny = options[:whiny]
84
89
 
@@ -86,36 +91,30 @@ module Paperclip
86
91
  end
87
92
 
88
93
  # What gets called when you call instance.attachment = File. It clears
89
- # errors, assigns attributes, and processes the file. It
90
- # also queues up the previous file for deletion, to be flushed away on
91
- # #save of its host. In addition to form uploads, you can also assign
92
- # another Paperclip attachment:
94
+ # errors, assigns attributes, and processes the file. It also queues up the
95
+ # previous file for deletion, to be flushed away on #save of its host. In
96
+ # addition to form uploads, you can also assign another Paperclip
97
+ # attachment:
93
98
  # new_user.avatar = old_user.avatar
94
- def assign uploaded_file
99
+ def assign(uploaded_file)
100
+ @file = Paperclip.io_adapters.for(uploaded_file,
101
+ @options[:adapter_options])
95
102
  ensure_required_accessors!
96
- file = Paperclip.io_adapters.for(uploaded_file)
103
+ ensure_required_validations!
97
104
 
98
- return nil if not file.assignment?
99
- self.clear(*only_process)
100
- return nil if file.nil?
105
+ if @file.assignment?
106
+ clear(*only_process)
101
107
 
102
- @queued_for_write[:original] = file
103
- instance_write(:file_name, cleanup_filename(file.original_filename))
104
- instance_write(:content_type, file.content_type.to_s.strip)
105
- instance_write(:file_size, file.size)
106
- instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
107
- instance_write(:created_at, Time.now) if has_enabled_but_unset_created_at?
108
- instance_write(:updated_at, Time.now)
109
-
110
- @dirty = true
111
-
112
- post_process(*only_process) if post_processing
113
-
114
- # Reset the file size if the original file was reprocessed.
115
- instance_write(:file_size, @queued_for_write[:original].size)
116
- instance_write(:fingerprint, @queued_for_write[:original].fingerprint) if instance_respond_to?(:fingerprint)
117
- updater = :"#{name}_file_name_will_change!"
118
- instance.send updater if instance.respond_to? updater
108
+ if @file.nil?
109
+ nil
110
+ else
111
+ assign_attributes
112
+ post_process_file
113
+ reset_file_if_original_reprocessed
114
+ end
115
+ else
116
+ nil
117
+ end
119
118
  end
120
119
 
121
120
  # Returns the public URL of the attachment with a given style. This does
@@ -140,9 +139,8 @@ module Paperclip
140
139
  #
141
140
  # +#new(Paperclip::Attachment, options_hash)+
142
141
  # +#for(style_name, options_hash)+
143
- def url(style_name = default_style, options = {})
144
- default_options = {:timestamp => @options[:use_timestamp], :escape => @options[:escape_url]}
145
142
 
143
+ def url(style_name = default_style, options = {})
146
144
  if options == true || options == false # Backwards compatibility.
147
145
  @url_generator.for(style_name, default_options.merge(:timestamp => options))
148
146
  else
@@ -150,6 +148,13 @@ module Paperclip
150
148
  end
151
149
  end
152
150
 
151
+ def default_options
152
+ {
153
+ :timestamp => @options[:use_timestamp],
154
+ :escape => @options[:escape_url]
155
+ }
156
+ end
157
+
153
158
  # Alias to +url+ that allows using the expiring_url method provided by the cloud
154
159
  # storage implementations, but keep using filesystem storage for development and
155
160
  # testing.
@@ -166,6 +171,18 @@ module Paperclip
166
171
  path.respond_to?(:unescape) ? path.unescape : path
167
172
  end
168
173
 
174
+ # :nodoc:
175
+ def staged_path(style_name = default_style)
176
+ if staged?
177
+ @queued_for_write[style_name].path
178
+ end
179
+ end
180
+
181
+ # :nodoc:
182
+ def staged?
183
+ ! @queued_for_write.empty?
184
+ end
185
+
169
186
  # Alias to +url+
170
187
  def to_s style_name = default_style
171
188
  url(style_name)
@@ -222,6 +239,10 @@ module Paperclip
222
239
  # the instance's errors and returns false, cancelling the save.
223
240
  def save
224
241
  flush_deletes unless @options[:keep_old_files]
242
+ process = only_process
243
+ if process.any? && !process.include?(:original)
244
+ @queued_for_write.except!(:original)
245
+ end
225
246
  flush_writes
226
247
  @dirty = false
227
248
  true
@@ -308,7 +329,7 @@ module Paperclip
308
329
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
309
330
  end
310
331
 
311
- # This method really shouldn't be called that often. It's expected use is
332
+ # This method really shouldn't be called that often. Its expected use is
312
333
  # in the paperclip:refresh rake task and that's it. It will regenerate all
313
334
  # thumbnails forcefully, by reobtaining the original file and going through
314
335
  # the post-process again.
@@ -316,8 +337,15 @@ module Paperclip
316
337
  # inconsistencies in timing of S3 commands. It's possible that calling
317
338
  # #reprocess! will lose data if the files are not kept.
318
339
  def reprocess!(*style_args)
319
- saved_only_process, @options[:only_process] = @options[:only_process], style_args
320
- saved_preserve_files, @options[:preserve_files] = @options[:preserve_files], true
340
+ saved_flags = @options.slice(
341
+ :only_process,
342
+ :preserve_files,
343
+ :check_validity_before_processing
344
+ )
345
+ @options[:only_process] = style_args
346
+ @options[:preserve_files] = true
347
+ @options[:check_validity_before_processing] = false
348
+
321
349
  begin
322
350
  assign(self)
323
351
  save
@@ -326,14 +354,13 @@ module Paperclip
326
354
  warn "#{e} - skipping file."
327
355
  false
328
356
  ensure
329
- @options[:only_process] = saved_only_process
330
- @options[:preserve_files] = saved_preserve_files
357
+ @options.merge!(saved_flags)
331
358
  end
332
359
  end
333
360
 
334
361
  # Returns true if a file has been assigned.
335
362
  def file?
336
- !original_filename.blank?
363
+ original_filename.present?
337
364
  end
338
365
 
339
366
  alias :present? :file?
@@ -352,7 +379,7 @@ module Paperclip
352
379
  # instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance's
353
380
  # "avatar_file_name" field (assuming the attachment is called avatar).
354
381
  def instance_write(attr, value)
355
- setter = :"#{name}_#{attr}="
382
+ setter = :"#{@name_string}_#{attr}="
356
383
  if instance.respond_to?(setter)
357
384
  instance.send(setter, value)
358
385
  end
@@ -361,7 +388,7 @@ module Paperclip
361
388
  # Reads the attachment-specific attribute on the instance. See instance_write
362
389
  # for more details.
363
390
  def instance_read(attr)
364
- getter = :"#{name}_#{attr}"
391
+ getter = :"#{@name_string}_#{attr}"
365
392
  if instance.respond_to?(getter)
366
393
  instance.send(getter)
367
394
  end
@@ -373,10 +400,24 @@ module Paperclip
373
400
  @options[:path].respond_to?(:call) ? @options[:path].call(self) : @options[:path]
374
401
  end
375
402
 
403
+ def active_validator_classes
404
+ @instance.class.validators.map(&:class)
405
+ end
406
+
407
+ def missing_required_validator?
408
+ (active_validator_classes.flat_map(&:ancestors) & Paperclip::REQUIRED_VALIDATORS).empty?
409
+ end
410
+
411
+ def ensure_required_validations!
412
+ if missing_required_validator?
413
+ raise Paperclip::Errors::MissingRequiredValidatorError
414
+ end
415
+ end
416
+
376
417
  def ensure_required_accessors! #:nodoc:
377
418
  %w(file_name).each do |field|
378
- unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=")
379
- raise Paperclip::Error.new("#{@instance.class} model missing required attr_accessor for '#{name}_#{field}'")
419
+ unless @instance.respond_to?("#{@name_string}_#{field}") && @instance.respond_to?("#{@name_string}_#{field}=")
420
+ raise Paperclip::Error.new("#{@instance.class} model missing required attr_accessor for '#{@name_string}_#{field}'")
380
421
  end
381
422
  end
382
423
  end
@@ -395,6 +436,61 @@ module Paperclip
395
436
  self.extend(storage_module)
396
437
  end
397
438
 
439
+ def assign_attributes
440
+ @queued_for_write[:original] = @file
441
+ assign_file_information
442
+ assign_fingerprint { @file.fingerprint }
443
+ assign_timestamps
444
+ end
445
+
446
+ def assign_file_information
447
+ instance_write(:file_name, cleanup_filename(@file.original_filename))
448
+ instance_write(:content_type, @file.content_type.to_s.strip)
449
+ instance_write(:file_size, @file.size)
450
+ end
451
+
452
+ def assign_fingerprint
453
+ if instance_respond_to?(:fingerprint)
454
+ instance_write(:fingerprint, yield)
455
+ end
456
+ end
457
+
458
+ def assign_timestamps
459
+ if has_enabled_but_unset_created_at?
460
+ instance_write(:created_at, Time.now)
461
+ end
462
+
463
+ instance_write(:updated_at, Time.now)
464
+ end
465
+
466
+ def post_process_file
467
+ dirty!
468
+
469
+ if post_processing
470
+ post_process(*only_process)
471
+ end
472
+ end
473
+
474
+ def dirty!
475
+ @dirty = true
476
+ end
477
+
478
+ def reset_file_if_original_reprocessed
479
+ instance_write(:file_size, @queued_for_write[:original].size)
480
+ assign_fingerprint { @queued_for_write[:original].fingerprint }
481
+ reset_updater
482
+ end
483
+
484
+ def reset_updater
485
+ if instance.respond_to?(updater)
486
+ instance.send(updater)
487
+ end
488
+ end
489
+
490
+ def updater
491
+ :"#{name}_file_name_will_change!"
492
+ end
493
+
398
494
  def extra_options_for(style) #:nodoc:
399
495
  process_options(:convert_options, style)
400
496
  end
@@ -417,7 +513,7 @@ module Paperclip
417
513
 
418
514
  instance.run_paperclip_callbacks(:post_process) do
419
515
  instance.run_paperclip_callbacks(:"#{name}_post_process") do
420
- unless @options[:check_validity_before_processing] && instance.errors.any?
516
+ if !@options[:check_validity_before_processing] || !instance.errors.any?
421
517
  post_process_styles(*style_args)
422
518
  end
423
519
  end
@@ -434,16 +530,30 @@ module Paperclip
434
530
  def post_process_style(name, style) #:nodoc:
435
531
  begin
436
532
  raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
437
- @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor|
438
- Paperclip.processor(processor).make(file, style.processor_options, self)
533
+ intermediate_files = []
534
+ original = @queued_for_write[:original]
535
+
536
+ @queued_for_write[name] = style.processors.
537
+ reduce(original) do |file, processor|
538
+ file = Paperclip.processor(processor).make(file, style.processor_options, self)
539
+ intermediate_files << file unless file == @queued_for_write[:original]
540
+ # if we're processing the original, close + unlink the source tempfile
541
+ if name == :original
542
+ @queued_for_write[:original].close(true)
543
+ end
544
+ file
439
545
  end
546
+
440
547
  unadapted_file = @queued_for_write[name]
441
- @queued_for_write[name] = Paperclip.io_adapters.for(@queued_for_write[name])
548
+ @queued_for_write[name] = Paperclip.io_adapters.
549
+ for(@queued_for_write[name], @options[:adapter_options])
442
550
  unadapted_file.close if unadapted_file.respond_to?(:close)
443
551
  @queued_for_write[name]
444
- rescue Paperclip::Error => e
552
+ rescue Paperclip::Errors::NotIdentifiedByImageMagickError => e
445
553
  log("An error was received while processing: #{e.inspect}")
446
554
  (@errors[:processing] ||= []) << e.message if @options[:whiny]
555
+ ensure
556
+ unlink_files(intermediate_files)
447
557
  end
448
558
  end
449
559
 
@@ -482,17 +592,25 @@ module Paperclip
482
592
  end
483
593
  end
484
594
 
485
- # called by storage after the writes are flushed and before @queued_for_writes is cleared
595
+ # called by storage after the writes are flushed and before @queued_for_write is cleared
486
596
  def after_flush_writes
487
- @queued_for_write.each do |style, file|
597
+ unlink_files(@queued_for_write.values)
598
+ end
599
+
600
+ def unlink_files(files)
601
+ Array(files).each do |file|
488
602
  file.close unless file.closed?
489
- file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path)
603
+
604
+ begin
605
+ file.unlink if file.respond_to?(:unlink)
606
+ rescue Errno::ENOENT
607
+ end
490
608
  end
491
609
  end
492
610
 
493
611
  # You can either specifiy :restricted_characters or you can define your own
494
612
  # :filename_cleaner object. This object needs to respond to #call and takes
495
- # the filename that will be cleaned. It should return the cleaned filenme.
613
+ # the filename that will be cleaned. It should return the cleaned filename.
496
614
  def filename_cleaner
497
615
  @options[:filename_cleaner] || FilenameCleaner.new(@options[:restricted_characters])
498
616
  end
@@ -51,8 +51,9 @@ module Paperclip
51
51
  end
52
52
 
53
53
  def definitions_for(klass)
54
- klass.ancestors.each_with_object({}) do |ancestor, inherited_definitions|
55
- inherited_definitions.merge! @attachments[ancestor]
54
+ parent_classes = klass.ancestors.reverse
55
+ parent_classes.each_with_object({}) do |ancestor, inherited_definitions|
56
+ inherited_definitions.deep_merge! @attachments[ancestor]
56
57
  end
57
58
  end
58
59
  end