3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'app/models/bodega/product.rb', line 3
def self.included(base)
base.class_eval do
has_many :order_products, as: :product, class_name: 'Bodega::OrderProduct'
has_many :orders, through: :order_products
monetize :price_cents
scope :for_sale, lambda {
today = Date.today
where(for_sale: true).
where(arel_table[:for_sale_at].lteq(today).or(arel_table[:for_sale_at].eq(nil))).
where(arel_table[:not_for_sale_at].gteq(today).or(arel_table[:not_for_sale_at].eq(nil)))
}
scope :popular, joins(%(LEFT JOIN "bodega_order_products" ON "bodega_order_products"."product_id" = "#{table_name}"."id" AND "bodega_order_products"."product_type" = '#{name}')).order('SUM(bodega_order_products.quantity) DESC').group("#{table_name}.id")
validates_numericality_of :number_in_stock, :if => :keep_stock?
end
end
|