Permalink
Browse files

Oops, wasn't working for Rails 2

  • Loading branch information...
1 parent 7c5e9ed commit 2503e93d52a2e0120b4eda39dab4d79ad4d96b6d Ary Borenszweig committed Dec 16, 2010
Showing with 62 additions and 57 deletions.
  1. +51 −53 lib/rgviz_rails/init.rb
  2. +4 −3 lib/rgviz_rails/view_helper.rb
  3. +5 −0 rails/init.rb
  4. +2 −1 rgviz-rails.gemspec
View
104 lib/rgviz_rails/init.rb
@@ -2,75 +2,73 @@
# includes the view helper to ActionView::Base
ActionView::Base.send(:include, Rgviz::ViewHelper)
-def _define_rgviz_class
- ::ActionController::Base.module_eval do
- def render_with_rgviz(*args, &block)
- if args.length == 1 && args[0].kind_of?(Hash)
- hash = args.first
- case hash[:rgviz]
- when nil then render_without_rgviz *args, &block
- else
- model = hash[:rgviz]
- conditions = hash[:conditions]
- extensions = hash[:extensions]
- query = params[:tq]
- tqx = params[:tqx] || ''
+module Rgviz
+ def self._define_rgviz_class
+ ::ActionController::Base.module_eval do
+ def render_with_rgviz(*args, &block)
+ if args.length == 1 && args[0].kind_of?(Hash)
+ hash = args.first
+ case hash[:rgviz]
+ when nil then render_without_rgviz *args, &block
+ else
+ model = hash[:rgviz]
+ conditions = hash[:conditions]
+ extensions = hash[:extensions]
+ query = params[:tq]
+ tqx = params[:tqx] || ''
- tqx = Rgviz::Tqx.parse(tqx)
+ tqx = Rgviz::Tqx.parse(tqx)
- begin
- executor = Rgviz::Executor.new model, query
- options = {}
- options[:conditions] = conditions if conditions
- options[:extensions] = extensions if extensions
- table = executor.execute options
+ begin
+ executor = Rgviz::Executor.new model, query
+ options = {}
+ options[:conditions] = conditions if conditions
+ options[:extensions] = extensions if extensions
+ table = executor.execute options
- yield table if block_given?
+ yield table if block_given?
- case tqx['out']
- when 'json'
- render_without_rgviz :text => Rgviz::JsRenderer.render(table, tqx)
- when 'html'
- render_without_rgviz :text => Rgviz::HtmlRenderer.render(table)
- when 'csv'
- csv_output = Rgviz::CsvRenderer.render(table)
- if tqx['outFileName']
- send_data csv_output, :filename => tqx['outFileName'], :type => 'text/csv'
+ case tqx['out']
+ when 'json'
+ render_without_rgviz :text => Rgviz::JsRenderer.render(table, tqx)
+ when 'html'
+ render_without_rgviz :text => Rgviz::HtmlRenderer.render(table)
+ when 'csv'
+ csv_output = Rgviz::CsvRenderer.render(table)
+ if tqx['outFileName']
+ send_data csv_output, :filename => tqx['outFileName'], :type => 'text/csv'
+ else
+ render_without_rgviz :text => csv_output
+ end
else
- render_without_rgviz :text => csv_output
+ render_without_rgviz :text => Rgviz::JsRenderer.render_error('not_supported', "Unsupported output type: #{out}", tqx)
+ end
+ rescue ParseException => e
+ case tqx['out']
+ when 'json'
+ render_without_rgviz :text => Rgviz::JsRenderer.render_error('invalid_query', e.message, tqx)
+ when 'html'
+ render_without_rgviz :text => "<b>Error:</b> #{e.message}"
+ when 'csv'
+ render_without_rgviz :text => "Error: #{e.message}"
+ else
+ render_without_rgviz :text => "<b>Unsupported output type:</b> #{out}"
end
- else
- render_without_rgviz :text => Rgviz::JsRenderer.render_error('not_supported', "Unsupported output type: #{out}", tqx)
- end
- rescue ParseException => e
- case tqx['out']
- when 'json'
- render_without_rgviz :text => Rgviz::JsRenderer.render_error('invalid_query', e.message, tqx)
- when 'html'
- render_without_rgviz :text => "<b>Error:</b> #{e.message}"
- when 'csv'
- render_without_rgviz :text => "Error: #{e.message}"
- else
- render_without_rgviz :text => "<b>Unsupported output type:</b> #{out}"
end
end
+ else
+ render_without_rgviz *args, &block
end
- else
- render_without_rgviz *args, &block
end
+ alias_method_chain :render, :rgviz
end
- alias_method_chain :render, :rgviz
end
end
-if Rails::VERSION::MAJOR == 2
- config.after_initialize do
- _define_rgviz_class
- end
-else
+if Rails::VERSION::MAJOR > 2
class Railtie < Rails::Railtie
initializer "define rgviz class" do
- _define_rgviz_class
+ Rgviz._define_rgviz_class
end
end
end
View
7 lib/rgviz_rails/view_helper.rb
@@ -197,10 +197,11 @@ def opts_to_json(opts, special_keys)
@first_time = 0
- if defined? :raw
- raw out
- else
+
+ if Rails::VERSION::MAJOR == 2
out
+ else
+ raw out
end
end
View
5 rails/init.rb
@@ -0,0 +1,5 @@
+if Rails::VERSION::MAJOR == 2
+ config.after_initialize do
+ Rgviz._define_rgviz_class
+ end
+end
View
3 rgviz-rails.gemspec
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = "rgviz-rails"
- s.version = "0.44"
+ s.version = "0.45"
s.author = "Ary Borenszweig"
s.email = "aborenszweig@manas.com.ar"
s.homepage = "http://code.google.com/p/rgviz-rails"
@@ -16,6 +16,7 @@ spec = Gem::Specification.new do |s|
"lib/rgviz_rails/adapters/postgresql_adapter.rb",
"lib/rgviz_rails/adapters/sqlite_adapter.rb",
"lib/rgviz_rails/init.rb",
+ "rails/init.rb",
"spec/blueprints.rb",
"spec/spec.opts",
"spec/spec_helper.rb",

0 comments on commit 2503e93

Please sign in to comment.