Module: MobileView::ForcedSwitching::ControllerAdditions

Included in:
ControllerAdditions
Defined in:
lib/mobile_view/forced_switching.rb

Overview

These controller additions are helpers that you can use in your controller to test if currently it is forcely (manually) switched to mobile version, or non-mobile version.

By force-switching to either version, MobileView will take it at the top precedence, and ignore the ascending methods (e.g. by User-Agent).

Force-switching should happen before has_mobile_view is invoked. See has_mobile_view‘s example.

They will be automatically included into the controller that includes ControllerAdditions. You don’t need to include this module manually.

TODO: Unit Testing

Examples

Use as before filters

class AppliactionController < ActionController::Base
  before_filter :force_mobile!, :if => :ie6?
  before_filter :force_non_mobile!, :if => :ipad?
  has_mobile_view
end

Manual Mobile Switching

A user can manually switch to mobile version or non-mobile version by specifying ‘_mobile_view` parameter.

For example:

  • ?_mobile_view=yes switches to mobile view.

  • ?_mobile_view=no switches to non-mobile view.

  • ?_mobile_view=auto don’t force switch; determine by User-Agent.

To accomplish the logic above:

class AppliactionController < ActionController::Base
  before_filter :manual_mobile_switching

  has_mobile_view

  protected

  # Detects `_mobile_view` parameter.
  #
  # If it is 'yes', force turn on mobile view by setting cookie mobile=1;
  # if it is 'no', force turn off mobile view by setting cookie mobile=0;
  # if it is 'auto', remove mobile cookie and fallback to User-Agent mode;
  # otherwise, no effect.
  def mobile_switching
    case params[:_mobile_view]
    when 'yes'
      force_mobile!
    when 'no'
      force_non_mobile!
    when 'auto'
      dismiss_mobile_forcing!
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#dismiss_mobile_forcing!Object (protected)

Dismiss any forced switching, and fallback to other methods like User-Agent sniffing



87
88
89
# File 'lib/mobile_view/forced_switching.rb', line 87

def dismiss_mobile_forcing!
  cookies.delete COOKIE_NAME
end

#force_mobile!Object (protected)

Force switch to mobile view.



77
78
79
# File 'lib/mobile_view/forced_switching.rb', line 77

def force_mobile!
  cookies[COOKIE_NAME] = FORCE_MOBILE_VALUE
end

#force_non_mobile!Object (protected)

Force switch to non-mobile (default) view.



82
83
84
# File 'lib/mobile_view/forced_switching.rb', line 82

def force_non_mobile!
  cookies[COOKIE_NAME] = FORCE_NON_MOBILE_VALUE
end

#forced_mobile?Boolean (protected)

Test if currently forced to mobile view.

Returns:

  • (Boolean)


97
98
99
# File 'lib/mobile_view/forced_switching.rb', line 97

def forced_mobile?
  cookies[COOKIE_NAME] == FORCE_MOBILE_VALUE
end

#forced_non_mobile?Boolean (protected)

Test if currently forced to non-mobile view.

Returns:

  • (Boolean)


102
103
104
# File 'lib/mobile_view/forced_switching.rb', line 102

def forced_non_mobile?
  cookies[COOKIE_NAME] == FORCE_NON_MOBILE_VALUE
end

#mobile_forcing?Boolean (protected)

Test if currently forced to mobile or non-mobile view.

Returns:

  • (Boolean)


92
93
94
# File 'lib/mobile_view/forced_switching.rb', line 92

def mobile_forcing?
  forced_mobile? || forced_non_mobile?
end