Skip to content

Commit

Permalink
BUGFIX: mobile ui was being cached for anon views
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Jan 9, 2014
1 parent e8afe87 commit 177983a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
12 changes: 4 additions & 8 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_dependency 'unread'
require_dependency 'age_words'
require_dependency 'configurable_urls'
require_dependency 'mobile_detection'

module ApplicationHelper
include CurrentUser
Expand Down Expand Up @@ -109,21 +110,16 @@ def login_path
end

def mobile_view?
return false unless SiteSetting.enable_mobile_theme
if session[:mobile_view]
session[:mobile_view] == '1'
else
mobile_device?
end
MobileDetection.resolve_mobile_view!(request.user_agent,params,session)
end

def mobile_device?
# TODO: this is dumb. user agent matching is a doomed approach. a better solution is coming.
request.user_agent =~ /Mobile|webOS|Nexus 7/ and !(request.user_agent =~ /iPad/)
MobileDetection.mobile_device?(request.user_agent)
end

def customization_disabled?
controller.class.name.split("::").first == "Admin" || session[:disable_customization]
end


end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
end

if !ENV['DISCOURSE_DISABLE_ANON_CACHE'] && enabled
Rails.configuration.middleware.insert 0, Middleware::AnonymousCache
# in an ideal world this is position 0, but mobile detection uses ... session and request and params
Rails.configuration.middleware.insert_after ActionDispatch::ParamsParser, Middleware::AnonymousCache
end

15 changes: 14 additions & 1 deletion lib/middleware/anonymous_cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_dependency "mobile_detection"

module Middleware
class AnonymousCache

Expand All @@ -10,8 +12,19 @@ def initialize(env)
@env = env
end

def is_mobile?
@is_mobile ||=
begin
session = @env["rack.session"]
params = ActionDispatch::Request.new(@env).params
user_agent = @env["HTTP_USER_AGENT"]

MobileDetection.resolve_mobile_view!(user_agent,params,session) ? :true : :false
end == :true
end

def cache_key
@cache_key ||= "ANON_CACHE_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}"
@cache_key ||= "ANON_CACHE_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}"
end

def cache_key_body
Expand Down
19 changes: 19 additions & 0 deletions lib/mobile_detection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module MobileDetection
def self.mobile_device?(user_agent)
# TODO: this is dumb. user agent matching is a doomed approach. a better solution is coming.
user_agent =~ /Mobile|webOS|Nexus 7/ && !(user_agent =~ /iPad/)
end

# we need this as a reusable chunk that is called from the cache
def self.resolve_mobile_view!(user_agent, params, session)
return false unless SiteSetting.enable_mobile_theme

session[:mobile_view] = params[:mobile_view] if params.has_key?(:mobile_view)

if session[:mobile_view]
session[:mobile_view] == '1'
else
mobile_device?(user_agent)
end
end
end

0 comments on commit 177983a

Please sign in to comment.