-
Notifications
You must be signed in to change notification settings - Fork 195
/
mobile_fu.rb
108 lines (85 loc) · 3.48 KB
/
mobile_fu.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module ActionController
module MobileFu
# These are various strings that can be found in mobile devices. Please feel free
# to add on to this list.
MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' +
'mobile'
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
# Add this to one of your controllers to use MobileFu.
#
# class ApplicationController < ActionController::Base
# has_mobile_fu
# end
#
# You can also pass any of the options available to before_filter (:only, :exclude)
#
# class WelcomeController < ActionController::Base
# has_mobile_fu :only => :index
# end
#
# You can also force mobile mode by passing in 'true' or :test_mode => true
#
# class ApplicationController < ActionController::Base
# has_mobile_fu(true)
# end
def has_mobile_fu(options = {})
include ActionController::MobileFu::InstanceMethods
if options.respond_to?(:delete) && options.delete( :test_mode ) || options === true
before_filter :force_mobile_format, options
else
before_filter :set_mobile_format, options
end
helper_method :is_mobile_device?
helper_method :in_mobile_view?
helper_method :is_device?
end
def is_mobile_device?
@@is_mobile_device
end
def in_mobile_view?
@@in_mobile_view
end
def is_device?(type)
@@is_device
end
end
module InstanceMethods
# Forces the request format to be :mobile
def force_mobile_format
request.format = :mobile
session[:mobile_view] = true if session[:mobile_view].nil?
end
# Determines the request format based on whether the device is mobile or if
# the user has opted to use either the 'Standard' view or 'Mobile' view.
def set_mobile_format
if is_mobile_device? && !request.xhr?
request.format = session[:mobile_view] == false ? :html : :mobile
session[:mobile_view] = true if session[:mobile_view].nil?
end
end
# Returns either true or false depending on whether or not the format of the
# request is either :mobile or not.
def in_mobile_view?
request.format.to_sym == :mobile
end
# Returns either true or false depending on whether or not the user agent of
# the device making the request is matched to a device in our regex.
def is_mobile_device?
request.user_agent.to_s.downcase =~ Regexp.new(ActionController::MobileFu::MOBILE_USER_AGENTS)
end
# Can check for a specific user agent
# e.g., is_device?('iphone') or is_device?('mobileexplorer')
def is_device?(type)
request.user_agent.to_s.downcase.include?(type.to_s.downcase)
end
end
end
end
ActionController::Base.send(:include, ActionController::MobileFu)