Class: DynamicActiveModel::Associations
- Inherits:
-
Object
- Object
- DynamicActiveModel::Associations
- Defined in:
- lib/dynamic-active-model/associations.rb
Overview
The Associations class is responsible for automatically detecting and setting up ActiveRecord relationships between models based on database schema analysis. It supports the following relationship types:
- belongs_to
- has_many
- has_one (automatically detected from unique constraints)
- has_and_belongs_to_many (automatically detected from join tables)
Instance Attribute Summary collapse
-
#database ⇒ Database
readonly
The database instance containing the models.
-
#join_tables ⇒ Array
readonly
List of detected join tables.
-
#table_indexes ⇒ Hash
readonly
Mapping of table names to their indexes.
Instance Method Summary collapse
-
#add_foreign_key(table_name, foreign_key, relationship_name = nil) ⇒ Object
Adds a custom foreign key relationship.
-
#build! ⇒ void
Builds all relationships between models based on foreign keys and constraints This method: 1.
-
#initialize(database) ⇒ Associations
constructor
Initializes a new Associations instance.
-
#use_foreign_key_constraints! ⇒ void
Relates columns through the database's foreign key constraints as well as by naming convention.
-
#use_has_many_through! ⇒ void
Also adds has_many :through associations across join models (see HasManyThrough).
-
#use_optional_belongs_to! ⇒ void
Makes every belongs_to optional, including those on NOT NULL foreign keys, so saving a record doesn't validate that its parent exists (or query for it).
Constructor Details
#initialize(database) ⇒ Associations
Initializes a new Associations instance
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dynamic-active-model/associations.rb', line 32 def initialize(database) @database = database @table_indexes = {} @join_tables = [] @foreign_keys = {} database.models.each do |model| @foreign_keys[model.table_name] = ForeignKey.new(model) # through the schema cache, so a Rails schema cache dump spares one query per table @table_indexes[model.table_name] = model.connection.schema_cache.indexes(model.table_name) @join_tables << model if join_table?(model) end end |
Instance Attribute Details
#database ⇒ Database (readonly)
Returns The database instance containing the models.
22 23 24 |
# File 'lib/dynamic-active-model/associations.rb', line 22 def database @database end |
#join_tables ⇒ Array (readonly)
Returns List of detected join tables.
28 29 30 |
# File 'lib/dynamic-active-model/associations.rb', line 28 def join_tables @join_tables end |
#table_indexes ⇒ Hash (readonly)
Returns Mapping of table names to their indexes.
25 26 27 |
# File 'lib/dynamic-active-model/associations.rb', line 25 def table_indexes @table_indexes end |
Instance Method Details
#add_foreign_key(table_name, foreign_key, relationship_name = nil) ⇒ Object
Adds a custom foreign key relationship
50 51 52 53 54 |
# File 'lib/dynamic-active-model/associations.rb', line 50 def add_foreign_key(table_name, foreign_key, relationship_name = nil) @foreign_keys .fetch(table_name.to_s) { raise ModelNotFound, "no model found for table #{table_name}" } .add(foreign_key, relationship_name) end |
#build! ⇒ void
This method returns an undefined value.
Builds all relationships between models based on foreign keys and constraints This method:
- Maps foreign keys to their corresponding models
- Adds belongs_to relationships
- Adds has_many or has_one relationships based on unique constraints
- Sets up has_and_belongs_to_many relationships for join tables
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dynamic-active-model/associations.rb', line 84 def build! foreign_key_to_models = create_foreign_key_to_model_map @database.models.each do |model| model.column_names.each { |column_name| add_column_relationships(model, column_name, foreign_key_to_models) } end @join_tables.each do |join_table_model| models = join_table_model.column_names.map do |column_name| foreign_key_to_models.dig(column_name.downcase, 0, 0) end.compact add_has_and_belongs_to_many(join_table_model, models) if models.size == 2 end HasManyThrough.new(@database.models, table_indexes).build! if @has_many_through end |
#use_foreign_key_constraints! ⇒ void
This method returns an undefined value.
Relates columns through the database's foreign key constraints as well as by naming convention. A column with a constraint follows the constraint.
59 60 61 |
# File 'lib/dynamic-active-model/associations.rb', line 59 def use_foreign_key_constraints! @constraints = ForeignKeyConstraints.new(@database) end |
#use_has_many_through! ⇒ void
This method returns an undefined value.
Also adds has_many :through associations across join models (see HasManyThrough)
65 66 67 |
# File 'lib/dynamic-active-model/associations.rb', line 65 def use_has_many_through! @has_many_through = true end |
#use_optional_belongs_to! ⇒ void
This method returns an undefined value.
Makes every belongs_to optional, including those on NOT NULL foreign keys, so saving a record doesn't validate that its parent exists (or query for it). For tools that write rows whose parents may be missing, such as data repair scripts.
73 74 75 |
# File 'lib/dynamic-active-model/associations.rb', line 73 def use_optional_belongs_to! @optional_belongs_to = true end |