class DBAdmin::Web
Constants
- DB
- DBs
Public Class Methods
convert_rails_adapter(adapter)
click to toggle source
# File lib/db_admin.rb, line 43 def self.convert_rails_adapter(adapter) hash = { 'postgresql' => 'postgres', 'sqlite3' => 'sqlite', 'sqlserver' => 'tinytds' } return hash[adapter] if hash.keys.include?(adapter) return adapter end
database_config_yml_hash()
click to toggle source
Load Rails './config/database.yml' or './database.yml'
# File lib/db_admin.rb, line 14 def self.database_config_yml_hash db_hash = load_database_yml return {} if db_hash.empty? db_hash = db_hash['development'] if db_hash['development'].is_a?(Hash) return {} if ['adapter', 'database'].any? { |item| db_hash[item].to_s.empty? } return db_hash .slice('adapter', 'host', 'database', 'username', 'password', 'encoding', 'port') .transform_keys { |key| key == 'username' ? :user : key.to_sym } .transform_values { |value| convert_rails_adapter(value) } end
load_database_yml()
click to toggle source
# File lib/db_admin.rb, line 29 def self.load_database_yml require 'yaml' begin YAML.load_file('config/database.yml') rescue Errno::ENOENT begin YAML.load_file('database.yml') rescue Errno::ENOENT {} end end end
Public Instance Methods
adapter_hash()
click to toggle source
# File lib/db_admin.rb, line 295 def adapter_hash { :sqlite => 'SQLite', :postgres => 'PostgreSQL', :mysql => 'MySQL', :mysql2 => 'MySQL', :oracle => 'Oracle', :sqlanywhere => 'SQL Anywhere' } end
belongs_to_table(column_name)
click to toggle source
# File lib/db_admin.rb, line 258 def belongs_to_table(column_name) match_data = column_name.to_s.match(/(.*)_id$/) if match_data table = match_data[1] begin if DB.table_exists?("#{table}es") return "#{table}es" elsif DB.table_exists?("#{table[0..table.size - 2]}ies") return "#{table[0..(table.size - 2)]}ies" elsif DB.table_exists?("#{table}s") return "#{table}s" elsif DB.table_exists?("#{table}") return "#{table}" end rescue Exception => e puts e.message end end nil end
column_name_with_belongs_to(column_name)
click to toggle source
# File lib/db_admin.rb, line 220 def column_name_with_belongs_to(column_name) if belongs_to_table(column_name) "<a href='/tables/#{belongs_to_table(column_name)}'>#{column_name[0..column_name.size - 4]}</a>_id" else column_name end end
column_text(value)
click to toggle source
# File lib/db_admin.rb, line 287 def column_text(value) if value.to_s.strip == '' ' ' else value end end
database_adapter()
click to toggle source
# File lib/db_admin.rb, line 212 def database_adapter adapter_hash[DB.database_type] end
database_hash(db)
click to toggle source
# File lib/db_admin.rb, line 299 def database_hash(db) eval(/{.*}/.match(db.inspect)[0]) end
database_host()
click to toggle source
# File lib/db_admin.rb, line 216 def database_host database_hash(DB)[:host] end
database_name()
click to toggle source
# File lib/db_admin.rb, line 200 def database_name begin if /{.*}/.match(DB.inspect) db_hash[:database] else /.*\/(.*)">$/.match(DB.inspect)[1] end rescue Exception => e puts e.message end end
long_string_become_short(string_value)
click to toggle source
# File lib/db_admin.rb, line 279 def long_string_become_short(string_value) if string_value.to_s.size > 33 "#{string_value[0..30]}<a href='#' onclick='show_full_content.call(this)' title='Click to show full content'>...</a>" else column_text(string_value) end end
notice_error()
click to toggle source
# File lib/db_admin.rb, line 228 def notice_error result = '' if session[:notice] result = "<div class='alert alert-success' role='alert'>#{session[:notice]}</div>" session[:notice] = nil end if session[:error] result = "<div class='alert alert-danger' role='alert'>#{session[:error]}</div>" session[:error] = nil end result end
show_column_text(row, column)
click to toggle source
# File lib/db_admin.rb, line 242 def show_column_text(row, column) if row[column].is_a?(Integer) && belongs_to_table(column) rand_id = rand(1000000000) "<li class='dropdown' style='list-style: none'> <a href='#' onclick='belongs_to_table_find.call(this)' data-rand-id=#{rand_id} data-url='/belongs_to_table_find/#{belongs_to_table(column)}/#{row[column]}' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false' title='Click to show relation data'> #{row[column]} </a> <ul id='ul_belongs_to_#{rand_id}' class='dropdown-menu' role='menu'>Searching...</ul> </li>" else row[column].is_a?(String) ? long_string_become_short(row[column]) : column_text(row[column]) end end
Private Instance Methods
connect_hash()
click to toggle source
# File lib/db_admin.rb, line 312 def connect_hash textarea_value_to_hash(params[:connect_hash]).merge(adapter: params[:adapter]) end
db_hash()
click to toggle source
# File lib/db_admin.rb, line 316 def db_hash eval(/{.*}/.match(DB.inspect)[0]) end
should_switch_db?(new_db)
click to toggle source
# File lib/db_admin.rb, line 320 def should_switch_db?(new_db) new_db.test_connection && new_db != DB end
textarea_value_to_hash(textarea_value)
click to toggle source
# File lib/db_admin.rb, line 306 def textarea_value_to_hash(textarea_value) hash = textarea_value.gsub(/(\r)+/, '').gsub(/(\n)+/, ',') hash = hash[0..hash.size - 2] if hash[hash.size - 1] == ',' eval("{#{hash}}") end