File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed
Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,9 @@ Gem::Specification.new do |s|
2020 s . require_paths = [ "lib" ]
2121 s . add_dependency "actionpack" , '>= 2.3.0'
2222 s . add_dependency "rabl"
23+ if RUBY_VERSION =~ /9/
24+ s . add_dependency "jbuilder"
25+ end
2326 s . add_dependency "json"
2427 s . add_development_dependency "rspec"
2528end
Original file line number Diff line number Diff line change 22require 'action_controller'
33require 'gon/helpers'
44require 'gon/rabl'
5+ if RUBY_VERSION =~ /9/
6+ require 'gon/jbuilder'
7+ end
58
69module Gon
710 class << self
@@ -67,5 +70,24 @@ def rabl(view_path, options = {})
6770 set_variable ( 'rabl' , rabl_data )
6871 end
6972 end
73+
74+ def jbuilder ( view_path , options = { } )
75+ raise NoMethodError . new ( 'You can use Jbuilder support only in 1.9+' ) if RUBY_VERSION !~ /9/
76+
77+ jbuilder_data = Gon ::Jbuilder . parse_jbuilder ( view_path , options [ :controller ] ||
78+ @request_env [ 'action_controller.instance' ] ||
79+ @request_env [ 'action_controller.rescue.response' ] .
80+ instance_variable_get ( '@template' ) .
81+ instance_variable_get ( '@controller' ) )
82+ if options [ :as ]
83+ set_variable ( options [ :as ] . to_s , jbuilder_data )
84+ elsif jbuilder_data . is_a? Hash
85+ jbuilder_data . each do |key , value |
86+ set_variable ( key , value )
87+ end
88+ else
89+ set_variable ( 'jbuilder' , jbuilder_data )
90+ end
91+ end
7092 end
7193end
Original file line number Diff line number Diff line change 1+ require 'jbuilder'
2+
3+ module Gon ::Jbuilder
4+ class << self
5+ def parse_jbuilder ( jbuilder_path , controller )
6+ source = File . read ( jbuilder_path )
7+ controller . instance_variables . each do |name |
8+ self . instance_variable_set ( name , controller . instance_variable_get ( name ) )
9+ end
10+ output = JbuilderTemplate . encode ( controller ) do |json |
11+ eval source
12+ end
13+ JSON . parse ( output )
14+ end
15+ end
16+ end
You can’t perform that action at this time.
0 commit comments