end of day commit for users and suppliers authenticated handling

This commit is contained in:
2012-08-25 15:55:56 +02:00
parent b7c57d41ce
commit e56badcbf8
29 changed files with 435 additions and 582 deletions
+9 -3
View File
@@ -7,9 +7,13 @@ class List
property :closed_at, type: Time
has_many :orders, dependent: :destroy
belongs_to :table
belongs_to :supplier
has_and_belongs_to_many :users, storing_keys: true
validates :table_id, presence: true
validates :supplier_id, presence: true
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
def close!
orders.map(&:close!)
@@ -30,9 +34,10 @@ class List
state == 'active'
end
def place_order(products)
return unless products.any?
@order = Order.create list: self, supplier: supplier
def place_order(user, products)
return false unless products.any?
return false unless user
@order = Order.create list: self, supplier: supplier, user: user
return unless @order.id
loaded_products = self.class.database.load_document products.keys
products.each do |product_id, number|
@@ -40,6 +45,7 @@ class List
product = loaded_products.find{|p| p.id == product_id} # to get the price
ProductOrder.create order: @order, product_id: product_id, amount: number, price: product.price if number > 0
end
@order
end
def as_json
+2
View File
@@ -10,12 +10,14 @@ class Order
has_many :product_orders, dependent: :destroy
#has_many :products, through: :product_orders
validates :supplier_id, presence: true
validates :user_id, presence: true
view :active_for_supplier_view, type: :custom, map_function: %[function(doc){
if(doc.ruby_class == 'Order' && (doc.state == 'placed' || doc.state == 'active')){
emit(doc.supplier_id, 1);
}
}], reduce_function: '_sum'
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
def self.active_for_supplier(supplier_id)
database.view(active_for_supplier_view(key: supplier_id, reduce: false, include_docs: true))