Class: Gitlab::Reflections::Relationships::Handlers::PolymorphicHasAssociationHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/gitlab/reflections/relationships/handlers/polymorphic_has_association_handler.rb

Overview

Handles polymorphic has_many and has_one associations with :as option Examples:

has_many :comments, as: :commentable
has_one :image, as: :imageable

Instance Attribute Summary

Attributes inherited from BaseHandler

#association_name, #model, #reflection

Instance Method Summary collapse

Methods inherited from BaseHandler

#build_relationships, #initialize

Constructor Details

This class inherits a constructor from Gitlab::Reflections::Relationships::Handlers::BaseHandler

Instance Method Details

#relationship_attributesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gitlab/reflections/relationships/handlers/polymorphic_has_association_handler.rb', line 12

def relationship_attributes
  polymorphic_name = reflection.options[:as].to_s

  {
    parent_table: model.table_name,
    child_table: nil, # Will be filled in for specific targets
    primary_key: reflection.active_record_primary_key,
    foreign_key: "#{polymorphic_name}_id",
    relationship_type: relationship_type,
    polymorphic: true,
    polymorphic_type_column: "#{polymorphic_name}_type",
    polymorphic_name: polymorphic_name,
    parent_association: {
      name: association_name.to_s,
      type: reflection.macro.to_s,
      model: model.name
    }
  }
end