Last active
August 29, 2015 14:15
-
-
Save norbajunior/1214673c2cb3155677c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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