Module: ProductTaxonomy::Serializers::Attribute::Docs::ReversedSerializer

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

Class Method Summary collapse

Class Method Details

.serialize(attribute, attribute_categories) ⇒ Hash

Returns The serialized attribute.

Parameters:

  • attribute (Attribute)

    The attribute to serialize.

  • attribute_categories (Array<Category>)

    The categories that the attribute belongs to.

Returns:

  • (Hash)

    The serialized attribute.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/product_taxonomy/models/serializers/attribute/docs/reversed_serializer.rb', line 29

def serialize(attribute, attribute_categories)
  attribute_categories ||= []
  {
    "id" => attribute.gid,
    "handle" => attribute.handle,
    "name" => attribute.name,
    "base_name" => attribute.extended? ? attribute.base_attribute.name : nil,
    "categories" => attribute_categories.sort_by(&:full_name).map do |category|
      {
        "id" => category.gid,
        "full_name" => category.full_name,
      }
    end,
    "values" => attribute.sorted_values.map do |value|
      {
        "id" => value.gid,
        "name" => value.name,
      }
    end,
  }
end

.serialize_allObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/product_taxonomy/models/serializers/attribute/docs/reversed_serializer.rb', line 9

def serialize_all
  attributes_to_categories = ProductTaxonomy::Category.all.each_with_object({}) do |category, hash|
    category.attributes.each do |attribute|
      hash[attribute] ||= []
      hash[attribute] << category
    end
  end

  serialized_attributes = ProductTaxonomy::Attribute.all.sort_by(&:name).map do |attribute|
    serialize(attribute, attributes_to_categories[attribute])
  end

  {
    "attributes" => serialized_attributes,
  }
end