Skip to content

Instantly share code, notes, and snippets.

@norbajunior
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norbajunior/1214673c2cb3155677c1 to your computer and use it in GitHub Desktop.
Save norbajunior/1214673c2cb3155677c1 to your computer and use it in GitHub Desktop.
# app/presenters/product_presenter.rb
class ProductPresenter < Presenter
def description
# ...
end
end
# app/presenters/person_presenter.rb
class PersonPresenter < Presenter
def name
"Caro #{name}"
end
end
# app/presenters/presenter.rb
class Presenter < SimpleDelegator
attr_reader :object
def initialize(object)
@object = object
__setobj__ @object
end
def eql?(target)
target == self || object.eql?(target)
end
private
def helpers
ApplicationController.helpers
end
def routes
ApplicationController.routes
end
end
# ========= Controllers =========== #
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id]).present
end
end
# app/controllers/people_controller.rb
class PeopleController < ApplicationController
def show
@person = Person.find(params[:id]).present
end
end
# config/initializers/activerecord.rb
ActiveRecord::Base.class_eval do
def present
"#{self.class.to_s}Presenter".constantize.new(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment