Module: ActionController::MobileFu::InstanceMethods

Defined in:
lib/mobile-fu.rb

Instance Method Summary collapse

Instance Method Details

#force_mobile_formatObject

Forces the request format to be :mobile



109
110
111
112
113
114
# File 'lib/mobile-fu.rb', line 109

def force_mobile_format
  unless request.xhr?
    request.format = :mobile
    session[:mobile_view] = true if session[:mobile_view].nil?
  end
end

#force_tablet_formatObject

Forces the request format to be :tablet



117
118
119
120
121
122
# File 'lib/mobile-fu.rb', line 117

def force_tablet_format
  unless request.xhr?
    request.format = :tablet
    session[:tablet_view] = true if session[:tablet_view].nil?
  end
end

#in_mobile_view?Boolean

Returns either true or false depending on whether or not the format of the request is either :mobile or not.

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/mobile-fu.rb', line 141

def in_mobile_view?
  return false unless request.format
  request.format.to_sym == :mobile
end

#in_tablet_view?Boolean

Returns either true or false depending on whether or not the format of the request is either :tablet or not.

Returns:

  • (Boolean)


149
150
151
152
# File 'lib/mobile-fu.rb', line 149

def in_tablet_view?
  return false unless request.format
  request.format.to_sym == :tablet
end

#is_device?(type) ⇒ Boolean

Can check for a specific user agent e.g., is_device?(‘iphone’) or is_device?(‘mobileexplorer’)

Returns:

  • (Boolean)


172
173
174
# File 'lib/mobile-fu.rb', line 172

def is_device?(type)
  request.user_agent.to_s.downcase.include? type.to_s.downcase
end

#is_mobile_device?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/mobile-fu.rb', line 161

def is_mobile_device?
  !is_tablet_device? && !!mobile_device
end

#is_tablet_device?Boolean

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.

Returns:

  • (Boolean)


157
158
159
# File 'lib/mobile-fu.rb', line 157

def is_tablet_device?
  ::MobileFu::Tablet.is_a_tablet_device? request.user_agent
end

#mobile_action?Boolean

Returns true if current action is supposed to use mobile format See #has_mobile_fu_for

Returns:

  • (Boolean)


178
179
180
181
182
183
184
# File 'lib/mobile-fu.rb', line 178

def mobile_action?
  if self.class.instance_variable_get("@mobile_include_actions").nil? #Now we know we dont have any includes, maybe excludes?
    return !mobile_exempt?
  else
    self.class.instance_variable_get("@mobile_include_actions").try(:include?, params[:action].try(:to_sym))
  end
end

#mobile_deviceObject



165
166
167
# File 'lib/mobile-fu.rb', line 165

def mobile_device
  request.headers['X_MOBILE_DEVICE']
end

#mobile_exempt?Boolean

Returns true if current action isn’t supposed to use mobile format See #has_no_mobile_fu_for

Returns:

  • (Boolean)


189
190
191
# File 'lib/mobile-fu.rb', line 189

def mobile_exempt?
  self.class.instance_variable_get("@mobile_exempt_actions").try(:include?, params[:action].try(:to_sym))
end

#set_mobile_formatObject

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 or ‘Tablet’ view.



128
129
130
131
132
133
134
135
136
# File 'lib/mobile-fu.rb', line 128

def set_mobile_format
  if request.format.html? && mobile_action? && is_mobile_device? && !request.xhr?
    request.format = :mobile unless session[:mobile_view] == false
    session[:mobile_view] = true if session[:mobile_view].nil?
  elsif request.format.html? && mobile_action? && is_tablet_device? && !request.xhr?
    request.format = :tablet unless session[:tablet_view] == false
    session[:tablet_view] = true if session[:tablet_view].nil?
  end
end

#set_request_format(force_mobile = false) ⇒ Object Also known as: set_device_type



103
104
105
# File 'lib/mobile-fu.rb', line 103

def set_request_format(force_mobile = false)
  force_mobile ? force_mobile_format : set_mobile_format
end