Sha256: 4bd45515bbf8ff75fec2284b5f4c643fe9e402277bdbc71042a8cebbea33be7e

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module MechanizeStore
  class Order < ActiveRecord::Base
    belongs_to :order_status
    belongs_to :store

    has_one :payment
    accepts_nested_attributes_for :payment, :allow_destroy => false

    has_one :freight, dependent: :destroy
    accepts_nested_attributes_for :freight, :allow_destroy => true

    has_many :order_items
    accepts_nested_attributes_for :order_items, :allow_destroy => false

    before_create :set_defaults
    after_create :after_create

    def set_defaults
        self.order_status_id = OrderStatus::ACCOMPLISHED
    end

    def after_create
        p = self.payment
        p.value = self.total_value
        p.value += self.freight.value if self.freight
        p.save!
    end

    def confirm!
        Order.transaction do
            unless self.generated?           
                paid_in = self.payment.paid_in
                self.generated_at = paid_in ||= DateTime.now
                self.save
            end
        end
    end

    def generated?
        return true if self.generated_at
    end

    def total_value
        self.order_items.collect{|o| o.total }.inject(0){|a, b| a+b }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mechanize_store-0.0.5 app/models/mechanize_store/order.rb
mechanize_store-0.0.4 app/models/mechanize_store/order.rb
mechanize_store-0.0.3 app/models/mechanize_store/order.rb