Module: Bodega::Product

Defined in:
app/models/bodega/product.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

Instance Method Details

#in_stock?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'app/models/bodega/product.rb', line 23

def in_stock?
  if keep_stock? && number_in_stock
    number_in_stock > 0
  else
    true
  end
end

#max_quantityObject



31
32
33
# File 'app/models/bodega/product.rb', line 31

def max_quantity
  keep_stock? ? number_in_stock : Bodega.config.max_quantity
end