mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Detect whether to use mobile view. Session var mobile_view can override automatic detection.
This commit is contained in:
parent
009dec833f
commit
9efa29e688
5 changed files with 46 additions and 1 deletions
|
@ -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']
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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)
|
||||
|
|
27
spec/helpers/application_helper_spec.rb
Normal file
27
spec/helpers/application_helper_spec.rb
Normal 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
|
Loading…
Reference in a new issue