Module: Rgviz

Defined in:
lib/rgviz_rails/tqx.rb,
lib/rgviz_rails/init.rb,
lib/rgviz_rails/executor.rb,
lib/rgviz_rails/js_renderer.rb,
lib/rgviz_rails/view_helper.rb,
lib/rgviz_rails/adapters/mysql_adapter.rb,
lib/rgviz_rails/adapters/sqlite_adapter.rb,
lib/rgviz_rails/adapters/postgresql_adapter.rb

Defined Under Namespace

Modules: JsRenderer, Tqx, ViewHelper Classes: ColumnVisitor, Executor, MagicNamesVisitor, MySqlAdapter, NotSupported, OrderVisitor, PostgreSqlAdapter, SqliteAdapter, WhereVisitor

Class Method Summary collapse

Class Method Details

._define_rgviz_classObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rgviz_rails/init.rb', line 6

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]
          virtual_columns = hash[:virtual_columns]
          query = params[:tq] || 'select *'
          tqx = params[:tqx] || ''

          tqx = Rgviz::Tqx.parse(tqx)

          begin
            options = {}
            options[:conditions] = conditions if conditions
            options[:virtual_columns] = virtual_columns if virtual_columns

            table = if RgvizRails.inherits_from_active_record(model)
                      Rgviz::Executor.new(model).execute query, options
                    elsif model.respond_to? :execute
                      model.execute query, options
                    elsif model.is_a? Rgviz::Table
                      model
                    else
                      raise "The argument to render :rgviz => ... must extend from ActiveRecord::Base, respond to execute or be an Rgviz::Table"
                    end

            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'
              else
                render_without_rgviz :text => csv_output
              end
            else
              render_without_rgviz :text => Rgviz::JsRenderer.render_error('not_supported', "Unsupported output type: #{out}", tqx)
            end
          rescue Exception => 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
        end
      else
        render_without_rgviz *args, &block
      end
    end
    alias_method_chain :render, :rgviz
  end
end

.find_rails_col(klass, name) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/rgviz_rails/executor.rb', line 599

def self.find_rails_col(klass, name)
  joins = []

  while true
    col = klass.send(:columns).select{|x| x.name == name}.first
    return [klass, col, joins] if col

    before = ""
    idx = name.index('_') or raise "Unknown column #{name}"
    while idx
      before += "_" unless before.blank?
      before += "#{name[0 ... idx]}"
      name = name[idx + 1 .. -1]
      assoc = klass.send :reflect_on_association, before.to_sym
      if assoc
        klass = assoc.klass
        joins << assoc
        idx = nil
      else
        idx = name.index '_'
        raise "Unknown association #{before}" unless idx
      end
    end
  end
end