Module: ActiveRecordMerger::AssociationFinder

Extended by:
AssociationFinder
Included in:
AssociationFinder
Defined in:
lib/active_record_merger/association_finder.rb

Defined Under Namespace

Classes: AssociationInfo

Instance Method Summary collapse

Instance Method Details

#call(model_class, filter = ->(_assoc) { true }) ⇒ Object

Finds and returns associations for a given ActiveRecord model.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_record_merger/association_finder.rb', line 11

def call(model_class, filter = ->(_assoc) { true })
  model_class.reflect_on_all_associations.map do |assoc|
    AssociationInfo.new(
      name:         assoc.name,
      type:         assoc.macro,
      class_name:   assoc.class_name,
      foreign_key:  assoc.foreign_key,
      foreign_type: assoc.foreign_type,
      through:      assoc.options[:through],
      polymorphic:  assoc.options[:polymorphic],
    )
  end.select { |assoc| filter.call(assoc) }
end