Collection Rabl

I use RABL to output the Sunspot / SOLR result set, and the search result object consists of several types of models. Currently in 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 one model; however, when several types of models are in the @ search.results collection, this fails because both classes do not have the same instance methods. Does anyone know how to have different type based attributes? Ultimately, it would be nice to conditionally expand another template in the result collection based on the type of object. Something like the pseudo code below:

 child @search.results => :results do |r| if r.class == Product extends "product/base" else extends "some other class base" end end 
+6
source share
1 answer

You can take full control with the help of "node" and completely eliminate this problem 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 it help?

+7
source

Source: https://habr.com/ru/post/902757/


All Articles