Module: Traits::Attribute::Key

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

Instance Method Summary collapse

Instance Method Details

#foreign_key?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/traits/attribute/key.rb', line 13

def foreign_key?
  attr_name       = name
  attr_translates = model_class.attribute_features[attr_name].try(:translates_with_globalize?)

  model.associations.any? do |assoc|
    if assoc.belongs_to?
      if attr_translates && assoc.features.translates_with_globalize?
        assoc.features.globalize_translatable.translation_from_key_name == attr_name
      else
        assoc.from_key_name == attr_name
      end
    end
  end
end

#key?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/traits/attribute/key.rb', line 28

def key?
  primary_key? || foreign_key? || polymorphic_key?
end

#primary_key?Boolean

Returns:

  • (Boolean)


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

def primary_key?
  column_definition.try(:primary) ||
    model_class.primary_key == column_definition.name ||
    type == :primary_key
end

#to_hashObject



32
33
34
35
36
37
38
# File 'lib/traits/attribute/key.rb', line 32

def to_hash
  super.merge!(
    key:         key?,
    primary_key: primary_key?,
    foreign_key: foreign_key?
  )
end