module Omniorder::Orderable
Represents common order behavior Include Omniorder::Orderable
in your Order
class to make it Omniorder
compatible.
Public Instance Methods
add_product(product, quantity = 1, external_reference = nil)
click to toggle source
# File lib/omniorder/orderable.rb, line 10 def add_product(product, quantity = 1, external_reference = nil) order_product = order_products.to_a.find { |op| op.product == product } if order_product.nil? order_products << Omniorder.order_product_type.new( :product => product, :quantity => quantity, :external_reference => external_reference ) else order_product.quantity += quantity end end
add_product_by_code(code, quantity = 1, external_reference = nil)
click to toggle source
# File lib/omniorder/orderable.rb, line 24 def add_product_by_code(code, quantity = 1, external_reference = nil) Omniorder.product_type.find_by_code(code).tap do |product| add_product product, quantity, external_reference unless product.nil? end end
generate_customer(attributes = {})
click to toggle source
Use generate not build so as not to conflict with ActiveRecord
# File lib/omniorder/orderable.rb, line 6 def generate_customer(attributes = {}) Omniorder.customer_type.new(attributes) end
product_count()
click to toggle source
# File lib/omniorder/orderable.rb, line 30 def product_count order_products.inject(0) { |sum, op| sum + op.quantity } end
product_list()
click to toggle source
Human-readable string representing order products
# File lib/omniorder/orderable.rb, line 39 def product_list order_products.sort.map(&:to_s).join('/') end
unique_product_count()
click to toggle source
# File lib/omniorder/orderable.rb, line 34 def unique_product_count order_products.count end