From 0fa0d1e0bafe75cd5eae4b9264b0997a85dc475d Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Wed, 28 Nov 2012 20:15:59 +0100 Subject: [PATCH] move to evented mustache system --- .../javascripts/supplier/application.js | 1 + .../javascripts/supplier/list.js.coffee | 12 ++ .../javascripts/supplier/order.js.coffee | 17 ++ .../javascripts/supplier/qsupplier.js.coffee | 145 ++++++++---------- .../javascripts/user/application.js.erb | 1 + app/assets/javascripts/user/quser.js.coffee | 43 +++--- .../supplier/active_lists.css.sass | 19 +++ .../stylesheets/supplier/application.css | 1 + .../stylesheets/user/active_list.css.sass | 2 +- app/controllers/application_controller.rb | 17 +- app/controllers/dashboard_controller.rb | 2 +- app/controllers/supplier_controller.rb | 10 +- app/controllers/user_controller.rb | 45 +++--- app/models/list.rb | 52 ++++++- app/models/order.rb | 35 ++++- app/templates/supplier/_active_list.mustache | 14 ++ app/templates/supplier/_active_order.mustache | 10 ++ app/templates/supplier/_list_order.mustache | 4 + .../user/_active_list_order.mustache | 4 + .../user/_active_list_orders.mustache | 6 - app/templates/user/_active_order.mustache | 2 +- app/views/layouts/phone.html.slim | 2 +- app/views/layouts/tablet.html.slim | 6 + app/views/supplier/active_lists.html.slim | 8 +- app/views/supplier/active_orders.html.slim | 8 +- app/views/supplier/home.html.slim | 3 + app/views/suppliers/lists/show.html.slim | 7 +- app/views/user/active_list.html.slim | 5 +- app/views/user/history_list.html.slim | 6 +- app/views/user/list_products.html.slim | 2 +- config/initializers/model_broadcast.rb | 11 ++ 31 files changed, 324 insertions(+), 176 deletions(-) create mode 100644 app/assets/javascripts/supplier/list.js.coffee create mode 100644 app/assets/javascripts/supplier/order.js.coffee create mode 100644 app/assets/stylesheets/supplier/active_lists.css.sass create mode 100644 app/templates/supplier/_active_list.mustache create mode 100644 app/templates/supplier/_active_order.mustache create mode 100644 app/templates/supplier/_list_order.mustache create mode 100644 app/templates/user/_active_list_order.mustache delete mode 100644 app/templates/user/_active_list_orders.mustache create mode 100644 config/initializers/model_broadcast.rb diff --git a/app/assets/javascripts/supplier/application.js b/app/assets/javascripts/supplier/application.js index de94066b..cb14dfc0 100644 --- a/app/assets/javascripts/supplier/application.js +++ b/app/assets/javascripts/supplier/application.js @@ -14,6 +14,7 @@ //= require jquery_ujs //= require jquery-ui //= require twitter/bootstrap +//= require mustache //= require_directory . //= require_self var path_mapping = { diff --git a/app/assets/javascripts/supplier/list.js.coffee b/app/assets/javascripts/supplier/list.js.coffee new file mode 100644 index 00000000..093cb8eb --- /dev/null +++ b/app/assets/javascripts/supplier/list.js.coffee @@ -0,0 +1,12 @@ +class List + constructor: (@attributes)-> + id: -> @attributes.id || @attributes._id + table_number: -> @attributes.table_number + total_amount: -> @attributes.total_amount + section_title: -> @attributes.section_title + needs_help: -> @attributes.needs_help + needs_payment: -> @attributes.needs_payment + active: -> @attributes.state == 'active' + products: -> @attributes.products || [] + +@List = List diff --git a/app/assets/javascripts/supplier/order.js.coffee b/app/assets/javascripts/supplier/order.js.coffee new file mode 100644 index 00000000..39e74cbc --- /dev/null +++ b/app/assets/javascripts/supplier/order.js.coffee @@ -0,0 +1,17 @@ +class Order + constructor: (@attributes)-> + table_number: -> @attributes.table_number + id: -> @attributes.id || @attributes._id + total_amount: -> @attributes.total_amount + section_title: -> @attributes.section_title + list_id: -> @attributes.list_id + state: -> @attributes.state + display: -> + order_txts = [] + return '' unless @attributes.products + for product in @attributes.products + order_txts.push(product.name + ' (' + product.number + ')') + order_txts.join(', ') + can_process: -> @attributes.state == 'placed' + +@Order = Order diff --git a/app/assets/javascripts/supplier/qsupplier.js.coffee b/app/assets/javascripts/supplier/qsupplier.js.coffee index 3806e8b2..19dfa2b3 100644 --- a/app/assets/javascripts/supplier/qsupplier.js.coffee +++ b/app/assets/javascripts/supplier/qsupplier.js.coffee @@ -1,6 +1,37 @@ root = exports ? this -data_host = '' root.Qsupplier= + watch_events: -> + faye = new Faye.Client('http://localhost:9292/faye') + faye.subscribe "/supplier/"+supplier_id, (e)=> + if(e.event == 'new_order') + body = $('#active-orders-table tbody') + order = new Order(e.data) + body.append @mustache('#active-order-template', order) + else if(e.event == 'list_needs_help') + $('#list-needs-help-indicator-'+e.data.id).removeClass('hide') + $('#list-is-helped-button-'+e.data.id).removeClass('hide') + else if(e.event == 'list_needs_payment') + $('#list-needs-payment-indicator-'+e.data.id).removeClass('hide') + else if(e.event == 'list_added') + $('#active-lists-table tbody').append @mustache('#active-list-template', new List(e.data)) + else if e.event == 'list_update' + list = new List(e.data) + row = $('#list-row-'+list.id()) + content = @mustache('#active-list-template', list) + if row.length then row.replaceWith(content) else $('#active-lists-table tbody').append(content) + else if e.event == 'list_closed' + $('#list-row-'+e.data.id).remove() + $('.of-list-'+e.data.id).remove() + else if e.event == 'list_helped' + list_id = e.data.id + $('#list-needs-help-indicator-'+list_id).addClass('hide') + $('#list-is-helped-button-'+list_id).addClass('hide') + else if e.event == 'order_being_processed' + $('#order-in-process-button-'+e.data.id).hide() + else if e.event == 'order_being_delivered' + $('#order-row-'+e.data.id).remove() + console.log(e) + false move_table_to_active_section: (table_id)-> table_container = $('#section-table-'+table_id) section_container = $('.section-tables-active') @@ -14,7 +45,7 @@ root.Qsupplier= data: {table: {section_id: current_section_id}}, dataType: 'json' ) - position_table_in_active_section: (section_container, table_container, make_draggable)-> + position_table_in_active_section: (section_container, table_container, make_draggable)-> make_draggable ||= false button_container = table_container.find('.action-button-container') button_container.html('') @@ -55,84 +86,39 @@ root.Qsupplier= data: {table: {section_id: ''}}, dataType: 'json' ) - - load_active_lists: () -> - $.get('/supplier/active_lists.json?section_id='+($('#current_section_selector').val() || ''), (res) -> + + load_active_lists: () -> + $.get('/supplier/active_lists.json?section_id='+($('#current_section_selector').val() || ''), (res) => body = $('#active-lists-table tbody') body.find('tr').remove() foot = $('#active-lists-table tfoot') for list in res.lists - order_txts = [] - row = $('').appendTo(body) - close_btn = $('') - close_callback = ( (lst, r) -> - -> - my_btn = $(this) - $.post('/supplier/close_list', {list_id: list._id}, (res)-> r.slideUp('slow')) - )(list, row) - close_btn.click(close_callback) - - needs_help_btn = $('') - needs_help_callback = ( (lst, r) -> - -> - my_btn = $(this) - $.post('/supplier/mark_list_as_helped', {list_id: list._id}, (res)-> my_btn.remove() ) - )(list, row) - needs_help_btn.click(needs_help_callback) - - - icons_td = $('').appendTo(row) - icons_td.append('').append(' ') if list.needs_help # or icon-bell - icons_td.append('') if list.needs_payment - - row.append($('').append($('').text(list.table_number))) - row.append($('').text(list.section_title)) - row.append($('').html(currency(list.total_amount))) - td_buttons = $('') - td_buttons.append(needs_help_btn).append(' ') if list.needs_help - td_buttons.append(close_btn) - row.append(td_buttons) - #foot.append(''+currency(res.total_amount)+''); + body.append @mustache('#active-list-template', new List(list)) + ) + mark_list_as_helped: (list_id)-> + $.post('/supplier/mark_list_as_helped', {list_id: list_id}, (res)-> + ) + close_list: (list_id)-> + $.post('/supplier/close_list', {list_id: list_id}, (res)-> ) - load_active_orders: () -> - $.get('/supplier/active_orders.json?section_id='+($('#current_section_selector').val() || ''), (res) -> + load_active_orders: () -> + $.get('/supplier/active_orders.json?section_id='+($('#current_section_selector').val() || ''), (res) => body = $('#active-orders-table tbody') body.find('tr').remove() foot = $('#active-orders-table tfoot') if(!res.orders && !res.orders.length) - alert('No orders in list'); - return; + alert('No orders in list') + return for order in res.orders - order_txts = [] - row = $('').appendTo(body) - process_btn = $('') - process_callback = ( (ord) -> - -> - my_btn = $(this) - $.post('/supplier/mark_order_in_process', {order_id: ord.id}, (res)-> my_btn.remove()) - )(order) - process_btn.click(process_callback) - - delivered_btn = $('') - delivered_callback = ( (ord, r) -> - -> - my_btn = $(this) - $.post('/supplier/order_is_delivered', {order_id: ord.id}, (res)-> r.slideUp('slow')) - )(order, row) - delivered_btn.click(delivered_callback) - for product in order.products - order_txts.push(product.name + ' (' + product['number'] + ')') - row.append($('').text(order_txts.join(', '))) - row.append($('').text(order.table_number)) - row.append($('').text(order.section_title)) - row.append($('').html(currency(order.total_amount))) - td_buttons = $('') - td_buttons.append(process_btn).append(' ') if order.state == 'placed' - td_buttons.append(delivered_btn) - row.append(td_buttons) - #foot.append(''+currency(res.total_amount)+''); + ord = new Order(order) + body.append @mustache('#active-order-template', ord) ) + mark_order_in_process: (order_id)-> + $.post('/supplier/mark_order_in_process', {order_id: order_id}) + mark_order_delivered: (order_id)-> + $.post('/supplier/order_is_delivered', {order_id: order_id}) + load_list: (list_id) -> $.get(data_host + '/supplier/lists/'+list_id+'.json', (res) -> body = $('#list-table tbody') @@ -140,23 +126,13 @@ root.Qsupplier= Qsupplier.build_list_table(body, foot, res) ) build_list_table: (body, foot, res) -> - body.find('tr').remove() - foot.find('tr').remove() if !res.orders && !res.orders.length alert('No orders in list') return + body.html('') for order in res.orders - order_txts = [] - row = $('').appendTo(body) - row.addClass(order.state) - #if(order.state == 'placed') row.addClass('info'); - #if(order.state == 'delivered') row.addClass('success'); - row.addClass('error') if order.state == 'cancelled' - for product in order.products - order_txts.push(product.name + ' (' + product['number'] + ')') - row.append($('').text(order_txts.join(', '))) - row.append($('').html(currency(order.total_amount))) - foot.append(''+currency(res.total_amount)+'') + body.append @mustache('#list-order-template', new Order(order)) + foot.find('.list-total').html(currency(res.total_amount)) update_section_tables_view: (section_id)-> $.get(data_host + '/supplier/sections/'+section_id+'/tables_view.json', (res)-> for table in res.tables @@ -181,3 +157,10 @@ root.Qsupplier= return alert('Please fill in a positive number representing the number of tables per column') unless by_column_count && by_column_count > 0 $.post('/supplier/sections/'+current_section_id+'/arrange_tables', {option: option, row_count: by_row_count, column_count: by_column_count}, -> window.location.reload()) false + mustache: (selector, locals)-> + locs = $.extend(locals, + currency: -> + (val)-> + currency(Mustache.render(val, this)) + ) + Mustache.to_html($(selector).html(), locs) diff --git a/app/assets/javascripts/user/application.js.erb b/app/assets/javascripts/user/application.js.erb index 0bbcd65e..d64e3b7e 100644 --- a/app/assets/javascripts/user/application.js.erb +++ b/app/assets/javascripts/user/application.js.erb @@ -27,6 +27,7 @@ //= require twitter/bootstrap/bootstrap-typeahead //= require twitter/bootstrap/bootstrap-affix //= require qwaiter +//= require supplier/order //= require mustache //= require_directory . //= require_self diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index f18a8c88..00e8bbcf 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -11,11 +11,19 @@ class Quser formatted watch_events: -> faye = new Faye.Client('http://localhost:9292/faye') - faye.subscribe "/user/"+QMobile.user_id(), (e)-> - debugger + faye.subscribe "/user/"+QMobile.user_id(), (e)=> if(e.event == 'list_closed') - redirect_to 'user_root', {list_closed: 'true'} - console.log(data) + #redirect_to 'user_root', {list_closed: 'true'} + redirect_to 'history_list', {list_id: e.data.id, list_closed: true} + if(e.event == 'list_helped') + window.active_list.needs_help = false + @list_needs_help_default_action() + if(e.event == 'order_being_processed') + $('#order-row-'+e.data.id).addClass('active') + if(e.event == 'order_being_delivered') + $('#order-row-'+e.data.id).addClass('delivered') + console.log(e) + false home_loader: -> $.getJSON(data_host + '/user/list_info.json?' + authentication_string, (res) => @handle_active_list_default_actions(res)) handle_active_list: (callback) -> @@ -34,14 +42,12 @@ class Quser if(response.ok == false && response.status && response.status == 401) direct_to_site('obtain_user_token') return - if response.table_number - $('.table-number').text(response.table_number) - if response.supplier_name - $('.supplier-name').text(response.supplier_name) - if response.not_present - $('.home-link').hide() - else - $('.home-link').show() + + $('.table-number').text(response.table_number) if response.table_number + $('.supplier-name').text(response.supplier_name) if response.supplier_name + + if response.not_present || response.list_active == false then $('.home-link').hide() else $('.home-link').show() + @list_needs_payment_default_action(response) @list_needs_help_default_action(response) # join_request_active is to ensure that there are no more modals loaded when one is still active @@ -126,9 +132,9 @@ class Quser $.post(data_host + '/user/list_needs_payment.json', authentication_object, (res) => window.active_list = res; window.Quser.list_needs_payment_default_action(res)) load_active_list: () -> $.getJSON(data_host + '/user/active_list.json?'+authentication_string, (res) => - window.active_list = res + window.active_list = res if res._id unless res.list_active - redirect_to 'history_list', {list_id: res._id, list_closed: true} + redirect_to 'history_list', {list_id: window.active_list._id, list_closed: true} return @handle_active_list_default_actions(res) body = $('#active-list-table tbody') @@ -146,6 +152,7 @@ class Quser foot = $('#history-list-table tfoot') @build_list_table(body, foot, res) $('.list-created-at').text(@format_date(res.created_at)) + $('.list-closed-at').text(@format_date(res.closed_at)) $('.supplier-name').text(res.supplier_name) ) load_list_history: -> @@ -196,12 +203,7 @@ class Quser alert('No orders in list') return m_obj = res - for order in m_obj.orders - order_txts = [] - for product in order.products - order_txts.push(product.name + ' (' + product['number'] + ')') - order.products_display = order_txts.join(', ') - body.append @mustache('#active-list-orders-template', m_obj) + body.append @mustache('#active-list-order-template', new Order(order)) for order in m_obj.orders foot.append @mustache('#active-list-orders-footer-template', m_obj) order_selected_products: ()-> @@ -211,7 +213,6 @@ class Quser h['table_id'] = match[1] if match for product_id, number of window.active_products_list h['products['+product_id+']'] = number - debugger $.post(data_host + '/user/order_selected_products', $.extend(h, authentication_object), ((res) => @handle_response(res)), 'json') handle_response: (res) -> if(typeof(res) == 'string') diff --git a/app/assets/stylesheets/supplier/active_lists.css.sass b/app/assets/stylesheets/supplier/active_lists.css.sass new file mode 100644 index 00000000..e17c957e --- /dev/null +++ b/app/assets/stylesheets/supplier/active_lists.css.sass @@ -0,0 +1,19 @@ +.list-status + .list-needs-help-indicator + display: inline-block + width: 30px + color: #400 + background-color: #aaf + text-align: center + margin-right: 7px + &.hide + display: none + .list-needs-payment-indicator + display: inline-block + width: 30px + color: #440 + background-color: #faa + text-align: center + margin-right: 7px + &.hide + display: none diff --git a/app/assets/stylesheets/supplier/application.css b/app/assets/stylesheets/supplier/application.css index 8f468b1e..15383502 100644 --- a/app/assets/stylesheets/supplier/application.css +++ b/app/assets/stylesheets/supplier/application.css @@ -3,6 +3,7 @@ *= require 'twitter-bootstrap/bootstrap_overrides' *= require 'jquery-ui-1.8.23.custom.css' *= require 'general' + *= require user/active_list *= require_directory . *= require_self */ diff --git a/app/assets/stylesheets/user/active_list.css.sass b/app/assets/stylesheets/user/active_list.css.sass index a489f03c..6a0bb9cd 100644 --- a/app/assets/stylesheets/user/active_list.css.sass +++ b/app/assets/stylesheets/user/active_list.css.sass @@ -1,4 +1,4 @@ -#active-list-table +.list-table, #active-list-table tbody tr td diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 03c4bf8a..2341ce93 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,6 +7,16 @@ class ApplicationController < ActionController::Base private + def broadcast_user(uid, event, data = {}) + message = {channel: "/user/#{uid}", data: {event: event, data: data}} + uri = URI.parse("http://localhost:9292/faye") + Net::HTTP.post_form(uri, :message => message.to_json) + end + def broadcast_supplier(sid, event, data = {}) + message = {channel: "/supplier/#{sid}", data: {event: event, data: data}} + uri = URI.parse("http://localhost:9292/faye") + Net::HTTP.post_form(uri, :message => message.to_json) + end def set_locale I18n.locale = :nl end @@ -14,7 +24,7 @@ class ApplicationController < ActionController::Base def layout_by_resource if devise_controller? case session[:user_return_to] - when /\/user\// then '' + when /\/user\// then 'obtain_token' when /obtain_token/ then 'obtain_token' else 'theme1' end @@ -50,9 +60,4 @@ class ApplicationController < ActionController::Base {ok: true, message: message}.merge(options).to_json end - def broadcast_user(uid, event, data = {}) - message = {channel: channel, data: {event: event, data: data}} - uri = URI.parse("http://localhost:9292/faye") - Net::HTTP.post_form(uri, :message => message.to_json) - end end diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 58d2f8f2..37a3a0ba 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -19,7 +19,7 @@ class DashboardController < ApplicationController # Testing action def select_qrcode - @tables = Table.all + @tables = Table.all.sample(2) render layout: 'phone' end diff --git a/app/controllers/supplier_controller.rb b/app/controllers/supplier_controller.rb index a2f7c719..788ea290 100644 --- a/app/controllers/supplier_controller.rb +++ b/app/controllers/supplier_controller.rb @@ -71,10 +71,8 @@ class SupplierController < ApplicationController h[:lists] = [] grand_total = 0.0 for list in @supplier.active_lists(section_id: params[:section_id].presence) - hl = list.as_json - hl[:total_amount] = list.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 = list.with_info_as_json grand_total += hl[:total_amount] - hl[:section_title] = list.table.section.try(:title) h[:lists] << hl end h[:total_amount] = grand_total.round(2) @@ -87,17 +85,13 @@ class SupplierController < ApplicationController def close_list @list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id]) @list.close! - for user_id in @list.user_ids - broadcast_user user_id, 'list_closed', @list - end render nothing: true end # POST /orders/1/is_helped def mark_list_as_helped @list = List.find_by_supplier_id_and_id(current_supplier.id, params[:list_id]) - @list.needs_help = false - @list.save + @list.is_helped! render nothing: true end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 392ad38f..08cbd197 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -93,6 +93,27 @@ class UserController < ApplicationController end end + def list_products_for_table + @table = Table.find(params[:table_id]) + respond_to do |format| + format.html do + end + format.json do + products = @table.supplier.products + products.include_relation(:product_categories) + products.sort_by!{|p| p.product_category.try(:position) || 90000} + h = products.inject({ + table_number: @table.number, + supplier_name: @table.supplier.name, + has_occupied_info: true, + is_occupied: @table.occupied? + }){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h} + render json: h + end + end + end + + def join_occupied_table @table = Table.find(params[:table_id]) end @@ -149,26 +170,6 @@ class UserController < ApplicationController end end - def list_products_for_table - @table = Table.find(params[:table_id]) - respond_to do |format| - format.html do - end - format.json do - products = @table.supplier.products - products.include_relation(:product_categories) - products.sort_by!{|p| p.product_category.try(:position) || 90000} - h = products.inject({ - table_number: @table.number, - supplier_name: @table.supplier.name, - has_occupied_info: true, - is_occupied: @table.occupied? - }){|h, p| n = p.product_category.try(:name) || 'other'; h[n] ||= []; h[n] << p; h} - render json: h - end - end - end - # GET /user/current_list.json # Information about the currently active list # This information includes detailed order information @@ -214,6 +215,7 @@ class UserController < ApplicationController 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) render json: list.as_json.merge(list_active: list.active?) end end @@ -224,8 +226,7 @@ class UserController < ApplicationController respond_to do |format| format.json do render json: {list_active: false} and return unless list.present? - list.needs_payment = true - list.save + list.needs_payment! render json: list.as_json.merge(list_active: list.active?) end end diff --git a/app/models/list.rb b/app/models/list.rb index cb513a70..484a409c 100644 --- a/app/models/list.rb +++ b/app/models/list.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index bd95f81d..cb3c1222 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -50,16 +50,47 @@ class Order def is_being_processed! self.state = 'active' - save + if save + for user_id in list.user_ids + broadcast_user user_id, 'order_being_processed', id: id + end + broadcast_supplier supplier_id, 'order_being_processed', id: id + end end def is_delivered! self.state = 'delivered' - save + if save + for user_id in list.user_ids + broadcast_user user_id, 'order_being_delivered', id: id + end + broadcast_supplier supplier_id, 'order_being_delivered', id: id + end end def close! self.state = 'closed' if placed? || active? save end + + def as_json(*args) + h = super.with_indifferent_access + h[:table_number] = table_number + h[:section_title] = list.table.section.try(:title) + h + end + + def with_products_as_json + return @with_products_as_json if @with_products_as_json.present? + product_orders.include_relation(:product) + ho = as_json + ho[:products] = [] + order_total = 0.0 + for product_order in product_orders + order_total += (product_order.amount * product_order.price).round(2) + ho[:products] << {name: product_order.product.name, id: product_order.product_id, number: product_order.amount, price: product_order.price} + end + ho[:total_amount] = order_total.round(2) + @with_products_as_json = ho + end end diff --git a/app/templates/supplier/_active_list.mustache b/app/templates/supplier/_active_list.mustache new file mode 100644 index 00000000..151e226f --- /dev/null +++ b/app/templates/supplier/_active_list.mustache @@ -0,0 +1,14 @@ + + + ? + + + {{table_number}} + {{section_title}} + {{#currency}}{{total_amount}}{{/currency}} + + + + + + diff --git a/app/templates/supplier/_active_order.mustache b/app/templates/supplier/_active_order.mustache new file mode 100644 index 00000000..bae0a0e1 --- /dev/null +++ b/app/templates/supplier/_active_order.mustache @@ -0,0 +1,10 @@ + + {{display}} + {{table_number}} + {{section_title}} + {{#currency}}{{total_amount}}{{/currency}} + + + + + diff --git a/app/templates/supplier/_list_order.mustache b/app/templates/supplier/_list_order.mustache new file mode 100644 index 00000000..42e72ea4 --- /dev/null +++ b/app/templates/supplier/_list_order.mustache @@ -0,0 +1,4 @@ + + {{display}} + {{#currency}}{{total_amount}}{{/currency}} + diff --git a/app/templates/user/_active_list_order.mustache b/app/templates/user/_active_list_order.mustache new file mode 100644 index 00000000..42e72ea4 --- /dev/null +++ b/app/templates/user/_active_list_order.mustache @@ -0,0 +1,4 @@ + + {{display}} + {{#currency}}{{total_amount}}{{/currency}} + diff --git a/app/templates/user/_active_list_orders.mustache b/app/templates/user/_active_list_orders.mustache deleted file mode 100644 index 8e2d7a2a..00000000 --- a/app/templates/user/_active_list_orders.mustache +++ /dev/null @@ -1,6 +0,0 @@ -{{#orders}} - - {{products_display}} - {{#currency}}{{total_amount}}{{/currency}} - -{{/orders}} diff --git a/app/templates/user/_active_order.mustache b/app/templates/user/_active_order.mustache index 5eb0a618..0ff92c03 100644 --- a/app/templates/user/_active_order.mustache +++ b/app/templates/user/_active_order.mustache @@ -19,7 +19,7 @@ - + {{#currency}}{{total}}{{/currency}} diff --git a/app/views/layouts/phone.html.slim b/app/views/layouts/phone.html.slim index c8b61bc0..0cbd6daf 100644 --- a/app/views/layouts/phone.html.slim +++ b/app/views/layouts/phone.html.slim @@ -19,7 +19,7 @@ html lang="en" link href="/favicon.ico" rel="shortcut icon" javascript: var data_host = '#{Rails.env == 'development' ? 'http://qwaiter.dev' : 'http://data.qwaiter.com' }'; - //var data_host = 'http://localhost:3000'; + var data_host = 'http://localhost:3000'; //data_host = 'http://192.168.1.148:3000'; var $locale = 'en'; var $url_vars = null; diff --git a/app/views/layouts/tablet.html.slim b/app/views/layouts/tablet.html.slim index 09190f82..37a424cf 100644 --- a/app/views/layouts/tablet.html.slim +++ b/app/views/layouts/tablet.html.slim @@ -11,11 +11,17 @@ html lang="en" /[if lt IE 9] = javascript_include_tag "http://html5shim.googlecode.com/svn/trunk/html5.js" = stylesheet_link_tag "supplier/application", :media => "all" + = javascript_include_tag 'http://localhost:9292/faye.js' link href="/images/apple-touch-icon-144x144.png" rel="apple-touch-icon-precomposed" sizes="144x144" link href="/images/apple-touch-icon-114x114.png" rel="apple-touch-icon-precomposed" sizes="114x114" link href="/images/apple-touch-icon-72x72.png" rel="apple-touch-icon-precomposed" sizes="72x72" link href="/images/apple-touch-icon.png" rel="apple-touch-icon-precomposed" link href="/favicon.ico" rel="shortcut icon" + + javascript: + var supplier_id = '#{current_supplier.id}'; + var data_host = '' + = yield :head body diff --git a/app/views/supplier/active_lists.html.slim b/app/views/supplier/active_lists.html.slim index 90c458d2..bd492c8b 100644 --- a/app/views/supplier/active_lists.html.slim +++ b/app/views/supplier/active_lists.html.slim @@ -10,11 +10,5 @@ th.currency= t('supplier.active_lists.price') th.actions tbody -- content_for :footer do - javascript: - var active_lists_interval; - jQuery(function(){ - Qsupplier.load_active_lists() - active_lists_interval = setInterval('Qsupplier.load_active_lists()', 7500); - }); +script#active-list-template[type="text/html"]= render 'active_list.mustache' diff --git a/app/views/supplier/active_orders.html.slim b/app/views/supplier/active_orders.html.slim index ab6cf525..ee38727a 100644 --- a/app/views/supplier/active_orders.html.slim +++ b/app/views/supplier/active_orders.html.slim @@ -10,10 +10,4 @@ th.currency= t('supplier.active_orders.price') th.actions tbody -- content_for :footer do - javascript: - jQuery(function(){ - Qsupplier.load_active_orders() - setInterval( 'Qsupplier.load_active_orders()', 7500); - }); - +script#active-order-template[type="text/html"]= render 'active_order.mustache' diff --git a/app/views/supplier/home.html.slim b/app/views/supplier/home.html.slim index c1640601..8a35e14c 100644 --- a/app/views/supplier/home.html.slim +++ b/app/views/supplier/home.html.slim @@ -17,4 +17,7 @@ linker.hide(); } }).change(); + Qsupplier.load_active_orders() + Qsupplier.load_active_lists() + Qsupplier.watch_events(); }); diff --git a/app/views/suppliers/lists/show.html.slim b/app/views/suppliers/lists/show.html.slim index 97ea9c70..c28c5172 100644 --- a/app/views/suppliers/lists/show.html.slim +++ b/app/views/suppliers/lists/show.html.slim @@ -3,7 +3,7 @@ dl.dl-horizontal dt= List.human_attribute_name(:created_at) dd= l @list.created_at, format: :short .well - table#list-table.table + table#list-table.table.list-table thead tr th= Order.model_name.human @@ -12,6 +12,11 @@ dl.dl-horizontal tr td colspan=2 = slider_image tfoot + tr + td + td.currency + strong.list-total +script#list-order-template[type="text/html"]= render 'supplier/list_order.mustache' .form-actions = link_to t("helpers.links.back"), suppliers_lists_path(date: @list.created_at.strftime('%Y-%m-%d')), class: 'btn' ' diff --git a/app/views/user/active_list.html.slim b/app/views/user/active_list.html.slim index 56972a35..aca726c9 100644 --- a/app/views/user/active_list.html.slim +++ b/app/views/user/active_list.html.slim @@ -16,11 +16,12 @@ tr td colspan=2 = slider_image tfoot -script#active-list-orders-template[type="text/html"]= render 'active_list_orders.mustache' +script#active-list-order-template[type="text/html"]= render 'active_list_order.mustache' script#active-list-orders-footer-template[type="text/html"]= render 'active_list_orders_footer.mustache' - content_for :footer do javascript: jQuery(function(){ Quser.load_active_list(); - setInterval( "Quser.load_active_list()", 7500); + Quser.watch_events(); + //setInterval( "Quser.load_active_list()", 7500); }) diff --git a/app/views/user/history_list.html.slim b/app/views/user/history_list.html.slim index fbd57630..aa868d44 100644 --- a/app/views/user/history_list.html.slim +++ b/app/views/user/history_list.html.slim @@ -2,10 +2,12 @@ dl.dl-horizontal dt= List.human_attribute_name(:created_at) dd.list-created-at + dt= List.human_attribute_name(:closed_at) + dd.list-closed-at dt= Supplier.model_name.human dd.supplier-name .well - table#history-list-table.table + table#history-list-table.table.list-table thead tr th= Order.model_name.human @@ -14,7 +16,7 @@ dl.dl-horizontal tr td colspan=2 = slider_image tfoot -script#active-list-orders-template[type="text/html"]= render 'active_list_orders.mustache' +script#active-list-order-template[type="text/html"]= render 'active_list_order.mustache' script#active-list-orders-footer-template[type="text/html"]= render 'active_list_orders_footer.mustache' - content_for :footer do javascript: diff --git a/app/views/user/list_products.html.slim b/app/views/user/list_products.html.slim index dee36534..7214f311 100644 --- a/app/views/user/list_products.html.slim +++ b/app/views/user/list_products.html.slim @@ -20,7 +20,7 @@ script#active-order-template[type="text/html"]= render 'active_order.mustache' jQuery(function(){ Quser.handle_active_list(function(){ Quser.load_active_list_products(); - setInterval('Quser.handle_active_list()', 7500); + Quser.watch_events(); }) }) diff --git a/config/initializers/model_broadcast.rb b/config/initializers/model_broadcast.rb new file mode 100644 index 00000000..8d10d000 --- /dev/null +++ b/config/initializers/model_broadcast.rb @@ -0,0 +1,11 @@ +require 'simply_stored/couch' +module ModelBroadcast + def broadcast_supplier(*args) + ApplicationController.new.send(:broadcast_supplier, *args) + end + def broadcast_user(*args) + ApplicationController.new.send(:broadcast_user, *args) + end +end +SimplyStored::Couch.send(:include, ModelBroadcast) +SimplyStored::Couch.send(:extend, ModelBroadcast)