Sha256: da3ee52c74f27d0e7744a14f9fbed7a36b21ed7c79a5b202ec18e2178ca23b47

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module RightnowOms
  class CartItem < ActiveRecord::Base
    acts_as_api

    belongs_to :cartable, polymorphic: true
    belongs_to :cart
    belongs_to :parent, class_name: 'RightnowOms::CartItem', foreign_key: :parent_id
    has_many :children, class_name: 'RightnowOms::CartItem', foreign_key: :parent_id, dependent: :destroy

    validates :cartable, presence: true, cartable: true
    validates :cart, presence: true
    validates :name, presence: true
    validates :price, presence: true, numericality: { greater_than_or_equal_to: 0 }
    validates :quantity, presence: true, numericality: { greater_than: 0 }
    validates :base_quantity, presence: true, numericality: { greater_than: 0 }

    api_accessible :default do |t|
      t.add :id
      t.add :parent_id
      t.add :cartable_id
      t.add :cartable_type
      t.add :name
      t.add :original_price
      t.add :group
      t.add :base_quantity
      t.add :price
      t.add :quantity
    end

    default_scope order("id ASC")

    before_validation :set_base_quantity

    def total
      price * quantity
    end

    def original_price
      cartable.cartable_price
    end

    class << self
      def roots
        where(parent_id: nil)
      end

      def find_by_cartable(cartable)
        find_by_cartable_id_and_cartable_type(cartable.id, cartable.class)
      end
    end

    private
    def set_base_quantity
      self.base_quantity = quantity unless base_quantity
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rightnow_oms-0.1.4 app/models/rightnow_oms/cart_item.rb
rightnow_oms-0.1.3 app/models/rightnow_oms/cart_item.rb