views:

227

answers:

2

There is a helper method in my custom helper module EntriesHelper that I need to use from inside a model. I watched the Railscasts episode Helpers Outside Views and learned about ActionController::Base.helpers, but this only gives you access to the built-in helpers. I don't want to simply include EntriesHelper in the model because of namespace pollution, and because the method I want to use in EntriesHelper in turn requires other helpers, and it gets ugly very quickly.

A: 

This may solve your problem

class ApplicationController < ActionController::Base

  def help
    UsedHelperLibrary.instance
  end

  class UsedHelperLibrary
    include Singleton

    #include whatever helpers you need
    #for example 
    #include ActionView::Helpers::UrlHelper
    #include ActionView::Helpers::TagHelper
  end

...

Now in your controllers just use

help.link_to

or whatever other method you need.

I assume, similarly you will be able to write

class ActiveRecord::Base

      def help
        UsedHelperLibrary.instance
      end

      class UsedHelperLibrary
        include Singleton

        #include whatever helpers you need
        #for example 
        #include ActionView::Helpers::UrlHelper
        #include ActionView::Helpers::TagHelper
      end
end

And then similarly use helpers in controllers

UPDATE Though it seems to be correct, it doesn’t want to work. So for now my answer is following:

  1. To use helpers in controllers -- use the first code. It's working
  2. To use helpers in models add the same code to the model, that you need. (Somehow it doesn't work in case you add it to the superclass ActiveRecord::Base). In this case you will have a singleton object shared by all instances of the same model, but not shared by all models.
Sergii Vozniuk
Re update #2: Do you mean just duplicate the logic within a model method? That's not very DRY and what I was trying to avoid. Or am I misunderstanding what you're suggesting?
Jimmy Cuadra
The idea is following: use a singleton with included helpers in the model.At first I thought that adding this singleton (UsedHelperLibrary) and method (help) to the superclass (ActiveRecord::Base) will solve your problem, but I tried it and it didn't work. So for now just add the singleton and method to the model. It doesn't mean to duplicate the logic in the model. (But "yes", if you need helpers in several models, then add the singleton and method code to each of them)
Sergii Vozniuk
A: 

Although you might not want to do this, it sounds like you may have too much in EntriesHelper and you'd benefit from breaking it up? If there's a method that you want to use in your model, but you think that including the helper there would pollute the model too much, then why not take the method you need, and place it into a new class like EntriesModelHelper which you can then include safely?

Masonoise
That would solve the namespace pollution issue except that the method inside `EntriesHelper` that I need to use relies on other built-in helpers. So I would have to then include all of those as well. Like I mentioned, that gets unwieldy pretty fast.
Jimmy Cuadra