How can I use variables within model? - ruby-on-rails

I'm trying to insert the URL of the page into uploaded image.
I already have the code like this below but it doesn't work.
Is there something wrong in my model? How can I fix this?
My associations
User has_one :profile
Profile belongs_to :user
models/user.rb
before_save :text_to_insert?
def text_to_insert
nickname = self.profile.nickname
end
has_attached_file :user_avatar,
:styles => {
:thumb=> "100x100>",
:small => "400x400>" },
:convert_options => {
:small => '-fill white -undercolor "#00000080" -gravity South -annotate +0+5 " example.com/'+ nickname +' "' }

before saving , you are using text_to_insert? method which doesn't exist thatwhy it is returning false,so it fails to save .
It looks like typos ,try removing ? after :text_to_insert ie
before_save :text_to_insert
Please be sure that is valid self.profile.nickname

Related

validates_attachment for optional field

I have an uload field which is optional, it can be left empty. But when it is is used, I want to validate the size and content of the attachment. So I use this validation in the model:
validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: { in: 0..500.kilobytes }
This works when there is an attachment, but fails when it is left empty. How can I make sure it only validates when there is an attached file?
The solutions mentioned here are not working unfortunately.
The link you provided is giving you what I would suggest - using the if: argument
--
if:
Using if: in your validation basically allows you to determine conditions on which the validator will fire. I see from the link, the guys are using if: :avatar_changed?
The problem you've likely encountered is you can either use a Proc or instance method to determine the condition; and as these guys are using a method on avatar (albeit an inbuilt one), it's not likely going to yield the result you want.
I would do this:
validates_attachment :attachment, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: { in: 0..500.kilobytes }, if: Proc.new {|a| a.attachment.present? }
This basically determines if the attachment object is present, providing either true or false to the validation
try this:
has_attached_file :attachment, :styles => { :small => "200x200>" }
validates_attachment :attachment,
:size => { :in => 0..500.kiobytes },
:content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }
its working on my app. except i have set a default attachment in case user chooses not to upload one.

Ruby validation - multiple permutations for a single hash

Say there is a hash field that can have two possible value permutations, "foo" and "bar". How can I validate the hash value is one of the two?
class ValidateMe
validates :type => { :type => "foo" or :type => "bar" }
end
This results in an error. What is the proper way to handle this use case?
My actual case is using Paperclip to attach an image. I need to enforce the image is only .png or .jpg
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => "image/png" }
end
Help with either code block is greatly appreciated. Thanks!
The best way to do this would be to pass an array of types to :content_type
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => ['image/png', 'image/jpeg'] }
end
(My answer is based on code in Paperclip - Validate File Type but not Presence)
This can also be done using regular expressions. (Not as preferable)
class ValidateMe
validates_attachment :image,
presence => true,
:content_type => { :content_type => /^image\/(jpeg|png)$/ }
end
(source How can I restrict Paperclip to only accept images?)
I think Btuman's first answer would be considered canonical: The content_type key of content_type in validates_attachment can accept an array of valid content-types.
You can use :inclusion attribute for more see this http://guides.rubyonrails.org/active_record_validations_callbacks.html

validate a field in function of the presence of another

I have 2 fields:
attr_accessible :in_home #=> boolean, setted at false by default
mount_uploader :carousel_picture, CarouselUploader#=>an image picture with CarrierWave + Rmagick. Nil by default
attr_accessible :carousel_picture
My question is: I try to validate the fact that when the user checks the "in_home" checkbox, he should have uploaded a "carousel_picture"
validates :in_home, :if => Proc.new { |obj| (obj.in_home && obj.carousel_picture?) == true }, :presence => {:message => "You should upload a carousel picture to set the item in the home page."},
But this validation doesn't work, I can create an object with the in_home checked and the carousel_picture empty.
When I try after in my rails console:
1.9.3p362 :001 > obj = MyObject.all.last
=> #<Gift id: 22, name: "foo", created_at: "2013-04-02 09:13:00", updated_at: "2013-04-02 09:13:00", carousel_picture: nil, in_home: true>
1.9.3p362 :002 > obj.in_home && obj.carousel_picture?
=> false
So I think there is something that I don't understand in how I do a validation of a field. Any help would be useful!
Thank you in advance
Try this,
validates_presence_of :carousel_picture, :if => lambda {|obj| obj.in_home == true}
I was trying to do something similar recently, I think the below code should work for you:
validates :carousel_picture, :presence => { :if => Proc.new { |a| a.in_home? }, message: "You should upload a carousel picture to set the item in the home page." }

