Sha256: fadc15bb39cc6d7859aa34e39fb0b87b90a858dd428096d1b8d90f8e2bfae9d2

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Roar::Rails
  module ModelMethods
    # DISCUSS: move this into a generic namespace as we could need that in Sinatra as well.
    def extend_with_representer!(model, representer=nil)
      representer ||= representer_for_model(model)
      model.extend(representer)
    end
    
  private
    def representer_for_model(model)
      class_name = model.class.name
      "#{class_name}Representer".constantize
    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_with)
        # this is the new behaviour.
        model.extend(representer) # FIXME: move to method.
        return super
      end
      
      
      representer = options.delete(:with_representer) and ActiveSupport::Deprecation.warn(":with_representer is deprecated and will be removed in roar-rails 1.0. Use :represent_with or :represent_items_with.")
      representer ||= options.delete(:represent_items_with) # new API.
      
      if model.respond_to?(:map!)
        ActiveSupport::Deprecation.warn("Calling #respond_with with a collection will misbehave in future versions of roar-rails. Use :represent_items_with to get the old behaviour.")
        
        model.map! do |m|
          extend_with_representer!(m, representer)
          m.to_hash
        end
      else
        extend_with_representer!(model, representer)
      end
      
      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roar-rails-0.0.7 lib/roar/rails/responder.rb