Class: RailsDbAdmin::Extjs::JsonDataBuilder
- Inherits:
-
Object
- Object
- RailsDbAdmin::Extjs::JsonDataBuilder
- Defined in:
- lib/rails_db_admin/extjs/json_data_builder.rb
Instance Method Summary collapse
- #build_json_data(options) ⇒ Object
- #get_row_data(table, id) ⇒ Object
-
#get_row_data_no_id(table, row_hash) ⇒ Object
This will retrieve data from tables without an ‘id’ field.
- #get_total_count(table) ⇒ Object
-
#initialize(database_connection_class) ⇒ JsonDataBuilder
constructor
A new instance of JsonDataBuilder.
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() unless [:table] raise '!Error Must specify table' end total_count = self.get_total_count([:table]) arel_table = Arel::Table::new([:table]) if [:limit] && [:offset] && [:order] query = arel_table.project(Arel.sql('*')).order([:order]). take(@connection.sanitize_limit([:limit])). skip([:offset].to_i) elsif [:limit] && [:order] query = arel_table.project(Arel.sql('*')).order([:order]). take(@connection.sanitize_limit([:limit])) elsif [:limit] && ![:order] query = arel_table.project(Arel.sql('*')). take(@connection.sanitize_limit([:limit])) elsif ![:limit] && [:order] query = arel_table.project(Arel.sql('*')).order([: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 = [: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 |