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