Module: Models::ItemAmazonHelper

Extended by:
ActiveSupport::Concern
Includes:
Memery
Included in:
Item
Defined in:
app/concerns/models/item_amazon_helper.rb

Overview

Helpers for items that are (or may be) listed on Amazon.

Encapsulates Amazon-specific attribute accessors, marketplace links, image
profiles, and ASIN synchronization.

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.with_asinActiveRecord::Relation<Models::ItemAmazonHelper>

A relation of Models::ItemAmazonHelpers that are with asin. Active Record Scope

Returns:

See Also:



14
# File 'app/concerns/models/item_amazon_helper.rb', line 14

scope :with_asin, -> { where.not(amazon_asin: nil) }

Instance Method Details

#all_amazon_image_profilesActiveRecord::Relation<ImageProfile>

Amazon image profiles linked to this item.

Returns:



61
62
63
# File 'app/concerns/models/item_amazon_helper.rb', line 61

def all_amazon_image_profiles
  image_profiles.amazon_image_profiles.image_order.joins(:image).includes(:image)
end

#amazon_descriptionString?

Amazon listing description for this item.

Returns:

  • (String, nil)


120
121
122
# File 'app/concerns/models/item_amazon_helper.rb', line 120

def amazon_description
  spec_output(:amazon_description)
end

#amazon_description=(val) ⇒ ProductSpecification?

Sets the Amazon listing description for this item.

Parameters:

  • val (String, nil)

    the new description

Returns:



128
129
130
# File 'app/concerns/models/item_amazon_helper.rb', line 128

def amazon_description=(val)
  create_or_set_amazon_spec_value(name: 'Description', text_blurb: val)
end

#amazon_family_itemsActiveRecord::Relation<Item>

Items that share this item's per-marketplace Amazon parent listing(s) — the
VariantGroup amazon rows linked via catalog_items.variant_group_id (e.g. for
image-profile fan-out across the family). Distinct from Item#variant_group,
which is the item's channel-agnostic DEFAULT family.

Returns:

  • (ActiveRecord::Relation<Item>)


39
40
41
42
43
44
# File 'app/concerns/models/item_amazon_helper.rb', line 39

def amazon_family_items
  group_ids = catalog_items.where.not(variant_group_id: nil).distinct.pluck(:variant_group_id)
  return Item.none if group_ids.empty?

  Item.where(id: CatalogItem.where(variant_group_id: group_ids).joins(:store_item).distinct.select('store_items.item_id'))
end

#amazon_feature_1String?

First Amazon bullet/feature for this item.

Returns:

  • (String, nil)


135
136
137
# File 'app/concerns/models/item_amazon_helper.rb', line 135

def amazon_feature_1
  spec_output(:amazon_feature_1)
end

#amazon_feature_1=(val) ⇒ ProductSpecification?

Sets the first Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



143
144
145
# File 'app/concerns/models/item_amazon_helper.rb', line 143

def amazon_feature_1=(val)
  create_or_set_amazon_spec_value(name: 'Feature 1', text_blurb: val)
end

#amazon_feature_2String?

Second Amazon bullet/feature for this item.

Returns:

  • (String, nil)


150
151
152
# File 'app/concerns/models/item_amazon_helper.rb', line 150

def amazon_feature_2
  spec_output(:amazon_feature_2)
end

#amazon_feature_2=(val) ⇒ ProductSpecification?

Sets the second Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



158
159
160
# File 'app/concerns/models/item_amazon_helper.rb', line 158

def amazon_feature_2=(val)
  create_or_set_amazon_spec_value(name: 'Feature 2', text_blurb: val)
end

#amazon_feature_3String?

Third Amazon bullet/feature for this item.

Returns:

  • (String, nil)


165
166
167
# File 'app/concerns/models/item_amazon_helper.rb', line 165

def amazon_feature_3
  spec_output(:amazon_feature_3)
end

#amazon_feature_3=(val) ⇒ ProductSpecification?

Sets the third Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



173
174
175
# File 'app/concerns/models/item_amazon_helper.rb', line 173

def amazon_feature_3=(val)
  create_or_set_amazon_spec_value(name: 'Feature 3', text_blurb: val)
end

#amazon_feature_4String?

Fourth Amazon bullet/feature for this item.

Returns:

  • (String, nil)


180
181
182
# File 'app/concerns/models/item_amazon_helper.rb', line 180

def amazon_feature_4
  spec_output(:amazon_feature_4)
end

#amazon_feature_4=(val) ⇒ ProductSpecification?

Sets the fourth Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



188
189
190
# File 'app/concerns/models/item_amazon_helper.rb', line 188

def amazon_feature_4=(val)
  create_or_set_amazon_spec_value(name: 'Feature 4', text_blurb: val)
end

#amazon_feature_5String?

Fifth Amazon bullet/feature for this item.

Returns:

  • (String, nil)


