Commit e377e85f by Jon Yurek

Recommend course of action re: validations

parent 7d807c6a
...@@ -174,6 +174,9 @@ validation. ...@@ -174,6 +174,9 @@ validation.
More information about the options to `has_attached_file` is available in the More information about the options to `has_attached_file` is available in the
documentation of [`Paperclip::ClassMethods`](http://rubydoc.info/gems/paperclip/Paperclip/ClassMethods). documentation of [`Paperclip::ClassMethods`](http://rubydoc.info/gems/paperclip/Paperclip/ClassMethods).
Validations
-----------
For validations, Paperclip introduces several validators to validate your attachment: For validations, Paperclip introduces several validators to validate your attachment:
* `AttachmentContentTypeValidator` * `AttachmentContentTypeValidator`
...@@ -207,6 +210,49 @@ validates_attachment :avatar, :presence => true, ...@@ -207,6 +210,49 @@ validates_attachment :avatar, :presence => true,
:size => { :in => 0..10.kilobytes } :size => { :in => 0..10.kilobytes }
``` ```
_NOTE: Post processing will not even *start* if the attachment is not valid
according to the validations. Your callbacks and processors will *only* be
called with valid attachments._
```ruby
class Message < ActiveRecord::Base
has_attached_file :asset, styles: {thumb: "100x100#"}
before_post_process :skip_for_audio
def skip_for_audio
! %w(audio/ogg application/ogg).include?(asset_content_type)
end
end
```
If you have other validations that depend on assignment order, the recommended
course of action is to prevent the assignment of the attachment until
afterwards, then assign manually:
```ruby
class Book < ActiveRecord::Base
has_attached_file :document, styles: {thumbnail: "60x60#"}
validates_attachment :document, content_type: "application/pdf"
validates_something_else # Other validations that conflict with Paperclip's
end
class BooksController < ApplicationController
def create
@book = Book.new(book_params)
@book.document = params[:book][:document]
@book.save
respond_with @book
end
private
def book_params
params.require(:book).permit(:title, :author)
end
end
```
Defaults Defaults
-------- --------
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. 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.
......
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