Class: ProductTaxonomy::Attribute
- Inherits:
-
Object
- Object
- ProductTaxonomy::Attribute
- Includes:
- ActiveModel::Validations, FormattedValidationErrors
- Defined in:
- lib/product_taxonomy/models/attribute.rb
Direct Known Subclasses
Constant Summary
Constants included from Indexed
Instance Attribute Summary collapse
-
#extended_attributes ⇒ Object
readonly
Returns the value of attribute extended_attributes.
-
#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.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
-
.load_from_source(source_data) ⇒ Object
Load attributes from source data.
-
.next_id ⇒ Integer
Get the next ID for a newly created attribute.
-
.reset ⇒ Object
Reset all class-level state.
-
.sorted_base_attributes ⇒ Array<Attribute>
Get base attributes only, sorted by name.
Instance Method Summary collapse
-
#add_extended_attribute(extended_attribute) ⇒ Object
Add an extended attribute to the attribute.
-
#add_value(value) ⇒ Object
Add a value to the attribute.
-
#extended? ⇒ Boolean
Whether the attribute is an extended attribute.
-
#gid ⇒ String
The global ID of the attribute.
-
#initialize(id:, name:, description:, friendly_id:, handle:, values:, is_manually_sorted: false) ⇒ Attribute
constructor
ID instead.
-
#manually_sorted? ⇒ Boolean
Whether the attribute is manually sorted.
-
#sorted_values(locale: "en") ⇒ Array<Value>
Get the sorted values of the attribute.
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:, description:, friendly_id:, handle:, values:, is_manually_sorted: false) ⇒ Attribute
ID instead.
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_attributes ⇒ Object (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_id ⇒ Object (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 |
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
89 90 91 |
# File 'lib/product_taxonomy/models/attribute.rb', line 89 def handle @handle end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
89 90 91 |
# File 'lib/product_taxonomy/models/attribute.rb', line 89 def id @id end |
#values ⇒ Object (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.
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_id ⇒ Integer
Get the next ID for a newly created attribute.
49 |
# File 'lib/product_taxonomy/models/attribute.rb', line 49 def next_id = (all.max_by(&:id)&.id || 0) + 1 |
.reset ⇒ Object
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_attributes ⇒ Array<Attribute>
Get base attributes only, sorted by name.
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.
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.
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.
133 134 135 |
# File 'lib/product_taxonomy/models/attribute.rb', line 133 def extended? is_a?(ExtendedAttribute) end |
#gid ⇒ String
The global ID of the attribute
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.
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.
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 |