Sha256: 889c18eea5d328b73cfa775bcbe0943c4c6a7cac0a192ac87cf3f92ab003b545

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'ruhl/rails/active_record_presenter'

class RuhlPresenter
  include ActiveRecordPresenter
  include FormHelper
  
  attr_reader :presentee, :context
  
  def initialize(obj, context)
    @presentee = obj
    @context = context
    define_paths(obj.class.name.underscore.downcase)
  end
    
  def method_missing(name, *args)
    if presentee.respond_to?(name)
      # Pass presenter method call to model so you don't have to
      # redefine every model method in the presenter class.
      presentee.send(name, *args)
    elsif context.respond_to?(name)
      # Instead of saying context.link_to('Some site', some_path)
      # can just use link_to
      context.send(name, *args)
    end
  end

  # Extend scope of respond_to? to model.
  def respond_to?(name)  
    if super
      true
    else
      presentee.respond_to?(name)
    end
  end  
end

module ActionController
  class Base    

    protected

    def present(object_sym, action_sym)
      render  :template => "#{object_sym.to_s.pluralize}/#{action_sym}", 
        :locals => {:object => presenter_for( instance_variable_get("@#{object_sym}") )}    
    end

    def presenter_for(obj)
      eval("#{obj.class.name}Presenter").new(obj, @template)
    end
    
    helper_method :presenter_for   
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruhl-0.15.0 lib/ruhl/rails/ruhl_presenter.rb