JPartial::Renderer
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.