Class: ProductTaxonomy::Value
- Inherits:
-
Object
- Object
- ProductTaxonomy::Value
- 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
Instance Attribute Summary collapse
-
#friendly_id ⇒ Object
readonly
Returns the value of attribute friendly_id.
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.all_values_sorted ⇒ Array<Value>
Sort values according to their English name, taking "other" into account.
-
.load_from_source(source_data) ⇒ Object
Load values from source data.
-
.next_id ⇒ Integer
Get the next ID for a newly created value.
-
.reset ⇒ Object
Reset all class-level state.
-
.sort_by_localized_name(values, locale: "en") ⇒ Array<Value>
Sort values by their localized name.
Instance Method Summary collapse
-
#full_name(locale: "en") ⇒ String
Get the full name of the value, including the primary attribute.
- #gid ⇒ Object
-
#initialize(id:, name:, friendly_id:, handle:) ⇒ Value
constructor
A new instance of Value.
-
#primary_attribute ⇒ ProductTaxonomy::Attribute
Get the primary attribute for this value.
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
Constructor Details
#initialize(id:, name:, friendly_id:, handle:) ⇒ Value
Returns a new instance of 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_id ⇒ Object (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 |
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
85 86 87 |
# File 'lib/product_taxonomy/models/value.rb', line 85 def handle @handle end |
#id ⇒ Object (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_sorted ⇒ Array<Value>
Sort values according to their English name, taking "other" into account.
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.
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_id ⇒ Integer
Get the next ID for a newly created value.
69 |
# File 'lib/product_taxonomy/models/value.rb', line 69 def next_id = (all.max_by(&:id)&.id || 0) + 1 |
.reset ⇒ Object
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.
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.
113 114 115 |
# File 'lib/product_taxonomy/models/value.rb', line 113 def full_name(locale: "en") "#{name(locale:)} [#{primary_attribute.name(locale:)}]" end |
#gid ⇒ Object
98 99 100 |
# File 'lib/product_taxonomy/models/value.rb', line 98 def gid "gid://shopify/TaxonomyValue/#{id}" end |
#primary_attribute ⇒ ProductTaxonomy::Attribute
Get the primary attribute for this value.
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 |