Sha256: 297d71a8c6700ce60a880d1dfe9f0437265b68330f87a12fdfb942d16633e074

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Bodega
  class OrderProduct < ActiveRecord::Base
    extend Bodega::Monetize

    belongs_to :order, class_name: 'Bodega::Order'
    belongs_to :product, polymorphic: true

    delegate :price, to: :product

    monetize :subtotal
    monetize :tax
    monetize :total

    validates_numericality_of :quantity, allow_blank: true, minimum: 1
    validates_presence_of :quantity

    def decorated_product
      product.respond_to?(:decorator) ? product.decorator.decorate(product) : product
    end

    def name
      decorated_product.respond_to?(:name) ? decorated_product.name : product.to_s
    end

    def quantity_and_name
      "#{quantity} x #{name.pluralize(quantity)}"
    end

    def subtotal
      read_attribute(:subtotal) || price * quantity
    end

    def total
      read_attribute(:total) || subtotal + calculate_tax
    end

    protected
    def calculate_tax
      self.tax = 0
    end

    def calculate_total
      self.subtotal = price * quantity
      self.total = subtotal + calculate_tax
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bodega-0.2.0 app/models/bodega/order_product.rb