paperclip-fix 4.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.hound.yml +1066 -0
  4. data/.rubocop.yml +1 -0
  5. data/.travis.yml +22 -0
  6. data/Appraisals +11 -0
  7. data/CONTRIBUTING.md +75 -0
  8. data/Gemfile +21 -0
  9. data/LICENSE +24 -0
  10. data/NEWS +420 -0
  11. data/README.md +979 -0
  12. data/RELEASING.md +17 -0
  13. data/Rakefile +44 -0
  14. data/UPGRADING +14 -0
  15. data/cucumber/paperclip_steps.rb +6 -0
  16. data/features/basic_integration.feature +80 -0
  17. data/features/migration.feature +94 -0
  18. data/features/rake_tasks.feature +62 -0
  19. data/features/step_definitions/attachment_steps.rb +110 -0
  20. data/features/step_definitions/html_steps.rb +15 -0
  21. data/features/step_definitions/rails_steps.rb +236 -0
  22. data/features/step_definitions/s3_steps.rb +14 -0
  23. data/features/step_definitions/web_steps.rb +107 -0
  24. data/features/support/env.rb +11 -0
  25. data/features/support/fakeweb.rb +13 -0
  26. data/features/support/file_helpers.rb +34 -0
  27. data/features/support/fixtures/boot_config.txt +15 -0
  28. data/features/support/fixtures/gemfile.txt +5 -0
  29. data/features/support/fixtures/preinitializer.txt +20 -0
  30. data/features/support/paths.rb +28 -0
  31. data/features/support/rails.rb +63 -0
  32. data/features/support/selectors.rb +19 -0
  33. data/gemfiles/3.2.gemfile +19 -0
  34. data/gemfiles/4.1.gemfile +19 -0
  35. data/gemfiles/4.2.gemfile +19 -0
  36. data/lib/generators/paperclip/USAGE +8 -0
  37. data/lib/generators/paperclip/paperclip_generator.rb +30 -0
  38. data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +15 -0
  39. data/lib/paperclip/attachment.rb +608 -0
  40. data/lib/paperclip/attachment_registry.rb +59 -0
  41. data/lib/paperclip/callbacks.rb +40 -0
  42. data/lib/paperclip/content_type_detector.rb +79 -0
  43. data/lib/paperclip/deprecations.rb +42 -0
  44. data/lib/paperclip/errors.rb +32 -0
  45. data/lib/paperclip/file_command_content_type_detector.rb +30 -0
  46. data/lib/paperclip/filename_cleaner.rb +16 -0
  47. data/lib/paperclip/geometry.rb +158 -0
  48. data/lib/paperclip/geometry_detector_factory.rb +48 -0
  49. data/lib/paperclip/geometry_parser_factory.rb +31 -0
  50. data/lib/paperclip/glue.rb +17 -0
  51. data/lib/paperclip/has_attached_file.rb +109 -0
  52. data/lib/paperclip/helpers.rb +56 -0
  53. data/lib/paperclip/interpolations/plural_cache.rb +18 -0
  54. data/lib/paperclip/interpolations.rb +197 -0
  55. data/lib/paperclip/io_adapters/abstract_adapter.rb +47 -0
  56. data/lib/paperclip/io_adapters/attachment_adapter.rb +36 -0
  57. data/lib/paperclip/io_adapters/data_uri_adapter.rb +22 -0
  58. data/lib/paperclip/io_adapters/empty_string_adapter.rb +18 -0
  59. data/lib/paperclip/io_adapters/file_adapter.rb +22 -0
  60. data/lib/paperclip/io_adapters/http_url_proxy_adapter.rb +15 -0
  61. data/lib/paperclip/io_adapters/identity_adapter.rb +12 -0
  62. data/lib/paperclip/io_adapters/nil_adapter.rb +34 -0
  63. data/lib/paperclip/io_adapters/registry.rb +32 -0
  64. data/lib/paperclip/io_adapters/stringio_adapter.rb +33 -0
  65. data/lib/paperclip/io_adapters/uploaded_file_adapter.rb +42 -0
  66. data/lib/paperclip/io_adapters/uri_adapter.rb +63 -0
  67. data/lib/paperclip/locales/de.yml +18 -0
  68. data/lib/paperclip/locales/en.yml +18 -0
  69. data/lib/paperclip/locales/es.yml +18 -0
  70. data/lib/paperclip/locales/ja.yml +18 -0
  71. data/lib/paperclip/locales/pt-BR.yml +18 -0
  72. data/lib/paperclip/locales/zh-CN.yml +18 -0
  73. data/lib/paperclip/locales/zh-HK.yml +18 -0
  74. data/lib/paperclip/locales/zh-TW.yml +18 -0
  75. data/lib/paperclip/logger.rb +21 -0
  76. data/lib/paperclip/matchers/have_attached_file_matcher.rb +54 -0
  77. data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +100 -0
  78. data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +59 -0
  79. data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +96 -0
  80. data/lib/paperclip/matchers.rb +64 -0
  81. data/lib/paperclip/media_type_spoof_detector.rb +89 -0
  82. data/lib/paperclip/missing_attachment_styles.rb +79 -0
  83. data/lib/paperclip/processor.rb +48 -0
  84. data/lib/paperclip/processor_helpers.rb +50 -0
  85. data/lib/paperclip/rails_environment.rb +25 -0
  86. data/lib/paperclip/railtie.rb +31 -0
  87. data/lib/paperclip/schema.rb +83 -0
  88. data/lib/paperclip/storage/filesystem.rb +90 -0
  89. data/lib/paperclip/storage/fog.rb +241 -0
  90. data/lib/paperclip/storage/s3.rb +440 -0
  91. data/lib/paperclip/storage.rb +3 -0
  92. data/lib/paperclip/style.rb +109 -0
  93. data/lib/paperclip/tempfile.rb +43 -0
  94. data/lib/paperclip/tempfile_factory.rb +23 -0
  95. data/lib/paperclip/thumbnail.rb +121 -0
  96. data/lib/paperclip/url_generator.rb +72 -0
  97. data/lib/paperclip/validators/attachment_content_type_validator.rb +88 -0
  98. data/lib/paperclip/validators/attachment_file_name_validator.rb +80 -0
  99. data/lib/paperclip/validators/attachment_file_type_ignorance_validator.rb +29 -0
  100. data/lib/paperclip/validators/attachment_presence_validator.rb +30 -0
  101. data/lib/paperclip/validators/attachment_size_validator.rb +115 -0
  102. data/lib/paperclip/validators/media_type_spoof_detection_validator.rb +27 -0
  103. data/lib/paperclip/validators.rb +74 -0
  104. data/lib/paperclip/version.rb +3 -0
  105. data/lib/paperclip.rb +213 -0
  106. data/lib/tasks/paperclip.rake +127 -0
  107. data/paperclip.gemspec +51 -0
  108. data/shoulda_macros/paperclip.rb +134 -0
  109. data/spec/database.yml +4 -0
  110. data/spec/paperclip/attachment_definitions_spec.rb +13 -0
  111. data/spec/paperclip/attachment_processing_spec.rb +82 -0
  112. data/spec/paperclip/attachment_registry_spec.rb +130 -0
  113. data/spec/paperclip/attachment_spec.rb +1494 -0
  114. data/spec/paperclip/content_type_detector_spec.rb +48 -0
  115. data/spec/paperclip/deprecations_spec.rb +65 -0
  116. data/spec/paperclip/file_command_content_type_detector_spec.rb +26 -0
  117. data/spec/paperclip/filename_cleaner_spec.rb +14 -0
  118. data/spec/paperclip/geometry_detector_spec.rb +39 -0
  119. data/spec/paperclip/geometry_parser_spec.rb +73 -0
  120. data/spec/paperclip/geometry_spec.rb +255 -0
  121. data/spec/paperclip/glue_spec.rb +44 -0
  122. data/spec/paperclip/has_attached_file_spec.rb +142 -0
  123. data/spec/paperclip/integration_spec.rb +667 -0
  124. data/spec/paperclip/interpolations_spec.rb +262 -0
  125. data/spec/paperclip/io_adapters/abstract_adapter_spec.rb +78 -0
  126. data/spec/paperclip/io_adapters/attachment_adapter_spec.rb +139 -0
  127. data/spec/paperclip/io_adapters/data_uri_adapter_spec.rb +83 -0
  128. data/spec/paperclip/io_adapters/empty_string_adapter_spec.rb +17 -0
  129. data/spec/paperclip/io_adapters/file_adapter_spec.rb +131 -0
  130. data/spec/paperclip/io_adapters/http_url_proxy_adapter_spec.rb +104 -0
  131. data/spec/paperclip/io_adapters/identity_adapter_spec.rb +8 -0
  132. data/spec/paperclip/io_adapters/nil_adapter_spec.rb +25 -0
  133. data/spec/paperclip/io_adapters/registry_spec.rb +35 -0
  134. data/spec/paperclip/io_adapters/stringio_adapter_spec.rb +64 -0
  135. data/spec/paperclip/io_adapters/uploaded_file_adapter_spec.rb +146 -0
  136. data/spec/paperclip/io_adapters/uri_adapter_spec.rb +127 -0
  137. data/spec/paperclip/matchers/have_attached_file_matcher_spec.rb +19 -0
  138. data/spec/paperclip/matchers/validate_attachment_content_type_matcher_spec.rb +99 -0
  139. data/spec/paperclip/matchers/validate_attachment_presence_matcher_spec.rb +69 -0
  140. data/spec/paperclip/matchers/validate_attachment_size_matcher_spec.rb +88 -0
  141. data/spec/paperclip/media_type_spoof_detector_spec.rb +79 -0
  142. data/spec/paperclip/meta_class_spec.rb +30 -0
  143. data/spec/paperclip/paperclip_missing_attachment_styles_spec.rb +84 -0
  144. data/spec/paperclip/paperclip_spec.rb +222 -0
  145. data/spec/paperclip/plural_cache_spec.rb +37 -0
  146. data/spec/paperclip/processor_helpers_spec.rb +57 -0
  147. data/spec/paperclip/processor_spec.rb +26 -0
  148. data/spec/paperclip/rails_environment_spec.rb +33 -0
  149. data/spec/paperclip/rake_spec.rb +103 -0
  150. data/spec/paperclip/schema_spec.rb +248 -0
  151. data/spec/paperclip/storage/filesystem_spec.rb +79 -0
  152. data/spec/paperclip/storage/fog_spec.rb +535 -0
  153. data/spec/paperclip/storage/s3_live_spec.rb +182 -0
  154. data/spec/paperclip/storage/s3_spec.rb +1526 -0
  155. data/spec/paperclip/style_spec.rb +255 -0
  156. data/spec/paperclip/tempfile_factory_spec.rb +33 -0
  157. data/spec/paperclip/thumbnail_spec.rb +500 -0
  158. data/spec/paperclip/url_generator_spec.rb +211 -0
  159. data/spec/paperclip/validators/attachment_content_type_validator_spec.rb +322 -0
  160. data/spec/paperclip/validators/attachment_file_name_validator_spec.rb +160 -0
  161. data/spec/paperclip/validators/attachment_presence_validator_spec.rb +85 -0
  162. data/spec/paperclip/validators/attachment_size_validator_spec.rb +229 -0
  163. data/spec/paperclip/validators/media_type_spoof_detection_validator_spec.rb +52 -0
  164. data/spec/paperclip/validators_spec.rb +164 -0
  165. data/spec/spec_helper.rb +43 -0
  166. data/spec/support/assertions.rb +71 -0
  167. data/spec/support/deprecations.rb +9 -0
  168. data/spec/support/fake_model.rb +25 -0
  169. data/spec/support/fake_rails.rb +12 -0
  170. data/spec/support/fixtures/12k.png +0 -0
  171. data/spec/support/fixtures/50x50.png +0 -0
  172. data/spec/support/fixtures/5k.png +0 -0
  173. data/spec/support/fixtures/animated +0 -0
  174. data/spec/support/fixtures/animated.gif +0 -0
  175. data/spec/support/fixtures/animated.unknown +0 -0
  176. data/spec/support/fixtures/bad.png +1 -0
  177. data/spec/support/fixtures/empty.html +1 -0
  178. data/spec/support/fixtures/empty.xlsx +0 -0
  179. data/spec/support/fixtures/fog.yml +8 -0
  180. data/spec/support/fixtures/rotated.jpg +0 -0
  181. data/spec/support/fixtures/s3.yml +8 -0
  182. data/spec/support/fixtures/spaced file.jpg +0 -0
  183. data/spec/support/fixtures/spaced file.png +0 -0
  184. data/spec/support/fixtures/text.txt +1 -0
  185. data/spec/support/fixtures/twopage.pdf +0 -0
  186. data/spec/support/fixtures/uppercase.PNG +0 -0
  187. data/spec/support/matchers/accept.rb +5 -0
  188. data/spec/support/matchers/exist.rb +5 -0
  189. data/spec/support/matchers/have_column.rb +23 -0
  190. data/spec/support/mock_attachment.rb +22 -0
  191. data/spec/support/mock_interpolator.rb +24 -0
  192. data/spec/support/mock_url_generator_builder.rb +27 -0
  193. data/spec/support/model_reconstruction.rb +60 -0
  194. data/spec/support/rails_helpers.rb +7 -0
  195. data/spec/support/test_data.rb +13 -0
  196. data/spec/support/version_helper.rb +9 -0
  197. metadata +606 -0
