pachi
9/22/2014 - 11:32 AM

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/

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