Sha256: e2d5dc8d8b9dcc2b65836dd36a5f2835149ba71870ec566b5e58bacc5ba1659d

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

module Roar::Rails
  module ModelMethods
  private
    # DISCUSS: move this into a generic namespace as we could need that in Sinatra as well.
    def extend_with!(model, representer)
      model.extend(representer)
    end
  
    def prepare_model!(model)
      representer = controller.representer_for(format, model, options)
      extend_with!(model, representer)
    end
  end
  
  module Responder
    include ModelMethods
    
    # DISCUSS: why THE FUCK is options not passed as a method argument but kept as an internal instance variable in the responder? this is something i will never understand about Rails.
    def display(model, *args)
      if representer = options.delete(:represent_items_with)
        render_items_with(model, representer) # convenience API, not recommended since it's missing hypermedia.
        return super
      end
      
      prepare_model!(model)
      
      super
    end
    
  private
    def render_items_with(collection, representer)
      collection.map! do |m|  # DISCUSS: i don't like changing the method argument here.
        extend_with!(m, representer)
        m.to_hash # FIXME: huh? and what about XML?
      end
    end
    
    include VersionStrategy
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roar-rails-0.0.11 lib/roar/rails/responder.rb
roar-rails-0.0.10 lib/roar/rails/responder.rb