From 926be8ec48a21eb53cc5b0f8b2f27aa22fb9c892 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Sat, 1 Dec 2012 16:12:04 +0100 Subject: [PATCH] better broadcasting for user --- .../javascripts/supplier/qsupplier.js.coffee | 9 ++++++ .../javascripts/user/application.js.erb | 2 +- app/assets/javascripts/user/quser.js.coffee | 10 +++++++ app/controllers/user_controller.rb | 6 ++-- app/models/list.rb | 30 ++++++++++++++----- app/templates/supplier/_active_list.mustache | 6 ++-- app/templates/supplier/_active_order.mustache | 4 +-- 7 files changed, 50 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index 8caefcc7..3f8c6350 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -30,6 +30,15 @@ root.Qsupplier= $('#order-in-process-button-'+e.data.id).hide() else if e.event == 'order_being_delivered' $('#order-row-'+e.data.id).remove() + else if e.event == 'list_changed_table' + list_row = $('#list-row-'+e.data.list_id) + list_row.find('.table_number').text(e.data.table.number).addClass('changed') + list_row.find('.section_title').text(e.data.section_title) + order_rows = $('.of-list-'+e.data.list_id) + order_rows.find('.table_number').text(e.data.table.number).addClass('changed') + order_rows.find('.section_title').text(e.data.section_title) + + console.log(e) false move_table_to_active_section: (table_id)-> diff --git a/app/assets/javascripts/user/application.js.erb b/app/assets/javascripts/user/application.js.erb index 3126c869..2f14b7eb 100644 --- a/app/assets/javascripts/user/application.js.erb +++ b/app/assets/javascripts/user/application.js.erb @@ -52,7 +52,7 @@ var $translations = { content: 'Request a waiter to your table' }, list_needs_payment: { - payment_already_requested: 'You already asked for the check', + payment_already_requested: 'You already asked for the bill', title: 'Ask for the check', content: 'Do you want to pay?' }, diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index bfbd169c..3e5379f1 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -22,6 +22,16 @@ class Quser $('#order-row-'+e.data.id).addClass('active') if(e.event == 'order_being_delivered') $('#order-row-'+e.data.id).addClass('delivered') + if(e.event == 'list_changed_table') + $('.table-number').text(e.data.table.number) + if(e.event == 'list_needs_help') + window.active_list.needs_help = true + @list_needs_help_default_action() + if(e.event == 'list_needs_payment') + window.active_list.needs_payment = true + @list_needs_payment_default_action() + + console.log(e) false home_loader: -> $.getJSON(data_host + '/user/list_info.json?' + authentication_string, (res) => @handle_active_list_default_actions(res)) diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 08cbd197..807047a4 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -213,9 +213,7 @@ class UserController < ApplicationController respond_to do |format| format.json do render json: {list_active: false} and return unless list.present? - list.needs_help = true - list.save - broadcast_supplier(list.supplier_id, 'list_needs_help', id: list.id) + list.needs_help! render json: list.as_json.merge(list_active: list.active?) end end @@ -299,7 +297,7 @@ class UserController < ApplicationController if @table.occupied? render json: {occupied: true} else - list.move_to_table @table + list.move_to_table! @table render json: {occupied: false} end end diff --git a/app/models/list.rb b/app/models/list.rb index 484a409c..6afe5e85 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -126,6 +126,29 @@ class List end end + def move_to_table! to_table + UserTableMove.create list: self, from_table_id: table_id, to_table: to_table + self.table = to_table + self.section_id = to_table.section_id + if save + for user_id in user_ids + broadcast_user user_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title) + end + broadcast_supplier supplier_id, 'list_changed_table', list_id: id, table: to_table, section_title: to_table.section.try(:title) + end + end + + def needs_help! + self.needs_help = true + if save + for user_id in user_ids + broadcast_user user_id, 'list_needs_help', id: id + end + broadcast_supplier(supplier_id, 'list_needs_help', id: id) + end + end + + def set_price list_total = 0.0 for order in orders @@ -162,13 +185,6 @@ class List order end - def move_to_table to_table - UserTableMove.create list: self, from_table_id: table_id, to_table: to_table - self.table = to_table - self.section_id = to_table.section_id - save - end - def as_json(*args) super.merge(table_number: table_number) end diff --git a/app/templates/supplier/_active_list.mustache b/app/templates/supplier/_active_list.mustache index 151e226f..e778839e 100644 --- a/app/templates/supplier/_active_list.mustache +++ b/app/templates/supplier/_active_list.mustache @@ -3,9 +3,9 @@ ? - {{table_number}} - {{section_title}} - {{#currency}}{{total_amount}}{{/currency}} + {{table_number}} + {{section_title}} + {{#currency}}{{total_amount}}{{/currency}} diff --git a/app/templates/supplier/_active_order.mustache b/app/templates/supplier/_active_order.mustache index bae0a0e1..edab1ace 100644 --- a/app/templates/supplier/_active_order.mustache +++ b/app/templates/supplier/_active_order.mustache @@ -1,7 +1,7 @@ {{display}} - {{table_number}} - {{section_title}} + {{table_number}} + {{section_title}} {{#currency}}{{total_amount}}{{/currency}}