Detect whether to use mobile view. Session var mobile_view can override automatic detection.

This commit is contained in:
Neil Lalonde 2013-08-27 14:57:42 -04:00
parent 009dec833f
commit 9efa29e688
5 changed files with 46 additions and 1 deletions

View file

@ -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']

View file

@ -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, '&#x2028;').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

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="<%= SiteSetting.default_locale %>">
<html lang="<%= SiteSetting.default_locale %>" class="<%= html_classes %>">
<head>
<meta charset="utf-8">
<title><%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %></title>

View file

@ -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)

View file

@ -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