Commit 45bf52aa by Janko Marohnić Committed by Mike Burns

Syntax highlight all the code in the readme

parent ad83b1d0
...@@ -78,24 +78,29 @@ on GitHub. ...@@ -78,24 +78,29 @@ on GitHub.
For Non-Rails usage: For Non-Rails usage:
class ModuleName < ActiveRecord::Base ```ruby
class ModuleName < ActiveRecord::Base
include Paperclip::Glue include Paperclip::Glue
... ...
end end
```
Quick Start Quick Start
----------- -----------
In your model: In your model:
class User < ActiveRecord::Base ```ruby
class User < ActiveRecord::Base
attr_accessible :avatar attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" } has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end end
```
In your migrations: In your migrations:
class AddAvatarColumnsToUsers < ActiveRecord::Migration ```ruby
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def self.up def self.up
add_attachment :users, :avatar add_attachment :users, :avatar
end end
...@@ -103,32 +108,41 @@ In your migrations: ...@@ -103,32 +108,41 @@ In your migrations:
def self.down def self.down
remove_attachment :users, :avatar remove_attachment :users, :avatar
end end
end end
```
(Or you can use migration generator: `rails generate paperclip user avatar`) (Or you can use migration generator: `rails generate paperclip user avatar`)
In your edit and new views: In your edit and new views:
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %> ```erb
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %> <%= form.file_field :avatar %>
<% end %> <% end %>
```
In your controller: In your controller:
def create ```ruby
def create
@user = User.create( params[:user] ) @user = User.create( params[:user] )
end end
```
In your show view: In your show view:
<%= image_tag @user.avatar.url %> ```erb
<%= image_tag @user.avatar.url(:medium) %> <%= image_tag @user.avatar.url %>
<%= image_tag @user.avatar.url(:thumb) %> <%= image_tag @user.avatar.url(:medium) %>
<%= image_tag @user.avatar.url(:thumb) %>
```
To detach a file, simply set the attribute to `nil`: To detach a file, simply set the attribute to `nil`:
@user.avatar = nil ```ruby
@user.save @user.avatar = nil
@user.save
```
Usage Usage
----- -----
...@@ -160,8 +174,10 @@ For validations, Paperclip introduces several validators to validate your attach ...@@ -160,8 +174,10 @@ For validations, Paperclip introduces several validators to validate your attach
Example Usage: Example Usage:
validates :avatar, :attachment_presence => true ```ruby
validates_with AttachmentPresenceValidator, :attributes => :avatar validates :avatar, :attachment_presence => true
validates_with AttachmentPresenceValidator, :attributes => :avatar
```
Validators can also be defined using the old helper style: Validators can also be defined using the old helper style:
...@@ -171,13 +187,17 @@ Validators can also be defined using the old helper style: ...@@ -171,13 +187,17 @@ Validators can also be defined using the old helper style:
Example Usage: Example Usage:
validates_attachment_presence :avatar ```ruby
validates_attachment_presence :avatar
```
Lastly, you can also define multiple validations on a single attachment using `validates_attachment`: Lastly, you can also define multiple validations on a single attachment using `validates_attachment`:
validates_attachment :avatar, :presence => true, ```ruby
validates_attachment :avatar, :presence => true,
:content_type => { :content_type => "image/jpg" }, :content_type => { :content_type => "image/jpg" },
:size => { :in => 0..10.kilobytes } :size => { :in => 0..10.kilobytes }
```
Defaults Defaults
-------- --------
...@@ -214,27 +234,32 @@ model. There are two types of method: ...@@ -214,27 +234,32 @@ model. There are two types of method:
### Table Definition ### Table Definition
class AddAttachmentToUsers < ActiveRecord::Migration ```ruby
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up def self.up
create_table :users do |t| create_table :users do |t|
t.attachment :avatar t.attachment :avatar
end end
end end
end end
```
If you're using Rails 3.2 or newer, this method works in `change` method as well: If you're using Rails 3.2 or newer, this method works in `change` method as well:
class AddAttachmentToUsers < ActiveRecord::Migration ```ruby
class AddAttachmentToUsers < ActiveRecord::Migration
def change def change
create_table :users do |t| create_table :users do |t|
t.attachment :avatar t.attachment :avatar
end end
end end
end end
```
### Schema Definition ### Schema Definition
class AddAttachmentToUsers < ActiveRecord::Migration ```ruby
class AddAttachmentToUsers < ActiveRecord::Migration
def self.up def self.up
add_attachment :users, :avatar add_attachment :users, :avatar
end end
...@@ -242,15 +267,18 @@ If you're using Rails 3.2 or newer, this method works in `change` method as well ...@@ -242,15 +267,18 @@ If you're using Rails 3.2 or newer, this method works in `change` method as well
def self.down def self.down
remove_attachment :users, :avatar remove_attachment :users, :avatar
end end
end end
```
If you're using Rails 3.2 or newer, you only need `add_attachment` in your `change` method: If you're using Rails 3.2 or newer, you only need `add_attachment` in your `change` method:
class AddAttachmentToUsers < ActiveRecord::Migration ```ruby
class AddAttachmentToUsers < ActiveRecord::Migration
def change def change
add_attachment :users, :avatar add_attachment :users, :avatar
end end
end end
```
### Vintage syntax ### Vintage syntax
...@@ -290,7 +318,9 @@ safer choice for the default file store._ ...@@ -290,7 +318,9 @@ safer choice for the default file store._
You may also choose to store your files using Amazon's S3 service. To do so, include You may also choose to store your files using Amazon's S3 service. To do so, include
the `aws-sdk` gem in your Gemfile: the `aws-sdk` gem in your Gemfile:
gem 'aws-sdk', '~> 1.3.4' ```ruby
gem 'aws-sdk', '~> 1.3.4'
```
And then you can specify using S3 from `has_attached_file`. And then you can specify using S3 from `has_attached_file`.
You can find more information about configuring and using S3 storage in You can find more information about configuring and using S3 storage in
...@@ -315,8 +345,10 @@ your Rails app's lib/paperclip\_processors directory is automatically loaded by ...@@ -315,8 +345,10 @@ your Rails app's lib/paperclip\_processors directory is automatically loaded by
paperclip, allowing you to easily define custom processors. You can specify a paperclip, allowing you to easily define custom processors. You can specify a
processor with the :processors option to `has_attached_file`: processor with the :processors option to `has_attached_file`:
has_attached_file :scan, :styles => { :text => { :quality => :better } }, ```ruby
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:ocr] :processors => [:ocr]
```
This would load the hypothetical class Paperclip::Ocr, which would have the This would load the hypothetical class Paperclip::Ocr, which would have the
hash "{ :quality => :better }" passed to it along with the uploaded file. For hash "{ :quality => :better }" passed to it along with the uploaded file. For
...@@ -326,7 +358,9 @@ The default processor is Paperclip::Thumbnail. For backwards compatibility ...@@ -326,7 +358,9 @@ The default processor is Paperclip::Thumbnail. For backwards compatibility
reasons, you can pass a single geometry string or an array containing a reasons, you can pass a single geometry string or an array containing a
geometry and a format, which the file will be converted to, like so: geometry and a format, which the file will be converted to, like so:
has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] } ```ruby
has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] }
```
This will convert the "thumb" style to a 32x32 square in png format, regardless This will convert the "thumb" style to a 32x32 square in png format, regardless
of what was uploaded. If the format is not specified, it is kept the same (i.e. of what was uploaded. If the format is not specified, it is kept the same (i.e.
...@@ -339,8 +373,10 @@ be given the result of the previous processor's execution. All processors will ...@@ -339,8 +373,10 @@ be given the result of the previous processor's execution. All processors will
receive the same parameters, which are what you define in the :styles hash. receive the same parameters, which are what you define in the :styles hash.
For example, assuming we had this definition: For example, assuming we had this definition:
has_attached_file :scan, :styles => { :text => { :quality => :better } }, ```ruby
has_attached_file :scan, :styles => { :text => { :quality => :better } },
:processors => [:rotator, :ocr] :processors => [:rotator, :ocr]
```
then both the :rotator processor and the :ocr processor would receive the then both the :rotator processor and the :ocr processor would receive the
options "{ :quality => :better }". This parameter may not mean anything to one options "{ :quality => :better }". This parameter may not mean anything to one
...@@ -373,7 +409,8 @@ _NOTE: Post processing will not even *start* if the attachment is not valid ...@@ -373,7 +409,8 @@ _NOTE: Post processing will not even *start* if the attachment is not valid
according to the validations. Your callbacks and processors will *only* be according to the validations. Your callbacks and processors will *only* be
called with valid attachments._ called with valid attachments._
class Message < ActiveRecord::Base ```ruby
class Message < ActiveRecord::Base
has_attached_file :asset, styles: {thumb: "100x100#"} has_attached_file :asset, styles: {thumb: "100x100#"}
before_post_process :skip_for_audio before_post_process :skip_for_audio
...@@ -381,7 +418,8 @@ called with valid attachments._ ...@@ -381,7 +418,8 @@ called with valid attachments._
def skip_for_audio def skip_for_audio
! %w(audio/ogg application/ogg).include?(asset_content_type) ! %w(audio/ogg application/ogg).include?(asset_content_type)
end end
end end
```
URI Obfuscation URI Obfuscation
--------------- ---------------
...@@ -391,10 +429,12 @@ publicly-available files. ...@@ -391,10 +429,12 @@ publicly-available files.
Example Usage: Example Usage:
has_attached_file :avatar, { ```ruby
has_attached_file :avatar, {
:url => "/system/:hash.:extension", :url => "/system/:hash.:extension",
:hash_secret => "longSecretString" :hash_secret => "longSecretString"
} }
```
The `:hash` interpolation will be replaced with a unique hash made up of whatever The `:hash` interpolation will be replaced with a unique hash made up of whatever
...@@ -411,7 +451,8 @@ A MD5 checksum of the original file assigned will be placed in the model if it ...@@ -411,7 +451,8 @@ A MD5 checksum of the original file assigned will be placed in the model if it
has an attribute named fingerprint. Following the user model migration example has an attribute named fingerprint. Following the user model migration example
above, the migration would look like the following. above, the migration would look like the following.
class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration ```ruby
class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
def self.up def self.up
add_column :users, :avatar_fingerprint, :string add_column :users, :avatar_fingerprint, :string
end end
...@@ -419,7 +460,8 @@ above, the migration would look like the following. ...@@ -419,7 +460,8 @@ above, the migration would look like the following.
def self.down def self.down
remove_column :users, :avatar_fingerprint remove_column :users, :avatar_fingerprint
end end
end end
```
Custom Attachment Processors Custom Attachment Processors
------- -------
...@@ -458,9 +500,11 @@ determine what style parameters are to be used based on the user role might ...@@ -458,9 +500,11 @@ determine what style parameters are to be used based on the user role might
look as follows where a boss will receive a `300x300` thumbnail otherwise a look as follows where a boss will receive a `300x300` thumbnail otherwise a
`100x100` thumbnail will be created. `100x100` thumbnail will be created.
class User < ActiveRecord::Base ```ruby
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") } has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") }
end end
```
### Dynamic Processors: ### Dynamic Processors:
...@@ -473,10 +517,12 @@ Presumably some users might return `[:thumbnail, :watermark]` for its ...@@ -473,10 +517,12 @@ Presumably some users might return `[:thumbnail, :watermark]` for its
processors, where a defined `watermark` processor is invoked after the processors, where a defined `watermark` processor is invoked after the
`thumbnail` processor already defined by Paperclip. `thumbnail` processor already defined by Paperclip.
class User < ActiveRecord::Base ```ruby
class User < ActiveRecord::Base
has_attached_file :avatar, :processors => lambda { |instance| instance.processors } has_attached_file :avatar, :processors => lambda { |instance| instance.processors }
attr_accessor :watermark attr_accessor :watermark
end end
```
Deployment Deployment
---------- ----------
...@@ -485,18 +531,22 @@ Paperclip is aware of new attachment styles you have added in previous deploys. ...@@ -485,18 +531,22 @@ Paperclip is aware of new attachment styles you have added in previous deploys.
`rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml` `rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
by default. You can change it by: by default. You can change it by:
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml' ```ruby
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
```
Here is an example for Capistrano: Here is an example for Capistrano:
namespace :deploy do ```ruby
namespace :deploy do
desc "build missing paperclip styles" desc "build missing paperclip styles"
task :build_missing_paperclip_styles, :roles => :app do task :build_missing_paperclip_styles, :roles => :app do
run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles" run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
end end
end end
after("deploy:update_code", "deploy:build_missing_paperclip_styles") after("deploy:update_code", "deploy:build_missing_paperclip_styles")
```
Now you don't have to remember to refresh thumbnails in production every time you add a new style. Now you don't have to remember to refresh thumbnails in production every time you add a new style.
Unfortunately it does not work with dynamic styles - it just ignores them. Unfortunately it does not work with dynamic styles - it just ignores them.
...@@ -504,14 +554,16 @@ Unfortunately it does not work with dynamic styles - it just ignores them. ...@@ -504,14 +554,16 @@ Unfortunately it does not work with dynamic styles - it just ignores them.
If you already have a working app and don't want `rake paperclip:refresh:missing_styles` to refresh old pictures, you need to tell If you already have a working app and don't want `rake paperclip:refresh:missing_styles` to refresh old pictures, you need to tell
Paperclip about existing styles. Simply create a `paperclip_attachments.yml` file by hand. For example: Paperclip about existing styles. Simply create a `paperclip_attachments.yml` file by hand. For example:
class User < ActiveRecord::Base ```ruby
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'} has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'}
end end
class Book < ActiveRecord::Base class Book < ActiveRecord::Base
has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'} has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
has_attached_file :sample, :styles => {:thumb => 'x100'} has_attached_file :sample, :styles => {:thumb => 'x100'}
end end
```
Then in `RAILS_ROOT/public/system/paperclip_attachments.yml`: Then in `RAILS_ROOT/public/system/paperclip_attachments.yml`:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment