Skip to content

Instantly share code, notes, and snippets.

@mattpuchlerz
Forked from keithnorm/gist:451937
Created June 24, 2010 20:56
Show Gist options
  • Select an option

  • Save mattpuchlerz/451965 to your computer and use it in GitHub Desktop.

Select an option

Save mattpuchlerz/451965 to your computer and use it in GitHub Desktop.
module App::Controllers::MobileSupport
def self.included(base)
base.send :before_filter, :redirect_to_appropriate_site
base.send :helper_method, :iphone_request?
base.send :helper_method, :touch_request?
end
def redirect_to_appropriate_site
if on_touch_site? and requesting_full_site?
redirect_to 'www...'
elsif on_full_site? and requesting_touch_site?
redirect_to 'touch...'
end
end
def on_touch_site?
request.subdomains.first == 'touch'
end
def on_full_site?
request.subdomains.first == 'www'
end
def android_request?
(request.env["HTTP_USER_AGENT"] || "") =~ /(Android\/.+Safari)/
end
def iphone_request?
(request.env["HTTP_USER_AGENT"] || "") =~ /(Mobile\/.+Safari)/
end
def palm_pre_request?
(request.env["HTTP_USER_AGENT"] || "") =~ /(webOS\/.+Safari)/
end
def requesting_touch_site?
params[:mobile] == 'true' or
( params[:mobile].nil? && cookies[:mobile] == 'true' ) or
android_request? or
iphone_request? or
palm_pre_request?
end
def requesting_full_site?
params[:mobile] == 'false'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment