Module: Homura::MobileDetection

Defined in:
lib/homura/mobile_detection.rb

Constant Summary collapse

MOBILE_USER_AGENTS =
/(IEMobile|Windows CE|NetFront|PlayStation|
PLAYSTATION|like Mac OS X|MIDP|UP\.Browser|
Symbian|Nintendo|Android)/x

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/homura/mobile_detection.rb', line 22

def self.included(base)
  base.class_eval do
    before_filter :set_mobile_preferences
    before_filter :prepend_view_path_if_mobile

    helper_method :mobile_request?
    helper_method :mobile_browser?
  end
end

Instance Method Details

#mobile_browser?Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/homura/mobile_detection.rb', line 61

def mobile_browser?
  request.env['HTTP_USER_AGENT'] &&
    request.env['HTTP_USER_AGENT'][MOBILE_USER_AGENTS]
end

#mobile_request?Boolean

Returns:

  • (Boolean)


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

def mobile_request?
  cookies['mobile'] == '1'
end

#prepend_view_path_if_mobileObject



45
46
47
48
49
# File 'lib/homura/mobile_detection.rb', line 45

def prepend_view_path_if_mobile
  if mobile_request?
    prepend_view_path Rails.root + 'app/mobile_views'
  end
end

#redirect_to_current_page_without_mobile_paramObject



51
52
53
54
55
# File 'lib/homura/mobile_detection.rb', line 51

def redirect_to_current_page_without_mobile_param
  path = request.fullpath.gsub(/mobile=[01]&?/, '').chomp('?')
  full_url = request.protocol + request.host_with_port + path
  redirect_to full_url
end

#set_mobile_preferencesObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/homura/mobile_detection.rb', line 32

def set_mobile_preferences
  case params[:mobile]
  when '1'
    cookies['mobile'] = '1'
    redirect_to_current_page_without_mobile_param
  when '0'
    cookies['mobile'] = '0'
    redirect_to_current_page_without_mobile_param
  else
    cookies['mobile'] ||= mobile_browser? ? '1' : '0'
  end
end