move to evented mustache system

This commit is contained in:
2012-11-28 20:15:59 +01:00
parent 3da9dc68d7
commit 0fa0d1e0ba
31 changed files with 324 additions and 176 deletions
+46 -6
View File
@@ -60,6 +60,7 @@ class List
list.save
user.active_list_id = list.id
user.save
#list.broadcast_supplier list.supplier_id, 'list_added', list.with_info_as_json
list
end
@@ -95,7 +96,34 @@ class List
orders.map(&:close!)
self.state = 'closed'
self.closed_at = Time.now
save
if save
for user in users
user.active_list_id = nil
user.save
broadcast_user user.id, 'list_closed', id: id
end
broadcast_supplier supplier_id, 'list_closed', id: id
end
end
def is_helped!
self.needs_help = false
if save
for user_id in user_ids
broadcast_user user_id, 'list_helped', id: id
end
broadcast_supplier supplier_id, 'list_helped', id: id
end
end
def needs_payment!
self.needs_payment = true
if save
for user_id in user_ids
broadcast_user user_id, 'list_needs_payment', id: id
end
broadcast_supplier supplier_id, 'list_needs_payment', id: id
end
end
def set_price
@@ -121,15 +149,17 @@ class List
def place_order(user, products)
return false unless products.any?
return false unless user
@order = Order.create list: self, supplier: supplier, user: user, section_id: section_id
return unless @order.id
order = Order.create list: self, supplier: supplier, user: user, section_id: section_id
return unless order.id
loaded_products = self.class.database.load_document products.keys
products.each do |product_id, number|
number = number.to_i
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
ProductOrder.create order: order, product_id: product_id, amount: number, price: product.price if number > 0
end
@order
broadcast_supplier supplier.id, 'new_order', order.with_products_as_json
broadcast_supplier supplier.id, 'list_update', with_info_as_json
order
end
def move_to_table to_table
@@ -139,7 +169,7 @@ class List
save
end
def as_json
def as_json(*args)
super.merge(table_number: table_number)
end
@@ -152,6 +182,8 @@ class List
list_total = 0.0
for order in orders
ho = {products: []}
ho[:id] = order.id
ho[:state] = order.state
order_total = 0.0
for product_order in order.product_orders
order_total += (product_order.amount * product_order.price).round(2)
@@ -184,4 +216,12 @@ class List
end
@join_requests_as_json = h
end
def with_info_as_json
return @with_info_as_json if @with_info_as_json.present?
hl = as_json
hl[:total_amount] = orders.inject(0.0){|sum, o| sum + o.product_orders.inject(0.0){|s, po| s + (po.amount * po.price).round(2)}}.round(2)
hl[:section_title] = table.section.try(:title)
@with_info_as_json = hl
end
end