diff --git a/Gemfile.lock b/Gemfile.lock index dd89859c..2957f0cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -108,9 +108,9 @@ GEM hike (1.2.1) i18n (0.6.1) journey (1.0.4) - jquery-rails (2.1.3) - railties (>= 3.1.0, < 5.0) - thor (~> 0.14) + jquery-rails (2.1.4) + railties (>= 3.0, < 5.0) + thor (>= 0.14, < 2.0) json (1.6.7) kaminari (0.14.1) actionpack (>= 3.0.0) diff --git a/app/assets/javascripts/user/quser.js.coffee b/app/assets/javascripts/user/quser.js.coffee index 8c2f7ba8..f18a8c88 100644 --- a/app/assets/javascripts/user/quser.js.coffee +++ b/app/assets/javascripts/user/quser.js.coffee @@ -9,6 +9,13 @@ class Quser formatted += ' ' formatted += utc.substr(11, 5) formatted + watch_events: -> + faye = new Faye.Client('http://localhost:9292/faye') + faye.subscribe "/user/"+QMobile.user_id(), (e)-> + debugger + if(e.event == 'list_closed') + redirect_to 'user_root', {list_closed: 'true'} + console.log(data) home_loader: -> $.getJSON(data_host + '/user/list_info.json?' + authentication_string, (res) => @handle_active_list_default_actions(res)) handle_active_list: (callback) -> @@ -204,6 +211,7 @@ 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/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1572efd1..03c4bf8a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,7 +14,7 @@ class ApplicationController < ActionController::Base def layout_by_resource if devise_controller? case session[:user_return_to] - when /\/user\// then 'phone' + when /\/user\// then '' when /obtain_token/ then 'obtain_token' else 'theme1' end @@ -49,4 +49,10 @@ class ApplicationController < ActionController::Base message = args.first || '' {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/supplier_controller.rb b/app/controllers/supplier_controller.rb index a3630100..a2f7c719 100644 --- a/app/controllers/supplier_controller.rb +++ b/app/controllers/supplier_controller.rb @@ -87,6 +87,9 @@ 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 diff --git a/app/views/layouts/phone.html.slim b/app/views/layouts/phone.html.slim index 3f964ad1..c8b61bc0 100644 --- a/app/views/layouts/phone.html.slim +++ b/app/views/layouts/phone.html.slim @@ -11,6 +11,7 @@ html lang="en" /[if lt IE 9] = javascript_include_tag "http://html5shim.googlecode.com/svn/trunk/html5.js" = stylesheet_link_tag "user/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" @@ -31,6 +32,7 @@ html lang="en" authentication_string: function(){return this.authentication_string_storage || ''}, authentication_object: function(){return this.authentication_object_storage || '{}'}, setAuthToken: function(token){ + this.auth_token = token; this.authentication_string_storage = 'auth_token='+token; this.authentication_object_storage = '{"auth_token": "'+token+'"}' }, @@ -39,9 +41,13 @@ html lang="en" goHome: function(){ redirect_to('user_root')}, connection_problem: function(){alert('There is a problem connecting to the server')}, locale: function(){ return $locale || ($locale = 'en')}, - setLocale: function(locale){$locale = locale; return locale} + setLocale: function(locale){$locale = locale; return locale}, + token: function(){return this.auth_token}, + setUserId: function(id){ this.stored_user_id = id}, + user_id: function(){return this.stored_user_id } }); - //QMobile.setAuthToken('i5brDZ1HS1okoEq3pMyh'); + QMobile.setAuthToken('#{current_user.authentication_token}'); + QMobile.setUserId('#{current_user.id}'); body class=action_name .navbar.navbar-fixed-top diff --git a/app/views/user/home.html.slim b/app/views/user/home.html.slim index b7b25125..13ab7020 100644 --- a/app/views/user/home.html.slim +++ b/app/views/user/home.html.slim @@ -9,6 +9,7 @@ - content_for :footer do javascript: $(function(){ + Quser.watch_events(); Quser.home_loader(); - setInterval("Quser.home_loader()", 7500); + //setInterval("Quser.home_loader()", 7500); }); diff --git a/app/views/user/list_products_for_table.html.slim b/app/views/user/list_products_for_table.html.slim index ceb3852b..81d59b49 100644 --- a/app/views/user/list_products_for_table.html.slim +++ b/app/views/user/list_products_for_table.html.slim @@ -7,25 +7,10 @@ tbody tr td= slider_image - table#active-order-table.table.hide - thead - tr - th= Product.model_name.human - th # - th.currency Total - th - tbody - tfoot - tr - td colspan=2 - button class="btn btn-primary" onClick="Quser.order_selected_products()" data-t="selected_products.order"= t('selected_products.order') - |  - button class="btn btn btn-warning" onClick="Quser.clear_selected_products()" data-t="selected_products.clear"= t('selected_products.clear') - td.currency - strong#active-order-total - td + #active-order-container script#products-category-template[type="text/html"]= render 'products_category.mustache' script#products-category-for-order-template[type="text/html"]= render 'products_category_for_order.mustache' +script#active-order-template[type="text/html"]= render 'active_order.mustache' - content_for :footer do javascript: jQuery(function(){ diff --git a/config.ru b/config.ru index 24e1b14d..41614483 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,6 @@ # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) +#require 'faye' +#use Faye::RackAdapter, :mount => '/faye', :timeout => 25 run Qrammer::Application diff --git a/faye/config.ru b/faye/config.ru new file mode 100644 index 00000000..e7327699 --- /dev/null +++ b/faye/config.ru @@ -0,0 +1,18 @@ +require 'faye' +#require File.expand_path('../config/initializers/faye_token.rb', __FILE__) + +class ServerAuth + def incoming(message, callback) + if message['channel'] !~ %r{^/meta/} + if false && message['ext']['auth_token'] != FAYE_TOKEN + message['error'] = 'Invalid authentication token' + end + end + callback.call(message) + end +end + +faye_server = Faye::RackAdapter.new(mount: '/faye', timeout: 45) +faye_server.listen 9292 +faye_server.add_extension(ServerAuth.new) +run faye_server