Class: Tush::AssociationHelpers
- Inherits:
-
Object
- Object
- Tush::AssociationHelpers
- Defined in:
- lib/tush/helpers/association_helpers.rb
Class Method Summary collapse
-
.class_for_foreign_key(association_info) ⇒ Object
Determine the class the foreign key points to.
-
.class_with_foreign_key(association_info) ⇒ Object
Determine the class that actually has the foreign key.
-
.create_foreign_key_mapping(model_classes) ⇒ Object
This method locates all foreign key columns for a a list of model classes for foreign keys declared within the list of models classes.
- .model_relation_info(model) ⇒ Object
- .model_to_relation_infos(models) ⇒ Object
- .relation_infos(relation_type, klass) ⇒ Object
Class Method Details
.class_for_foreign_key(association_info) ⇒ Object
Determine the class the foreign key points to
35 36 37 38 39 40 41 |
# File 'lib/tush/helpers/association_helpers.rb', line 35 def self.class_for_foreign_key(association_info) if association_info.macro == :belongs_to association_info.class_name.constantize else association_info.active_record end end |
.class_with_foreign_key(association_info) ⇒ Object
Determine the class that actually has the foreign key.
44 45 46 47 48 49 50 51 52 |
# File 'lib/tush/helpers/association_helpers.rb', line 44 def self.class_with_foreign_key(association_info) if association_info.macro == :belongs_to association_info.active_record else # has_one and has_many keys are stored in a model that's # different than the one they're declared in. association_info.class_name.constantize end end |
.create_foreign_key_mapping(model_classes) ⇒ Object
This method locates all foreign key columns for a a list of model classes for foreign keys declared within the list of models classes.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tush/helpers/association_helpers.rb', line 56 def self.create_foreign_key_mapping(model_classes) model_to_foreign_keys = {} model_classes.each do |model_class| model_to_foreign_keys[model_class] = [] end model_to_relation_infos(model_classes).each do |model, relation_infos| SUPPORTED_ASSOCIATIONS.each do |association_type| associations = relation_infos[association_type] associations.each do |association| klass = self.class_with_foreign_key(association) # An association with a class that wasn't # included in our list of model_classes might be found. unless model_to_foreign_keys.keys.include?(klass) model_to_foreign_keys[klass] = [] end association_hash = { :foreign_key => association.foreign_key, :class => self.class_for_foreign_key(association) } unless model_to_foreign_keys[klass].include?(association_hash) model_to_foreign_keys[klass] << association_hash end end end end model_to_foreign_keys end |
.model_relation_info(model) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/tush/helpers/association_helpers.rb', line 13 def self.model_relation_info(model) relation_infos = {} SUPPORTED_ASSOCIATIONS.each do |association_type| relation_infos[association_type] = self.relation_infos(association_type, model) end relation_infos end |
.model_to_relation_infos(models) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tush/helpers/association_helpers.rb', line 23 def self.model_to_relation_infos(models) models = models.uniq model_to_relation_infos_hash = {} models.each do |model| model_to_relation_infos_hash[model] = self.model_relation_info(model) end model_to_relation_infos_hash end |
.relation_infos(relation_type, klass) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/tush/helpers/association_helpers.rb', line 5 def self.relation_infos(relation_type, klass) if klass.is_a?(String) klass = klass.constantize end klass.reflect_on_all_associations(relation_type) end |