Created
October 16, 2014 15:05
-
-
Save sunny/f5dd096d00e472e9f52d to your computer and use it in GitHub Desktop.
Rails initializer that lets you prettify and comment your jbuilder JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "jbuilder" | |
# Enable prettification in your `.jbuilder` files | |
# Via https://github.com/rails/jbuilder/issues/195#issuecomment-44440569 | |
# | |
# Example: | |
# json.prettify! if params[:pretty] == "1" | |
# | |
class Jbuilder | |
# Enable or disable prettification | |
# Example: | |
# json.prettify! | |
# json.prettify!(false) | |
def prettify!(enabled = true) | |
@prettify = enabled | |
end | |
alias_method :_original_target, :target! | |
def target! | |
@prettify ? ::JSON.pretty_generate(@attributes) : _original_target | |
end | |
end | |
# Add comments attributes in your JSON files. | |
# | |
# This can be usefull in understanding certain aspects of your JSON | |
# or when building an automatic documentation. | |
# | |
# Example: | |
# json.commentify! if params[:comments] == "1" | |
# | |
# json.comment "Unique identifier" | |
# json.id 42 | |
# json.comment "Product name" | |
# json.name "Foo" | |
# | |
# Would render: | |
# { | |
# "_comment_0": "Unique identifier", | |
# "id": 42, | |
# "_comment_1": "Product name", | |
# "name": "Foo" | |
# } | |
# | |
# This output can then be transformed by your automatic documentation, | |
# for example by transforming them in JS comments with the following | |
# regular expression: | |
# | |
# output.gsub(/^( *)"_comment_\d+": "(.*)",$/, '\1// \2') | |
# | |
# Would return: | |
# { | |
# // Unique identifier | |
# "id": 42, | |
# // Product name | |
# "name": "Foo" | |
# } | |
# | |
class Jbuilder | |
# Enable or disable comments | |
# Example: | |
# json.commentify! | |
# json.commentify!(false) | |
def commentify!(enabled = true) | |
@commentify = enabled | |
@comment_index ||= 0 | |
end | |
# Add a comment. | |
# Example: | |
# json.comment "foo" | |
def comment(text) | |
return unless @commentify | |
set!(:"_comment_#{@comment_index}", text) | |
@comment_index += 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment