mender_paperclip 2.4.3
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.
- data/LICENSE +26 -0
- data/README.md +402 -0
- data/Rakefile +86 -0
- data/generators/paperclip/USAGE +5 -0
- data/generators/paperclip/paperclip_generator.rb +27 -0
- data/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/init.rb +4 -0
- data/lib/generators/paperclip/USAGE +8 -0
- data/lib/generators/paperclip/paperclip_generator.rb +33 -0
- data/lib/generators/paperclip/templates/paperclip_migration.rb.erb +19 -0
- data/lib/paperclip/attachment.rb +454 -0
- data/lib/paperclip/callback_compatibility.rb +61 -0
- data/lib/paperclip/geometry.rb +120 -0
- data/lib/paperclip/interpolations.rb +181 -0
- data/lib/paperclip/iostream.rb +45 -0
- data/lib/paperclip/matchers/have_attached_file_matcher.rb +57 -0
- data/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb +81 -0
- data/lib/paperclip/matchers/validate_attachment_presence_matcher.rb +54 -0
- data/lib/paperclip/matchers/validate_attachment_size_matcher.rb +95 -0
- data/lib/paperclip/matchers.rb +33 -0
- data/lib/paperclip/missing_attachment_styles.rb +87 -0
- data/lib/paperclip/options.rb +79 -0
- data/lib/paperclip/processor.rb +58 -0
- data/lib/paperclip/railtie.rb +26 -0
- data/lib/paperclip/storage/filesystem.rb +81 -0
- data/lib/paperclip/storage/fog.rb +162 -0
- data/lib/paperclip/storage/s3.rb +262 -0
- data/lib/paperclip/storage.rb +3 -0
- data/lib/paperclip/style.rb +95 -0
- data/lib/paperclip/thumbnail.rb +105 -0
- data/lib/paperclip/upfile.rb +62 -0
- data/lib/paperclip/version.rb +3 -0
- data/lib/paperclip.rb +478 -0
- data/lib/tasks/paperclip.rake +97 -0
- data/rails/init.rb +2 -0
- data/shoulda_macros/paperclip.rb +124 -0
- data/test/attachment_test.rb +1120 -0
- data/test/database.yml +4 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/animated.gif +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/fog.yml +8 -0
- data/test/fixtures/s3.yml +8 -0
- data/test/fixtures/spaced file.png +0 -0
- data/test/fixtures/text.txt +1 -0
- data/test/fixtures/twopage.pdf +0 -0
- data/test/fixtures/uppercase.PNG +0 -0
- data/test/fog_test.rb +191 -0
- data/test/geometry_test.rb +206 -0
- data/test/helper.rb +152 -0
- data/test/integration_test.rb +654 -0
- data/test/interpolations_test.rb +195 -0
- data/test/iostream_test.rb +71 -0
- data/test/matchers/have_attached_file_matcher_test.rb +24 -0
- data/test/matchers/validate_attachment_content_type_matcher_test.rb +87 -0
- data/test/matchers/validate_attachment_presence_matcher_test.rb +26 -0
- data/test/matchers/validate_attachment_size_matcher_test.rb +51 -0
- data/test/options_test.rb +68 -0
- data/test/paperclip_missing_attachment_styles_test.rb +80 -0
- data/test/paperclip_test.rb +329 -0
- data/test/processor_test.rb +10 -0
- data/test/storage/filesystem_test.rb +52 -0
- data/test/storage/s3_live_test.rb +51 -0
- data/test/storage/s3_test.rb +633 -0
- data/test/style_test.rb +180 -0
- data/test/thumbnail_test.rb +383 -0
- data/test/upfile_test.rb +53 -0
- metadata +243 -0
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
LICENSE
|
3
|
+
|
4
|
+
The MIT License
|
5
|
+
|
6
|
+
Copyright (c) 2008 Jon Yurek and thoughtbot, inc.
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
10
|
+
in the Software without restriction, including without limitation the rights
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
13
|
+
furnished to do so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
16
|
+
all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24
|
+
THE SOFTWARE.
|
25
|
+
|
26
|
+
|
data/README.md
ADDED
@@ -0,0 +1,402 @@
|
|
1
|
+
# Paperclip [](http://travis-ci.org/thoughtbot/paperclip)
|
2
|
+
|
3
|
+
Paperclip is intended as an easy file attachment library for ActiveRecord. The
|
4
|
+
intent behind it was to keep setup as easy as possible and to treat files as
|
5
|
+
much like other attributes as possible. This means they aren't saved to their
|
6
|
+
final locations on disk, nor are they deleted if set to nil, until
|
7
|
+
ActiveRecord::Base#save is called. It manages validations based on size and
|
8
|
+
presence, if required. It can transform its assigned image into thumbnails if
|
9
|
+
needed, and the prerequisites are as simple as installing ImageMagick (which,
|
10
|
+
for most modern Unix-based systems, is as easy as installing the right
|
11
|
+
packages). Attached files are saved to the filesystem and referenced in the
|
12
|
+
browser by an easily understandable specification, which has sensible and
|
13
|
+
useful defaults.
|
14
|
+
|
15
|
+
See the documentation for `has_attached_file` in Paperclip::ClassMethods for
|
16
|
+
more detailed options.
|
17
|
+
|
18
|
+
The complete [RDoc](http://rdoc.info/gems/paperclip) is online.
|
19
|
+
|
20
|
+
Requirements
|
21
|
+
------------
|
22
|
+
|
23
|
+
ImageMagick must be installed and Paperclip must have access to it. To ensure
|
24
|
+
that it does, on your command line, run `which convert` (one of the ImageMagick
|
25
|
+
utilities). This will give you the path where that utility is installed. For
|
26
|
+
example, it might return `/usr/local/bin/convert`.
|
27
|
+
|
28
|
+
Then, in your environment config file, let Paperclip know to look there by adding that
|
29
|
+
directory to its path.
|
30
|
+
|
31
|
+
In development mode, you might add this line to `config/environments/development.rb)`:
|
32
|
+
|
33
|
+
Paperclip.options[:command_path] = "/usr/local/bin/"
|
34
|
+
|
35
|
+
If you're on Mac OSX, you'll want to run the following with Homebrew:
|
36
|
+
|
37
|
+
brew install imagemagick
|
38
|
+
|
39
|
+
If you are dealing with pdf uploads or running the test suite, also run:
|
40
|
+
|
41
|
+
brew install gs
|
42
|
+
|
43
|
+
Installation
|
44
|
+
------------
|
45
|
+
|
46
|
+
Paperclip is distributed as a gem, which is how it should be used in your app. It's
|
47
|
+
technically still installable as a plugin, but that's discouraged, as Rails plays
|
48
|
+
well with gems.
|
49
|
+
|
50
|
+
Include the gem in your Gemfile:
|
51
|
+
|
52
|
+
gem "paperclip", "~> 2.4"
|
53
|
+
|
54
|
+
Or, if you don't use Bundler (though you probably should, even in Rails 2), with config.gem
|
55
|
+
|
56
|
+
# In config/environment.rb
|
57
|
+
...
|
58
|
+
Rails::Initializer.run do |config|
|
59
|
+
...
|
60
|
+
config.gem "paperclip", :version => "~> 2.4"
|
61
|
+
...
|
62
|
+
end
|
63
|
+
For Non-Rails usage:
|
64
|
+
|
65
|
+
class ModuleName < ActiveRecord::Base
|
66
|
+
include Paperclip::Glue
|
67
|
+
...
|
68
|
+
end
|
69
|
+
|
70
|
+
Quick Start
|
71
|
+
-----------
|
72
|
+
|
73
|
+
In your model:
|
74
|
+
|
75
|
+
class User < ActiveRecord::Base
|
76
|
+
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
|
77
|
+
end
|
78
|
+
|
79
|
+
In your migrations:
|
80
|
+
|
81
|
+
class AddAvatarColumnsToUser < ActiveRecord::Migration
|
82
|
+
def self.up
|
83
|
+
add_column :users, :avatar_file_name, :string
|
84
|
+
add_column :users, :avatar_content_type, :string
|
85
|
+
add_column :users, :avatar_file_size, :integer
|
86
|
+
add_column :users, :avatar_updated_at, :datetime
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.down
|
90
|
+
remove_column :users, :avatar_file_name
|
91
|
+
remove_column :users, :avatar_content_type
|
92
|
+
remove_column :users, :avatar_file_size
|
93
|
+
remove_column :users, :avatar_updated_at
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
In your edit and new views:
|
98
|
+
|
99
|
+
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
|
100
|
+
<%= form.file_field :avatar %>
|
101
|
+
<% end %>
|
102
|
+
|
103
|
+
In your controller:
|
104
|
+
|
105
|
+
def create
|
106
|
+
@user = User.create( params[:user] )
|
107
|
+
end
|
108
|
+
|
109
|
+
In your show view:
|
110
|
+
|
111
|
+
<%= image_tag @user.avatar.url %>
|
112
|
+
<%= image_tag @user.avatar.url(:medium) %>
|
113
|
+
<%= image_tag @user.avatar.url(:thumb) %>
|
114
|
+
|
115
|
+
To detach a file, simply set the attribute to `nil`:
|
116
|
+
|
117
|
+
@user.avatar = nil
|
118
|
+
@user.save
|
119
|
+
|
120
|
+
Usage
|
121
|
+
-----
|
122
|
+
|
123
|
+
The basics of paperclip are quite simple: Declare that your model has an
|
124
|
+
attachment with the has_attached_file method, and give it a name. Paperclip
|
125
|
+
will wrap up up to four attributes (all prefixed with that attachment's name,
|
126
|
+
so you can have multiple attachments per model if you wish) and give them a
|
127
|
+
friendly front end. The attributes are `<attachment>_file_name`,
|
128
|
+
`<attachment>_file_size`, `<attachment>_content_type`, and `<attachment>_updated_at`.
|
129
|
+
Only `<attachment>_file_name` is required for paperclip to operate. More
|
130
|
+
information about the options to has_attached_file is available in the
|
131
|
+
documentation of Paperclip::ClassMethods.
|
132
|
+
|
133
|
+
Attachments can be validated with Paperclip's validation methods,
|
134
|
+
validates_attachment_presence, validates_attachment_content_type, and
|
135
|
+
validates_attachment_size.
|
136
|
+
|
137
|
+
Storage
|
138
|
+
-------
|
139
|
+
|
140
|
+
The files that are assigned as attachments are, by default, placed in the
|
141
|
+
directory specified by the :path option to has_attached_file. By default, this
|
142
|
+
location is ":rails_root/public/system/:attachment/:id/:style/:filename". This
|
143
|
+
location was chosen because on standard Capistrano deployments, the
|
144
|
+
public/system directory is symlinked to the app's shared directory, meaning it
|
145
|
+
will survive between deployments. For example, using that :path, you may have a
|
146
|
+
file at
|
147
|
+
|
148
|
+
/data/myapp/releases/20081229172410/public/system/avatars/13/small/my_pic.png
|
149
|
+
|
150
|
+
_NOTE: This is a change from previous versions of Paperclip, but is overall a
|
151
|
+
safer choice for the default file store._
|
152
|
+
|
153
|
+
You may also choose to store your files using Amazon's S3 service. You can find
|
154
|
+
more information about S3 storage at the description for
|
155
|
+
Paperclip::Storage::S3.
|
156
|
+
|
157
|
+
Files on the local filesystem (and in the Rails app's public directory) will be
|
158
|
+
available to the internet at large. If you require access control, it's
|
159
|
+
possible to place your files in a different location. You will need to change
|
160
|
+
both the :path and :url options in order to make sure the files are unavailable
|
161
|
+
to the public. Both :path and :url allow the same set of interpolated
|
162
|
+
variables.
|
163
|
+
|
164
|
+
Post Processing
|
165
|
+
---------------
|
166
|
+
|
167
|
+
Paperclip supports an extensible selection of post-processors. When you define
|
168
|
+
a set of styles for an attachment, by default it is expected that those
|
169
|
+
"styles" are actually "thumbnails". However, you can do much more than just
|
170
|
+
thumbnail images. By defining a subclass of Paperclip::Processor, you can
|
171
|
+
perform any processing you want on the files that are attached. Any file in
|
172
|
+
your Rails app's lib/paperclip_processors directory is automatically loaded by
|
173
|
+
paperclip, allowing you to easily define custom processors. You can specify a
|
174
|
+
processor with the :processors option to has_attached_file:
|
175
|
+
|
176
|
+
has_attached_file :scan, :styles => { :text => { :quality => :better } },
|
177
|
+
:processors => [:ocr]
|
178
|
+
|
179
|
+
This would load the hypothetical class Paperclip::Ocr, which would have the
|
180
|
+
hash "{ :quality => :better }" passed to it along with the uploaded file. For
|
181
|
+
more information about defining processors, see Paperclip::Processor.
|
182
|
+
|
183
|
+
The default processor is Paperclip::Thumbnail. For backwards compatability
|
184
|
+
reasons, you can pass a single geometry string or an array containing a
|
185
|
+
geometry and a format, which the file will be converted to, like so:
|
186
|
+
|
187
|
+
has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] }
|
188
|
+
|
189
|
+
This will convert the "thumb" style to a 32x32 square in png format, regardless
|
190
|
+
of what was uploaded. If the format is not specified, it is kept the same (i.e.
|
191
|
+
jpgs will remain jpgs).
|
192
|
+
|
193
|
+
Multiple processors can be specified, and they will be invoked in the order
|
194
|
+
they are defined in the :processors array. Each successive processor will
|
195
|
+
be given the result of the previous processor's execution. All processors will
|
196
|
+
receive the same parameters, which are what you define in the :styles hash.
|
197
|
+
For example, assuming we had this definition:
|
198
|
+
|
199
|
+
has_attached_file :scan, :styles => { :text => { :quality => :better } },
|
200
|
+
:processors => [:rotator, :ocr]
|
201
|
+
|
202
|
+
then both the :rotator processor and the :ocr processor would receive the
|
203
|
+
options "{ :quality => :better }". This parameter may not mean anything to one
|
204
|
+
or more or the processors, and they are expected to ignore it.
|
205
|
+
|
206
|
+
_NOTE: Because processors operate by turning the original attachment into the
|
207
|
+
styles, no processors will be run if there are no styles defined._
|
208
|
+
|
209
|
+
If you're interested in caching your thumbnail's width, height and size in the
|
210
|
+
database, take a look at the [paperclip-meta](https://github.com/y8/paperclip-meta) gem.
|
211
|
+
|
212
|
+
Also, if you're interested in generating the thumbnail on-the-fly, you might want
|
213
|
+
to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly) gem.
|
214
|
+
|
215
|
+
Events
|
216
|
+
------
|
217
|
+
|
218
|
+
Before and after the Post Processing step, Paperclip calls back to the model
|
219
|
+
with a few callbacks, allowing the model to change or cancel the processing
|
220
|
+
step. The callbacks are `before_post_process` and `after_post_process` (which
|
221
|
+
are called before and after the processing of each attachment), and the
|
222
|
+
attachment-specific `before_<attachment>_post_process` and
|
223
|
+
`after_<attachment>_post_process`. The callbacks are intended to be as close to
|
224
|
+
normal ActiveRecord callbacks as possible, so if you return false (specifically
|
225
|
+
- returning nil is not the same) in a before_ filter, the post processing step
|
226
|
+
will halt. Returning false in an after_ filter will not halt anything, but you
|
227
|
+
can access the model and the attachment if necessary.
|
228
|
+
|
229
|
+
_NOTE: Post processing will not even *start* if the attachment is not valid
|
230
|
+
according to the validations. Your callbacks and processors will *only* be
|
231
|
+
called with valid attachments._
|
232
|
+
|
233
|
+
URI Obfuscation
|
234
|
+
---------------
|
235
|
+
|
236
|
+
Paperclip has an interpolation called `:hash` for obfuscating filenames of
|
237
|
+
publicly-available files. For more on this feature read the author's own
|
238
|
+
explanation.
|
239
|
+
|
240
|
+
[https://github.com/thoughtbot/paperclip/pull/416](https://github.com/thoughtbot/paperclip/pull/416)
|
241
|
+
|
242
|
+
MD5 Checksum / Fingerprint
|
243
|
+
-------
|
244
|
+
|
245
|
+
A MD5 checksum of the original file assigned will be placed in the model if it
|
246
|
+
has an attribute named fingerprint. Following the user model migration example
|
247
|
+
above, the migration would look like the following.
|
248
|
+
|
249
|
+
class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
|
250
|
+
def self.up
|
251
|
+
add_column :users, :avatar_fingerprint, :string
|
252
|
+
end
|
253
|
+
|
254
|
+
def self.down
|
255
|
+
remove_column :users, :avatar_fingerprint
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
Custom Attachment Processors
|
260
|
+
-------
|
261
|
+
|
262
|
+
Custom attachment processors can be implemented and their only requirement is
|
263
|
+
to inherit from `Paperclip::Processor` (see `lib/paperclip/processor.rb`).
|
264
|
+
For example, when `:styles` are specified for an image attachment, the
|
265
|
+
thumbnail processor (see `lib/paperclip/thumbnail.rb`) is loaded without having
|
266
|
+
to specify it as a `:processor` parameter to `has_attached_file`. When any
|
267
|
+
other processor is defined it must be called out in the `:processors`
|
268
|
+
parameter if it is to be applied to the attachment. The thumbnail processor
|
269
|
+
uses the imagemagick `convert` command to do the work of resizing image
|
270
|
+
thumbnails. It would be easy to create a custom processor that watermarks
|
271
|
+
an image using imagemagick's `composite` command. Following the
|
272
|
+
implementation pattern of the thumbnail processor would be a way to implement a
|
273
|
+
watermark processor. All kinds of attachment processors can be created;
|
274
|
+
a few utility examples would be compression and encryption processors.
|
275
|
+
|
276
|
+
|
277
|
+
Dynamic Configuration
|
278
|
+
---------------------
|
279
|
+
|
280
|
+
Callable objects (lambdas, Procs) can be used in a number of places for dynamic
|
281
|
+
configuration throughout Paperclip. This strategy exists in a number of
|
282
|
+
components of the library but is most significant in the possibilities for
|
283
|
+
allowing custom styles and processors to be applied for specific model
|
284
|
+
instances, rather than applying defined styles and processors across all
|
285
|
+
instances.
|
286
|
+
|
287
|
+
Dynamic Styles:
|
288
|
+
|
289
|
+
Imagine a user model that had different styles based on the role of the user.
|
290
|
+
Perhaps some users are bosses (e.g. a User model instance responds to #boss?)
|
291
|
+
and merit a bigger avatar thumbnail than regular users. The configuration to
|
292
|
+
determine what style parameters are to be used based on the user role might
|
293
|
+
look as follows where a boss will receive a `300x300` thumbnail otherwise a
|
294
|
+
`100x100` thumbnail will be created.
|
295
|
+
|
296
|
+
class User < ActiveRecord::Base
|
297
|
+
has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") }
|
298
|
+
end
|
299
|
+
|
300
|
+
Dynamic Processors:
|
301
|
+
|
302
|
+
Another contrived example is a user model that is aware of which file processors
|
303
|
+
should be applied to it (beyond the implied `thumbnail` processor invoked when
|
304
|
+
`:styles` are defined). Perhaps we have a watermark processor available and it is
|
305
|
+
only used on the avatars of certain models. The configuration for this might be
|
306
|
+
where the instance is queried for which processors should be applied to it.
|
307
|
+
Presumably some users might return `[:thumbnail, :watermark]` for its
|
308
|
+
processors, where a defined `watermark` processor is invoked after the
|
309
|
+
`thumbnail` processor already defined by Paperclip.
|
310
|
+
|
311
|
+
class User < ActiveRecord::Base
|
312
|
+
has_attached_file :avatar, :processors => lambda { |instance| instance.processors }
|
313
|
+
attr_accessor :watermark
|
314
|
+
end
|
315
|
+
|
316
|
+
Deploy
|
317
|
+
------
|
318
|
+
|
319
|
+
Paperclip is aware of new attachment styles you have added in previous deploy. The only thing you should do after each deployment is to call
|
320
|
+
`rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
|
321
|
+
by default. You can change it by:
|
322
|
+
|
323
|
+
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
|
324
|
+
|
325
|
+
Here is an example for Capistrano:
|
326
|
+
|
327
|
+
namespace :deploy do
|
328
|
+
desc "build missing paperclip styles"
|
329
|
+
task :build_missing_paperclip_styles, :roles => :app do
|
330
|
+
run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
after("deploy:update_code", "deploy:build_missing_paperclip_styles")
|
335
|
+
|
336
|
+
Now you don't have to remember to refresh thumbnails in production everytime you add new style.
|
337
|
+
Unfortunately it does not work with dynamic styles - it just ignores them.
|
338
|
+
|
339
|
+
If you already have working app and don't want `rake paperclip:refresh:missing_styles` to refresh old pictures, you need to tell
|
340
|
+
Paperclip about existing styles. Simply create paperclip_attachments.yml file by hand. For example:
|
341
|
+
|
342
|
+
class User < ActiveRecord::Base
|
343
|
+
has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'}
|
344
|
+
end
|
345
|
+
|
346
|
+
class Book < ActiveRecord::Base
|
347
|
+
has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
|
348
|
+
has_attached_file :sample, :styles => {:thumb => 'x100'}
|
349
|
+
end
|
350
|
+
|
351
|
+
Then in `RAILS_ROOT/public/system/paperclip_attachments.yml`:
|
352
|
+
|
353
|
+
---
|
354
|
+
:User:
|
355
|
+
:avatar:
|
356
|
+
- :thumb
|
357
|
+
- :croppable
|
358
|
+
- :big
|
359
|
+
:Book:
|
360
|
+
:cover:
|
361
|
+
- :small
|
362
|
+
- :large
|
363
|
+
:sample:
|
364
|
+
- :thumb
|
365
|
+
|
366
|
+
Testing
|
367
|
+
-------
|
368
|
+
|
369
|
+
Paperclip provides rspec-compatible matchers for testing attachments. See the
|
370
|
+
documentation on [Paperclip::Shoulda::Matchers](http://rubydoc.info/gems/paperclip/Paperclip/Shoulda/Matchers)
|
371
|
+
for more information.
|
372
|
+
|
373
|
+
Contributing
|
374
|
+
------------
|
375
|
+
|
376
|
+
If you'd like to contribute a feature or bugfix: Thanks! To make sure your
|
377
|
+
fix/feature has a high chance of being included, please read the following
|
378
|
+
guidelines:
|
379
|
+
|
380
|
+
1. Ask on the mailing list[http://groups.google.com/group/paperclip-plugin], or
|
381
|
+
post a new GitHub Issue[http://github.com/thoughtbot/paperclip/issues].
|
382
|
+
2. Make sure there are tests! We will not accept any patch that is not tested.
|
383
|
+
It's a rare time when explicit tests aren't needed. If you have questions
|
384
|
+
about writing tests for paperclip, please ask the mailing list.
|
385
|
+
|
386
|
+
Please see CONTRIBUTING.md for details.
|
387
|
+
|
388
|
+
Credits
|
389
|
+
-------
|
390
|
+
|
391
|
+

|
392
|
+
|
393
|
+
Paperclip is maintained and funded by [thoughtbot, inc](http://thoughtbot.com/community)
|
394
|
+
|
395
|
+
Thank you to all [the contributors](https://github.com/thoughtbot/paperclip/contributors)!
|
396
|
+
|
397
|
+
The names and logos for thoughtbot are trademarks of thoughtbot, inc.
|
398
|
+
|
399
|
+
License
|
400
|
+
-------
|
401
|
+
|
402
|
+
Paperclip is Copyright © 2008-2011 thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'appraisal'
|
4
|
+
|
5
|
+
require 'rake'
|
6
|
+
require 'rake/testtask'
|
7
|
+
require 'rdoc/task'
|
8
|
+
require 'cucumber/rake/task'
|
9
|
+
|
10
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
|
11
|
+
require 'paperclip'
|
12
|
+
|
13
|
+
desc 'Default: run unit tests.'
|
14
|
+
task :default => [:clean, 'appraisal:install', :all]
|
15
|
+
|
16
|
+
desc 'Test the paperclip plugin under all supported Rails versions.'
|
17
|
+
task :all do |t|
|
18
|
+
exec('rake appraisal test cucumber')
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Test the paperclip plugin.'
|
22
|
+
Rake::TestTask.new(:test) do |t|
|
23
|
+
t.libs << 'lib' << 'profile'
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
t.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'Run integration test'
|
29
|
+
Cucumber::Rake::Task.new do |t|
|
30
|
+
t.cucumber_opts = %w{--format progress}
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Start an IRB session with all necessary files required.'
|
34
|
+
task :shell do |t|
|
35
|
+
chdir File.dirname(__FILE__)
|
36
|
+
exec 'irb -I lib/ -I lib/paperclip -r rubygems -r active_record -r tempfile -r init'
|
37
|
+
end
|
38
|
+
|
39
|
+
desc 'Generate documentation for the paperclip plugin.'
|
40
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
41
|
+
rdoc.rdoc_dir = 'doc'
|
42
|
+
rdoc.title = 'Paperclip'
|
43
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
47
|
+
|
48
|
+
desc 'Update documentation on website'
|
49
|
+
task :sync_docs => 'rdoc' do
|
50
|
+
`rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/paperclip`
|
51
|
+
end
|
52
|
+
|
53
|
+
desc 'Clean up files.'
|
54
|
+
task :clean do |t|
|
55
|
+
FileUtils.rm_rf "doc"
|
56
|
+
FileUtils.rm_rf "tmp"
|
57
|
+
FileUtils.rm_rf "pkg"
|
58
|
+
FileUtils.rm_rf "public"
|
59
|
+
FileUtils.rm "test/debug.log" rescue nil
|
60
|
+
FileUtils.rm "test/paperclip.db" rescue nil
|
61
|
+
Dir.glob("paperclip-*.gem").each{|f| FileUtils.rm f }
|
62
|
+
end
|
63
|
+
|
64
|
+
desc 'Build the gemspec.'
|
65
|
+
task :gemspec do |t|
|
66
|
+
exec 'gem build paperclip.gemspec'
|
67
|
+
end
|
68
|
+
|
69
|
+
desc "Print a list of the files to be put into the gem"
|
70
|
+
task :manifest => :clean do
|
71
|
+
spec.files.each do |file|
|
72
|
+
puts file
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
desc "Generate a gemspec file for GitHub"
|
77
|
+
task :gemspec => :clean do
|
78
|
+
File.open("#{spec.name}.gemspec", 'w') do |f|
|
79
|
+
f.write spec.to_ruby
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
desc "Build the gem into the current directory"
|
84
|
+
task :gem => :gemspec do
|
85
|
+
`gem build #{spec.name}.gemspec`
|
86
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class PaperclipGenerator < Rails::Generator::NamedBase
|
2
|
+
attr_accessor :attachments, :migration_name
|
3
|
+
|
4
|
+
def initialize(args, options = {})
|
5
|
+
super
|
6
|
+
@class_name, @attachments = args[0], args[1..-1]
|
7
|
+
end
|
8
|
+
|
9
|
+
def manifest
|
10
|
+
file_name = generate_file_name
|
11
|
+
@migration_name = file_name.camelize
|
12
|
+
record do |m|
|
13
|
+
m.migration_template "paperclip_migration.rb.erb",
|
14
|
+
File.join('db', 'migrate'),
|
15
|
+
:migration_file_name => file_name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def generate_file_name
|
22
|
+
names = attachments.map{|a| a.underscore }
|
23
|
+
names = names[0..-2] + ["and", names[-1]] if names.length > 1
|
24
|
+
"add_attachments_#{names.join("_")}_to_#{@class_name.underscore}"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class <%= migration_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
<% attachments.each do |attachment| -%>
|
4
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
|
5
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
|
6
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer
|
7
|
+
add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime
|
8
|
+
<% end -%>
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
<% attachments.each do |attachment| -%>
|
13
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
|
14
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
|
15
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_size
|
16
|
+
remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at
|
17
|
+
<% end -%>
|
18
|
+
end
|
19
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
class PaperclipGenerator < ActiveRecord::Generators::Base
|
4
|
+
desc "Create a migration to add paperclip-specific fields to your model. " +
|
5
|
+
"The NAME argument is the name of your model, and the following " +
|
6
|
+
"arguments are the name of the attachments"
|
7
|
+
|
8
|
+
argument :attachment_names, :required => true, :type => :array, :desc => "The names of the attachment(s) to add.",
|
9
|
+
:banner => "attachment_one attachment_two attachment_three ..."
|
10
|
+
|
11
|
+
def self.source_root
|
12
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_migration
|
16
|
+
migration_template "paperclip_migration.rb.erb", "db/migrate/#{migration_file_name}"
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def migration_name
|
22
|
+
"add_attachment_#{attachment_names.join("_")}_to_#{name.underscore}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def migration_file_name
|
26
|
+
"#{migration_name}.rb"
|
27
|
+
end
|
28
|
+
|
29
|
+
def migration_class_name
|
30
|
+
migration_name.camelize
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
<% attachment_names.each do |attachment| -%>
|
4
|
+
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string
|
5
|
+
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string
|
6
|
+
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer
|
7
|
+
add_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime
|
8
|
+
<% end -%>
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
<% attachment_names.each do |attachment| -%>
|
13
|
+
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_name
|
14
|
+
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_content_type
|
15
|
+
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_file_size
|
16
|
+
remove_column :<%= name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at
|
17
|
+
<% end -%>
|
18
|
+
end
|
19
|
+
end
|