Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paperclip
Commits
45bf52aa
Commit
45bf52aa
authored
Sep 04, 2012
by
Janko Marohnić
Committed by
Mike Burns
Sep 07, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Syntax highlight all the code in the readme
parent
ad83b1d0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
157 additions
and
105 deletions
+157
-105
README.md
+157
-105
No files found.
README.md
View file @
45bf52aa
...
@@ -78,57 +78,71 @@ on GitHub.
...
@@ -78,57 +78,71 @@ on GitHub.
For Non-Rails usage:
For Non-Rails usage:
class ModuleName < ActiveRecord::Base
```
ruby
include Paperclip::Glue
class
ModuleName
<
ActiveRecord
::
Base
...
include
Paperclip
::
Glue
end
...
end
```
Quick Start
Quick Start
-----------
-----------
In your model:
In your model:
class User < ActiveRecord::Base
```
ruby
attr_accessible :avatar
class
User
<
ActiveRecord
::
Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
attr_accessible
:avatar
end
has_attached_file
:avatar
,
:styles
=>
{
:medium
=>
"300x300>"
,
:thumb
=>
"100x100>"
}
end
```
In your migrations:
In your migrations:
class AddAvatarColumnsToUsers < ActiveRecord::Migration
```
ruby
def self.up
class
AddAvatarColumnsToUsers
<
ActiveRecord
::
Migration
add_attachment :users, :avatar
def
self
.
up
end
add_attachment
:users
,
:avatar
end
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.file_field :avatar %>
<%=
form_for
@user
,
:url
=>
users_path
,
:html
=>
{
:multipart
=>
true
}
do
|
form
|
%>
<% end %>
<%=
form
.
file_field
:avatar
%>
<%
end
%>
```
In your controller:
In your controller:
def create
```
ruby
@user = User.create( params[:user] )
def
create
end
@user
=
User
.
create
(
params
[
:user
]
)
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
:content_type => { :content_type => "image/jpg" },
validates_attachment
:avatar
,
:presence
=>
true
,
:size => { :in => 0..10.kilobytes }
:content_type
=>
{
:content_type
=>
"image/jpg"
},
:size
=>
{
:in
=>
0
..
10
.
kilobytes
}
```
Defaults
Defaults
--------
--------
...
@@ -214,43 +234,51 @@ model. There are two types of method:
...
@@ -214,43 +234,51 @@ model. There are two types of method:
### Table Definition
### Table Definition
class AddAttachmentToUsers < ActiveRecord::Migration
```
ruby
def self.up
class
AddAttachmentToUsers
<
ActiveRecord
::
Migration
create_table :users do |t|
def
self
.
up
t.attachment :avatar
create_table
:users
do
|
t
|
end
t
.
attachment
:avatar
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
def change
class
AddAttachmentToUsers
<
ActiveRecord
::
Migration
create_table :users do |t|
def
change
t.attachment :avatar
create_table
:users
do
|
t
|
end
t
.
attachment
:avatar
end
end
end
end
end
```
### Schema Definition
### Schema Definition
class AddAttachmentToUsers < ActiveRecord::Migration
```
ruby
def self.up
class
AddAttachmentToUsers
<
ActiveRecord
::
Migration
add_attachment :users, :avatar
def
self
.
up
end
add_attachment
:users
,
:avatar
end
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
def change
class
AddAttachmentToUsers
<
ActiveRecord
::
Migration
add_attachment :users, :avatar
def
change
end
add_attachment
:users
,
:avatar
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
:processors => [:ocr]
has_attached_file
:scan
,
:styles
=>
{
:text
=>
{
:quality
=>
:better
}
},
: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
:processors => [:rotator, :ocr]
has_attached_file
:scan
,
:styles
=>
{
:text
=>
{
:quality
=>
:better
}
},
: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,15 +409,17 @@ _NOTE: Post processing will not even *start* if the attachment is not valid
...
@@ -373,15 +409,17 @@ _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
has_attached_file :asset, styles: {thumb: "100x100#"}
class
Message
<
ActiveRecord
::
Base
has_attached_file
:asset
,
styles:
{
thumb:
"100x100#"
}
before_post_process :skip_for_audio
before_post_process
:skip_for_audio
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
:url => "/system/:hash.:extension",
has_attached_file
:avatar
,
{
:hash_secret => "longSecretString"
:url
=>
"/system/:hash.:extension"
,
}
: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,15 +451,17 @@ A MD5 checksum of the original file assigned will be placed in the model if it
...
@@ -411,15 +451,17 @@ 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
def self.up
class
AddAvatarFingerprintColumnToUser
<
ActiveRecord
::
Migration
add_column :users, :avatar_fingerprint, :string
def
self
.
up
end
add_column
:users
,
:avatar_fingerprint
,
:string
end
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
has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") }
class
User
<
ActiveRecord
::
Base
end
has_attached_file
:avatar
,
:styles
=>
lambda
{
|
attachment
|
{
:thumb
=>
(
attachment
.
instance
.
boss?
?
"300x300>"
:
"100x100>"
)
}
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
has_attached_file :avatar, :processors => lambda { |instance| instance.processors }
class
User
<
ActiveRecord
::
Base
attr_accessor :watermark
has_attached_file
:avatar
,
:processors
=>
lambda
{
|
instance
|
instance
.
processors
}
end
attr_accessor
:watermark
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
desc "build missing paperclip styles"
namespace
:deploy
do
task :build_missing_paperclip_styles, :roles => :app do
desc
"build missing paperclip styles"
run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
task
:build_missing_paperclip_styles
,
:roles
=>
:app
do
end
run
"cd
#{
release_path
}
; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
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
has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'}
class
User
<
ActiveRecord
::
Base
end
has_attached_file
:avatar
,
:styles
=>
{
:thumb
=>
'x100'
,
:croppable
=>
'600x600>'
,
:big
=>
'1000x1000>'
}
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`
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment