Module: Models::Publication::ClassMethods

Defined in:
app/concerns/models/publication.rb

Overview

ActiveSupport::Concern mixin: class methods.

Instance Method Summary collapse

Instance Method Details

#convert_names_to_localesArray<String>

One-off migration: strip language words out of legacy publication names and
move them into publication_locales. Returns a log line per publication.

Returns:

  • (Array<String>)

    "Old name: ... -> ..." log lines



981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'app/concerns/models/publication.rb', line 981

def convert_names_to_locales
  # e.g "TempZone Cable Bilingual Manual  - French - English 2021"
  # "Towel Warmer Hardwired Series TWS6 Grande, Maple, Milan Installation Manual English"
  res = []

  Item.publications.each do |pub|
    old_name = pub.name
    new_name = pub.name
    publication_locales = pub.publication_locales || []
    changed = false
    { 'english' => 'en', 'french' => 'fr', 'spanish' => 'es' }.each do |language, locale|
      r = Regexp.new(language, 'i')
      next unless r.match?(new_name)

      new_name = new_name.gsub(r, '')
      publication_locales |= [locale]
      changed = true
    end
    # Remove version
    new_name = new_name.gsub(/\( version\)/i, '')
    # remove reference to Bilingual
    new_name = new_name.gsub(/bilingual/i, '')
    # Squeeze double dashing
    new_name = new_name.squeeze('-')
    # Remove empty parenthesis
    new_name = new_name.gsub(/\(\s?\)/, '')
    # Remove weird double dashing
    new_name = new_name.gsub(/-\s*-/, '')
    # Remove any odd ending characters
    new_name = new_name.gsub(/[-_\s]$/, '')
    # how dare you?
    new_name = new_name.gsub('Warmly Yours', 'WarmlyYours')
    # Squeeze double spacing
    new_name = new_name.squeeze(' ')
    # A default
    publication_locales = ['en'] if publication_locales.blank?

    pub.publication_base_name = new_name
    pub.publication_locales = publication_locales
    pub.update_columns(publication_base_name: new_name, publication_locales: publication_locales, name: pub.compose_name_with_languages)
    res << "Old name: #{old_name} -> #{new_name} -> #{publication_locales.join(', ')}"
  end
  res
end

#embeddable_publicationsActiveRecord::Relation<Item>

Scope for embeddable publications

Returns:

  • (ActiveRecord::Relation<Item>)

    active publications with extracted text



930
931
932
# File 'app/concerns/models/publication.rb', line 930

def embeddable_publications
  publications.active.with_publication_attached.where.not(search_text: [nil, ''])
end

#find_publication(sku, store) ⇒ Item?

Find the public-facing publication for a SKU in a store, walking the
lookup fallbacks: exact revision match, revision of a discontinued record,
SKU alias, canonical-SKU latest revision, bare numeric id.

Parameters:

  • sku (String)

    SKU (possibly with a trailing revision letter) or id

  • store (Store)

    the store the publication must be public in

Returns:

  • (Item, nil)

    the matching publication



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'app/concerns/models/publication.rb', line 952

def find_publication(sku, store)
  publication = nil
  sku_match = sku.match(/(.*)-([[:alpha:]])$/) # look for a revision at the end
  store_id = store.id
  publication_all_scope = Item.publications.in_store(store_id) # Including inactive ones
  publication_scope = Item.publications_for_public_in_store(store_id)
  # Analyze sku for revisions publications.with_publication_attached.active.in_store(1,2)
  if sku_match # means we have a properly formed sku + revision and will perform a direct match
    publication = publication_scope.find_by(sku: sku)
    # perhaps the old version was obsoleted, let's try a direct search and look for a revision
    if publication.nil? && (legacy_pub = publication_all_scope.find_by(sku: sku))
      publication = legacy_pub.revised_publication_for_public
    end
    # Or perhaps it is now an old alias
    publication ||= publication_scope.sku_aliases_search(sku).first
    # Look for the canonical part of the sku and most recent revision
    if publication.nil? && (canonical_sku = sku_match.try(:[], 1))
      publication = publication_scope.canonical_search(canonical_sku).first
    end
  elsif /^\d+$/.match?(sku) # Maybe just an ID?
    publication ||= publication_scope.find_by(id: sku)
  end
  publication ||= publication_scope.canonical_search(sku).first
  publication
end

#find_publications(skus, store) ⇒ Item?

Provide an array of publications sku, finds the first one available based on the store

Parameters:

  • skus (String, Array<String>)

    candidate SKUs, tried in order

  • store (Store)

    the store the publication must be public in

Returns:

  • (Item, nil)

    the first matching publication



938
939
940
941
942
943
944
# File 'app/concerns/models/publication.rb', line 938

def find_publications(skus, store)
  pub = nil
  [skus].flatten.each do |sku|
    break if (pub = find_publication(sku, store))
  end
  pub
end