From 9efa29e6884588fb212a442476bda8eb0a23efcb Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 27 Aug 2013 14:57:42 -0400 Subject: [PATCH] Detect whether to use mobile view. Session var mobile_view can override automatic detection. --- app/controllers/application_controller.rb | 5 +++++ app/helpers/application_helper.rb | 12 ++++++++++ app/views/layouts/application.html.erb | 2 +- lib/autospec/runner.rb | 1 + spec/helpers/application_helper_spec.rb | 27 +++++++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 spec/helpers/application_helper_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a9b6abbc9..c1f920181 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,6 +26,7 @@ class ApplicationController < ActionController::Base end end + before_filter :set_mobile_view before_filter :inject_preview_style before_filter :block_if_maintenance_mode before_filter :authorize_mini_profiler @@ -117,6 +118,10 @@ class ApplicationController < ActionController::Base end end + def set_mobile_view + session[:mobile_view] = params[:mobile_view] if params.has_key?(:mobile_view) + end + def inject_preview_style style = request['preview-style'] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 606936b19..92f0ba791 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -19,6 +19,10 @@ module ApplicationHelper end end + def html_classes + mobile_view? ? 'mobile' : '' + end + def escape_unicode(javascript) if javascript javascript.gsub(/\342\200\250/u, '
').gsub(/(<\/)/u, '\u003C/').html_safe @@ -100,4 +104,12 @@ module ApplicationHelper def login_path return "#{Discourse::base_uri}/login" end + + def mobile_view? + if session[:mobile_view] + session[:mobile_view] == '1' + else + request.user_agent =~ /Mobile|webOS/ + end + end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 49bde9e01..78246fc03 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,5 +1,5 @@ - + <%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %> diff --git a/lib/autospec/runner.rb b/lib/autospec/runner.rb index a7f94dc80..bc8feb97f 100644 --- a/lib/autospec/runner.rb +++ b/lib/autospec/runner.rb @@ -34,6 +34,7 @@ class Autospec::Runner watch_reload('spec/spec_helper.rb') watch_reload('config/(.*).rb') + watch_reload(%r{app/helpers/(.*).rb}) def self.run(opts={}) self.new.run(opts) diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb new file mode 100644 index 000000000..6300905d2 --- /dev/null +++ b/spec/helpers/application_helper_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe ApplicationHelper do + + describe 'mobile_view?' do + it "is true if mobile_view is '1' in the session" do + session[:mobile_view] = '1' + helper.mobile_view?.should be_true + end + + it "is false if mobile_view is '0' in the session" do + session[:mobile_view] = '0' + helper.mobile_view?.should be_false + end + + it "is false if mobile_view is not set and user agent is not mobile" do + controller.request.stubs(:user_agent).returns('Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36') + helper.mobile_view?.should be_false + end + + it "is true if mobile_view is not set and user agent is mobile" do + controller.request.stubs(:user_agent).returns('Mozilla/5.0 (iPhone; U; ru; CPU iPhone OS 4_2_1 like Mac OS X; ru) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5') + helper.mobile_view?.should be_true + end + end + +end