Skip to content

Instantly share code, notes, and snippets.

@zeitan
Created March 21, 2012 19:55
Show Gist options
  • Save zeitan/2152091 to your computer and use it in GitHub Desktop.
Save zeitan/2152091 to your computer and use it in GitHub Desktop.
Product.rb
#Product es la representacion logica de un producto dentro de la BD de allihay, son estos a los que se les hace chekin, pertenecen a alguna categoria y pueden tener atributos via la tabla de Product_Characteristics
# t.string "name", :null => false
# t.integer "category_id"
# t.datetime "created_at"
# t.datetime "updated_at"
#
#Implementa las siguientes validaciones
# validates :name, :presence => true, :uniqueness => { :case_sensitive => false }, :allow_blank => false
# validate :category_should_exists
#
#Implementa busquedas con sunspot
# searchable do
# text :name
# end
#
#Pertenece a un Category. Tiene muchos Characteristics a traves de ProductCharacteristic. Tiene muchos Comment a traves de ProductComment. Tiene muchos Checkin
class Product < ActiveRecord::Base
include Commentable
belongs_to :category
has_many :product_characteristics, :dependent => :destroy
has_many :characteristics, :through => :product_characteristics
has_many :checkins, :dependent => :destroy
has_many :product_comments
has_many :comments, :through => :product_comments
has_many :universal_codes, :dependent => :destroy
validate :category_should_exists
after_create :associte_universal_code_and_characteristic
attr_accessor :universal_code, :presentation, :characteristics
accepts_nested_attributes_for :product_characteristics
def create_product(category, name, universal_code, presentation, characteristics)
uc = universal_codes_exists(universal_code)
if uc.nil?
producto=Product.new
producto.name=name
producto.category=category
#producto=create(:name => name, :category_id => category)
producto.universal_code = universal_code
producto.presentation = presentation
producto.characteristics = characteristics
producto.create
else
return uc
end
end
# valida que el universal code exista si existe toma ese product como nombre
def universal_codes_exists(code)
uc = UniversalCode.where(:code => code)
if !uc.empty?
return uc.first.product
end
nil
end
def associte_universal_code_and_characteristic
logger.info '------------------------------------------'
logger.info self.id
logger.info "-----------------#{self.characteristics}----------------"
logger.info "-----------------#{self.universal_code}----------------"
if !@presentation.nil? and !@presentation.empty?
ProductCharacteristic.create(:product_id => self.id, :characteristic_id => @characteristics, :value => @presentation)
end
UniversalCode.create(:code =>@universal_code, :product_id => self.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment