Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Created October 14, 2012 17:28
Show Gist options
  • Save tomas-stefano/3889255 to your computer and use it in GitHub Desktop.
Save tomas-stefano/3889255 to your computer and use it in GitHub Desktop.
Presenter
require 'delegate'
require 'active_support/core_ext/object'
class Presenter < SimpleDelegator
# include Rails router methods to the presenter
#
include Rails.application.routes.url_helpers
delegate :render, :image_tag, :link_to, to: :helpers
# This method will return a presenter for each item of collection.
#
# users = UserPresenter.collect(User.all)
#
def self.collect(collection)
collection.collect { |item| new(item) }
end
# Returns the target object that was initialized.
#
# Presenter.new(nil).target => nil
# Presenter.new(User.new).target => <#User>
#
def target
__getobj__
end
# This method is to use all the helpers methods available on the view
#
# helper.render 'videos/new'
# helper.link_to "A Link", routes.video_path
#
def helpers
ApplicationController.helpers
end
alias_method :h, :helpers
end
## This last line works properly
ApplicationController.helpers.view_paths.unshift("app/views")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment