From 157b2406a0a11787cf61f7cf8ad6907696aae827 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Mon, 27 Aug 2012 15:46:18 +0200 Subject: [PATCH] updates for qr handling --- app/assets/javascripts/qrammer.js.coffee | 10 ------ app/assets/javascripts/quser.js.coffee | 23 ++++++++++-- app/controllers/application_controller.rb | 12 ++++--- app/controllers/supplier_controller.rb | 3 ++ app/controllers/tables_controller.rb | 6 ++-- app/controllers/user_controller.rb | 39 +++++++++++++++------ app/models/table.rb | 4 +-- app/views/dashboard/select_qrcode.html.slim | 2 +- app/views/layouts/phone.html.slim | 2 +- app/views/supplier/home.html.slim | 2 ++ app/views/user/home.html.slim | 2 ++ config/locales/en.yml | 2 +- config/routes.rb | 4 ++- stories | 4 +++ 14 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 app/views/supplier/home.html.slim diff --git a/app/assets/javascripts/qrammer.js.coffee b/app/assets/javascripts/qrammer.js.coffee index 80dfe417..ad0980ae 100644 --- a/app/assets/javascripts/qrammer.js.coffee +++ b/app/assets/javascripts/qrammer.js.coffee @@ -23,16 +23,6 @@ root.Qrammer = clear_active_list: -> window.active_products_list = {} $('#active-order-table').hide() - handle_response: (res) -> - if(typeof(res) == 'string') - return unless res.length - if res[0] == '{' - res = JSON.parse(res) - else - eval(res) - return - window.location = '/' if res['message'] && !res['ok'] - window.location = '/user/active_list' if res['ok'] load_active_orders: () -> $.get('/supplier/active_orders.json', (res) -> body = $('#active-orders-table tbody') diff --git a/app/assets/javascripts/quser.js.coffee b/app/assets/javascripts/quser.js.coffee index 08ce0633..154580fc 100644 --- a/app/assets/javascripts/quser.js.coffee +++ b/app/assets/javascripts/quser.js.coffee @@ -3,7 +3,7 @@ root.Quser= handle_active_list: (callback) -> $.get('/user/list_info.json', (res) -> if !res.list_active - window.location = '/phone_home?list_closed=true' + window.location = '/user?list_closed=true' return window.active_list = res callback.call() if callback @@ -60,10 +60,20 @@ root.Quser= foot.append(''+Qrammer.currency(res.total_amount)+'') ) order_selected_products: ()-> - h = {list_id: active_list_id} + h = {} for product_id, info of window.active_products_list h['products['+product_id+']'] = info.number - $.post('/user/order_selected_products', h, ((res) -> Qrammer.handle_response(res)), 'json') + $.post('/user/order_selected_products', h, ((res) -> Quser.handle_response(res)), 'json') + handle_response: (res) -> + if(typeof(res) == 'string') + return unless res.length + if res[0] == '{' + res = JSON.parse(res) + else + eval(res) + return + window.location = '/user' if res['message'] && !res['ok'] + window.location = res.location || '/user/list_products' if res['ok'] build_product_list: -> table = $('#active-order-table') tbody = table.find('tbody') @@ -118,6 +128,13 @@ root.Quser= body.append(row) ) + actions_for_table: (table_id)-> + $.get('/user/table_info.json?table_id='+table_id, (res)-> + if res.occupied + alert('Table is occupied') + else + $.post('/user/create_list.json', {table_id: table_id}, (res)-> Quser.handle_response(res)) + , 'json') add_product: (product, count) -> count ||= 1 window.active_products_list = {} unless window.active_products_list diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8a754bc5..a752a327 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -30,10 +30,14 @@ class ApplicationController < ActionController::Base alias :active_list_object :active_list helper_method :active_list_object - def js_alert(message) - {ok: false, message: message}.to_json + def js_alert(*args) + options = args.extract_options! + message = args.first || '' + {ok: false, message: message}.merge(options).to_json end - def js_notice(message) - {ok: true, message: message}.to_json + def js_notice(*args) + options = args.extract_options! + message = args.first || '' + {ok: true, message: message}.merge(options).to_json end end diff --git a/app/controllers/supplier_controller.rb b/app/controllers/supplier_controller.rb index eeaae29f..0073594d 100644 --- a/app/controllers/supplier_controller.rb +++ b/app/controllers/supplier_controller.rb @@ -1,6 +1,9 @@ class SupplierController < ApplicationController before_filter :authenticate_supplier! + def home + render layout: 'tablet' + end # GET /suppliers/1/active_orders # GET /suppliers/1/active_orders.json def active_orders diff --git a/app/controllers/tables_controller.rb b/app/controllers/tables_controller.rb index 3febd009..25d2a3c2 100644 --- a/app/controllers/tables_controller.rb +++ b/app/controllers/tables_controller.rb @@ -86,10 +86,12 @@ class TablesController < ApplicationController # GET /tables/qrcode.png # GET /tables/qrcode.svg def qrcode + @table = Table.find(params[:id]) + code = @table.id respond_to do |format| format.html - format.svg { render :qrcode => request.url, :level => :l, :unit => 10 } - format.png { render qrcode: request.url } + format.svg { render :qrcode => code, :level => :l, :unit => 10 } + format.png { render qrcode: code } end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 6f440a97..7c650d3f 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -12,14 +12,29 @@ class UserController < ApplicationController def create_list @table = Table.find(params[:table_id]) if @table.occupied? - redirect_to root_path, alert: t('table.is_occupied') + respond_to do |format| + format.html { redirect_to root_path, alert: t('table.is_occupied') } + format.json { render json: js_alert(t('messages.table_is_occupied'))} + end else @list = List.new(table: @table, supplier_id: @table.supplier_id) @list.add_user current_user #@list.add_user(current_user) @list.save session[:active_list_id] = @list.id - redirect_to user_list_products_path + respond_to do |format| + format.html { redirect_to user_list_products_path } + format.json { render json: js_notice('table_created')} + end + end + end + + def table_info + @table = Table.find(params[:table_id]) + respond_to do |format| + format.json do + render json: {occupied: @table.occupied?} + end end end @@ -81,8 +96,13 @@ class UserController < ApplicationController def list_info respond_to do |format| format.json do - render json: {list_active: false} and return unless list.present? - render json: list.as_json.merge(list_active: true) + if !list.try(:active?) + session[:active_list_id] = nil + render json: {list_active: false} + return + else + render json: list.as_json.merge(list_active: list.active? ) + end end end end @@ -94,7 +114,7 @@ class UserController < ApplicationController render json: {list_active: false} and return unless list.present? list.needs_help = true list.save - render json: list.as_json.merge(list_active: true) + render json: list.as_json.merge(list_active: list.active?) end end end @@ -106,7 +126,7 @@ class UserController < ApplicationController render json: {list_active: false} and return unless list.present? list.needs_payment = true list.save - render json: list.as_json.merge(list_active: true) + render json: list.as_json.merge(list_active: list.active?) end end end @@ -134,20 +154,17 @@ class UserController < ApplicationController def order_selected_products + @list = list respond_to do |format| format.html do - redirect_to(root_path, alert: t('messages.cannot_order_without_list_id')) and return if params[:list_id].blank? - @list = List.find(params[:list_id]) redirect_to(root_path, alert: t('messages.cannot_order_on_non_active_list')) and return unless @list.active? @list.place_order current_user, params[:products] redirect_to root_path, notice: t('messages.order_is_placed') end format.js do - render js: js_alert(t('messages.cannot_order_without_list_id')) and return if params[:list_id].blank? - @list = List.find(params[:list_id]) render js: js_alert(t('messages.cannot_order_on_non_active_list')) and return unless @list.active? @list.place_order current_user, params[:products] - render js: js_notice( t('messages.order_is_placed') ) + render js: js_notice( t('messages.order_is_placed'), location: user_active_list_path ) end end end diff --git a/app/models/table.rb b/app/models/table.rb index 39ee6b94..ec0d0d64 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -12,12 +12,12 @@ class Table view :active_lists, type: :custom, map_function: %|function(doc){ if(doc.ruby_class == 'List' && doc.state == 'active'){ - emit(doc._id, 1); + emit(doc.table_id, 1); } }|, reduce_function: '_sum' def occupied? - not self.class.database.view(self.class.active_lists(key: list_id, reduce: true)).zero? + not self.class.database.view(self.class.active_lists(key: id, reduce: true)).zero? end end diff --git a/app/views/dashboard/select_qrcode.html.slim b/app/views/dashboard/select_qrcode.html.slim index 00a5b09b..4dbfab79 100644 --- a/app/views/dashboard/select_qrcode.html.slim +++ b/app/views/dashboard/select_qrcode.html.slim @@ -1,4 +1,4 @@ .page-header= title 'Select Qr code' ul#qr-list - for table in @tables - li= link_to image_tag(url_for(qrcode_table_path(table, format: :png))), user_create_list_path(table_id: table.id) + li= link_to_function image_tag(url_for(qrcode_table_path(table, format: :png))), %|Quser.actions_for_table('#{table.id}')| diff --git a/app/views/layouts/phone.html.slim b/app/views/layouts/phone.html.slim index bbeb712f..d92f2849 100644 --- a/app/views/layouts/phone.html.slim +++ b/app/views/layouts/phone.html.slim @@ -33,7 +33,7 @@ html lang="en" ul.nav#top-navigation-list - if list_open? li= link_to 'Move table', '#' - li= link_to 'View history', user_history_path + li= link_to 'View history', user_list_history_path .container diff --git a/app/views/supplier/home.html.slim b/app/views/supplier/home.html.slim new file mode 100644 index 00000000..1e855cbc --- /dev/null +++ b/app/views/supplier/home.html.slim @@ -0,0 +1,2 @@ += render template: 'supplier/active_lists' += render template: 'supplier/active_orders' diff --git a/app/views/user/home.html.slim b/app/views/user/home.html.slim index cf06ed8b..4d39a24c 100644 --- a/app/views/user/home.html.slim +++ b/app/views/user/home.html.slim @@ -4,5 +4,7 @@ ul.nav.nav-tabs.nav-stacked li= link_to 'Active list', user_active_list_path - else li= link_to 'Join table with Qr scan', '/select_qrcode' + li + button.btn.btn-primary onClick="QMobile.scanQr()" Scan Qr li= link_to 'Subscribe to list', '#' li= link_to 'Check out menu', '#' diff --git a/config/locales/en.yml b/config/locales/en.yml index 824add53..c570e8ab 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -11,11 +11,11 @@ en: errors: title: There are problems found during saving (%{count}) messages: - cannot_order_without_list_id: You cannot place an order without specifying a list cannot_order_on_non_active_list: You cannot place an order on a closed list order_is_placed: Your order has been received in good order the_list_has_been_closed: The %{list} has been closed illegal_history_list_attempt: The list you want to access is not yours + table_is_occupied: The table you want to sit on is already occupied action: index: label: Listing %{models} diff --git a/config/routes.rb b/config/routes.rb index 18576b7a..8cef3713 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,9 +18,10 @@ Qrammer::Application.routes.draw do resources :products resources :product_categories - get '/supplier' => 'supplier#active_orders', as: :supplier_root + get '/supplier' => 'supplier#home', as: :supplier_root get '/supplier/active_orders' => 'supplier#active_orders', as: :supplier_active_orders get '/supplier/active_lists' => 'supplier#active_lists', as: :supplier_active_lists + get 'supplier/lists/:list_id' => 'supplier#show_list', as: :supplier_show_list post '/supplier/close_list' => 'supplier#close_list', as: :supplier_close_list post '/supplier/mark_list_as_helped' => 'supplier#mark_list_as_helped', as: :supplier_mark_list_as_helped @@ -38,6 +39,7 @@ Qrammer::Application.routes.draw do match '/user/list_history' => 'user#list_history', as: :user_list_history match '/user/list_history/:list_id' => 'user#history_list', as: :user_history_list post '/user/order_selected_products' => 'user#order_selected_products', as: :user_order_selected_products + get '/user/table_info' => 'user#table_info', as: :user_table_info match '/show_products' => 'dashboard#show_products', as: :user_products diff --git a/stories b/stories index 30ab7ae7..e27558d5 100644 --- a/stories +++ b/stories @@ -37,4 +37,8 @@ Actielijst: - Plaats omgevingen in ihpone / ipad omgeving (gerelateerd) - Barman en vaste tafels +Jukebox koppeling +Wireless wachtwoord via qr code + + handle closed list on list update for user list