3

Is it possible to include / use Application Helper methods inside of an config/initializers/browser_blocker.rb?

I am using the browser gem to detect and block older non modern browsers.

Rails.configuration.middleware.use Browser::Middleware do
    include ApplicationHelper
    redirect_to :controller => 'error', :action => 'browser-upgrade-required' if browser_is_not_supported
end

Helper method I am currently working with:

  # test browser version
  def browser_is_not_supported
    return true unless browser.modern?
    return true if browser.chrome? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_GOOGLE'].to_i
    return true if browser.firefox? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_FIREFOX'].to_i
    return true if browser.safari? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_SAFARI'].to_i
    return true if browser.opera? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_OPERA'].to_i
    return true if browser.ie? && browser.version.to_i < ENV['BROWSER_BASE_VERSION_MSFT'].to_i
  end
2
  • why do you need ApplicationHelper functionality there? It would be best to just use the specific modules instead, hard to give an example since your example is not using any helper methods Commented Apr 12, 2014 at 20:59
  • because I was hoping to use the browser_is_not_supported in other parts of the application? Is this possible? Commented Apr 12, 2014 at 21:00

2 Answers 2

7

This is one way to do it:

# lib/browser_util.rb
module BrowserUtil
  def self.supported?(browser)
    # your code ...
  end
end

and wrap that from ApplicationHelper for use in views

module ApplicationHelper
  def is_browser_supported?
    BrowserUtil.supported?(browser)
  end
end

in middleware

Rails.configuration.middleware.use Browser::Middleware do
  unless BrowserUtil.supported?(browser)
    redirect_to :controller => 'error', :action => 'browser-upgrade-required' 
  end
end

UPDATE: it does not need to be in a separate module (BrowserUtil)

module ApplicationHelper
  def self.foo
    "FOO"
  end

  def foo
    ApplicationHelper.foo
  end
end

in middleware use

ApplicationHelper.foo

in views it would use the included method

foo
Sign up to request clarification or add additional context in comments.

3 Comments

I like this idea a bit better based on your recommendations, but now I am getting "undefined local variable or method `browser' for ApplicationHelper:Module"
ahh, I forgot to send (browser), I think I have this now, testing atm
FIXED! THANKS! I liked your points about overriding methods. That could have been dangers, thank you for your help.
0

Ofcourse you can but that's bad idea, i agree with that that logic supposed to somewhere in app but sometime you have to deal with it. I am saying this because you can block the request before old_browser request gets it and load rails stack.

Anyway, this is how can you can do it

Rails.configuration.middleware.use Browser::Middleware do
  self.class.send(:include,ApplicationHelper)
   redirect_to :controller => 'error', :action => 'browser-upgrade-required' unless    browser_is_supported?
end

3 Comments

Like @Paritosh said - "but that's bad idea": just to be clear you are mixing ALL of the methods from your ApplicationHelper into this class: github.com/fnando/browser/blob/master/lib/browser/middleware.rb , so if you were to add any methods with the same names to ApplicationHelper your new method will overwrite the browser version - these types of defects are very hard to track down when they occur
so, would you rather go for duplicating the method in 2 places? I am not opposed to switching it to your version @house9 if you feel that would be better in the long term.
its not really duplicated, but 'callable' via class method or instance method - I personally would go that route instead of using include in this specific case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.