Class: ProductTaxonomy::Value

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

Overview

An attribute value in the product taxonomy. Referenced by a Attribute. For example, an attribute called "Color" could have values "Red", "Blue", and "Green".

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:, friendly_id:, handle:) ⇒ Value

Returns a new instance of Value.

Parameters:

  • id (Integer)

    The ID of the value.

  • name (String)

    The name of the value.

  • friendly_id (String)

    The friendly ID of the value.

  • handle (String)

    The handle of the value.



91
92
93
94
95
96
# File 'lib/product_taxonomy/models/value.rb', line 91

def initialize(id:, name:, friendly_id:, handle:)
  @id = id
  @name = name
  @friendly_id = friendly_id
  @handle = handle
end

Instance Attribute Details

#friendly_idObject (readonly)

Returns the value of attribute friendly_id.



85
86
87
# File 'lib/product_taxonomy/models/value.rb', line 85

def friendly_id
  @friendly_id
end

#handleObject (readonly)

Returns the value of attribute handle.



85
86
87
# File 'lib/product_taxonomy/models/value.rb', line 85

def handle
  @handle
end

#idObject (readonly)

Returns the value of attribute id.



85
86
87
# File 'lib/product_taxonomy/models/value.rb', line 85

def id
  @id
end

Class Method Details

.all_values_sortedArray<Value>

Sort values according to their English name, taking "other" into account.

Parameters:

  • values (Array<Value>)

    The values to sort.

Returns:

  • (Array<Value>)

    The sorted values.



56
57
58
59
60
61
62
63
64
# File 'lib/product_taxonomy/models/value.rb', line 56

def all_values_sorted
  all.sort_by do |value|
    [
      value.name(locale: "en").downcase == "other" ? 1 : 0,
      value.name(locale: "en"),
      value.id,
    ]
  end
end

.load_from_source(source_data) ⇒ Object

Load values 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 values from.

Raises:

  • (ArgumentError)


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

def load_from_source(source_data)
  raise ArgumentError, "source_data must be an array" unless source_data.is_a?(Array)

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

    Value.create_validate_and_add!(
      id: value_data["id"],
      name: value_data["name"],
      friendly_id: value_data["friendly_id"],
      handle: value_data["handle"],
    )
  end
end

.next_idInteger

Get the next ID for a newly created value.

Returns:

  • (Integer)

    The next ID.



69
# File 'lib/product_taxonomy/models/value.rb', line 69

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

.resetObject

Reset all class-level state



32
33
34
35
# File 'lib/product_taxonomy/models/value.rb', line 32

def reset
  @localizations = nil
  @hashed_models = nil
end

.sort_by_localized_name(values, locale: "en") ⇒ Array<Value>

Sort values by their localized name.

Parameters:

  • values (Array<Value>)

    The values to sort.

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

    The locale to sort by.

Returns:

  • (Array<Value>)

    The sorted values.



42
43
44
45
46
47
48
49
50
# File 'lib/product_taxonomy/models/value.rb', line 42

def sort_by_localized_name(values, locale: "en")
  values.sort_by.with_index do |value, idx|
    [
      value.name(locale: "en").downcase == "other" ? 1 : 0,
      *AlphanumericSorter.normalize_value(value.name(locale:)),
      idx,
    ]
  end
end

Instance Method Details

#full_name(locale: "en") ⇒ String

Get the full name of the value, including the primary attribute.

Parameters:

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

    The locale to get the name in.

Returns:

  • (String)

    The full name of the value.



113
114
115
# File 'lib/product_taxonomy/models/value.rb', line 113

def full_name(locale: "en")
  "#{name(locale:)} [#{primary_attribute.name(locale:)}]"
end

#gidObject



98
99
100
# File 'lib/product_taxonomy/models/value.rb', line 98

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

#primary_attributeProductTaxonomy::Attribute

Get the primary attribute for this value.

Returns:



105
106
107
# File 'lib/product_taxonomy/models/value.rb', line 105

def primary_attribute
  @primary_attribute ||= Attribute.find_by(friendly_id: primary_attribute_friendly_id)
end