No commit activity in last 3 years
No release in over 3 years
Use jbuilder partial files to render ruby hashes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

JPartial::Renderer

Gem Version Code Climate Test Coverage

Gem to use jbuilder partial files to render ruby hashes inside a rails application

Install

Include the following line in your Gemfile

gem 'jpartial-renderer'

Motivation

The motivation to provide this funcionality is to rehuse the json definitions inside the jbuilder files in the views of the application. This is extremely usefull if you want to implement websocket updates to the UI using jsons in the payload of the events. With this you can define the jsons in one place (the jbuilder partials) and reuse them.

Usage

Let's say you have the follwing model

class Post < ActiveRecord::Base
end

with this associated jbuilder partial view in posts/_show.json.jbuilder

json.(post, :id, :message)

Then you can use this library in the following way

module MyTestModule
    extend JPartial::Renderer
    
    def self.show_post_hash(post)
        render_json 'posts/show', post: post, wrap_with: :post
    end
end

and the output of the method is

{ post => { id => 1, message => 'Post message' } }

Notice that the option wrap_with let's you wrap the partial inside a hash key.