Module: MobileSiteController

Defined in:
lib/mobile_site_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



88
89
90
91
92
93
# File 'lib/mobile_site_controller.rb', line 88

def self.included(base) #:nodoc:
  base.class_eval do
    before_filter :redirect_if_mobile
    alias_method_chain :process_page, :mobile
  end
end

Instance Method Details

#app?Boolean

Returns true if the requested host matches the defined app host (or in the absence of such a definition, if the host begins app.)

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/mobile_site_controller.rb', line 23

def app?
  app_host = Radiant.config['app.host']
  match = unless app_host.blank?
    request.host == app_host
  else
    request.host =~ /^app\./
  end
  !!match
end

#mobile?Boolean

Returns true if the requested host matches the defined mobile host (or in the absence of such a definition, if the host begins m.)

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'lib/mobile_site_controller.rb', line 10

def mobile?
  mobile_host = Radiant.config['mobile.host']
  match = unless mobile_host.blank?
    request.host == mobile_host
  else
    request.host =~ /^m\./
  end
  !!match
end

#mobile_device?Boolean

Returns true if the request comes from a mobile device (based on the supplied user-agent string)

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/mobile_site_controller.rb', line 35

def mobile_device?
  ua = request.user_agent.to_s.downcase
  ua =~ Regexp.new(positive_markers.join('|')) && ua =~ Regexp.new(required_markers.join('|')) && ua !~ Regexp.new(negative_markers.join('|'))
end

#negative_markersObject

Returns the list of string fragments whose presence indicates that this is a tablet and should not be treated as a mobile device. eg. Ipad UA includes ‘mobile’ (which in this context is a false positive) and also ‘ipad’ (which allows us to eliminate it).



50
51
52
# File 'lib/mobile_site_controller.rb', line 50

def negative_markers
  @negative_ua_markers ||= Radiant.config['mobile.ua.negative'].split(/,\s*/)
end

#positive_markersObject

Returns the list of string fragments whose presence indicates that this is a mobile device. The default list (and the approach) is borrowed from mobile-fu: github.com/brendanlim/mobile-fu/blob/master/lib/mobile_fu.rb



43
44
45
# File 'lib/mobile_site_controller.rb', line 43

def positive_markers
  @positive_ua_markers ||= Radiant.config['mobile.ua.positive'].split(/,\s*/)
end

#process_page_with_mobile(page) ⇒ Object

Extends the process_page method to place a ‘mobile’ and ‘app’ flags in the page-rendering context to support presentation choices in radius tags.



64
65
66
67
68
# File 'lib/mobile_site_controller.rb', line 64

def process_page_with_mobile(page)
  page.app = app?
  page.mobile = app? || mobile?
  process_page_without_mobile(page)
end

#redirect_if_mobileObject

Issues a redirect to the mobile site if this request comes from a mobile device, and if the mobile site is configured, and if the request does not have a ‘nomobile’ parameter. If there is a ‘nomobile’ parameter, a session marker is placed to indicate that the browser should not be redirected in future.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mobile_site_controller.rb', line 75

def redirect_if_mobile
  if params['url'] =~ /\?mobile/
    session[:nomobile] = false
  elsif params['url'] =~ /\?nomobile/
    session[:nomobile] = true
  end
  if @config['mobile.redirect?'] && @config['mobile.host'] && !session[:nomobile] && !mobile? && mobile_device?
    uri = request.path_parameters['url']
    uri = uri.join('/') if uri.respond_to? :join
    redirect_to request.protocol + @config['mobile.host'] + uri
  end
end

#required_markersObject

Returns the list of string fragments that must be present or this is not a mobile device. eg. android tablet UA includes ‘android’ but not ‘mobile’, so we require the mobile flag.



57
58
59
# File 'lib/mobile_site_controller.rb', line 57

def required_markers
  @required_ua_markers ||= Radiant.config['mobile.ua.required'].split(/,\s*/)
end