config.after_initialize do class ::ActionController::Base alias_method :original_render, :render def render(*args, &block) if args.length == 1 && args[0].kind_of?(Hash) hash = args.first case hash[:rgviz] when nil then original_render *args, &block else model = hash[:rgviz] conditions = hash[:conditions] query = params[:tq] tqx = params[:tqx] || '' pieces = tqx.split ';' reqId = '0' version = '0.6' responseHandler = 'google.visualization.Query.setResponse' out = 'json' pieces.each do |p| key_value = p.split ':' if key_value.length == 2 case key_value[0] when 'reqId' reqId = key_value[1] when 'version' version = key_value[1] when 'responseHandler' responseHandler = key_value[1] when 'out' out = key_value[1] end end end begin executor = Rgviz::Executor.new model, Rgviz::Parser.new(query).parse options = {} options[:conditions] = conditions if conditions table = executor.execute options if out == 'json' original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'ok',version:'#{version}',table:#{table.to_json}});" elsif out == 'html' original_render :text => Rgviz::HtmlRenderer.render(table) else original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'error',version:'#{version}',errors:[{reason:'not_supported', message:'Unsupported output type: #{out}'}]});" end rescue ParseException => e msg = e.message.gsub("'", "\\'") if out == 'json' original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'error',version:'#{version}',errors:[{reason:'invalid_query', message:'#{msg}'}]});" elsif out == 'html' original_render :text => "Error: #{msg}" else original_render :text => "Unsupported output type: #{out}" end end end else original_render *args, &block end end end end