Using "extends" in RABL when the required node is not attribute

1k Views Asked by At

When a required node is an attribute or method, it is easy to call extends.

But when it is not an attribute? It could be an instance variable or an attr accessor.

Example below:


// dispatches is an attribute of the class
child :dispatches => :dispatches do
  extends "/dispatches/_base"
end

# completed post is not an attribute of the class
if @completed_post
  node(:post) do |post|
    {
      id: @completed_post.id.to_s,
      _id: @completed_post.id.to_s,
      display_text: @completed_post.display_text,
      # and many others
    }
end

This required additional node could either be an @instance or @object[:some_attribute]

1

There are 1 best solutions below

0
On

Not sure if this will help you but you can pass "locals" to extended template like so:

extends('posts/show', :locals => { :hide_comments => true })

Then in your posts/show template:

# app/views/posts/show.json.rabl
object @post

node(:comments) { |post| post.comments } unless locals[:hide_comments]