Sha256: bb37e8adf48d680399e87106a27942a60ff0487756bb2da4f0b617a18ca062ab

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module ActiveRecord
  module Acts
    module ShoppingCart
      module Cart
        module InstanceMethods
          #
          # Adds a product to the cart
          #
          def add(object, price, quantity = 1)
            cart_item = item_for(object)

            unless cart_item
              cart_items.create(:item => object, :price => price, :quantity => quantity)
            else
              cart_item.quantity = (cart_item.quantity + quantity)
              cart_item.save
            end
          end

          #
          # Remove an item from the cart
          #
          def remove(object, quantity = 1)
            if cart_item = item_for(object)
              if cart_item.quantity <= quantity
                cart_item.delete
              else
                cart_item.quantity = (cart_item.quantity - quantity)
                cart_item.save
              end
            end
          end

          #
          # Returns the subtotal by summing the price times quantity for all the items in the cart
          #
          def subtotal
            ("%.2f" % cart_items.inject(0) { |sum, item| sum += (item.price * item.quantity) }).to_f
          end

          def shipping_cost
            0
          end

          def taxes
            subtotal * self.tax_pct * 0.01
          end

          def tax_pct
            8.25
          end

          #
          # Returns the total by summing the subtotal, taxes and shipping_cost
          #
          def total
            ("%.2f" % (self.subtotal + self.taxes + self.shipping_cost)).to_f
          end

          #
          # Return the number of unique items in the cart
          #
          def total_unique_items
            cart_items.inject(0) { |sum, item| sum += item.quantity }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_shopping_cart-0.1.0 lib/active_record/acts/shopping_cart/cart/instance_methods.rb