Skip to content

Instantly share code, notes, and snippets.

@pachisaez
Last active August 29, 2015 14:06
Show Gist options
  • Save pachisaez/ec51afc90bdca9be10fb to your computer and use it in GitHub Desktop.
Save pachisaez/ec51afc90bdca9be10fb to your computer and use it in GitHub Desktop.
Detecting the device that is accessing the app so you can adapt views and behavior to it, via the Mobvious plugin (https://github.com/jistr/mobvious). Rendering .mobile files only if the device is a smartphone. session[:device] will contain "desktop", "tablet" or "mobile".
class ApplicationController < ActionController::Base
before_filter :prepare_for_mobile
# we'll render *.mobile.erb files instead of *.html.erb files when the
# device session variable is set to "mobile"
def prepare_for_mobile
session[:device] = params[:device] if params[:device]
request.format = :mobile if mobile_device?
end
def mobile_device?
unless session[:device]
strategy = Mobvious::Strategies::MobileESP.new(:mobile_tablet_desktop)
session[:device] = strategy.get_device_type(request).to_s
end
session[:device] == "mobile"
end
helper_method :mobile_device?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment