Class: ProductTaxonomy::Disclosure

Inherits:
Object
  • Object
show all
Extended by:
Indexed, Localized
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_id must 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

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:, 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_valuesObject (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_attributesObject (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_preferencesObject (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_requirementsObject (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

#idObject (readonly)

Returns the value of attribute id.



110
111
112
# File 'lib/product_taxonomy/models/disclosure.rb', line 110

def id
  @id
end

#internal_labelObject (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

#jurisdictionsObject (readonly)

Returns the value of attribute jurisdictions.



110
111
112
# File 'lib/product_taxonomy/models/disclosure.rb', line 110

def jurisdictions
  @jurisdictions
end

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_idObject (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_idObject (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

#sourceObject (readonly)

Returns the value of attribute source.



110
111
112
# File 'lib/product_taxonomy/models/disclosure.rb', line 110

def source
  @source
end

#symbolObject (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.

Parameters:

  • source_data (Array<Hash>)

    The source data to load disclosures from.

Raises:

  • (ArgumentError)


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_idInteger

Get the next ID for a newly created disclosure.

Returns:

  • (Integer)

    The next ID.



66
# File 'lib/product_taxonomy/models/disclosure.rb', line 66

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

.resetObject

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

#childrenArray<Disclosure>

The direct children of this disclosure.

Returns:



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

#gidString

The global ID of the disclosure.

Returns:

  • (String)


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.

Returns:

  • (Boolean)


191
192
193
# File 'lib/product_taxonomy/models/disclosure.rb', line 191

def leaf?
  children.empty?
end

#parentDisclosure?

The parent disclosure, or nil for root nodes.

Returns:



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).

Returns:

  • (Boolean)


169
170
171
# File 'lib/product_taxonomy/models/disclosure.rb', line 169

def root?
  parent_public_id.nil?
end