简体   繁体   中英

Ruby/Rails - Presenter class class and instance methods

I have a base presenter that I'm using for mainly JSON and CSV type presenting. I was posed with an issue that I wasn't sure how to handle. If I have a PhotoPresenter with a class method I can't access the instance method help so I had to create the self.help method but I think this sucks..any ideas on how to avoid this overlap with instance and class level methods. Probably just tired and being stupid about it

class Presenter
  include Rails.application.routes.url_helpers

  def self.as_collection(collection)
    collection.collect{|object| self.new(object)}
  end

  def help
    Helper.instance
  end

  def self.help
    Helper.instance
  end

  class Helper
    include Singleton
    include ActionView::Helpers::TextHelper
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::UrlHelper
  end

end

You could avoid some duplication as follows:

def self.help
  Helper.instance
end

def help
  self.class.help
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM