Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created October 19, 2008 18:49
Show Gist options
  • Save mwmitchell/17906 to your computer and use it in GitHub Desktop.
Save mwmitchell/17906 to your computer and use it in GitHub Desktop.
class Product
include DataMapper::Resource
include Paperclip::Resource
property :id, Serial
property :name, String, :nullable => false, :index => :unique
property :description, String
property :price, Float, :default=>0.0
property :quantity, Integer, :default=>0
property :position, Integer, :default=>0
property :is_salable, Boolean, :default=>true
property :is_public, Boolean, :default=>true
property :has_free_shipping, Boolean, :default=>false
property :has_unlimited_quantity, Boolean, :default=>false
has n, :product_tags
validates_is_unique :name
validates_is_number :price
validates_is_number :quantity
validates_is_number :position
has_attached_file :image_1, :styles => { :medium => "300x300>", :thumb => "100x100>" }
after :save do |p|
self.product_tags.save
end
def attrs_for_tags=(data)
return if data.empty?
data.each do |tag_id|
next if product_tags.any?{|pt|pt.tag_id==tag_id.to_i}
self.product_tags << ProductTag.new(:tag=>Tag.get(tag_id))
end
end
def tag_ids_for_deleting=(ids)
return if ids.empty?
ids.each do |k,id|
self.product_tags.each do |pt|
self.product_tags.delete(pt) if id.to_i == pt.tag_id
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment