Skip to content

Instantly share code, notes, and snippets.

@danielnegri
Created April 7, 2011 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielnegri/907771 to your computer and use it in GitHub Desktop.
Save danielnegri/907771 to your computer and use it in GitHub Desktop.
Código fonte adaptado do Spree
class Product < ActiveRecord::Base
# Associations
has_many :order_items
# Attachment
AttachmentSizes = {
:thumb => [64, 64],
:product => [240, 240],
:large => [600, 600]
}
styles = AttachmentSizes.each_with_object({}) { |(name, size), all|
all[name] = ["%dx%d%s" % [size[0], size[1], size[0] < size[1] ? '>' : '#'], :png]
}
has_attached_file :attachment,
:default_url => "/images/default-product.png",
:url => "/images/products/:id/:style.png",
:path => ":rails_root/public/images/products/:id/:style.png",
:styles => styles
# Validations
validates_presence_of :name
validates_length_of :name, :minimum => 3
validates_numericality_of :price
validates_attachment_size :attachment, :less_than => 2.megabytes
validates_attachment_content_type :attachment, :content_type => %w[image/jpeg image/pjpeg image/png image/x-png image/gif]
# Scopes
scope :not_deleted, where(:deleted_at => nil)
# Others
def attachment_geometry(style = :original)
@geometry ||= {}
@geometry[style] ||= Paperclip::Geometry.from_file(attachment.path(style))
end
def self.search(search)
if search && search.strip() != ''
search = "%#{search}%".downcase()
where('lower(name) LIKE :search OR lower(description) LIKE :search', :search => search).order(:name)
else
order("updated_at desc")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment