Class: RailsDbAdmin::Extjs::JsonDataBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_admin/extjs/json_data_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(database_connection_class) ⇒ JsonDataBuilder

Returns a new instance of JsonDataBuilder.



5
6
7
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 5

def initialize(database_connection_class)
  @connection = database_connection_class.connection
end

Instance Method Details

#build_json_data(options) ⇒ Object



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
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 9

def build_json_data(options)
  unless options[:table]
    raise '!Error Must specify table'
  end

  total_count = self.get_total_count(options[:table])
  arel_table = Arel::Table::new(options[:table])

  if options[:query_filter].present?
    table_name = options[:table].classify.constantize
    accepted_columns = table_name.columns.select {|column| column.type == :string or column.type == :text}
    where_clause = ""
    accepted_columns.each_with_index do |column, index|
      if index == 0
        where_clause = table_name.arel_table[column.name.to_sym].matches("%#{options[:query_filter]}%")
      else
        where_clause = where_clause.or(table_name.arel_table[column.name.to_sym].matches("%#{options[:query_filter]}%"))
      end
    end
    rows = table_name.where(where_clause)
  else

    if options[:limit] && options[:offset] && options[:order]
      query = arel_table.project(Arel.sql('*')).order(options[:order]).
        take(@connection.sanitize_limit(options[:limit])).
        skip(options[:offset].to_i)
    elsif options[:limit] && options[:order]
      query = arel_table.project(Arel.sql('*')).order(options[:order]).
        take(@connection.sanitize_limit(options[:limit]))
    elsif options[:limit] && !options[:order]
      query = arel_table.project(Arel.sql('*')).
        take(@connection.sanitize_limit(options[:limit]))
    elsif !options[:limit] && options[:order]
      query = arel_table.project(Arel.sql('*')).order(options[:order])
    else
      query = arel_table.project(Arel.sql('*'))
    end

    # This is a temporary partial fix to handle postgres boolean columns which is use activerecord when possible
    begin
      rows = options[:table].classify.constantize.find_by_sql(query.to_sql)
    rescue
      rows = @connection.select_all(query.to_sql)
    end

  end

  records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows)

  if !records.empty? && !records[0].has_key?("id")
    records = RailsDbAdmin::TableSupport.add_fake_id_col(records)
  end

  {:total => total_count, :data => records}
end

#get_row_data(table, id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 65

def get_row_data(table, id)
  arel_table = Arel::Table::new(table)

  query = arel_table.project(
    Arel.sql('*')).where(arel_table[id[0].to_sym].eq(id[1]))

  rows = @connection.select_all(query.to_sql)
  records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows)
  records[0]
end

#get_row_data_no_id(table, row_hash) ⇒ Object

This will retrieve data from tables without an ‘id’ field. Will also add a ‘fake_id’ so that it can be used by editable ExtJS grids.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 79

def get_row_data_no_id(table, row_hash)

  arel_table = Arel::Table::new(table)
  query = arel_table.project(Arel.sql('*'))
  row_hash.each do |k, v|
    query = query.where(arel_table[k.to_sym].eq(v))
  end

  rows = @connection.select_all(query.to_sql)
  records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows)
  records = RailsDbAdmin::TableSupport.add_fake_id_col(records)
  records[0]
end

#get_total_count(table) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 93

def get_total_count(table)
  total_count = 0
  rows = @connection.select_all("SELECT COUNT(*) as count FROM #{table}")
  records = RailsDbAdmin::TableSupport.database_rows_to_hash(rows)
  total_count = records[0][:count]

  total_count
end