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
# 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[: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

  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



49
50
51
52
53
54
55
56
57
58
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 49

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.



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

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



77
78
79
80
81
82
83
84
# File 'lib/rails_db_admin/extjs/json_data_builder.rb', line 77

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