1

This question have two parts:

  1. It is possible to load, or not, a specific Helper file depending on which platform are you using?
  2. How can I override a function in other helper file?

I want to load a helper file named MobileHelper when I detect a session using a mobile. In a desktop platform I don't want to load this file.

On the other side, I have a WelcomeHelper with a show_comments function. This MobileHelper have to change the logic of this show_comments function.

By now, I made a partial solution calling the mobile version function, with other name, inside the original function if it detects that you are using a mobile.

module WelcomeHelper
  def show_comments
    return mobile_show_comments if user_agent.mobile?
    # original code
  end
end

module MobileHelper
  def mobile_show_comments
    # mobile code
  end
end

The final version I want to have is two equal named functions in two helper files and one of this files must be load only on mobile platforms

MARC.RS
  • 1,108
  • 1
  • 11
  • 15

2 Answers2

0
  1. It is possible to load a file given a request header such as the user-agent (which will sort of tell you what platform the user is on). But once a file is loaded it will remain loaded since your server remains running between requests. But, by default Rails will load all files (including helpers) when you start up the server and will remain loaded between requests. So it is not a good approach to load files on a request basis unless you manually manage the removing of modules or classes yourself: How to undefine class in Ruby?

  2. The way Rails includes helpers into views and controllers is very simple, it just adds all the methods defined in your helpers to the controller and view. You won't be able to override a method from one helper to the next because of this.

1 possible solution for your case that I see is to:

# check for the platform the user is on:
if request.user_agent.match /iphone|android/
  # call your mobile helper's method here
else
  # call default helper's method here
end

Since all your helpers are already loaded when Rails boots up you'll have to dispatch to the appropriate helper based on the current request's user_agent.

Community
  • 1
  • 1
DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
0

It's a bit dated but you might find mobile_fu helpful. Their approach is to register .mobile as a mime type and then deal with the differences between desktop browser and mobile browser in the through targeted views.

Another potential tact would be to namespace your routes so that there is one set of routes for desktop and another for mobile. When the user first accesses the site you'd redirect them to a page within the proper namespace. With this approach you could create a DesktopBaseController and MobileBaseController (each inheriting from ApplicationController) that include the appropriate helpers for desktop/mobile. The namespaced controllers could inherit from the appropriate Desktop/Mobile Base. This potentially leads to lots of decisions about how to keep the code DRY but it provides a great deal of flexibility for dealing with differences in desktop and mobile browsing (e.g., pagers may get fewer records in addition to rendering differently, etc).

AndyV
  • 3,696
  • 1
  • 19
  • 17
  • This solution would be usefull if the project wasn't practically finished. In the future I will try to use it. – MARC.RS Dec 09 '13 at 14:01