Module: Models::Publication::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#convert_names_to_localesObject



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# File 'app/concerns/models/publication.rb', line 595

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_publicationsObject

Scope for embeddable publications



556
557
558
# File 'app/concerns/models/publication.rb', line 556

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

#find_publication(sku, store) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'app/concerns/models/publication.rb', line 569

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) ⇒ Object

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



561
562
563
564
565
566
567
# File 'app/concerns/models/publication.rb', line 561

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