2014-12-04 18:50:02 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: products
|
|
|
|
#
|
2014-12-04 19:32:31 +00:00
|
|
|
# id :integer not null, primary key
|
2015-03-19 15:22:55 +00:00
|
|
|
# name :string not null
|
|
|
|
# price_cents :integer default("0"), not null
|
2015-02-09 16:06:24 +00:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2015-03-19 15:22:55 +00:00
|
|
|
# avatar_file_name :string
|
|
|
|
# avatar_content_type :string
|
|
|
|
# avatar_file_size :integer
|
|
|
|
# avatar_updated_at :datetime
|
|
|
|
# category :integer default("0")
|
|
|
|
# stock :integer default("0"), not null
|
2015-09-02 18:33:35 +00:00
|
|
|
# calories :integer
|
|
|
|
# deleted :boolean default("f")
|
2015-09-21 06:54:50 +00:00
|
|
|
# barcode :string default(""), not null
|
2014-12-04 18:50:02 +00:00
|
|
|
#
|
|
|
|
|
2014-11-24 20:45:32 +00:00
|
|
|
class Product < ActiveRecord::Base
|
2015-09-18 11:20:57 +00:00
|
|
|
include Avatarable
|
|
|
|
|
2015-02-09 16:06:24 +00:00
|
|
|
has_many :order_items
|
2014-12-04 18:50:02 +00:00
|
|
|
|
2014-12-10 20:25:21 +00:00
|
|
|
enum category: %w(food beverages other)
|
|
|
|
|
2015-09-21 06:40:15 +00:00
|
|
|
validates :name, presence: true, uniqueness: true
|
2015-09-18 11:20:57 +00:00
|
|
|
validates :price_cents, presence: true, numericality: { only_integer: true, greater_than: 0 }
|
|
|
|
validates :stock, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
|
|
|
validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 }
|
2015-09-20 19:21:18 +00:00
|
|
|
validates :barcode, presence: true, uniqueness: true
|
2014-12-09 13:39:27 +00:00
|
|
|
|
2015-08-25 13:53:27 +00:00
|
|
|
scope :for_sale, -> { where deleted: false }
|
|
|
|
|
2014-12-09 13:39:27 +00:00
|
|
|
def price
|
2015-02-09 16:06:24 +00:00
|
|
|
self.price_cents / 100.0
|
2014-12-09 13:39:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def price=(value)
|
|
|
|
if value.is_a? String then value.sub!(',', '.') end
|
2015-02-04 16:05:02 +00:00
|
|
|
self.price_cents = (value.to_f * 100).to_int
|
2014-12-09 13:39:27 +00:00
|
|
|
end
|
2014-11-24 20:45:32 +00:00
|
|
|
end
|