Smarter paperclip validations - ruby-on-rails

I'm using paperclip in a rails app and have the following three validations in my model
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than=>1.megabyte
validates_attachment_content_type :photo, :content_type=>['image/jpeg', 'image/png', 'image/gif']
If the user forgets to add an attachment, all three validations fail and thus, the user is presented with the following three errors:
# Photo file name must be set.
# Photo file size file size must be between 0 and 1048576 bytes.
# Photo content type is not included in the list
I think it would be best to just show the first error in this instance since the other two errors are purely consequential... I would prefer the user to only ever see the second two errors if an attachment has been added but doesn't meet the validation criteria.
I'm certain there is no pre-baked validation that does this sort of thing and from reading the code in vendor/plugins/paperclip/lib/paperclip.rb I see that the validates_attachment_size method supports the :unless parameter as shown:
def validates_attachment_presence name, options = {}
message = options[:message] || "must be set."
validates_presence_of :"#{name}_file_name",
:message => message,
:if => options[:if],
:unless => options[:unless]
end
So, I was thinking that I could do something like the following:
validates_attachment_size :photo, :less_than=>1.megabyte, :unless=> :photo.blank
But that breaks the app. Anyone have any experience of doing this sort of thing? Would be a nice contribution to the paperclip source code.
EDIT:
I've tried using this:
validates_attachment_size :photo, :less_than=>1.megabyte,
:unless=> Proc.new { |image| image[:photo].nil? }
It doesn't quite work though as I've just managed to upload a 5mb mp3 with this validation in place. But it's promising as the error message doesn't appear when the user has not attached a photo.

validates_attachment_size :photo, :less_than => 1.megabyte,
:unless => Proc.new { |imports| imports.photo_file_name.blank? }

I think you can do it other way. Don't mess with validations. You probably have something like this in your form:
<%= f.error_messages %>
You can remove it and write your own helper to display error messages. Errors are stored in hash:
#photo.errors
Or if you want to get to them through form builder:
f.object.errors

Related

Rails 3.2 + Paperclip: Assign default image on user creation

