Created
February 16, 2012 07:26
-
-
Save melchoy/1842958 to your computer and use it in GitHub Desktop.
Mobile Subdomains in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MobileSite | |
def self.included(base) | |
base.class_eval do | |
# Uncomment for JQM Integration | |
#rescue_from ActionView::MissingTemplate, :with => :view_unavailable | |
before_filter :set_mobile_preferences | |
before_filter :redirect_to_mobile_if_applicable | |
before_filter :set_mobile_view_path | |
private | |
def set_mobile_preferences | |
if params[:mobile_site] | |
cookies.delete(:prefer_full_site) | |
elsif params[:full_site] | |
cookies.permanent[:prefer_full_site] = 1 | |
redirect_to_full_site if mobile_request? | |
end | |
end | |
# Uncomment for Responsive CSS | |
def set_mobile_view_path | |
if mobile_request? | |
# Add Mobile view paths as overrides for mobile devices | |
mobile_view_paths = ["#{Rails.root}/app/views_mobile"] | |
Dir.glob("#{Rails.root}/vendor/plugins/*/app/views_mobile").map{ |f| mobile_view_paths << File.absolute_path(f) } | |
prepend_view_path(mobile_view_paths) | |
end | |
end | |
# Uncomment for JQM Integration | |
#def set_mobile_view_path | |
# if mobile_request? | |
# # Clear the default view paths | |
# lookup_context.view_paths = [] | |
# | |
# # Add Mobile view paths | |
# mobile_view_paths = ["#{Rails.root}/app/views_mobile"] | |
# Dir.glob("#{Rails.root}/vendor/plugins/*/app/views_mobile").map{ |f| mobile_view_paths << File.absolute_path(f) } | |
# prepend_view_path(mobile_view_paths) | |
# end | |
# | |
# # Add Shared view paths for mobile & full site | |
# shared_view_paths = ["#{Rails.root}/app/views_shared"] | |
# Dir.glob("#{Rails.root}/vendor/plugins/*/app/views_shared").map{ |f| shared_view_paths << File.absolute_path(f) } | |
# prepend_view_path(shared_view_paths) | |
#end | |
# Uncomment for JQM Integration | |
#def view_unavailable | |
# if mobile_request? | |
# render :template => 'error_pages/unavailable', :layout => 'application' | |
# end | |
#end | |
def redirect_to_full_site | |
redirect_to request.protocol + request.host_with_port.gsub(/^m\./, '') + | |
request.fullpath and return | |
end | |
def redirect_to_mobile_if_applicable | |
unless mobile_request? || cookies[:prefer_full_site] || !mobile_device? | |
redirect_to request.protocol + "m." + request.host_with_port.gsub(/^www\./, '') + | |
request.fullpath and return | |
end | |
end | |
def mobile_request? | |
request.subdomains.first == 'm' | |
end | |
helper_method :mobile_request? | |
def mobile_device? | |
request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod|Android|Mobile|webOS)/] | |
end | |
helper_method :mobile_device? | |
end | |
end | |
end | |
class ActionController::Base | |
include MobileSite | |
end | |
# Based on http://erniemiller.org/2011/01/05/mobile-devices-and-rails-maintaining-your-sanity/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment