module Bourgeois class Presenter < ::SimpleDelegator # Return the original delegated object attr_reader :object def initialize(object, view = nil) @view = view super(@object = object) end # Return a String representation of the presenter + the original object def inspect "#<#{self.class} object=#{@object.inspect}>" end # We need to explicitely define this method because it's not # catched by the delegator def kind_of?(mod) @object.kind_of?(mod) end # ActionView::Helpers::FormBuilder needs this def self.model_name klass.model_name end # ActionView::Helpers::FormBuilder needs this too def self.human_attribute_name(*args) klass.human_attribute_name(*args) end # Declare a new block helper method # # @example # class UserPresenter < Bourgeois::Presenter # helper :with_profile, if: -> { profile.present? } # end # # presenter = UserPresenter.new(User.new(profile: 'Foo')) # presenter.with_profile do # puts 'User has a profile:' # puts presenter.profile # end def self.helper(name, opts = {}) define_method(name) do |&block| execute_helper(block, opts) end end # Wrap a resource or a collection into its related presenter # # @example # present User.new(name: 'Remi') do |user| # puts user.inspect # => #