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_localesObject



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'app/concerns/models/publication.rb', line 647

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



608
609
610
# File 'app/concerns/models/publication.rb', line 608

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

#find_publication(sku, store) ⇒ Object



621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'app/concerns/models/publication.rb', line 621

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



613
614
615
616
617
618
619
# File 'app/concerns/models/publication.rb', line 613

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