From 1eeda440ab854e3f8e9a76953f0ee15c62034e40 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Mon, 25 Aug 2014 16:56:30 +0200 Subject: [PATCH] User app error handling upgrades --- .../javascripts/user/app/router.js.coffee | 1 + ...coffee => application_route.js.coffee.erb} | 45 +++++++++++++++++-- .../user/app/templates/error.emblem | 2 + app/controllers/dashboard_controller.rb | 7 +++ app/models/user.rb | 2 + app/models/user_app_log.rb | 5 +++ app/views/layouts/user/foundation.html.slim | 5 ++- app/views/user/obtain_token.html.slim | 2 +- config/locales/user.en.yml | 10 +++++ config/locales/user.nl.yml | 11 +++++ config/routes.rb | 2 + 11 files changed, 86 insertions(+), 6 deletions(-) rename app/assets/javascripts/user/app/routes/{application_route.js.coffee => application_route.js.coffee.erb} (53%) create mode 100644 app/assets/javascripts/user/app/templates/error.emblem create mode 100644 app/models/user_app_log.rb diff --git a/app/assets/javascripts/user/app/router.js.coffee b/app/assets/javascripts/user/app/router.js.coffee index 3cd25f75..a42dcce5 100644 --- a/app/assets/javascripts/user/app/router.js.coffee +++ b/app/assets/javascripts/user/app/router.js.coffee @@ -12,3 +12,4 @@ App.Router.map -> @resource 'join_requests' @resource 'lists', -> @resource 'list', path: ':list_id' + @route 'error' diff --git a/app/assets/javascripts/user/app/routes/application_route.js.coffee b/app/assets/javascripts/user/app/routes/application_route.js.coffee.erb similarity index 53% rename from app/assets/javascripts/user/app/routes/application_route.js.coffee rename to app/assets/javascripts/user/app/routes/application_route.js.coffee.erb index 3664324a..d566b54b 100644 --- a/app/assets/javascripts/user/app/routes/application_route.js.coffee +++ b/app/assets/javascripts/user/app/routes/application_route.js.coffee.erb @@ -41,8 +41,47 @@ App.ApplicationRoute = Ember.Route.extend Ember.$.post '/user/needs_help.json' #Ember.$.post('/user/needs_help.json').then (res) => #@set('list.needs_help', true) # also done by faye + error: (error, transition, klass)-> + message = null + if error.status is 404 + if klass.routeName is 'table' + message = t('errors.404.model.message', model: t("models.#{klass.routeName}"), id: transition.state.params.table.table_id) + else + message = t('errors.404.general.message') + @transitionTo('index').then (route)-> + route.get('controller.controllers.application').set 'notice', message + scanQr: -> +<% if Rails.env.user_app? %> + ar = @ + scanner = cordova.require("cordova/plugin/BarcodeScanner") + scanner.scan (result)-> + if result.cancelled + ar.transitionTo('index') + $log + action: 'qrscan' + result: 'cancel' + else + table_id_index = result.text.indexOf('s?t='); + if table_id_index > -1 + table_id = result.text.substr(table_id_index + 4); + ar.transitionTo 'table', table_id + else + ar.transitionTo('index').then (route)-> + route.get('controller.controllers.application').set 'notice', "we got a barcode: '#{result.text}' but cannot extract a table id" + $log + action: 'qrscan' + error: 'cannot extract table id' + info: result.text + + , (error)-> + $log + action: 'qrscan' + error: 'scan failed' + info: error + ar.transitionTo('index').then (route)-> + route.get('controller.controllers.application').set 'notice', "scanning failed: #{error}" +<% else %> + # @transitionTo 'table', 'abc' @transitionTo 'select_qrcode' - events: -> - error: (error)-> - debugger +<% end %> diff --git a/app/assets/javascripts/user/app/templates/error.emblem b/app/assets/javascripts/user/app/templates/error.emblem new file mode 100644 index 00000000..3e54e33b --- /dev/null +++ b/app/assets/javascripts/user/app/templates/error.emblem @@ -0,0 +1,2 @@ +.row: h2=t 'app.error.title' +.row: p=t 'app.error.body' diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 884d91b0..36618e4d 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -1,10 +1,17 @@ class DashboardController < ApplicationController layout 'theme1' + before_action :allow_all_origins, only: :error_report def demo_both render layout: 'demo-both' end + def user_app_log + log = UserAppLog.new(params: params[:log]) + log.user = current_user + log.save + end + # Testing action def select_qrcode #@tables = Table.all.sample(2) | List.active.map(&:table) diff --git a/app/models/user.rb b/app/models/user.rb index 04324e03..0cebf9ef 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,6 +22,8 @@ class User validates_uniqueness_of :email before_save :ensure_authentication_token + + has_many :error_logs view :by_authentication_token, key: :authentication_token view :by_email, key: :email diff --git a/app/models/user_app_log.rb b/app/models/user_app_log.rb new file mode 100644 index 00000000..fac73ac4 --- /dev/null +++ b/app/models/user_app_log.rb @@ -0,0 +1,5 @@ +class UserAppLog + include SimplyStored::Couch + belongs_to :user + property :params +end diff --git a/app/views/layouts/user/foundation.html.slim b/app/views/layouts/user/foundation.html.slim index 85841acc..8a4dd044 100644 --- a/app/views/layouts/user/foundation.html.slim +++ b/app/views/layouts/user/foundation.html.slim @@ -8,7 +8,7 @@ html lang="en" = javascript_include_tag "vendor/modernizr" = stylesheet_link_tag "user/foundation/application" = javascript_include_tag "user/flat/application" - - if ENV['QWAITER_MOBILE_EXPORT'] == 'yes' + - if Rails.env.user_app? javascript: var QMobile, Qwaiter, Quser; var $data_host = 'http://data.mozo.bar'; @@ -18,7 +18,7 @@ html lang="en" Qstorage.setItem('root_url', '##root_url##'); var $platform = '##platform##'; var $obtain_token_url = 'http://mozo.bar/user/obtain_token'; - + var $log = function(params){$.post('http://log.mozo.bar/user_app', {log: params})}; - else javascript: var QMobile, Qwaiter, Quser; @@ -29,5 +29,6 @@ html lang="en" #{user_dynamic_root_url}; #{user_dynamic_obtain_token_url}; var $platform = 'web' + var $log = function(params){console.log('App log:'); console.log(params);console.log('=======================================')}; body #ember-app-container diff --git a/app/views/user/obtain_token.html.slim b/app/views/user/obtain_token.html.slim index d385825b..e638faf3 100644 --- a/app/views/user/obtain_token.html.slim +++ b/app/views/user/obtain_token.html.slim @@ -7,7 +7,7 @@ Qstorage = window.localStorage; Qstorage.setItem('auth_token', '#{current_user.authentication_token}'); Qstorage.setItem('user_id', '#{current_user.id}'); - if(window.opener && window.opener != window) window.close(); + if(!window.opener || window.opener != window) window.close(); // window.location = (Qstorage.getItem('root_url') || '/user/index.html') + '?user_id=#{current_user.id}&auth_token=#{current_user.authentication_token}'; // localStorage.setItem('auth_token', '#{current_user.authentication_token}'); // localStorage.setItem('user_id', '#{current_user.id}'); diff --git a/config/locales/user.en.yml b/config/locales/user.en.yml index c23fba15..a2f8579a 100644 --- a/config/locales/user.en.yml +++ b/config/locales/user.en.yml @@ -90,3 +90,13 @@ en: closed: 'Closed' lists: show_more: Show more + app: + error: + title: There is a problem with the application + body: Please try to restart + errors: + 404: + model: + message: Cannot find %{model} with id %{id} + general: + message: Cannot find resource diff --git a/config/locales/user.nl.yml b/config/locales/user.nl.yml index aab8fc85..2cc576dd 100644 --- a/config/locales/user.nl.yml +++ b/config/locales/user.nl.yml @@ -88,3 +88,14 @@ nl: closed: 'Afgesloten' lists: show_more: Meer tonen + app: + error: + title: Er is een probleem met de applicatie + body: Herstart de applicatie om het op te lossen + + errors: + 404: + model: + message: %{model} met id %{id} kan niet worden gevonden + general: + message: Data niet beschikbaar diff --git a/config/routes.rb b/config/routes.rb index 1d0a6116..61914a0c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,8 @@ Qwaiter::Application.routes.draw do root to: 'users#index' end + post '/user_app' => 'dashboard#user_app_log' #TODO: separate high speed app at log.mozo.bar + # SUPPLIER get '/supplier' => 'supplier#home', as: :supplier_root get '/supplier/active_orders' => 'supplier#active_orders', as: :supplier_active_orders