tap/app/models/product.rb

47 lines
1.4 KiB
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string not null
# price_cents :integer default("0"), not null
2015-02-09 17:06:24 +01:00
# created_at :datetime
# updated_at :datetime
# 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
# calories :integer
# deleted :boolean default("f")
#
2014-11-24 21:45:32 +01:00
class Product < ActiveRecord::Base
2015-09-18 13:20:57 +02:00
include Avatarable
2015-02-09 17:06:24 +01:00
has_many :order_items
enum category: %w(food beverages other)
2015-09-18 13:20:57 +02:00
validates :name, presence: true
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 }
2014-12-09 14:39:27 +01:00
scope :for_sale, -> { where deleted: false }
2014-12-09 14:39:27 +01:00
def price
2015-02-09 17:06:24 +01:00
self.price_cents / 100.0
2014-12-09 14:39:27 +01:00
end
def price=(value)
if value.is_a? String then value.sub!(',', '.') end
2015-02-04 17:05:02 +01:00
self.price_cents = (value.to_f * 100).to_int
2014-12-09 14:39:27 +01:00
end
2015-09-14 20:26:16 +02:00
def take_out_of_sale!
update_attribute :deleted, true
end
2014-11-24 21:45:32 +01:00
end