From 1809897aa254117de7af0818379c708aaf3d5fd1 Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Tue, 3 Jul 2012 23:34:14 +0200 Subject: [PATCH] should fix the mobile toggle for the case: desktop --to--> mobile #3299 also, (possibly) fix tablet issue #3421, + tests --- app/controllers/application_controller.rb | 16 ++++++---- app/controllers/home_controller.rb | 11 +++++-- .../application_controller_spec.rb | 29 ++++++++++++++++++- spec/controllers/home_controller_spec.rb | 16 +++++++++- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 17b339c13..9951449b4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2010-2011, Diaspora Inc. This file is +# Copyright (c) 2010-2012, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. @@ -10,7 +10,7 @@ class ApplicationController < ActionController::Base before_filter :set_locale before_filter :set_git_header if (AppConfig[:git_update] && AppConfig[:git_revision]) before_filter :set_grammatical_gender - before_filter :tablet_device_fallback + before_filter :mobile_switch inflection_method :grammatical_gender => :gender @@ -106,9 +106,15 @@ class ApplicationController < ActionController::Base @grammatical_gender || nil end - def tablet_device_fallback - # we currently don't have any special tablet views... - request.format = :html if is_tablet_device? + # use :mobile view for mobile and :html for everything else + # (except if explicitly specified, e.g. :json, :xml) + def mobile_switch + if session[:mobile_view] == true && request.format.html? + request.format = :mobile + elsif request.format.tablet? + # we currently don't have any special tablet views... + request.format = :html + end end def after_sign_in_path_for(resource) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 889c02e9d..076a7ab46 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2010-2011, Diaspora Inc. This file is +# Copyright (c) 2010-2012, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. @@ -25,7 +25,14 @@ class HomeController < ApplicationController end def toggle_mobile - session[:mobile_view] = !session[:mobile_view] + if session[:mobile_view].nil? + # we're most probably not on mobile, but user wants it anyway + session[:mobile_view] = true + else + # switch from mobile to normal html + session[:mobile_view] = !session[:mobile_view] + end + redirect_to :back end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index d22361d01..cad61acf6 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2010-2011, Diaspora Inc. This file is +# Copyright (c) 2010-2012, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. @@ -33,4 +33,31 @@ describe ApplicationController do end end end + + describe '#mobile_switch' do + it 'sets the format to :mobile' do + request.format = :html + session[:mobile_view] = true + get :index + request.format.mobile?.should be_true + end + + it 'uses :html for :tablets' do + request.format = :tablet + session[:tablet_view] = true + get :index + request.format.html?.should be_true + end + + it "doesn't mess up other formats, like json" do + get :index, :format => 'json' + request.format.json?.should be_true + end + + it "doesn't mess up other formats, like xml, even with :mobile session" do + session[:mobile_view] = true + get :index, :format => 'xml' + request.format.xml?.should be_true + end + end end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 8389fdecf..88941e4bc 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -1,4 +1,4 @@ -# Copyright (c) 2010-2011, Diaspora Inc. This file is +# Copyright (c) 2010-2012, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. @@ -36,4 +36,18 @@ describe HomeController do response.should redirect_to(person_path(alice.person)) end end + + describe '#toggle_mobile' do + it 'changes :mobile to :html' do + session[:mobile_view] = true + get :toggle_mobile + session[:mobile_view].should be_false + end + + it 'changes :html to :mobile' do + session[:mobile_view] = nil + get :toggle_mobile + session[:mobile_view].should be_true + end + end end