diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 17b339c13db65b2ebb3814bda5dc2d00e1a99ad2..9951449b48827f7ee3a52a924b813c3e0bdf459a 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 889c02e9dbd7b25cdc80b333cb48dafbfffff868..076a7ab46c02847f758e88d143f4c075b2b79967 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 d22361d01ed67488528900863f915a9e2b56285e..cad61acf6e8d5321d95cd5ae91a14e5dbacb819a 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 8389fdecf23c78f6c37a02d666dbb341a540902b..88941e4bc9469a04d35520c8bdc5937f570a1261 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