Skip to content

Instantly share code, notes, and snippets.

@vocino
Created March 21, 2010 15:02
Show Gist options
  • Save vocino/339347 to your computer and use it in GitHub Desktop.
Save vocino/339347 to your computer and use it in GitHub Desktop.
class Photo < ActiveRecord::Base
belongs_to :portfolio
has_attached_file :image, :styles => {:full => ["440x9999>", :jpg],
:medium => ["200x255#", :jpg],
:large => ["440x555#", :jpg]},
:processors => [:cropper],
:storage => :cloud_files,
:cloudfiles_credentials => "#{RAILS_ROOT}/config/rackspace_cloudfiles.yml"
validates_attachment_presence :image
validate :must_not_exeed_free_allotment
is_taggable :categories
# Image cropping coords
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_image, :if => :cropping?
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
# helper method used by the cropper view to get the real image geometry
def avatar_geometry(style = :original)
@geometry ||= {}
@geometry[style] ||= Paperclip::Geometry.from_file avatar.path(style)
end
# Set passed-in order for passed-in ids.
def self.order(ids)
ids.each_with_index {|id, pos| find(id).update_attribute(:position, pos) }
# When we have switched to MySQL we can use this for max performance
# update_all(
# ['position = FIND_IN_SET(id, ?)', ids.join(',')],
# { :id => ids }
# )
end
private
def reprocess_image
image.reprocess!
end
def must_not_exeed_free_allotment
if portfolio.photos.count >= Subscription::FREE_ALLOTMENT &&
portfolio.user.active_subscription.nil?
errors.add_to_base("You already have #{portfolio.photos.count} photos. To add more, please upgrade your account.")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment