Sha256: 6469947fbb9b79276c9578828e9485e2e3c30efb209a4df5027e348badce6053

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

module Bodega
  module Product
    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

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bodega-0.4.12 app/models/bodega/product.rb
bodega-0.4.9 app/models/bodega/product.rb
bodega-0.4.4 app/models/bodega/product.rb
bodega-0.4.3 app/models/bodega/product.rb
bodega-0.4.2 app/models/bodega/product.rb
bodega-0.4.1 app/models/bodega/product.rb
bodega-0.4.0 app/models/bodega/product.rb