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



56
57
58
# File 'lib/material/concerns/attributes.rb', line 56

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

#attribute_valuesObject



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

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

#display_attributesObject



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

def display_attributes
  unique_display_attributes
end

#formatted_attributesObject



60
61
62
63
64
65
# File 'lib/material/concerns/attributes.rb', line 60

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

#human_attribute_value(attribute) ⇒ Object



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

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

#relationship_attribute?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/material/concerns/attributes.rb', line 48

def relationship_attribute?(attribute_name)
  relationship_attributes.include?(attribute_name) || relationship_attributes.include?(attribute_name.chomp("_id"))
end

#relationship_attributesObject



75
76
77
# File 'lib/material/concerns/attributes.rb', line 75

def relationship_attributes
  source_class.reflect_on_all_associations.reject { |assoc| assoc.macro == :has_many }.map(&:name).map(&:to_s)
end

#sorted_attribute_namesObject



67
68
69
70
71
72
73
# File 'lib/material/concerns/attributes.rb', line 67

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

#unique_display_attributesObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/material/concerns/attributes.rb', line 36

def unique_display_attributes
  [].tap do |attrs|
    attribute_names.each do |attribute_name|
      next if relationship_attribute?(attribute_name)

      attrs << attribute_name
    end

    attrs.concat(relationship_attributes).uniq
  end
end