How can I add variable as text annotation into convert option?

I want to insert UserProfile.nickname as text annotation into convert option.
This is what I have now.
How can I fix this?
user_profile.rb
before_save :text_to_insert
def text_to_insert
nickname = self.nickname
end
has_attached_file :user_avatar,
:styles => {
:thumb=> "100x100>",
:small => "400x400>" },
:convert_options => {
:small => lambda {|a| %Q{ -gravity south -pointsize 25 -fill black -annotate +0+0 "#{nickname}" -fill white -annotate +2+2 "#{nickname} "} } }
Try #{:nickname} or maybe #{self.nickname} instead.
I'm with same problem.
Usually they recomend you to create a custom paperclip processor to do it, but years ago i done something like that in a way which remembers the one o mentioned in your question, but i dont remember exactly how.
If you accomplished, please post your solution here and Let me know if works ;)

Image orientation and validation with Paperclip?

I'm looking for a way to determine image orientation preferably with Paperclip, but is it even possible or do I need to user RMagick or another image library for this?
Case scenario: When a user uploads an image i want to check the orientation/size/dimensions to determine if the image is in portrait/landscape or square and save this attribute to the model.
Here's what I generally do in my image models. Perhaps it will help:
I use IM's -auto-orient option when converting. This ensures images are always rotated properly after upload
I read the EXIF data after processing and get the width and height (among other things)
You can then just have an instance method that outputs an orientation string based on width and height
has_attached_file :attachment,
:styles => {
:large => "900x600>",
:medium => "600x400>",
:square => "100x100#",
:small => "300x200>" },
:convert_options => { :all => '-auto-orient' },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:s3_permissions => 'public-read',
:s3_protocol => 'https',
:path => "images/:id_partition/:basename_:style.:extension"
after_attachment_post_process :post_process_photo
def post_process_photo
imgfile = EXIFR::JPEG.new(attachment.queued_for_write[:original].path)
return unless imgfile
self.width = imgfile.width
self.height = imgfile.height
self.model = imgfile.model
self.date_time = imgfile.date_time
self.exposure_time = imgfile.exposure_time.to_s
self.f_number = imgfile.f_number.to_f
self.focal_length = imgfile.focal_length.to_s
self.description = imgfile.image_description
end
Thanks for the answer jonnii.
Although I did find what I was looking for in the PaperClip::Geometry module.
This worked find:
class Image < ActiveRecord::Base
after_save :set_orientation
has_attached_file :data, :styles => { :large => "685x", :thumb => "100x100#" }
validates_attachment_content_type :data, :content_type => ['image/jpeg', 'image/pjpeg'], :message => "has to be in jpeg format"
private
def set_orientation
self.orientation = Paperclip::Geometry.from_file(self.data.to_file).horizontal? ? 'horizontal' : 'vertical'
end
end
This of course makes both vertical and square images have the vertical attribute but that's what I wanted anyway.
When I take a photo with my camera the dimensions of the image are the same regardless if the photo is landscape or portrait. However, my camera is smart enough to rotate the image for me! How thoughtful! The way this work is that is uses something called exif data which is meta data placed on the image by the camera. It includes stuff like: the type of camera, when the photo was taken, orientation etc...
With paperclip you can set up callbacks, specifically what you'll want to do is have a callback on before_post_process that checks the orientation of the image by reading the exif data using a library (you can find a list here: http://blog.simplificator.com/2008/01/14/ruby-and-exif-data/), and then rotating the image clockwise or counterclockwise 90 degrees (you won't know which way they rotated the camera when they took the photo).
I hope this helps!

Resources