data/README.md ADDED
@@ -0,0 +1,979 @@
1
+ > This is a fork from [paperclip](https://github.com/thoughtbot/paperclip) gem to fix version 4.3.7 dependency on `mimemagic 0.3.0`
2
+
3
+ Paperclip
4
+ =========
5
+
6
+ [![Build Status](https://secure.travis-ci.org/thoughtbot/paperclip.svg?branch=master)](http://travis-ci.org/thoughtbot/paperclip) [![Dependency Status](https://gemnasium.com/thoughtbot/paperclip.svg?travis)](https://gemnasium.com/thoughtbot/paperclip) [![Code Climate](https://codeclimate.com/github/thoughtbot/paperclip.svg)](https://codeclimate.com/github/thoughtbot/paperclip) [![Inline docs](http://inch-ci.org/github/thoughtbot/paperclip.svg)](http://inch-ci.org/github/thoughtbot/paperclip) [![Security](https://hakiri.io/github/thoughtbot/paperclip/master.svg)](https://hakiri.io/github/thoughtbot/paperclip/master)
7
+
8
+ - [Requirements](#requirements)
9
+ - [Ruby on Rails](#ruby-and-rails)
10
+ - [Image Processor](#image-processor)
11
+ - [file](#file)
12
+ - [Installation](#installation)
13
+ - [Quick Start](#quick-start)
14
+ - [Models](#models)
15
+ - [Migrations](#migrations)
16
+ - [Edit and New Views](#edit-and-new-views)
17
+ - [Edit and New Views with Simple Form](#edit-and-new-views-with-simple-form)
18
+ - [Controller](#controller)
19
+ - [Show View](#show-view)
20
+ - [Deleting an Attachment](#deleting-an-attachment)
21
+ - [Usage](#usage)
22
+ - [Validations](#validations)
23
+ - [Security Validations](#security-validations)
24
+ - [Defaults](#defaults)
25
+ - [Migrations](#migrations-1)
26
+ - [Table Definition](#table-definition)
27
+ - [Schema Definition](#schema-definition)
28
+ - [Vintage Syntax](#vintage-syntax)
29
+ - [Storage](#storage)
30
+ - [Understanding Storage](#understanding-storage)
31
+ - [Post Processing](#post-processing)
32
+ - [Events](#events)
33
+ - [URI Obfuscation](#uri-obfuscation)
34
+ - [MD5 Checksum / Fingerprint](#md5-checksum--fingerprint)
35
+ - [File Preservation for Soft-Delete](#file-preservation-for-soft-delete)
36
+ - [Custom Attachment Processors](#custom-attachment-processors)
37
+ - [Dynamic Configuration](#dynamic-configuration)
38
+ - [Dynamic Styles](#dynamic-styles)
39
+ - [Dynamic Processors](#dynamic-processors)
40
+ - [Logging](#logging)
41
+ - [Deployment](#deployment)
42
+ - [Testing](#testing)
43
+ - [Contributing](#contributing)
44
+ - [License](#license)
45
+ - [About thoughtbot](#about-thoughtbot)
46
+
47
+ Paperclip is intended as an easy file attachment library for Active Record. The
48
+ intent behind it was to keep setup as easy as possible and to treat files as
49
+ much like other attributes as possible. This means they aren't saved to their
50
+ final locations on disk, nor are they deleted if set to nil, until
51
+ ActiveRecord::Base#save is called. It manages validations based on size and
52
+ presence, if required. It can transform its assigned image into thumbnails if
53
+ needed, and the prerequisites are as simple as installing ImageMagick (which,
54
+ for most modern Unix-based systems, is as easy as installing the right
55
+ packages). Attached files are saved to the filesystem and referenced in the
56
+ browser by an easily understandable specification, which has sensible and
57
+ useful defaults.
58
+
59
+ See the documentation for `has_attached_file` in [`Paperclip::ClassMethods`](http://rubydoc.info/gems/paperclip/Paperclip/ClassMethods) for
60
+ more detailed options.
61
+
62
+ The complete [RDoc](http://rdoc.info/gems/paperclip) is online.
63
+
64
+ ---
65
+
66
+ Requirements
67
+ ------------
68
+
69
+ ### Ruby and Rails
70
+
71
+ Paperclip now requires Ruby version **>= 2.0.0** and Rails version **3.2, >= 4.1** (Only if you're going to use Paperclip with Ruby on Rails.)
72
+
73
+ If you're still on Ruby 1.8.7 or Ruby on Rails 2.3.x, you can still use Paperclip 2.7.x with your project. Also, everything in this README might not apply to your version of Paperclip, and you should read [the README for version 2.7](http://rubydoc.info/gems/paperclip/2.7.0) instead.
74
+
75
+ ### Image Processor
76
+
77
+ [ImageMagick](http://www.imagemagick.org) must be installed and Paperclip must have access to it. To ensure
78
+ that it does, on your command line, run `which convert` (one of the ImageMagick
79
+ utilities). This will give you the path where that utility is installed. For
80
+ example, it might return `/usr/local/bin/convert`.
81
+
82
+ Then, in your environment config file, let Paperclip know to look there by adding that
83
+ directory to its path.
84
+
85
+ In development mode, you might add this line to `config/environments/development.rb)`:
86
+
87
+ ```ruby
88
+ Paperclip.options[:command_path] = "/usr/local/bin/"
89
+ ```
90
+
91
+ If you're on Mac OS X, you'll want to run the following with Homebrew:
92
+
93
+ brew install imagemagick
94
+
95
+ If you are dealing with pdf uploads or running the test suite, you'll also need
96
+ to install GhostScript. On Mac OS X, you can also install that using Homebrew:
97
+
98
+ brew install gs
99
+
100
+ If you're on Ubuntu, you'll want to run the following with apt-get:
101
+
102
+ sudo apt-get install imagemagick -y
103
+
104
+ If you're on Ubuntu (or any Debian base Linux distribution), you'll want to run the following with apt-get:
105
+
106
+ sudo apt-get install imagemagick -y
107
+
108
+ ### `file`
109
+
110
+ The Unix [`file` command](http://en.wikipedia.org/wiki/File_(command)) is required for content-type checking.
111
+ This utility isn't available in Windows, but comes bundled with Ruby [Devkit](https://github.com/oneclick/rubyinstaller/wiki/Development-Kit),
112
+ so Windows users must make sure that the devkit is installed and added to the system `PATH`.
113
+
114
+ **Manual Installation**
115
+
116
+ If you're using Windows 7+ as a development environment, you may need to install the `file.exe` application manually. The `file spoofing` system in Paperclip 4+ relies on this; if you don't have it working, you'll receive `Validation failed: Upload file has an extension that does not match its contents.` errors.
117
+
118
+ To manually install, you should perform the following:
119
+
120
+ > **Download & install `file` from [this URL](http://gnuwin32.sourceforge.net/packages/file.htm)**
121
+
122
+ To test, you can use the image below:
123
+ ![untitled](https://cloud.githubusercontent.com/assets/1104431/4524452/a1f8cce4-4d44-11e4-872e-17adb96f79c9.png)
124
+
125
+ Next, you need to integrate with your environment - preferably through the `PATH` variable, or by changing your `config/environments/development.rb` file
126
+
127
+ **PATH**
128
+
129
+ 1. Click "Start"
130
+ 2. On "Computer", right-click and select "Properties"
131
+ 3. In Properties, select "Advanced System Settings"
132
+ 4. Click the "Environment Variables" button
133
+ 5. Locate the "PATH" var - at the end, add the path to your newly installed `file.exe` (typically `C:\Program Files (x86)\GnuWin32\bin`)
134
+ 6. Restart any CMD shells you have open & see if it works
135
+
136
+ OR
137
+
138
+ **Environment**
139
+
140
+ 1. Open `config/environments/development.rb`
141
+ 2. Add the following line: `Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'`
142
+ 3. Restart your Rails server
143
+
144
+ Either of these methods will give your Rails setup access to the `file.exe` functionality, thus providing the ability to check the contents of a file (fixing the spoofing problem)
145
+
146
+ ---
147
+
148
+ Installation
149
+ ------------
150
+
151
+ Paperclip is distributed as a gem, which is how it should be used in your app.
152
+
153
+ Include the gem in your Gemfile:
154
+
155
+ ```ruby
156
+ gem "paperclip", "~> 4.3"
157
+ ```
158
+
159
+ Or, if you want to get the latest, you can get master from the main paperclip repository:
160
+
161
+ ```ruby
162
+ gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
163
+ ```
164
+
165
+ If you're trying to use features that don't seem to be in the latest released gem, but are
166
+ mentioned in this README, then you probably need to specify the master branch if you want to
167
+ use them. This README is probably ahead of the latest released version, if you're reading it
168
+ on GitHub.
169
+
170
+ For Non-Rails usage:
171
+
172
+ ```ruby
173
+ class ModuleName < ActiveRecord::Base
174
+ include Paperclip::Glue
175
+ ...
176
+ end
177
+ ```
178
+
179
+ ---
180
+
181
+ Quick Start
182
+ -----------
183
+
184
+ ### Models
185
+
186
+ **Rails 3**
187
+
188
+ ```ruby
189
+ class User < ActiveRecord::Base
190
+ attr_accessible :avatar
191
+ has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
192
+ validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
193
+ end
194
+ ```
195
+
196
+ **Rails 4**
197
+
198
+ ```ruby
199
+ class User < ActiveRecord::Base
200
+ has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
201
+ validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
202
+ end
203
+ ```
204
+
205
+ ### Migrations
206
+
207
+ ```ruby
208
+ class AddAvatarColumnsToUsers < ActiveRecord::Migration
209
+ def up
210
+ add_attachment :users, :avatar
211
+ end
212
+
213
+ def down
214
+ remove_attachment :users, :avatar
215
+ end
216
+ end
217
+ ```
218
+
219
+ (Or you can use the Rails migration generator: `rails generate paperclip user avatar`)
220
+
221
+ ### Edit and New Views
222
+
223
+ ```erb
224
+ <%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
225
+ <%= form.file_field :avatar %>
226
+ <% end %>
227
+ ```
228
+
229
+ ### Edit and New Views with Simple Form
230
+ ```erb
231
+ <%= simple_form_for @user, url: users_path do |form| %>
232
+ <%= form.input :avatar, as: :file %>
233
+ <% end %>
234
+ ```
235
+
236
+ ### Controller
237
+
238
+ **Rails 3**
239
+
240
+ ```ruby
241
+ def create
242
+ @user = User.create( params[:user] )
243
+ end
244
+ ```
245
+
246
+ **Rails 4**
247
+
248
+ ```ruby
249
+ def create
250
+ @user = User.create( user_params )
251
+ end
252
+
253
+ private
254
+
255
+ # Use strong_parameters for attribute whitelisting
256
+ # Be sure to update your create() and update() controller methods.
257
+
258
+ def user_params
259
+ params.require(:user).permit(:avatar)
260
+ end
261
+ ```
262
+
263
+ ### Show View
264
+
265
+ ```erb
266
+ <%= image_tag @user.avatar.url %>
267
+ <%= image_tag @user.avatar.url(:medium) %>
268
+ <%= image_tag @user.avatar.url(:thumb) %>
269
+ ```
270
+
271
+ ### Deleting an Attachment
272
+
273
+ Set the attribute to `nil` and save.
274
+
275
+ ```ruby
276
+ @user.avatar = nil
277
+ @user.save
278
+ ```
279
+ ---
280
+
281
+ Usage
282
+ -----
283
+
284
+ The basics of Paperclip are quite simple: Declare that your model has an
285
+ attachment with the `has_attached_file` method, and give it a name.
286
+
287
+ Paperclip will wrap up to four attributes (all prefixed with that attachment's name,
288
+ so you can have multiple attachments per model if you wish) and give them a
289
+ friendly front end. These attributes are:
290
+
291
+ * `<attachment>_file_name`
292
+ * `<attachment>_file_size`
293
+ * `<attachment>_content_type`
294
+ * `<attachment>_updated_at`
295
+
296
+ By default, only `<attachment>_file_name` is required for Paperclip to operate.
297
+ You'll need to add `<attachment>_content_type` in case you want to use content type
298
+ validation.
299
+
300
+ More information about the options passed to `has_attached_file` is available in the
301
+ documentation of [`Paperclip::ClassMethods`](http://rubydoc.info/gems/paperclip/Paperclip/ClassMethods).
302
+
303
+ Validations
304
+ -----------
305
+
306
+ For validations, Paperclip introduces several validators to validate your attachment:
307
+
308
+ * `AttachmentContentTypeValidator`
309
+ * `AttachmentPresenceValidator`
310
+ * `AttachmentSizeValidator`
311
+
312
+ Example Usage:
313
+
314
+ ```ruby
315
+ validates :avatar, :attachment_presence => true
316
+ validates_with AttachmentPresenceValidator, :attributes => :avatar
317
+ validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 1.megabytes
318
+
319
+ ```
320
+
321
+ Validators can also be defined using the old helper style:
322
+
323
+ * `validates_attachment_presence`
324
+ * `validates_attachment_content_type`
325
+ * `validates_attachment_size`
326
+
327
+ Example Usage:
328
+
329
+ ```ruby
330
+ validates_attachment_presence :avatar
331
+ ```
332
+
333
+ Lastly, you can also define multiple validations on a single attachment using `validates_attachment`:
334
+
335
+ ```ruby
336
+ validates_attachment :avatar, :presence => true,
337
+ :content_type => { :content_type => "image/jpeg" },
338
+ :size => { :in => 0..10.kilobytes }
339
+ ```
340
+
341
+ _NOTE: Post-processing will not even **start** if the attachment is not valid
342
+ according to the validations. Your callbacks and processors will **only** be
343
+ called with valid attachments._
344
+
345
+ ```ruby
346
+ class Message < ActiveRecord::Base
347
+ has_attached_file :asset, styles: {thumb: "100x100#"}
348
+
349
+ before_post_process :skip_for_audio
350
+
351
+ def skip_for_audio
352
+ ! %w(audio/ogg application/ogg).include?(asset_content_type)
353
+ end
354
+ end
355
+ ```
356
+
357
+ If you have other validations that depend on assignment order, the recommended
358
+ course of action is to prevent the assignment of the attachment until
359
+ afterwards, then assign manually:
360
+
361
+ ```ruby
362
+ class Book < ActiveRecord::Base
363
+ has_attached_file :document, styles: {thumbnail: "60x60#"}
364
+ validates_attachment :document, content_type: { content_type: "application/pdf" }
365
+ validates_something_else # Other validations that conflict with Paperclip's
366
+ end
367
+
368
+ class BooksController < ApplicationController
369
+ def create
370
+ @book = Book.new(book_params)
371
+ @book.document = params[:book][:document]
372
+ @book.save
373
+ respond_with @book
374
+ end
375
+
376
+ private
377
+
378
+ def book_params
379
+ params.require(:book).permit(:title, :author)
380
+ end
381
+ end
382
+ ```
383
+
384
+ **A note on content_type validations and security**
385
+
386
+ You should ensure that you validate files to be only those MIME types you
387
+ explicitly want to support. If you don't, you could be open to
388
+ <a href="https://www.owasp.org/index.php/Testing_for_Stored_Cross_site_scripting_(OWASP-DV-002)">XSS attacks</a>
389
+ if a user uploads a file with a malicious HTML payload.
390
+
391
+ If you're only interested in images, restrict your allowed content_types to
392
+ image-y ones:
393
+
394
+ ```ruby
395
+ validates_attachment :avatar,
396
+ :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] }
397
+ ```
398
+
399
+ `Paperclip::ContentTypeDetector` will attempt to match a file's extension to an
400
+ inferred content_type, regardless of the actual contents of the file.
401
+
402
+ ---
403
+
404
+ Security Validations
405
+ ====================
406
+
407
+ Thanks to a report from [Egor Homakov](http://homakov.blogspot.com/) we have
408
+ taken steps to prevent people from spoofing Content-Types and getting data
409
+ you weren't expecting onto your server.
410
+
411
+ NOTE: Starting at version 4.0.0, all attachments are *required* to include a
412
+ content_type validation, a file_name validation, or to explicitly state that
413
+ they're not going to have either. *Paperclip will raise an error* if you do not
414
+ do this.
415
+
416
+ ```ruby
417
+ class ActiveRecord::Base
418
+ has_attached_file :avatar
419
+ # Validate content type
420
+ validates_attachment_content_type :avatar, :content_type => /\Aimage/
421
+ # Validate filename
422
+ validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/]
423
+ # Explicitly do not validate
424
+ do_not_validate_attachment_file_type :avatar
425
+ end
426
+ ```
427
+
428
+ This keeps Paperclip secure-by-default, and will prevent people trying to mess
429
+ with your filesystem.
430
+
431
+ NOTE: Also starting at version 4.0.0, Paperclip has another validation that
432
+ cannot be turned off. This validation will prevent content type spoofing. That
433
+ is, uploading a PHP document (for example) as part of the EXIF tags of a
434
+ well-formed JPEG. This check is limited to the media type (the first part of the
435
+ MIME type, so, 'text' in `text/plain`). This will prevent HTML documents from
436
+ being uploaded as JPEGs, but will not prevent GIFs from being uploaded with a
437
+ `.jpg` extension. This validation will only add validation errors to the form. It
438
+ will not cause errors to be raised.
439
+
440
+ This can sometimes cause false validation errors in applications that use custom
441
+ file extensions. In these cases you may wish to add your custom extension to the
442
+ list of file extensions allowed for your MIME type configured by the `mime-types`
443
+ gem:
444
+
445
+ ```ruby
446
+ # Allow ".foo" as an extension for files with the MIME type "text/plain".
447
+ text_plain = MIME::Types["text/plain"].first
448
+ text_plain.extensions << "foo"
449
+ MIME::Types.index_extensions text_plain
450
+ ```
451
+
452
+ ---
453
+
454
+ Defaults
455
+ --------
456
+ Global defaults for all your Paperclip attachments can be defined by changing the Paperclip::Attachment.default_options Hash. This can be useful for setting your default storage settings per example so you won't have to define them in every `has_attached_file` definition.
457
+
458
+ If you're using Rails, you can define a Hash with default options in `config/application.rb` or in any of the `config/environments/*.rb` files on config.paperclip_defaults. These will get merged into `Paperclip::Attachment.default_options` as your Rails app boots. An example:
459
+
460
+ ```ruby
461
+ module YourApp
462
+ class Application < Rails::Application
463
+ # Other code...
464
+
465
+ config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/public"}, :fog_directory => "", :fog_host => "localhost"}
466
+ end
467
+ end
468
+ ```
469
+
470
+ Another option is to directly modify the `Paperclip::Attachment.default_options` Hash - this method works for non-Rails applications or is an option if you prefer to place the Paperclip default settings in an initializer.
471
+
472
+ An example Rails initializer would look something like this:
473
+
474
+ ```ruby
475
+ Paperclip::Attachment.default_options[:storage] = :fog
476
+ Paperclip::Attachment.default_options[:fog_credentials] = {:provider => "Local", :local_root => "#{Rails.root}/public"}
477
+ Paperclip::Attachment.default_options[:fog_directory] = ""
478
+ Paperclip::Attachment.default_options[:fog_host] = "http://localhost:3000"
479
+ ```
480
+ ---
481
+
482
+ Migrations
483
+ ----------
484
+
485
+ Paperclip defines several migration methods which can be used to create the necessary columns in your
486
+ model. There are two types of helper methods to aid in this, as follows:
487
+
488
+ ### Add Attachment Column To A Table
489
+
490
+ The `attachment` helper can be used when creating a table:
491
+
492
+ ```ruby
493
+ class CreateUsersWithAttachments < ActiveRecord::Migration
494
+ def up
495
+ create_table :users do |t|
496
+ t.attachment :avatar
497
+ end
498
+
499
+ # This is assuming you are only using the users table for Paperclip attachment. Drop with care!
500
+ def down
501
+ drop_table :users
502
+ end
503
+ end
504
+ ```
505
+
506
+ You can also use the `change` method, instead of the `up`/`down` combination above, as shown below:
507
+
508
+ ```ruby
509
+ class CreateUsersWithAttachments < ActiveRecord::Migration
510
+ def change
511
+ create_table :users do |t|
512
+ t.attachment :avatar
513
+ end
514
+ end
515
+ end
516
+ ```
517
+
518
+ ### Schema Definition
519
+
520
+ Alternatively, the `add_attachment` and `remove_attachment` methods can be used to add new Paperclip columns to an existing table:
521
+
522
+ ```ruby
523
+ class AddAttachmentColumnsToUsers < ActiveRecord::Migration
524
+ def up
525
+ add_attachment :users, :avatar
526
+ end
527
+
528
+ def down
529
+ remove_attachment :users, :avatar
530
+ end
531
+ end
532
+ ```
533
+
534
+ Or you can do this with the `change` method:
535
+
536
+ ```ruby
537
+ class AddAttachmentColumnsToUsers < ActiveRecord::Migration
538
+ def change
539
+ add_attachment :users, :avatar
540
+ end
541
+ end
542
+ ```
543
+
544
+ ### Vintage syntax
545
+
546
+ Vintage syntax (such as `t.has_attached_file` and `drop_attached_file`) is still supported in
547
+ Paperclip 3.x, but you're advised to update those migration files to use this new syntax.
548
+
549
+ ---
550
+
551
+ Storage
552
+ -------
553
+
554
+ Paperclip ships with 3 storage adapters:
555
+
556
+ * File Storage
557
+ * S3 Storage (via `aws-sdk`)
558
+ * Fog Storage
559
+
560
+ If you would like to use Paperclip with another storage, you can install these
561
+ gems along side with Paperclip:
562
+
563
+ * [paperclip-azure-storage](https://github.com/gmontard/paperclip-azure-storage)
564
+ * [paperclip-dropbox](https://github.com/janko-m/paperclip-dropbox)
565
+
566
+ ### Understanding Storage
567
+
568
+ The files that are assigned as attachments are, by default, placed in the
569
+ directory specified by the `:path` option to `has_attached_file`. By default, this
570
+ location is `:rails_root/public/system/:class/:attachment/:id_partition/:style/:filename`.
571
+ This location was chosen because on standard Capistrano deployments, the
572
+ `public/system` directory is symlinked to the app's shared directory, meaning it
573
+ will survive between deployments. For example, using that `:path`, you may have a
574
+ file at
575
+
576
+ /data/myapp/releases/20081229172410/public/system/users/avatar/000/000/013/small/my_pic.png
577
+
578
+ _**NOTE**: This is a change from previous versions of Paperclip, but is overall a
579
+ safer choice for the default file store._
580
+
581
+ You may also choose to store your files using Amazon's S3 service. To do so, include
582
+ the `aws-sdk` gem in your Gemfile:
583
+
584
+ ```ruby
585
+ gem 'aws-sdk', '~> 1.6'
586
+ ```
587
+
588
+ And then you can specify using S3 from `has_attached_file`.
589
+ You can find more information about configuring and using S3 storage in
590
+ [the `Paperclip::Storage::S3` documentation](http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3).
591
+
592
+ Files on the local filesystem (and in the Rails app's public directory) will be
593
+ available to the internet at large. If you require access control, it's
594
+ possible to place your files in a different location. You will need to change
595
+ both the `:path` and `:url` options in order to make sure the files are unavailable
596
+ to the public. Both `:path` and `:url` allow the same set of interpolated
597
+ variables.
598
+
599
+ ---
600
+
601
+ Post Processing
602
+ ---------------
603
+
604
+ Paperclip supports an extensible selection of post-processors. When you define
605
+ a set of styles for an attachment, by default it is expected that those
606
+ "styles" are actually "thumbnails." However, you can do much more than just
607
+ thumbnail images. By defining a subclass of Paperclip::Processor, you can
608
+ perform any processing you want on the files that are attached. Any file in
609
+ your Rails app's `lib/paperclip` and `lib/paperclip_processors` directories is
610
+ automatically loaded by Paperclip, allowing you to easily define custom
611
+ processors. You can specify a processor with the `:processors` option to
612
+ `has_attached_file`:
613
+
614
+ ```ruby
615
+ has_attached_file :scan, :styles => { :text => { :quality => :better } },
616
+ :processors => [:ocr]
617
+ ```
618
+
619
+ This would load the hypothetical class Paperclip::Ocr, which would have the
620
+ hash "{ :quality => :better }" passed to it along with the uploaded file. For
621
+ more information about defining processors, see Paperclip::Processor.
622
+
623
+ The default processor is Paperclip::Thumbnail. For backwards compatibility
624
+ reasons, you can pass a single geometry string or an array containing a
625
+ geometry and a format that the file will be converted to, like so:
626
+
627
+ ```ruby
628
+ has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] }
629
+ ```
630
+
631
+ This will convert the "thumb" style to a 32x32 square in PNG format, regardless
632
+ of what was uploaded. If the format is not specified, it is kept the same (i.e.
633
+ JPGs will remain JPGs). For more information on the accepted style formats, see
634
+ [here](http://www.imagemagick.org/script/command-line-processing.php#geometry).
635
+
636
+ Multiple processors can be specified, and they will be invoked in the order
637
+ they are defined in the `:processors` array. Each successive processor will
638
+ be given the result of the previous processor's execution. All processors will
639
+ receive the same parameters, which are defined in the `:styles` hash.
640
+ For example, assuming we had this definition:
641
+
642
+ ```ruby
643
+ has_attached_file :scan, :styles => { :text => { :quality => :better } },
644
+ :processors => [:rotator, :ocr]
645
+ ```
646
+
647
+ then both the :rotator processor and the :ocr processor would receive the
648
+ options `{ :quality => :better }`. This parameter may not mean anything to one
649
+ or more or the processors, and they are expected to ignore it.
650
+
651
+ _NOTE: Because processors operate by turning the original attachment into the
652
+ styles, no processors will be run if there are no styles defined._
653
+
654
+ If you're interested in caching your thumbnail's width, height and size in the
655
+ database, take a look at the [paperclip-meta](https://github.com/teeparham/paperclip-meta) gem.
656
+
657
+ Also, if you're interested in generating the thumbnail on-the-fly, you might want
658
+ to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly) gem.
659
+
660
+ ---
661
+
662
+ Events
663
+ ------
664
+
665
+ Before and after the Post Processing step, Paperclip calls back to the model
666
+ with a few callbacks, allowing the model to change or cancel the processing
667
+ step. The callbacks are `before_post_process` and `after_post_process` (which
668
+ are called before and after the processing of each attachment), and the
669
+ attachment-specific `before_<attachment>_post_process` and
670
+ `after_<attachment>_post_process`. The callbacks are intended to be as close to
671
+ normal ActiveRecord callbacks as possible, so if you return false (specifically
672
+ \- returning nil is not the same) in a `before_filter`, the post processing step
673
+ will halt. Returning false in an `after_filter` will not halt anything, but you
674
+ can access the model and the attachment if necessary.
675
+
676
+ _NOTE: Post processing will not even **start** if the attachment is not valid
677
+ according to the validations. Your callbacks and processors will **only** be
678
+ called with valid attachments._
679
+
680
+ ```ruby
681
+ class Message < ActiveRecord::Base
682
+ has_attached_file :asset, styles: {thumb: "100x100#"}
683
+
684
+ before_post_process :skip_for_audio
685
+
686
+ def skip_for_audio
687
+ ! %w(audio/ogg application/ogg).include?(asset_content_type)
688
+ end
689
+ end
690
+ ```
691
+
692
+ ---
693
+
694
+ URI Obfuscation
695
+ ---------------
696
+
697
+ Paperclip has an interpolation called `:hash` for obfuscating filenames of
698
+ publicly-available files.
699
+
700
+ Example Usage:
701
+
702
+ ```ruby
703
+ has_attached_file :avatar, {
704
+ :url => "/system/:hash.:extension",
705
+ :hash_secret => "longSecretString"
706
+ }
707
+ ```
708
+
709
+
710
+ The `:hash` interpolation will be replaced with a unique hash made up of whatever
711
+ is specified in `:hash_data`. The default value for `:hash_data` is `":class/:attachment/:id/:style/:updated_at"`.
712
+
713
+ `:hash_secret` is required - an exception will be raised if `:hash` is used without `:hash_secret` present.
714
+
715
+ For more on this feature, read [the author's own explanation](https://github.com/thoughtbot/paperclip/pull/416)
716
+
717
+ MD5 Checksum / Fingerprint
718
+ -------
719
+
720
+ An MD5 checksum of the original file assigned will be placed in the model if it
721
+ has an attribute named fingerprint. Following the user model migration example
722
+ above, the migration would look like the following:
723
+
724
+ ```ruby
725
+ class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
726
+ def up
727
+ add_column :users, :avatar_fingerprint, :string
728
+ end
729
+
730
+ def down
731
+ remove_column :users, :avatar_fingerprint
732
+ end
733
+ end
734
+ ```
735
+
736
+ File Preservation for Soft-Delete
737
+ -------
738
+
739
+ An option is available to preserve attachments in order to play nicely with soft-deleted models. (acts_as_paranoid, paranoia, etc.)
740
+
741
+ ```ruby
742
+ has_attached_file :some_attachment, {
743
+ :preserve_files => "true",
744
+ }
745
+ ```
746
+
747
+ This will prevent ```some_attachment``` from being wiped out when the model gets destroyed, so it will still exist when the object is restored later.
748
+
749
+ ---
750
+
751
+ Custom Attachment Processors
752
+ -------
753
+
754
+ Custom attachment processors can be implemented and their only requirement is
755
+ to inherit from `Paperclip::Processor` (see `lib/paperclip/processor.rb`).
756
+ For example, when `:styles` are specified for an image attachment, the
757
+ thumbnail processor (see `lib/paperclip/thumbnail.rb`) is loaded without having
758
+ to specify it as a `:processor` parameter to `has_attached_file`. When any
759
+ other processor is defined, it must be called out in the `:processors`
760
+ parameter if it is to be applied to the attachment. The thumbnail processor
761
+ uses the ImageMagick `convert` command to do the work of resizing image
762
+ thumbnails. It would be easy to create a custom processor that watermarks
763
+ an image using ImageMagick's `composite` command. Following the
764
+ implementation pattern of the thumbnail processor would be a way to implement a
765
+ watermark processor. All kinds of attachment processors can be created;
766
+ a few utility examples would be compression and encryption processors.
767
+
768
+ ---
769
+
770
+ Dynamic Configuration
771
+ ---------------------
772
+
773
+ Callable objects (lambdas, Procs) can be used in a number of places for dynamic
774
+ configuration throughout Paperclip. This strategy exists in a number of
775
+ components of the library but is most significant in the possibilities for
776
+ allowing custom styles and processors to be applied for specific model
777
+ instances, rather than applying defined styles and processors across all
778
+ instances.
779
+
780
+ ### Dynamic Styles:
781
+
782
+ Imagine a user model that had different styles based on the role of the user.
783
+ Perhaps some users are bosses (e.g. a User model instance responds to `#boss?`)
784
+ and merit a bigger avatar thumbnail than regular users. The configuration to
785
+ determine what style parameters are to be used based on the user role might
786
+ look as follows where a boss will receive a `300x300` thumbnail otherwise a
787
+ `100x100` thumbnail will be created.
788
+
789
+ ```ruby
790
+ class User < ActiveRecord::Base
791
+ has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") } }
792
+ end
793
+ ```
794
+
795
+ ### Dynamic Processors:
796
+
797
+ Another contrived example is a user model that is aware of which file processors
798
+ should be applied to it (beyond the implied `thumbnail` processor invoked when
799
+ `:styles` are defined). Perhaps we have a watermark processor available and it is
800
+ only used on the avatars of certain models. The configuration for this might be
801
+ where the instance is queried for which processors should be applied to it.
802
+ Presumably some users might return `[:thumbnail, :watermark]` for its
803
+ processors, where a defined `watermark` processor is invoked after the
804
+ `thumbnail` processor already defined by Paperclip.
805
+
806
+ ```ruby
807
+ class User < ActiveRecord::Base
808
+ has_attached_file :avatar, :processors => lambda { |instance| instance.processors }
809
+ attr_accessor :processors
810
+ end
811
+ ```
812
+
813
+ ---
814
+
815
+ Logging
816
+ ----------
817
+
818
+ By default Paperclip outputs logging according to your logger level. If you want to disable logging (e.g. during testing) add this in to your environment's configuration:
819
+ ```ruby
820
+ Your::Application.configure do
821
+ ...
822
+ Paperclip.options[:log] = false
823
+ ...
824
+ end
825
+ ```
826
+
827
+ More information in the [rdocs](http://rdoc.info/github/thoughtbot/paperclip/Paperclip.options)
828
+
829
+ ---
830
+
831
+ Deployment
832
+ ----------
833
+
834
+ Paperclip is aware of new attachment styles you have added in previous deploys. The only thing you should do after each deployment is to call
835
+ `rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
836
+ by default. You can change it by:
837
+
838
+ ```ruby
839
+ Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
840
+ ```
841
+
842
+ Here is an example for Capistrano:
843
+
844
+ ```ruby
845
+ namespace :deploy do
846
+ desc "build missing paperclip styles"
847
+ task :build_missing_paperclip_styles do
848
+ on roles(:app) do
849
+ within release_path do
850
+ with rails_env: fetch(:rails_env) do
851
+ execute :rake, "paperclip:refresh:missing_styles"
852
+ end
853
+ end
854
+ end
855
+ end
856
+ end
857
+
858
+ after("deploy:compile_assets", "deploy:build_missing_paperclip_styles")
859
+ ```
860
+
861
+ Now you don't have to remember to refresh thumbnails in production every time you add a new style.
862
+ Unfortunately, it does not work with dynamic styles - it just ignores them.
863
+
864
+ If you already have a working app and don't want `rake paperclip:refresh:missing_styles` to refresh old pictures, you need to tell
865
+ Paperclip about existing styles. Simply create a `paperclip_attachments.yml` file by hand. For example:
866
+
867
+ ```ruby
868
+ class User < ActiveRecord::Base
869
+ has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'}
870
+ end
871
+
872
+ class Book < ActiveRecord::Base
873
+ has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
874
+ has_attached_file :sample, :styles => {:thumb => 'x100'}
875
+ end
876
+ ```
877
+
878
+ Then in `RAILS_ROOT/public/system/paperclip_attachments.yml`:
879
+
880
+ ```yml
881
+ ---
882
+ :User:
883
+ :avatar:
884
+ - :thumb
885
+ - :croppable
886
+ - :big
887
+ :Book:
888
+ :cover:
889
+ - :small
890
+ - :large
891
+ :sample:
892
+ - :thumb
893
+ ```
894
+
895
+ ---
896
+
897
+ Testing
898
+ -------
899
+
900
+ Paperclip provides rspec-compatible matchers for testing attachments. See the
901
+ documentation on [Paperclip::Shoulda::Matchers](http://rubydoc.info/gems/paperclip/Paperclip/Shoulda/Matchers)
902
+ for more information.
903
+
904
+ **Parallel Tests**
905
+
906
+ Because of the default `path` for Paperclip storage, if you try to run tests in
907
+ parallel, you may find that files get overwritten because the same path is being
908
+ calculated for them in each test process. While this fix works for
909
+ parallel_tests, a similar concept should be used for any other mechanism for
910
+ running tests concurrently.
911
+
912
+ ```ruby
913
+ if ENV['PARALLEL_TEST_GROUPS']
914
+ Paperclip::Attachment.default_options[:path] = ":rails_root/public/system/:rails_env/#{ENV['TEST_ENV_NUMBER'].to_i}/:class/:attachment/:id_partition/:filename"
915
+ else
916
+ Paperclip::Attachment.default_options[:path] = ":rails_root/public/system/:rails_env/:class/:attachment/:id_partition/:filename"
917
+ end
918
+ ```
919
+
920
+ The important part here being the inclusion of `ENV['TEST_ENV_NUMBER']`, or a
921
+ similar mechanism for whichever parallel testing library you use.
922
+
923
+ **Integration Tests**
924
+
925
+ Using integration tests with FactoryGirl may save multiple copies of
926
+ your test files within the app. To avoid this, specify a custom path in
927
+ the `config/environments/test.rb` like so:
928
+
929
+ ```ruby
930
+ Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension"
931
+ ```
932
+
933
+ Then, make sure to delete that directory after the test suite runs by adding
934
+ this to `spec_helper.rb`.
935
+
936
+ ```ruby
937
+ config.after(:suite) do
938
+ FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"])
939
+ end
940
+ ```
941
+ ---
942
+
943
+ Contributing
944
+ ------------
945
+
946
+ If you'd like to contribute a feature or bugfix: Thanks! To make sure your
947
+ fix/feature has a high chance of being included, please read the following
948
+ guidelines:
949
+
950
+ 1. Post a [pull request](https://github.com/thoughtbot/paperclip/compare/).
951
+ 2. Make sure there are tests! We will not accept any patch that is not tested.
952
+ It's a rare time when explicit tests aren't needed. If you have questions
953
+ about writing tests for paperclip, please open a
954
+ [GitHub issue](https://github.com/thoughtbot/paperclip/issues/new).
955
+
956
+ Please see `CONTRIBUTING.md` for more details on contributing and running test.
957
+
958
+ Thank you to all [the contributors](https://github.com/thoughtbot/paperclip/contributors)!
959
+
960
+ License
961
+ -------
962
+
963
+ Paperclip is Copyright © 2008-2015 thoughtbot, inc. It is free software, and may be
964
+ redistributed under the terms specified in the MIT-LICENSE file.
965
+
966
+ About thoughtbot
967
+ ----------------
968
+
969
+ ![thoughtbot](https://thoughtbot.com/logo.png)
970
+
971
+ Paperclip is maintained and funded by thoughtbot.
972
+ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
973
+
974
+ We love open source software!
975
+ See [our other projects][community] or
976
+ [hire us][hire] to design, develop, and grow your product.
977
+
978
+ [community]: https://thoughtbot.com/community?utm_source=github
979
+ [hire]: https://thoughtbot.com?utm_source=github