Detect whether to use mobile view. Session var mobile_view can override automatic detection.
This commit is contained in:
parent
009dec833f
commit
9efa29e688
|
@ -26,6 +26,7 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before_filter :set_mobile_view
|
||||||
before_filter :inject_preview_style
|
before_filter :inject_preview_style
|
||||||
before_filter :block_if_maintenance_mode
|
before_filter :block_if_maintenance_mode
|
||||||
before_filter :authorize_mini_profiler
|
before_filter :authorize_mini_profiler
|
||||||
|
@ -117,6 +118,10 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_mobile_view
|
||||||
|
session[:mobile_view] = params[:mobile_view] if params.has_key?(:mobile_view)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def inject_preview_style
|
def inject_preview_style
|
||||||
style = request['preview-style']
|
style = request['preview-style']
|
||||||
|
|
|
@ -19,6 +19,10 @@ module ApplicationHelper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_classes
|
||||||
|
mobile_view? ? 'mobile' : ''
|
||||||
|
end
|
||||||
|
|
||||||
def escape_unicode(javascript)
|
def escape_unicode(javascript)
|
||||||
if javascript
|
if javascript
|
||||||
javascript.gsub(/\342\200\250/u, '
').gsub(/(<\/)/u, '\u003C/').html_safe
|
javascript.gsub(/\342\200\250/u, '
').gsub(/(<\/)/u, '\u003C/').html_safe
|
||||||
|
@ -100,4 +104,12 @@ module ApplicationHelper
|
||||||
def login_path
|
def login_path
|
||||||
return "#{Discourse::base_uri}/login"
|
return "#{Discourse::base_uri}/login"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mobile_view?
|
||||||
|
if session[:mobile_view]
|
||||||
|
session[:mobile_view] == '1'
|
||||||
|
else
|
||||||
|
request.user_agent =~ /Mobile|webOS/
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="<%= SiteSetting.default_locale %>">
|
<html lang="<%= SiteSetting.default_locale %>" class="<%= html_classes %>">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title><%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %></title>
|
<title><%= content_for?(:title) ? yield(:title) + ' - ' + SiteSetting.title : SiteSetting.title %></title>
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Autospec::Runner
|
||||||
|
|
||||||
watch_reload('spec/spec_helper.rb')
|
watch_reload('spec/spec_helper.rb')
|
||||||
watch_reload('config/(.*).rb')
|
watch_reload('config/(.*).rb')
|
||||||
|
watch_reload(%r{app/helpers/(.*).rb})
|
||||||
|
|
||||||
def self.run(opts={})
|
def self.run(opts={})
|
||||||
self.new.run(opts)
|
self.new.run(opts)
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue