summaryrefslogtreecommitdiff
path: root/app/models/concerns/manual_inverse_association.rb
blob: ff61412767e8b8a3b6b9e3db47766f41a86b588e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

module ManualInverseAssociation
  extend ActiveSupport::Concern

  class_methods do
    def manual_inverse_association(association, inverse)
      define_method(association) do
        super().tap do |value|
          next unless value

          child_association = value.association(inverse)
          child_association.set_inverse_instance(self)
          child_association.target = self
        end
      end
    end
  end
end