@@ -6,63 +6,24 @@ def global
66 Global
77 end
88
9- protected
10-
11- def check_method ( method , *args , &block )
12-
13- end
14-
15- private
16-
17-
18-
19- def get_variable ( name )
20- raise NotImplementedError
21- end
22-
23- def set_variable ( name , value )
24- raise NotImplementedError
25- end
26-
27- %w( rabl jbuilder ) . each do |builder_name |
28- define_method builder_name do |*options |
29- if options . first . is_a? String
30- warn "[DEPRECATION] view_path argument is now optional. If you need to specify it please use #{ builder } (:template => 'path')"
31- options = options . extract_options! . merge ( :template => options [ 0 ] )
32- else
33- options = ( options && options . first . is_a? ( Hash ) ) ? options . first : { }
9+ def render_data ( options )
10+ data = Gon . all_variables
11+ namespace = options [ :namespace ] || 'gon'
12+ start = '<script>window.' + namespace + ' = {};'
13+ script = ''
14+
15+ if options [ :camel_case ]
16+ data . each do |key , val |
17+ script << "#{ namespace } .#{ key . to_s . camelize ( :lower ) } =#{ val . to_json } ;"
3418 end
35-
36- builder_module = get_builder ( builder_name )
37-
38- data = builder_module . send ( "parse_#{ builder_name } " , get_template_path ( options , builder_name ) , get_controller ( options ) )
39-
40- if options [ :as ]
41- set_variable ( options [ :as ] . to_s , data )
42- elsif data . is_a? Hash
43- data . each do |key , value |
44- set_variable ( key , value )
45- end
46- else
47- set_variable ( builder_name , data )
19+ else
20+ data . each do |key , val |
21+ script << "#{ namespace } .#{ key . to_s } =#{ val . to_json } ;"
4822 end
4923 end
50- end
51- alias_method :orig_jbuilder , :jbuilder
5224
53- def jbuilder ( *options )
54- raise NoMethodError . new ( 'You can use Jbuilder support only in 1.9+' ) if RUBY_VERSION < '1.9'
55- orig_jbuilder ( *options )
56- end
57-
58- private
59-
60- def get_builder ( builder_name )
61- begin
62- "Gon::#{ builder_name . classify } " . constantize
63- rescue
64- raise NoMethodError . new ( "You should define #{ builder_name . classify } in your Gemfile" )
65- end
25+ script = start + Gon ::Escaper . escape ( script ) + '</script>'
26+ script . html_safe
6627 end
6728
6829 def get_controller ( options )
@@ -75,12 +36,24 @@ def get_controller(options)
7536
7637 def get_template_path ( options , extension )
7738 if options [ :template ]
78- File . extname ( options [ :template ] ) == ".#{ extension } " ? options [ :template ] : "#{ options [ :template ] } .#{ extension } "
39+ if right_extension? ( extension , options [ :template ] )
40+ options [ :template ]
41+ else
42+ [ options [ :template ] , extension ] . join ( '.' )
43+ end
7944 else
80- "app/views/#{ get_controller ( options ) . controller_path } /#{ get_controller ( options ) . action_name } .json.#{ extension } "
45+ controller = get_controller ( options ) . controller_path
46+ action = get_controller ( options ) . action_name
47+ "app/views/#{ controller } /#{ action } .json.#{ extension } "
8148 end
8249 end
8350
51+ private
52+
53+ def right_extension? ( extension , template_path )
54+ File . extname ( template_path ) == ".#{ extension } "
55+ end
56+
8457 end
8558 end
8659end
0 commit comments