Module: Mobvious::Rails::Controller

Defined in:
lib/mobvious/rails/controller.rb

Overview

A module holding Rails controller extensions.

Just put include Mobvious::Rails::Controller into your ApplicationController.

Instance Method Summary collapse

Instance Method Details

#device_typeSymbol

Returns device type for current request.

Returns:

  • (Symbol)

    device type for current request



36
37
38
# File 'lib/mobvious/rails/controller.rb', line 36

def device_type
  request.env['mobvious.device_type']
end

#for_device_type(*wanted_device_types) { ... } ⇒ Object

Executes a block of code only when a request was made by a given client device type.

Example:

for_device_type :mobile do
  logger.info "This gets executed only for mobile phones."
end

Parameters:

  • wanted_device_type

    a symbol identifying a device type

Yields:

  • a block to execute if the specified device type matches device type of the current request

Returns:

  • value that the yielded block returns, or nil if it didn't get executed



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mobvious/rails/controller.rb', line 19

def for_device_type(*wanted_device_types)
  unless block_given?
    raise ArgumentError, "Device helper takes a block of code to execute for given device."
  end

  unless device_type
    raise "Mobvious device type not set. Did you add the Mobvious rack middleware?"
  end

  if wanted_device_types.include? device_type
    yield
  else
    nil
  end
end