Sha256: c80bc97d3d3fae2b39b3be06144127a678372dadb638e33f946f441dc3ccb73c

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

module Carter
  module ActiveRecord #:nodoc:
    module Cartable #:nodoc:
      
      # Options
      #   acts_as_cartable :name => "name_column", :price => "price_column", :unique => true / false
      #   :unique => true # this allows only one item of this type in the cart.
      def acts_as_cartable(options = {})
        configuration = { :name => "name", :price => "price", :unique => false }
        configuration.update(options) if options.is_a?(Hash)
        @cartable_configuration = configuration
        has_many :cart_items, :as => :cartable
        has_many :carts, :through => :cart_items
        register_cartable(self)
        include InstanceMethods
        true
      end
      
      def cartable_configuration
        @cartable_configuration ||= {}
      end
      
      protected
      
      def register_cartable(klass)
        Carter.settings.cartables = (Carter.settings.cartables << klass.name).uniq
        # ::Cart.add_cart_association(klass)
      end
    end
    
    module InstanceMethods
    
      def cartable_price
        self.send(cartable_configuration_value_by_key(:price))
      end
      
      def cartable_name
        self.send(cartable_configuration_value_by_key(:name))
      end
      
      def in_cart?(cart, owner=nil)
        cart.cart_item_for_cartable_and_owner(self, owner).present?
      end
      
      def allow_multiples?
        !cartable_configuration_value_by_key(:unique)
      end
      
      def after_purchase_method
        cartable_configuration_value_by_key :after_purchase_method
      end
      
      private
      
      def cartable_configuration_value_by_key(key)
        self.class.cartable_configuration[key]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carter-0.8.1 lib/carter/cartable.rb