195
196
197
# File 'app/concerns/models/item_amazon_helper.rb', line 195

def amazon_feature_5
  spec_output(:amazon_feature_5)
end

#amazon_feature_5=(val) ⇒ ProductSpecification?

Sets the fifth Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



203
204
205
# File 'app/concerns/models/item_amazon_helper.rb', line 203

def amazon_feature_5=(val)
  create_or_set_amazon_spec_value(name: 'Feature 5', text_blurb: val)
end

#amazon_feature_6String?

Sixth Amazon bullet/feature for this item.

Returns:

  • (String, nil)


210
211
212
# File 'app/concerns/models/item_amazon_helper.rb', line 210

def amazon_feature_6
  spec_output(:amazon_feature_6)
end

#amazon_feature_6=(val) ⇒ ProductSpecification?

Sets the sixth Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



218
219
220
# File 'app/concerns/models/item_amazon_helper.rb', line 218

def amazon_feature_6=(val)
  create_or_set_amazon_spec_value(name: 'Feature 6', text_blurb: val)
end

#amazon_feature_7String?

Seventh Amazon bullet/feature for this item.

Returns:

  • (String, nil)


225
226
227
# File 'app/concerns/models/item_amazon_helper.rb', line 225

def amazon_feature_7
  spec_output(:amazon_feature_7)
end

#amazon_feature_7=(val) ⇒ ProductSpecification?

Sets the seventh Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



233
234
235
# File 'app/concerns/models/item_amazon_helper.rb', line 233

def amazon_feature_7=(val)
  create_or_set_amazon_spec_value(name: 'Feature 7', text_blurb: val)
end

#amazon_feature_8String?

Eighth Amazon bullet/feature for this item.

Returns:

  • (String, nil)


240
241
242
# File 'app/concerns/models/item_amazon_helper.rb', line 240

def amazon_feature_8
  spec_output(:amazon_feature_8)
end

#amazon_feature_8=(val) ⇒ ProductSpecification?

Sets the eighth Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



248
249
250
# File 'app/concerns/models/item_amazon_helper.rb', line 248

def amazon_feature_8=(val)
  create_or_set_amazon_spec_value(name: 'Feature 8', text_blurb: val)
end

#amazon_feature_9String?

Ninth Amazon bullet/feature for this item.

Returns:

  • (String, nil)


255
256
257
# File 'app/concerns/models/item_amazon_helper.rb', line 255

def amazon_feature_9
  spec_output(:amazon_feature_9)
end

#amazon_feature_9=(val) ⇒ ProductSpecification?

Sets the ninth Amazon bullet/feature for this item.

Parameters:

  • val (String, nil)

    the new feature text

Returns:



263
264
265
# File 'app/concerns/models/item_amazon_helper.rb', line 263

def amazon_feature_9=(val)
  create_or_set_amazon_spec_value(name: 'Feature 9', text_blurb: val)
end

#amazon_featuresArray<String>

All populated Amazon bullet/features for this item.

Returns:

  • (Array<String>)


270
271
272
# File 'app/concerns/models/item_amazon_helper.rb', line 270

def amazon_features
  [amazon_feature_1, amazon_feature_2, amazon_feature_3, amazon_feature_4, amazon_feature_5, amazon_feature_6, amazon_feature_7, amazon_feature_8, amazon_feature_9].filter_map(&:presence).uniq
end

#amazon_generic_keywordString?

Generic search keywords for the Amazon listing.

Returns:

  • (String, nil)


98
99
100
# File 'app/concerns/models/item_amazon_helper.rb', line 98

def amazon_generic_keyword
  spec_output(:amazon_keywords) || spec_output(:amazon_generic_keyword)
end

#amazon_generic_keyword=(val) ⇒ ProductSpecification?

Sets the generic search keywords for the Amazon listing.

Parameters:

  • val (String, nil)

    the new keywords

Returns:



113
114
115
# File 'app/concerns/models/item_amazon_helper.rb', line 113

def amazon_generic_keyword=(val)
  create_or_set_amazon_spec_value(name: 'Generic Keyword', text_blurb: val)
end

#amazon_marketplacesActiveRecord::Relation<AmazonMarketplace>

Marketplaces this item is listed in through its Amazon catalog items.

Returns:

See Also:



23
# File 'app/concerns/models/item_amazon_helper.rb', line 23

has_many :amazon_marketplaces, -> { distinct }, through: :catalog_items

#amazon_product_typesArray<String>

Desired Amazon product types across this item's catalog items.

Returns:

  • (Array<String>)


291
292
293
# File 'app/concerns/models/item_amazon_helper.rb', line 291

def amazon_product_types
  catalog_items.where.not(amazon_desired_product_type: nil).distinct.order(:amazon_desired_product_type).pluck(:amazon_desired_product_type)
end

#amazon_target_keywordsString?

Target keywords for the Amazon listing.

Returns:

  • (String, nil)


