Last active
December 15, 2015 15:59
-
-
Save cue232s/5285635 to your computer and use it in GitHub Desktop.
How do I validate the dimensions of attached files given this set up? I have Promotion Model which has a 'has_one' association with the Promotion_Attachment Model. The Promotion_Attachment Model manages a few attached files. I want to be able to validate the dimensions of each uploaded image file upon creation. I'm having trouble figuring out th…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PromotionAttachment < ActiveRecord::Base | |
belongs_to :promotion | |
attr_accessible \ | |
:like_gate_image, | |
:promotion_image, | |
:app_icon, | |
:video_url | |
validate :like_gate_image_dimensions | |
# If ENABLE_S3 returns TRUE (config/initializers/_tmg.rb) then files will be sent to s3 otherwise they will be stored locally | |
if ENABLE_S3 | |
[:like_gate_image, :promotion_image].each do |file_name| | |
class_eval do | |
has_attached_file file_name, | |
{ styles: | |
{ medium: "300x300>}", | |
thumb: "100x100>" | |
}, | |
storage: :s3, | |
s3_credentials: "#{::Rails.root}/config/aws.yml", | |
path: ":attachment/:id/:style.:extension", | |
} | |
end | |
end | |
else | |
[:like_gate_image, :promotion_image].each do |file_name| | |
class_eval do | |
has_attached_file file_name, | |
{ styles: | |
{ medium: "300x300>}", | |
thumb: "100x100>" | |
}, | |
:url => "/system/:attachment/:id/:style.:extension", | |
} | |
end | |
end | |
end | |
def like_gate_image_dimensions #i'd like to be able to check that a like_gate_image meets the right dimensions | |
dimensions = Paperclip::Geometry.from_file(file.queued_for_write[:original].path) | |
self.width = dimensions.width | |
self.height = dimensions.height | |
if dimensions.width != 810 && dimensions.height > 200 | |
errors.add(:like_gate_image,'width must be equal to 810px, and the height must be atleast 200px.') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment