Report abuse

Presenter


			
class Presenter
  extend Forwardable

  class << self
    def composed_of(*args)
      options = args.pop
      model = options[:from]
      attributes = args.map{|attr| [attr.to_s, "#{attr}="]}.flatten

      include_model( model )
      def_delegators( model, *attributes )
    end

    def include_model( model )
      attr_accessor model
      include_errors_from model
    end
  end

  def initialize(params=nil)
    params.each_pair do |attribute, value| 
      self.send :"#{attribute}=", value
    end unless params.nil?
  end

  def self.delegate_attributes(*args)
    options = args.pop
    attributes = args.map{|attr| [attr.to_s, "#{attr}="]}.flatten
    def_delegators( options[:to], *attributes)
  end

end

ListingPresenter


			
require 'validatable'
class ListingPresenter < Presenter
  extend Forwardable
  include Validatable

  composed_of :title, :description, :price, :bedroom, :bathroom, :square_feet, :category, :mls, :url, 
              :url_text, :vtour, :directions, :year_built, :builder, :subdivision, :lot_size, :school_district,
              :listing_agency, :from => :listing  

  composed_of :street_address, :city, :state, :postal_code, :from => :location

  def initialize_with_delegates(params=nil)
    @listing  = Listing.new
    @location = Location.new
    initialize_without_delegates(params)
  end
  alias_method_chain :initialize, :delegates

  def save
    self.valid?
    Listing.transaction do
      listing.location = location
      listing.save
    end
  end

end