Module: InverseOf::Reflection::AssociationReflection

Defined in:
lib/inverse_of.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/inverse_of.rb', line 15

def self.included(base)
  base.alias_method_chain :check_validity!, :inverse_of
end

Instance Method Details

#check_validity_of_inverse!Object



24
25
26
27
28
29
30
# File 'lib/inverse_of.rb', line 24

def check_validity_of_inverse!
  unless options[:polymorphic]
    if has_inverse? && inverse_of.nil?
      raise InverseOfAssociationNotFoundError.new(self)
    end
  end
end

#check_validity_with_inverse_of!Object



19
20
21
22
# File 'lib/inverse_of.rb', line 19

def check_validity_with_inverse_of!
  check_validity_of_inverse!
  check_validity_without_inverse_of!
end

#has_inverse?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/inverse_of.rb', line 32

def has_inverse?
  !@options[:inverse_of].nil?
end

#inverse_ofObject



36
37
38
39
40
# File 'lib/inverse_of.rb', line 36

def inverse_of
  if has_inverse?
    @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
  end
end

#polymorphic_inverse_of(associated_class) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/inverse_of.rb', line 42

def polymorphic_inverse_of(associated_class)
  if has_inverse?
    if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of])
      inverse_relationship
    else
      raise InverseOfAssociationNotFoundError.new(self, associated_class)
    end
  end
end