Sha256: 4f622fdfe750d09ffab48a1ba22eb8cf659ded1bcbf88866ab4bc18e92c7aaf8

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module ActiveRecord
  module Acts
    module ShoppingCart
      module Item

        #
        # Returns the cart item for the specified object
        #
        def item_for(object)
          shopping_cart_items.where(item: object).first
        end

        #
        # Returns the subtotal of a specified item by multiplying the quantity times
        # the price of the item.
        #
        def subtotal_for(object)
          item = item_for(object)
          item ? item.subtotal : 0
        end

        #
        # Returns the quantity of the specified object
        #
        def quantity_for(object)
          item = item_for(object)
          item ? item.quantity : 0
        end

        #
        # Updates the quantity of the specified object
        #
        def update_quantity_for(object, new_quantity)
          item = item_for(object)
          item.update_quantity(new_quantity) if item
        end

        #
        # Returns the price of the specified object
        #
        def price_for(object)
          item = item_for(object)
          item ? item.price : 0
        end

        #
        # Updates the price of the specified object
        #
        def update_price_for(object, new_price)
          item = item_for(object)
          item.update_price(new_price) if item
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_shopping_cart-0.4.1 lib/active_record/acts/shopping_cart/item.rb
acts_as_shopping_cart-0.4.0 lib/active_record/acts/shopping_cart/item.rb