I am working on adding a profile picture to the User model in my Rails app. I've already gotten screenshots successfully working with another model, but for some reason I'm having a lot of difficulties with profile pictures. To handle profile pictures, I've created a new ProfilePics model:
class ProfilePic < ActiveRecord::Base
attr_accessible :image, :user_id
has_attached_file :profile_pic, :default_url => "/system/user_profile_pics/profile.png",
:url => "/system/user_profile_pics/:id/:basename.:extension",
:path => ':rails_root/public:url'
:styles => { :large => "800x400", :thumb => "36x36" }
# **** Associations ****
# State that each profile picture can have an associated user
belongs_to :users
# **** Validations ****
# Only allow the user to upload .bmp, .gif, .jpg, .jpeg, and .png files
validates_attachment_content_type :image, :content_type => /^image\/(bmp|gif|jpg|jpeg|png)/
# Validate the presence of the user id
validates :user_id, :presence => true
# Order all profile pictures by ID, from first to last
default_scope :order => 'profile_pics.id ASC'
end
When a user signs up, he/she should be set the default profile picture. This picture is the image file specified in the :default_url argument for the has_attached_file method. However, I can't seem to figure out how to assign the user the default profile picture in the controller, after the User has been created. I don't want to add the profile picture to the sign up form, and if I just omit it from the controller, I receive the following error message:
undefined method `before_image_post_process'
I haven't made the profile picture a requirement on user creation. I believe I have all of the correct database tables set up, but for some reason I keep getting this error. Here's my attempt at assigning the user the default profile picture in the controller:
if #user.save
# Create a profile picture for the user
#user.profile_pic = ProfilePic.new(:image => nil, :user_id => #user.id)
...
end
When debugging, immediately after saving the user, typing "#user.profile_pic" in the console returns the same 'before_image_post_process' error.
Does anyone have any insight on this issue? Thank you very much in advance!
You are getting this error because you defined the attached file attribute as profile_pic but you are doing the Paperclip validation on the image attribute.
When you define a has_attached_file attribute, Paperclip automatically creates a <name>_post_process callback which it uses later in the validation (where is the name of the has_attached_file attribute).
You created profile_pic_post_process but then the validation is looking for image_post_process, hence the error.
Change your validation line in the ProfilePic model:
# Only allow the user to upload .bmp, .gif, .jpg, .jpeg, and .png files
validates_attachment_content_type :profile_pic, :content_type => /^image\/(bmp|gif|jpg|jpeg|png)/

Custom Paperclip processor not being called

I have a user model that is generated using Devise. I am extending this model using paperclip to enable file upload and also the processing of a file using a custom paperclip processor.
My paperclip field is declared in the user model as follows. PaperClipStorage is a hash that I create with the paperclip variables. Also, the being stored on AWS S3.
has_attached_file :rb_resume, PaperclipStorageHash.merge(:style => { :contents => 'resume_contents'}, :processors => [:resume_builder])
validates_attachment_content_type :rb_resume, :if => lambda { |x| x.rb_resume? }, :content_type => ['application/pdf', 'application/x-pdf', 'application/msword', 'application/x-doc']
The validates_attachment_content_type check is being done to make sure that it only processes pdf and MS word files.
My processor looks as follows
module Paperclip
class ResumeBuilder < Processor
def initialize(file,options = {}, attachment = nil)
#file = file
#attachment = attachment
puts "Attachment is not null " if !attachment.nil?
end
def make
rb = MyModule::MyClass.new(#file.path) ### Do something with the file
section_layout = rb.parse_html
#attachment.instance_write(:whiny, section_layout)
#file
end
end
end
In my user model I also have an after_save callback that is supposed to take the section_layout generated in the processors make method. Code is as follows
after_save :save_sections
def save_sections
section_layout = rb_resume.instance_read(:whiny)
# Do something with section_layout...
end
Now my problem is that the processor code is never being called, and I can't figure out why.
Because of that the section_layout variable is always nil.
Another point to note is that the same model also has two other has_attached_file attributes. None of the other two use a custom processor.
I've been struggling with this for last 3 hours. Any help would be greatly appreciate.
Thanks
Paul
Error in my has_attached_file declaration
has_attached_file :rb_resume, PaperclipStorageHash.merge(:style => { :contents => 'resume_contents'}, :processors => [:resume_builder])
should actually be
has_attached_file :rb_resume, PaperclipStorageHash.merge(:styles => { :contents => 'resume_contents'}, :processors => [:resume_builder])
Notice the plural styles as opposed to singular style

Dynamic Attachment Size for Paperclip (Rails)

Is there anyway to have the validates_attachment_size except a dynamic file size limit? Here's an example:
class Document < ActiveRecord::Base
belongs_to :folder
has_attached_file :document
validates_attachment_size :document, :less_than => get_current_file_size_limit
private
def get_current_file_size_limit
10.megabytes # This will dynamically change
end
end
I've tried this but I keep getting an error saying "unknown method". Lambdas and Procs don't work either. Has anyone ever tried this? Thanks
Paperclip doesn't allow to pass function as size limit parameter. So you probably need to write custom validation:
validate :validate_image_size
def validate_image_size
if document.file? && document.size > get_current_file_size_limit
errors.add_to_base(" ... Your error message")
end
end
Long shot...
validates_attachment_size :document, :less_than => :get_current_file_size_limit
Usually when passing a function you have to pass the symbol and not the actual function.
There is a built-in Paperclip validation now:
validates_attachment_size :mp3, :less_than => 10.megabytes
Change mp3 to whatever your paperclipped file's name is.
See this post for more helpful Paperclip tips: http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip

Conditional Validation with Paperclip difficult

I have an "item", which goes through a multi-page creation process. Images are uploaded at step five, and I keep track of the steps by using the attribute "complete". When validating whether an image is attached with paperclip, I get problems using the code below:
validates_attachment_presence :pic1, :if => Proc.new { |u| u.complete == "step5"}
It seems that I can't access the "complete" attribute, as the active-record object seems to be the paperclip image. Is there a way for me to check at which point in the process I am and validate conditionally?
Thanks,
Michael
How about
validates_attachment_presence :pic1, :if => 'complete == "setp5"?'
or
validates_attachment_presence :pic1, :if => :is_step5?
def is_step5?
self.complete == "step5"
end

Ruby on Rails ActiveRecord conditional validation (and more..)

I have a Product model which validates multiple attributes (including a Paperclip image attachment) like so:
validates_presence_of :name
validates_format_of :name, :with => /^([a-zA-Z0-9\ \-]{3,128})$/i
...
has_attached_file :image
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ["image/jpeg", "image/png", "image/gif"]
Everything is working fine. What I want now is to make an (unobtrusive) hidden iframe in-place upload script using javascript. My problem is that I cannot just upload the image without the rest of the data, because it will fail validation (no name present) and also I cannot send the rest of the form without the image (same thing, fails validation).
So basically what I need (and don't know how to achieve) is to conditionally apply the model validations according to what the action is currently in progress (uploading the image or editing other data).
I hope I was clear enough. Any help is appreciated. Thanks.
Railscasts have a nice video screencast about conditional validations.
I hate having to wade through a video just to get a simple answer. In fact, I think that a blog entry is superior to a simple tutorial video simply for the fact that it is searchable. Here is a simple case in plain text for anyone else looking:
To validate the presence of password only for the create action, do this:
validates_presence_of :password, :on => :create
A comment for Peter D.
Thank you very much. I'm unable to watch that screen cast presently (though I plan to) and was looking for a speedy, brief answer.
Incorporated your suggestion into a model I have and it's working perfectly. (Though I suspect at some point I'm going to need validation on update when passwords are being changed. I'll take it as "technical debt" now though, in order to move on.)
The bit I added:
validates :password, :presence => true, :confirmation => true, :on => :create

Resources