Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Created December 19, 2011 00:45
Show Gist options
  • Save tomas-stefano/1494965 to your computer and use it in GitHub Desktop.
Save tomas-stefano/1494965 to your computer and use it in GitHub Desktop.
Presenter on Rails
require 'delegate'
module Presenter
def presenter
presenter = "#{self.class.name}Presenter".constantize
presenter.new(self)
end
# Example of usage:
#
# class PersonPresenter < Presenter::Proxy
# def full_name
# "#{last_name}, #{first_name}"
# end
# end
#
# person = Person.new(:first_name => "Gabriel", :last_name => "Sobrinho")
# person.presenter.full_name #=> "Sobrinho, Gabriel"
class Proxy < SimpleDelegator
end
end
class ActiveRecord::Base
include Presenter
end
@tomas-stefano
Copy link
Author

IMHO, don't need create this proxy object. Presenter::Proxy Inherits from SimpleDelegator becomes more easy and clean. See my fork for more details. I think is better if you like inherits from SimpleDelegator directly in presenter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment