Module: Traits::Attribute::Polymorphism

Included in:
Traits::Attribute
Defined in:
lib/traits/concerns/attribute/polymorphism.rb

Instance Method Summary collapse

Instance Method Details

#polymorphic_key?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
# File 'lib/traits/concerns/attribute/polymorphism.rb', line 4

def polymorphic_key?
  model.associations
    .first_where(
      polymorphic?:  true,
      from_key_name: name
    ).present?
end

#polymorphic_type?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/traits/concerns/attribute/polymorphism.rb', line 12

def polymorphic_type?
  model.associations
    .first_where(
      polymorphic?:                        true,
      attribute_name_for_polymorphic_type: name
    ).present?
end

#to_hashObject



20
21
22
23
24
25
# File 'lib/traits/concerns/attribute/polymorphism.rb', line 20

def to_hash
  super.merge!(
    polymorphic_key:  polymorphic_key?,
    polymorphic_type: polymorphic_type?
  )
end