105
106
107
# File 'app/concerns/models/item_amazon_helper.rb', line 105

def amazon_target_keywords
  spec_output(:amazon_target_keywords)
end

#amazon_titleString?

Amazon listing title for this item.

Returns:

  • (String, nil)


68
69
70
# File 'app/concerns/models/item_amazon_helper.rb', line 68

def amazon_title
  spec_output(:amazon_title)
end

#amazon_title=(val) ⇒ ProductSpecification?

Sets the Amazon listing title for this item.

Parameters:

  • val (String, nil)

    the new title

Returns:



76
77
78
# File 'app/concerns/models/item_amazon_helper.rb', line 76

def amazon_title=(val)
  create_or_set_amazon_spec_value(name: 'Title', text_blurb: val)
end

#amazon_title_differentiationString?

Title differentiation text for the Amazon listing.

Returns:

  • (String, nil)


83
84
85
# File 'app/concerns/models/item_amazon_helper.rb', line 83

def amazon_title_differentiation
  spec_output(:amazon_title_differentiation)
end

#amazon_title_differentiation=(val) ⇒ ProductSpecification?

Sets the title differentiation text for the Amazon listing.

Parameters:

  • val (String, nil)

    the new differentiation text

Returns:



91
92
93
# File 'app/concerns/models/item_amazon_helper.rb', line 91

def amazon_title_differentiation=(val)
  create_or_set_amazon_spec_value(name: 'Title Differentiation', text_blurb: val)
end

#amazon_transparency_codesActiveRecord::Relation<AmazonTransparencyCode>

The legacy item->variation link is decommissioned: Amazon parents are
per-marketplace via catalog_items.variant_group_id (the VariantGroup amazon
row). The historical values are preserved on the renamed
items.legacy_amazon_variation_id column, but there is intentionally NO
association for them — calling item.amazon_variation now raises NoMethodError
so accidental use fails loudly. (See RenameItemsAmazonVariationIdToLegacy.)

Returns:

See Also:



21
# File 'app/concerns/models/item_amazon_helper.rb', line 21

has_many :amazon_transparency_codes, inverse_of: :item, dependent: :destroy

#create_or_set_amazon_spec_value(name:, text_blurb:, locale: Mobility.locale, skip_spec_refresh: true, link_to_product_line: false) ⇒ ProductSpecification?

Creates or updates an Amazon-specific product specification.

Parameters:

  • name (String)

    human-readable specification name

  • text_blurb (String)

    specification text

  • locale (Symbol, String) (defaults to: Mobility.locale)

    locale for the localized value

  • skip_spec_refresh (Boolean) (defaults to: true)

    whether to skip refreshing derived spec data

  • link_to_product_line (Boolean) (defaults to: false)

    whether to link the spec to the product line

Returns:



54
55
56
# File 'app/concerns/models/item_amazon_helper.rb', line 54

def create_or_set_amazon_spec_value(name:, text_blurb:, locale: Mobility.locale, skip_spec_refresh: true, link_to_product_line: false)
  create_or_set_spec_value(grouping: 'X-Amazon', text_blurb:, name:, locale:, skip_spec_refresh:, link_to_product_line:)
end

#description_for_amazonString?

Description to use for the Amazon listing, falling back to the public description.

Returns:

  • (String, nil)


298
299
300
# File 'app/concerns/models/item_amazon_helper.rb', line 298

def description_for_amazon
  amazon_description.presence || public_description_text.presence
end

#is_amazon_item?Boolean

Whether this item has Amazon listing data.

Returns:

  • (Boolean)


277
278
279
# File 'app/concerns/models/item_amazon_helper.rb', line 277

def is_amazon_item?
  amazon_asin.present? || catalog_items.amazons.exists?
end

#reset_amazon_fieldsInteger

Clears Amazon-specific product specifications from this item.

Returns:

  • (Integer)

    number of deleted specifications



305
306
307
308
309
# File 'app/concerns/models/item_amazon_helper.rb', line 305

def reset_amazon_fields
  fields = %i[amazon_title amazon_title_differentiation amazon_description amazon_generic_keyword]
  fields += (1..9).to_a.map { |n| :"amazon_feature_#{n}" }
  direct_product_specifications.delete_by(token: fields)
end

#sync_amazon_asinsvoid

This method returns an undefined value.

Propagates this item's ASIN to its Amazon catalog items after a save.



314
315
316
317
318
319
320
# File 'app/concerns/models/item_amazon_helper.rb', line 314

def sync_amazon_asins
  return unless saved_change_to_amazon_asin?

  catalog_items.amazons.each do |ci|
    ci.update!(third_party_part_number: amazon_asin)
  end
end

#title_for_amazonString

Title to use for the Amazon listing, falling back to the public name.

Returns:

  • (String)


284
285
286
# File 'app/concerns/models/item_amazon_helper.rb', line 284

def title_for_amazon
  amazon_title.presence || public_name&.first(250)
end