Module: Material::Attributes

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/material/concerns/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute_typesObject



35
36
37
# File 'lib/material/concerns/attributes.rb', line 35

def attribute_types
  attribute_names.each_with_object({}) { |attribute, hash| hash[attribute] = type_for_attribute(attribute).type }
end

#attribute_valuesObject



31
32
33
# File 'lib/material/concerns/attributes.rb', line 31

def attribute_values
  attribute_names.each_with_object({}) { |attribute, hash| hash[attribute] = public_send(attribute.to_sym) }
end

#formatted_attributesObject



39
40
41
42
43
# File 'lib/material/concerns/attributes.rb', line 39

def formatted_attributes
  attribute_types.each_with_object({}) do |(attribute, attribute_type), hash|
    hash[attribute] = format_by_type(attribute_values[attribute], type: attribute_type)
  end
end

#human_attribute_value(attribute) ⇒ Object



27
28
29
# File 'lib/material/concerns/attributes.rb', line 27

def human_attribute_value(attribute)
  formatted_attributes.fetch(attribute)
end

#relationship_attributesObject



53
54
55
# File 'lib/material/concerns/attributes.rb', line 53

def relationship_attributes
  source_class.reflect_on_all_associations.map(&:foreign_key)
end

#sorted_attribute_namesObject



45
46
47
48
49
50
51
# File 'lib/material/concerns/attributes.rb', line 45

def sorted_attribute_names
  [
    (head_attributes & attribute_names),
    (attribute_names - head_attributes - tail_attributes).sort,
    (tail_attributes & attribute_names),
  ].flatten
end