开发者

Include a Class in another model / class / lib

I need to use function "image_path" in my lib class. I tried this (and couple of other variations):

class CustomHelpers::Base
  include ActionView::Helpers::AssetTagHelper
  def self.image_url(source)
    abs_path = image_path(source)
    unless abs_path =~ /^http/
      abs_path = "#{request.protocol}#{request.host_with_开发者_运维知识库port}#{abs_path}"
    end
    abs_path
  end
end

But it didn't work. Am I doing it right?

Another question is, how do I find the right class to include? For example if I look at this module: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html is there a rule of thumb how to include that module in a model / library / class / anything else ?


You include a module, so all methods on it are in InstanceMethods.

But you try call it by ClassMethods.

So try extend ActionView::Helpers::AssetTagHelper, not include it


Best would be to put all such classes under some directories and include such paths in autoload_paths. So if had to include lib,presenters and jobs directories then this is how I would autoload them in my app.

config/application.rb

 config.autoload_paths += ["#{config.root}/lib", './app/jobs', './app/presenters']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