Module: ProductTaxonomy::Serializers::Attribute::Docs::BaseAndExtendedSerializer

Defined in:
lib/product_taxonomy/models/serializers/attribute/docs/base_and_extended_serializer.rb

Class Method Summary collapse

Class Method Details

.serialize(attribute) ⇒ Hash

Parameters:

Returns:

  • (Hash)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/product_taxonomy/models/serializers/attribute/docs/base_and_extended_serializer.rb', line 20

def serialize(attribute)
  result = {
    "id" => attribute.gid,
    "name" => attribute.extended? ? attribute.base_attribute.name : attribute.name,
    "handle" => attribute.handle,
    "extended_name" => attribute.extended? ? attribute.name : nil,
    "values" => attribute.values.map do |value|
      {
        "id" => value.gid,
        "name" => value.name,
      }
    end,
  }
  result.delete("extended_name") unless attribute.extended?
  result
end

.serialize_allObject



9
10
11
12
13
14
15
16
# File 'lib/product_taxonomy/models/serializers/attribute/docs/base_and_extended_serializer.rb', line 9

def serialize_all
  ProductTaxonomy::Attribute.sorted_base_attributes.flat_map do |attribute|
    extended_attributes = attribute.extended_attributes.sort_by(&:name).map do |extended_attribute|
      serialize(extended_attribute)
    end
    extended_attributes + [serialize(attribute)]
  end
end