Sha256: f172e06ce3197f1b4654fcb26a58b1467eab60dee3bef5ea7af350bb99a407a5

Contents?: true

Size: 1.67 KB

Versions: 11

Compression:

Stored size: 1.67 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).nil?
      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

11 entries across 11 versions & 1 rubygems

Version Path
carter-0.8.0 lib/carter/cartable.rb
carter-0.7.2 lib/carter/cartable.rb
carter-0.7.1 lib/carter/cartable.rb
carter-0.7.0 lib/carter/cartable.rb
carter-0.6.4 lib/carter/cartable.rb
carter-0.6.3 lib/carter/cartable.rb
carter-0.6.2 lib/carter/cartable.rb
carter-0.6.1 lib/carter/cartable.rb
carter-0.6.0 lib/carter/cartable.rb
carter-0.5.6 lib/carter/cartable.rb
carter-0.5.5 lib/carter/cartable.rb