2014-12-04 19:50:02 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: products
|
|
|
|
#
|
2014-12-04 20:32:31 +01:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(255)
|
2014-12-08 17:56:48 +01:00
|
|
|
# price :integer
|
2014-12-04 20:32:31 +01:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# avatar_file_name :string(255)
|
|
|
|
# avatar_content_type :string(255)
|
|
|
|
# avatar_file_size :integer
|
|
|
|
# avatar_updated_at :datetime
|
2014-12-10 21:25:21 +01:00
|
|
|
# category :integer default(0)
|
2014-12-04 19:50:02 +01:00
|
|
|
#
|
|
|
|
|
2014-11-24 21:45:32 +01:00
|
|
|
class Product < ActiveRecord::Base
|
2014-12-05 07:55:28 +01:00
|
|
|
has_many :order_products
|
2014-12-04 20:32:31 +01:00
|
|
|
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
|
2014-12-04 19:50:02 +01:00
|
|
|
|
2014-12-10 21:25:21 +01:00
|
|
|
enum category: %w(food beverages other)
|
|
|
|
|
2014-12-04 19:50:02 +01:00
|
|
|
validates :name, presence: true
|
2014-12-08 17:56:48 +01:00
|
|
|
validates :price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
2014-12-10 09:20:12 +01:00
|
|
|
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] },
|
|
|
|
default_url: "http://babeholder.pixoil.com/img/190/190"
|
2014-12-08 11:41:38 +01:00
|
|
|
|
|
|
|
def count(order)
|
|
|
|
order_products.find_by(order: order).count
|
|
|
|
end
|
2014-12-09 14:39:27 +01:00
|
|
|
|
|
|
|
def price
|
|
|
|
(read_attribute(:price) || 0) / 100.0
|
|
|
|
end
|
|
|
|
|
|
|
|
def price=(value)
|
|
|
|
if value.is_a? String then value.sub!(',', '.') end
|
|
|
|
write_attribute(:price, (value.to_f * 100).to_int)
|
|
|
|
end
|
|
|
|
|
2014-11-24 21:45:32 +01:00
|
|
|
end
|