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
|
2015-03-19 16:22:55 +01:00
|
|
|
# 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
|
2015-03-19 16:22:55 +01: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 20:33:35 +02:00
|
|
|
# calories :integer
|
|
|
|
# deleted :boolean default("f")
|
2014-12-04 19:50:02 +01:00
|
|
|
#
|
|
|
|
|
2014-11-24 21:45:32 +01:00
|
|
|
class Product < ActiveRecord::Base
|
2015-02-09 17:06:24 +01:00
|
|
|
has_many :order_items
|
2015-03-28 14:36:07 +01:00
|
|
|
has_attached_file :avatar, styles: { dagschotel: "80x80>", medium: "100x100>", small: "40x40>" }, 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
|
2015-02-04 17:05:02 +01:00
|
|
|
validates :price_cents, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
2015-09-03 23:12:20 +02:00
|
|
|
validates :stock, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
2015-07-08 13:31:32 +02:00
|
|
|
validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 }
|
2015-03-12 13:25:11 +01:00
|
|
|
validates_attachment :avatar,
|
|
|
|
presence: true,
|
|
|
|
content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
|
2014-12-09 14:39:27 +01:00
|
|
|
|
2015-08-25 15:53:27 +02: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-08-25 15:53:27 +02:00
|
|
|
|
|
|
|
def out_of_sale
|
|
|
|
update_attribute :deleted, true
|
|
|
|
end
|
2014-11-24 21:45:32 +01:00
|
|
|
end
|