Class: ProductTaxonomy::Disclosure
- Inherits:
-
Object
- Object
- ProductTaxonomy::Disclosure
- Includes:
- ActiveModel::Validations, FormattedValidationErrors
- Defined in:
- lib/product_taxonomy/models/disclosure.rb
Overview
A legally-required product disclosure (e.g. a safety warning or chemical
exposure notice). Disclosures form a shallow hierarchy: grouping/root nodes
(no parent_public_id) organize the axis, and leaf nodes carry the
jurisdiction information, plus the optional copy that merchants surface.
Constant Summary collapse
- SURFACES =
Surfaces on which a disclosure may be displayed.
["product_page", "cart", "checkout"].freeze
- PUBLIC_ID_FORMAT =
public_idmust be a lowercase slug: alphanumeric segments joined by-or_. /\A[a-z0-9]+(?:[_-][a-z0-9]+)*\z/- STRING_FIELDS =
String-typed fields validated for type on load.
[ :name, :internal_label, :description, :legal_citation, :symbol, :display_requirements, :title, :content, :source, ].freeze
- LEAF_REQUIRED_FIELDS =
Fields required on every leaf disclosure, in addition to jurisdictions.
[:legal_citation, :source].freeze
- DISPLAY_REQUIRED_FIELDS =
Fields required on a leaf disclosure that names a display surface.
[:title, :content].freeze
Constants included from Indexed
Instance Attribute Summary collapse
-
#disclosure_attribute_values ⇒ Object
readonly
Returns the value of attribute disclosure_attribute_values.
-
#disclosure_attributes ⇒ Object
readonly
Returns the value of attribute disclosure_attributes.
-
#display_preferences ⇒ Object
readonly
Returns the value of attribute display_preferences.
-
#display_requirements ⇒ Object
readonly
Returns the value of attribute display_requirements.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#internal_label ⇒ Object
readonly
Returns the value of attribute internal_label.
-
#jurisdictions ⇒ Object
readonly
Returns the value of attribute jurisdictions.
-
#legal_citation ⇒ Object
readonly
Returns the value of attribute legal_citation.
-
#parent_public_id ⇒ Object
readonly
Returns the value of attribute parent_public_id.
-
#public_id ⇒ Object
readonly
Returns the value of attribute public_id.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
-
.load_from_source(source_data) ⇒ void
Load disclosures from source data.
-
.next_id ⇒ Integer
Get the next ID for a newly created disclosure.
-
.reset ⇒ Object
Reset all class-level state.
Instance Method Summary collapse
-
#children ⇒ Array<Disclosure>
The direct children of this disclosure.
-
#gid ⇒ String
The global ID of the disclosure.
-
#initialize(id:, public_id:, name:, internal_label:, parent_public_id: nil, description: nil, jurisdictions: nil, legal_citation: nil, symbol: nil, display_requirements: nil, display_preferences: nil, title: nil, content: nil, source: nil, disclosure_attributes: nil, disclosure_attribute_values: nil) ⇒ Disclosure
constructor
A new instance of Disclosure.
-
#leaf? ⇒ Boolean
Whether this is a leaf node (no other disclosure points at it).
-
#parent ⇒ Disclosure?
The parent disclosure, or nil for root nodes.
-
#root? ⇒ Boolean
Whether this is a grouping/root node (has no parent).
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:, public_id:, name:, internal_label:, parent_public_id: nil, description: nil, jurisdictions: nil, legal_citation: nil, symbol: nil, display_requirements: nil, display_preferences: nil, title: nil, content: nil, source: nil, disclosure_attributes: nil, disclosure_attribute_values: nil) ⇒ Disclosure
Returns a new instance of Disclosure.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 123 def initialize( id:, public_id:, name:, internal_label:, parent_public_id: nil, description: nil, jurisdictions: nil, legal_citation: nil, symbol: nil, display_requirements: nil, display_preferences: nil, title: nil, content: nil, source: nil, disclosure_attributes: nil, disclosure_attribute_values: nil ) @id = id @public_id = public_id @parent_public_id = parent_public_id @name = name @internal_label = internal_label @description = description @jurisdictions = jurisdictions @legal_citation = legal_citation @symbol = symbol @display_requirements = display_requirements @display_preferences = display_preferences @title = title @content = content @source = source @disclosure_attributes = disclosure_attributes @disclosure_attribute_values = disclosure_attribute_values end |
Instance Attribute Details
#disclosure_attribute_values ⇒ Object (readonly)
Returns the value of attribute disclosure_attribute_values.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def disclosure_attribute_values @disclosure_attribute_values end |
#disclosure_attributes ⇒ Object (readonly)
Returns the value of attribute disclosure_attributes.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def disclosure_attributes @disclosure_attributes end |
#display_preferences ⇒ Object (readonly)
Returns the value of attribute display_preferences.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def display_preferences @display_preferences end |
#display_requirements ⇒ Object (readonly)
Returns the value of attribute display_requirements.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def display_requirements @display_requirements end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def id @id end |
#internal_label ⇒ Object (readonly)
Returns the value of attribute internal_label.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def internal_label @internal_label end |
#jurisdictions ⇒ Object (readonly)
Returns the value of attribute jurisdictions.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def jurisdictions @jurisdictions end |
#legal_citation ⇒ Object (readonly)
Returns the value of attribute legal_citation.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def legal_citation @legal_citation end |
#parent_public_id ⇒ Object (readonly)
Returns the value of attribute parent_public_id.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def parent_public_id @parent_public_id end |
#public_id ⇒ Object (readonly)
Returns the value of attribute public_id.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def public_id @public_id end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def source @source end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
110 111 112 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 110 def symbol @symbol end |
Class Method Details
.load_from_source(source_data) ⇒ void
This method returns an undefined value.
Load disclosures from source data. By default this is deserialized from
data/disclosures.yml.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 45 def load_from_source(source_data) raise ArgumentError, "source_data must be an array" unless source_data.is_a?(Array) source_data.each do |disclosure_data| raise ArgumentError, "source_data must contain hashes" unless disclosure_data.is_a?(Hash) disclosure = disclosure_from(disclosure_data) Disclosure.add(disclosure) disclosure.validate!(:create) end end |
.next_id ⇒ Integer
Get the next ID for a newly created disclosure.
66 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 66 def next_id = (all.max_by(&:id)&.id || 0) + 1 |
.reset ⇒ Object
Reset all class-level state.
58 59 60 61 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 58 def reset @localizations = nil @hashed_models = nil end |
Instance Method Details
#children ⇒ Array<Disclosure>
The direct children of this disclosure.
183 184 185 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 183 def children Disclosure.all.select { |disclosure| disclosure.parent_public_id == public_id } end |
#gid ⇒ String
The global ID of the disclosure.
162 163 164 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 162 def gid "gid://shopify/TaxonomyDisclosure/#{id}" end |
#leaf? ⇒ Boolean
Whether this is a leaf node (no other disclosure points at it). Leaf nodes are the ones that carry jurisdiction and display information.
191 192 193 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 191 def leaf? children.empty? end |
#parent ⇒ Disclosure?
The parent disclosure, or nil for root nodes.
176 177 178 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 176 def parent parent_public_id && Disclosure.find_by(public_id: parent_public_id) end |
#root? ⇒ Boolean
Whether this is a grouping/root node (has no parent).
169 170 171 |
# File 'lib/product_taxonomy/models/disclosure.rb', line 169 def root? parent_public_id.nil? end |