Sha256: 27fa17bc4bf823fa86a7aebd2ffdf9f9e66884a5d2b17c3885edc9c30bd79e0f

Contents?: true

Size: 1.87 KB

Versions: 9

Compression:

Stored size: 1.87 KB

Contents

require 'ruhl/rails/active_record'
require 'ruhl/rails/helper'

module Ruhl
  module Rails
    class Presenter
      include Ruhl::Rails::ActiveRecord
      include Ruhl::Rails::Helper
  
      attr_reader :presentee, :context
  
      def initialize(context, obj = nil)
        @context = context

        # May only want to use the form helper
        if obj
          @presentee = obj
          define_paths(obj.class.name.underscore.downcase)
        end
      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
  end
end

module ActionController
  class Base    

    protected

    def present(action_sym = action_name, object = nil)
      object_sym = object || controller_name.singularize

      render  :template => "#{controller_name}/#{action_sym}", 
        :locals => {:object => presenter_for(object_sym) }    
    end

    def presenter_for(object)

      if object.is_a?(Symbol) || object.is_a?(String)
        # Set instance variable if it exists
        if instance_variables.include?("@#{object}")
          obj = instance_variable_get("@#{object}")
        end
        name = object.to_s.camelize
      else
        name = object.class.name.camelize
        obj = object
      end

      Object.const_get("#{name}Presenter").new(@template, obj)
    end

    helper_method :presenter_for   
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruhl-0.26.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.25.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.24.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.23.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.22.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.21.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.20.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.19.0 lib/ruhl/rails/ruhl_presenter.rb
ruhl-0.18.0 lib/ruhl/rails/ruhl_presenter.rb