Module: ModelsReflectionsHelper
- Included in:
- HousingMisc::ModelReflectionsController
- Defined in:
- lib/housing_misc/models_reflections_helper.rb
Constant Summary collapse
- VAL_HASH_KEYS =
["macros", "throughs", "foreign_keys", "primary_keys", "polymorphic", "as", "type", "join_table", "association_foreign_key"]
Instance Method Summary collapse
- #convert_rows_to_col(array_of_rows) ⇒ Object
- #get_join_table_name(table1_name, table2_name) ⇒ Object
- #get_model_reflections(model) ⇒ Object
- #get_primary_key(macro, reflection_model_name, model_name) ⇒ Object
- #get_table_names_primary_keys_and_model_names(model_names, model) ⇒ Object
- #tables_reflections_hash_generator(base_models) ⇒ Object
- #update_base_models(model_name, model) ⇒ Object
Instance Method Details
#convert_rows_to_col(array_of_rows) ⇒ Object
76 77 78 79 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 76 def convert_rows_to_col(array_of_rows) column = array_of_rows.map{|row| row[0]} return column end |
#get_join_table_name(table1_name, table2_name) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 98 def get_join_table_name(table1_name, table2_name) table_name = "" table1_name.length.times do |i| next if table1_name[i]==table2_name[i] && table1_name[i] != "_" if table1_name[i]==table2_name[i] && table1_name[i] == "_" table_name = table1_name[0,i+1] + (table1_name[i+1, table1_name.length]<table2_name[i+1, table2_name.length] ? (table1_name[i+1, table1_name.length] + "_" + table2_name[i+1, table2_name.length]) : (table2_name[i+1, table2_name.length] + "_" + table1_name[i+1, table1_name.length])) else table_name = table1_name<table2_name ? (table1_name + "_" + table2_name) : (table2_name + "_" + table1_name) end break end return table_name end |
#get_model_reflections(model) ⇒ Object
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 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 15 def get_model_reflections(model) val_hash = {} model_reflections = {} reflections = model.constantize.reflections model_table_name = model.constantize.table_name reflections.keys.each do |reflection| next if reflections[reflection].[:polymorphic] # models_foreign_type = reflections[reflection].foreign_type # sql = "SELECT DISTINCT #{models_foreign_type} FROM #{model.constantize.table_name}" # rows_of_model = ActiveRecord::Base.connection.exec_query(sql).rows # model_names = convert_rows_to_col(rows_of_model) # val_hash["table_name"] = "#{reflection}_polymorphic" # val_hash["table_names"], val_hash["primary_keys"], val_hash["model_names"] = get_table_names_primary_keys_and_model_names(model_names, model) # val_hash["polymorphic"] = ["TRUE"] # val_hash["foreign_type"] = [models_foreign_type] # else model_name = reflections[reflection].class_name.constantize rescue ("#{model}::#{reflections[reflection].class_name}".constantize rescue "") table_name = model_name.table_name rescue "" next if table_name.blank? val_hash["table_name"] = table_name val_hash["throughs"] = [reflections[reflection].[:through] || "FALSE"] val_hash["polymorphic"] = ["FALSE"] val_hash["as"] = [reflections[reflection].[:as] || "FALSE"] val_hash["type"] = [reflections[reflection].type || "FALSE"] val_hash["join_table"] = reflections[reflection].macro == :has_and_belongs_to_many ? [reflections[reflection].[:join_table] || get_join_table_name(model_table_name, table_name)] : ["FALSE"] val_hash["association_foreign_key"] = reflections[reflection].macro == :has_and_belongs_to_many ? [reflections[reflection].[:association_foreign_key] || reflections[reflection].association_foreign_key] : ["FALSE"] val_hash["primary_keys"] = [reflections[reflection].[:primary_key] || get_primary_key(reflections[reflection].macro, model_name, model)] val_hash["model_name"] = model_name.to_s update_base_models(val_hash["model_name"], model) #end val_hash["macros"] = [reflections[reflection].macro] val_hash["foreign_keys"] = [reflections[reflection].foreign_key] if model_reflections.key?(val_hash["table_name"]) VAL_HASH_KEYS.each do |val_hash_key| model_reflections[val_hash["table_name"]][val_hash_key].concat(val_hash[val_hash_key]) end else model_reflections[val_hash["table_name"]] = val_hash end val_hash = {} end model_reflections["primary_key"] = model.constantize.primary_key model_reflections["model"] = model return model_table_name, model_reflections end |
#get_primary_key(macro, reflection_model_name, model_name) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 81 def get_primary_key(macro, reflection_model_name, model_name) primary_key = "" case macro when :belongs_to, :has_and_belongs_to_many primary_key = reflection_model_name.primary_key when :has_one, :has_many primary_key = model_name.constantize.primary_key end return primary_key end |
#get_table_names_primary_keys_and_model_names(model_names, model) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 61 def get_table_names_primary_keys_and_model_names(model_names, model) table_names = [] primary_keys = [] table_models = [] model_names.each do |model_name| table_name = model_name.constantize.table_name rescue ("#{model}::#{model_name}".constantize.table_name rescue "") next if table_name.blank? table_names.push(table_name) table_models.push(model_name) primary_keys.push(model_name.constantize.primary_key) update_base_models(model_name, model) end return table_names, primary_keys, table_models end |
#tables_reflections_hash_generator(base_models) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 3 def tables_reflections_hash_generator(base_models) @base_models = base_models @tables_reflections_hash = {} loop do break if @base_models.empty? model = @base_models.shift table, model_reflections = get_model_reflections(model) @tables_reflections_hash.store(table, model_reflections) end return @tables_reflections_hash end |
#update_base_models(model_name, model) ⇒ Object
92 93 94 95 96 |
# File 'lib/housing_misc/models_reflections_helper.rb', line 92 def update_base_models(model_name, model) if !@tables_reflections_hash.key?(model_name.constantize.table_name) && (model_name != model) && !@base_models.include?(model_name) @base_models.push(model_name) end end |