Class: ProductTaxonomy::Attribute

Inherits:
Object
  • Object
show all
Extended by:
Indexed, Localized
Includes:
ActiveModel::Validations, FormattedValidationErrors
Defined in:
lib/product_taxonomy/models/attribute.rb

Direct Known Subclasses

ExtendedAttribute

Constant Summary

Constants included from Indexed

Indexed::NotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Localized

localizations, validate_localizations!

Methods included from Indexed

add, all, create_validate_and_add!, duplicate?, extended, find_by, find_by!, hashed_by, hashed_models, size

Methods included from FormattedValidationErrors

#validate!

Constructor Details

#initialize(id:, name:, description:, friendly_id:, handle:, values:, is_manually_sorted: false) ⇒ Attribute

ID instead.

Parameters:

  • id (Integer)

    The ID of the attribute.

  • name (String)

    The name of the attribute.

  • description (String)

    The description of the attribute.

  • friendly_id (String)

    The friendly ID of the attribute.

  • handle (String)

    The handle of the attribute.

  • values (Array<Value, String>)

    An array of resolved Value objects. When resolving fails, use the friendly



98
99
100
101
102
103
104
105
106
107
# File 'lib/product_taxonomy/models/attribute.rb', line 98

def initialize(id:, name:, description:, friendly_id:, handle:, values:, is_manually_sorted: false)
  @id = id
  @name = name
  @description = description
  @friendly_id = friendly_id
  @handle = handle
  @values = values
  @extended_attributes = []
  @is_manually_sorted = is_manually_sorted
end

Instance Attribute Details

#extended_attributesObject (readonly)

Returns the value of attribute extended_attributes.



89
90
91
# File 'lib/product_taxonomy/models/attribute.rb', line 89

def extended_attributes
  @extended_attributes
end

#friendly_idObject (readonly)

Returns the value of attribute friendly_id.



89
90
91
# File 'lib/product_taxonomy/models/attribute.rb', line 89

def friendly_id
  @friendly_id
end

#handleObject (readonly)

Returns the value of attribute handle.



89
90
91
# File 'lib/product_taxonomy/models/attribute.rb', line 89

def handle
  @handle
end

#idObject (readonly)

Returns the value of attribute id.



89
90
91
# File 'lib/product_taxonomy/models/attribute.rb', line 89

def id
  @id
end

#valuesObject (readonly)

Returns the value of attribute values.



89
90
91
# File 'lib/product_taxonomy/models/attribute.rb', line 89

def values
  @values
end

Class Method Details

.load_from_source(source_data) ⇒ Object

Load attributes from source data. By default, this data is deserialized from a YAML file in the data directory.

Parameters:

  • source_data (Array<Hash>)

    The source data to load attributes from.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/product_taxonomy/models/attribute.rb', line 14

def load_from_source(source_data)
  raise ArgumentError, "source_data must be a hash" unless source_data.is_a?(Hash)
  raise ArgumentError, "source_data must contain keys \"base_attributes\" and \"extended_attributes\"" unless
    source_data.keys.sort == ["base_attributes", "extended_attributes"]

  source_data.each do |type, attributes|
    attributes.each do |attribute_data|
      raise ArgumentError, "source_data must contain hashes" unless attribute_data.is_a?(Hash)

      attribute = case type
      when "base_attributes" then attribute_from(attribute_data)
      when "extended_attributes" then extended_attribute_from(attribute_data)
      end
      Attribute.add(attribute)
      attribute.validate!(:create)
    end
  end
end

.next_idInteger

Get the next ID for a newly created attribute.

Returns:

  • (Integer)

    The next ID.



49
# File 'lib/product_taxonomy/models/attribute.rb', line 49

def next_id = (all.max_by(&:id)&.id || 0) + 1

.resetObject

Reset all class-level state



34
35
36
37
# File 'lib/product_taxonomy/models/attribute.rb', line 34

def reset
  @localizations = nil
  @hashed_models = nil
end

.sorted_base_attributesArray<Attribute>

Get base attributes only, sorted by name.

Returns:

  • (Array<Attribute>)

    The sorted base attributes.



42
43
44
# File 'lib/product_taxonomy/models/attribute.rb', line 42

def sorted_base_attributes
  all.reject(&:extended?).sort_by(&:name)
end

Instance Method Details

#add_extended_attribute(extended_attribute) ⇒ Object

Add an extended attribute to the attribute.

Parameters:



112
113
114
# File 'lib/product_taxonomy/models/attribute.rb', line 112

def add_extended_attribute(extended_attribute)
  @extended_attributes << extended_attribute
end

#add_value(value) ⇒ Object

Add a value to the attribute.

Parameters:

  • value (Value)

    The value to add.



119
120
121
# File 'lib/product_taxonomy/models/attribute.rb', line 119

def add_value(value)
  @values << value
end

#extended?Boolean

Whether the attribute is an extended attribute.

Returns:

  • (Boolean)


133
134
135
# File 'lib/product_taxonomy/models/attribute.rb', line 133

def extended?
  is_a?(ExtendedAttribute)
end

#gidString

The global ID of the attribute

Returns:

  • (String)


126
127
128
# File 'lib/product_taxonomy/models/attribute.rb', line 126

def gid
  "gid://shopify/TaxonomyAttribute/#{id}"
end

#manually_sorted?Boolean

Whether the attribute is manually sorted.

Returns:

  • (Boolean)


140
141
142
# File 'lib/product_taxonomy/models/attribute.rb', line 140

def manually_sorted?
  @is_manually_sorted
end

#sorted_values(locale: "en") ⇒ Array<Value>

Get the sorted values of the attribute.

Parameters:

  • locale (String) (defaults to: "en")

    The locale to sort by.

Returns:

  • (Array<Value>)

    The sorted values.



148
149
150
151
152
153
154
# File 'lib/product_taxonomy/models/attribute.rb', line 148

def sorted_values(locale: "en")
  if manually_sorted?
    values
  else
    Value.sort_by_localized_name(values, locale:)
  end
end