Class: RailsDbAdmin::TableSupport
- Inherits:
-
Object
- Object
- RailsDbAdmin::TableSupport
- Defined in:
- lib/rails_db_admin/table_support.rb
Class Method Summary collapse
-
.add_fake_id_col(rows_hash) ⇒ Object
Accepts an array of table row hashes and adds a ‘fake_id’ field to each one with a generated number.
-
.arel_attr(data, arel_table) ⇒ Object
Construct a hash of ARel relation objects as keys and assign with values for use in update calls.
- .database_rows_to_hash(rows) ⇒ Object
Instance Method Summary collapse
- #clean_nulls!(table, data) ⇒ Object
- #columns(table) ⇒ Object
- #delete_row(table, id) ⇒ Object
-
#initialize(database_connection_class) ⇒ TableSupport
constructor
A new instance of TableSupport.
- #insert_row(table, data, no_id = false) ⇒ Object
- #primary_key(table) ⇒ Object
- #primary_key?(table) ⇒ Boolean
- #table_contains_column(table, column_name) ⇒ Object
- #update_table(table, id, data) ⇒ Object
- #update_table_without_id(table, data) ⇒ Object
Constructor Details
#initialize(database_connection_class) ⇒ TableSupport
Returns a new instance of TableSupport.
4 5 6 |
# File 'lib/rails_db_admin/table_support.rb', line 4 def initialize(database_connection_class) @connection = database_connection_class.connection end |
Class Method Details
.add_fake_id_col(rows_hash) ⇒ Object
Accepts an array of table row hashes and adds a ‘fake_id’ field to each one with a generated number. Useful for prepraring many-to-many data to be edited in ExtJS grids
150 151 152 153 154 155 156 157 |
# File 'lib/rails_db_admin/table_support.rb', line 150 def self.add_fake_id_col(rows_hash) nums = (1..(rows_hash.length)).to_a result = rows_hash.map do |item| item[:fake_id] = nums.shift item end return result end |
.arel_attr(data, arel_table) ⇒ Object
Construct a hash of ARel relation objects as keys and assign with values for use in update calls
138 139 140 141 142 143 144 |
# File 'lib/rails_db_admin/table_support.rb', line 138 def self.arel_attr data, arel_table cln_hsh = {} data.each do |k, v| cln_hsh[arel_table[k.to_sym]] = v end cln_hsh end |
.database_rows_to_hash(rows) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rails_db_admin/table_support.rb', line 109 def self.database_rows_to_hash(rows) records = [] rows.each do |row| # record = nil # row.each {|k, v| # record = record.nil? ? {k.to_sym => v} : record.merge({k.to_sym => v}) # } # records << record # simplifying the above with to_hash.symbolize_keys row_data = row.to_hash.symbolize_keys # any hashes need to be converted to json strings row_data.each do |k, v| if v.is_a?(Hash) row_data[k] = v.to_json end end records << row_data end records.reverse end |
Instance Method Details
#clean_nulls!(table, data) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rails_db_admin/table_support.rb', line 90 def clean_nulls!(table, data) if data.class == Array data.each { |x| clean_nulls!(table, x) } end data.collect do |k, v| if v == "" || v == 0 column = columns(table).collect do |x| if (x.name == k) break x end end if column.null data[k] = nil end end end end |
#columns(table) ⇒ Object
8 9 10 |
# File 'lib/rails_db_admin/table_support.rb', line 8 def columns(table) @columns ||= @connection.columns(table) end |
#delete_row(table, id) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rails_db_admin/table_support.rb', line 49 def delete_row(table, id) arel_table = Arel::Table::new(table) pk = id[0].to_sym query = arel_table.where(arel_table[pk].eq(id[1])).compile_delete @connection.execute(query.to_sql) end |
#insert_row(table, data, no_id = false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rails_db_admin/table_support.rb', line 57 def insert_row(table, data, no_id=false) clean_nulls!(table, data) arel_table = Arel::Table::new(table) data = RailsDbAdmin::TableSupport.arel_attr(data, arel_table) sql = arel_table.compile_insert(data) #TODO: Test with Oracle; ActiveRecord source indicates #that we may need to pass in the id to use here id = @connection.insert(sql.to_sql) if no_id #need to gen a random number for fake_id... id = Random.rand(500-100) + 100 end id end |
#primary_key(table) ⇒ Object
74 75 76 |
# File 'lib/rails_db_admin/table_support.rb', line 74 def primary_key(table) [@connection.primary_key(table), nil] end |
#primary_key?(table) ⇒ Boolean
78 79 80 |
# File 'lib/rails_db_admin/table_support.rb', line 78 def primary_key?(table) @connection.supports_primary_key? && !@connection.primary_key(table).nil? end |
#table_contains_column(table, column_name) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/rails_db_admin/table_support.rb', line 83 def table_contains_column(table, column_name) column_names = columns(table).map { |column| column.name.to_sym } column_names.include?(column_name) end |
#update_table(table, id, data) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rails_db_admin/table_support.rb', line 12 def update_table(table, id, data) arel_table = Arel::Table::new(table) clean_nulls!(table, data) data = RailsDbAdmin::TableSupport.arel_attr(data, arel_table) pk = id[0].to_sym query = arel_table.where(arel_table[pk].eq(id[1])).compile_update(data) @connection.execute(query.to_sql) end |
#update_table_without_id(table, data) ⇒ Object
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/table_support.rb', line 23 def update_table_without_id(table, data) #makes all values strings data[0].delete('fake_id') data[1].delete('fake_id') data.map! do |item| item.each do |k, v| item[k] = v.to_s end item end clean_nulls!(table, data) changed_values = data[0].diff(data[1]) arel_table = Arel::Table::new(table) updates = RailsDbAdmin::TableSupport.arel_attr(changed_values, arel_table) query = arel_table data[1].each do |k, v| query = query.where(arel_table[k.to_sym].eq(v)) end query = query.compile_update(updates) @connection.execute(query.to_sql) end |