Module: ActiveCart::Acts::OrderTotal::ClassMethods

Defined in:
lib/active_cart/acts_as_cart.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) acts_as_order_total(options = {})

acts_as_order_total - Turns an ActiveModel into an order_total store.

In the same way there is a seperation between items and cart_items, there is a difference between concrete order_total objects and this order_total store. This model acts as a way of archiving the order total results for a given cart, so an invoice can be retrieved later. It doesn't matter if the concrete order_total object is an ActiveModel class or not, as long as it matches the api

Options:

cart: The cart model. Association as a belongs_to. Default: cart
name_column: The column that stores the name of the item. Default: name
price_column: The column that stores the price of the item. Default: price
foreign_key: The column that stores the reference to the cart. Default: [cart]_id (Where cart is the value of the cart option)

Example

class OrderTotal < ActiveModel::Base
  acts_as_order_total
end


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/active_cart/acts_as_cart.rb', line 243

def acts_as_order_total(options = {})
  cattr_accessor :aaot_config
  
  self.aaot_config = {
    :cart => :cart,
    :name_column => :name,
    :price_column => :price
  }
  self.aaot_config.merge!(options)
  self.aaot_config[:foreign_key] = (self.aaot_config[:cart].to_s + "_id").to_sym unless options[:foreign_key]

  class_eval do
    include ActiveCart::Item
  
    def id
      read_attribute(:id)
    end

    def name
      read_attribute(self.aaot_config[:name_column])
    end

    def price
      read_attribute(self.aaot_config[:price_column])
    end
  end

  belongs_to self.aaot_config[:cart], :foreign_key => self.aaot_config[:foreign_key]
end