Module: Traits::Attribute::Polymorphism

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

Instance Method Summary collapse

Instance Method Details

#polymorphic_key?Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
# File 'lib/traits/attribute/polymorphism.rb', line 7

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

#polymorphic_type?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/traits/attribute/polymorphism.rb', line 15

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

#to_hashObject



23
24
25
26
27
28
# File 'lib/traits/attribute/polymorphism.rb', line 23

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