Rabl Multi-model collection

I am using RABL to output a Sunspot/SOLR result set and the search results object is composed of multiple model types. Currently within the rabl view I have:

object false

child @search.results => :results do
  attribute :id, :resource, :upccode
  attribute :display_description => :description

  code :start_date do |r|
    r.utc_start_date.to_i
  end

  code :end_date do |r|
    r.utc_end_date.to_i
  end

end

child @search => :stats do
  attribute :total
end

The above works for a single model; however, when multiple model types are within the @search.results collection, it fails because both classes do not have the same instance methods. Does anyone know how to have different attributes based on the type? Ultimately, it would be nice to conditionally extend a different template within the results collection based on the type of object. Something like the below pseudo code:

child @search.results => :results do |r|
  if r.class == Product
    extends "product/base"
  else
    extends "some other class base"
  end
end

Answers


You can take full control with 'node' and avoid this issue entirely in the 'worst' case:

node :results do
  @search.results.map do |r|
    if r.is_a?(Product)
      partial("product/base", :object => r)
    else # render other base class
      partial("other/base", :object => r)
    end
  end
end

Does that help?


Need Your Help

Question about default UISplitView iPad app template

objective-c ipad ios4 uisplitviewcontroller uipopovercontroller

Create default iPad split view based apps from template from xcode 4, then we have codes like

Is there a common navigation convention in an MVVM Light Windows Phone application?

windows-phone-7 mvvm-light windows-phone-7.1

As my app gets bigger I am finding that the same type of entity is appearing more and more throughout, typically in a listbox.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.