Module: Www::PublicationsHelper
- Includes:
- Memery
- Included in:
- DesktopMenuComponent, MobileMenuComponent
- Defined in:
- app/helpers/www/publications_helper.rb
Instance Method Summary collapse
-
#find_publication_by_locale(*skus) ⇒ Object
Given a list of skus, find the best match for the locale in play.
-
#find_publication_url(*skus) ⇒ Object
Pass an array of publication sku, i return the url for the first matching one based on the locale / store availability.
-
#find_publications_by_tag_in_current_locale(tag) ⇒ Object
Find publications by tag in current locale NOTE: Eager loads :primary_image to avoid N+1 when rendering publication cards.
- #publication_link(sku, within_list = true, new_tab = nil) ⇒ Object
- #publication_public_url(item) ⇒ Object
- #render_docs(docs, grouped = false) ⇒ Object
- #render_docs_by_sku(skus = [], hide_header = false) ⇒ Object
- #render_publication_card_with_thumbnail(*publication_skus, thumbnail_image_id: nil, thumbnail_transform_options: {}) ⇒ Object
Instance Method Details
#find_publication_by_locale(*skus) ⇒ Object
Given a list of skus, find the best match for the locale in play
32 33 34 35 36 37 38 |
# File 'app/helpers/www/publications_helper.rb', line 32 def find_publication_by_locale(*skus) Array(skus).each do |sku| pub = find_publication(sku, locale_store) return pub if pub end nil end |
#find_publication_url(*skus) ⇒ Object
Pass an array of publication sku, i return the url for the first matching one based
on the locale / store availability
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/helpers/www/publications_helper.rb', line 11 def find_publication_url(*skus) path = nil skus.each do |sku| if (pub = find_publication(sku, locale_store)) path = www_publication_path(pub.sku, format: :pdf) break end end unless path msg = "None of #{skus.join(',')} is not found in find_publication_url in store #{locale_store.id}" Rails.logger.warn msg ErrorReporting.error(msg) if Rails.env.production? end if block_given? yield(path) else path end end |
#find_publications_by_tag_in_current_locale(tag) ⇒ Object
Find publications by tag in current locale
NOTE: Eager loads :primary_image to avoid N+1 when rendering publication cards
87 88 89 90 91 92 93 |
# File 'app/helpers/www/publications_helper.rb', line 87 def find_publications_by_tag_in_current_locale(tag) Item.publications .publications_for_public_in_store(locale_store.id) .tagged_with(tag) .includes(:primary_image) .order(:name) end |
#publication_link(sku, within_list = true, new_tab = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'app/helpers/www/publications_helper.rb', line 40 def publication_link(sku, within_list = true, new_tab = nil) return unless pub = find_publication(sku, locale_store) link = fa_icon('file-lines', class: 'icon-li', text: link_to(pub.public_name, www_publication_path(pub.sku, format: :pdf), target: new_tab)) if within_list content_tag(:li, link) else link end end |
#publication_public_url(item) ⇒ Object
4 5 6 |
# File 'app/helpers/www/publications_helper.rb', line 4 def publication_public_url(item) delocalized_path(url_for(action: 'show', id: item.sku, host: request.host.gsub(/^crm\./, 'www.'), controller: 'publications')) end |
#render_docs(docs, grouped = false) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/helpers/www/publications_helper.rb', line 51 def render_docs(docs, grouped = false) html = '' if grouped == true docs = docs.group_by { |p| p.product_category } docs = Hash[docs.sort_by { |cat, docs| cat.name }] docs.each do |category, publications| html << content_tag(:h4, category.name) pubs_html = '' publications.sort_by { |p| p.public_name }.each do |pub| pubs_html << content_tag(:li, link_to(pub.public_name, www_publication_path(pub.sku, format: :pdf))) end html << content_tag(:ul, pubs_html.html_safe, class: 'list-unstyled icons-ul') end else docs.each do |pub| pubs_html << content_tag(:li, link_to(pub.public_name, www_publication_path(pub.sku, format: :pdf))) end html << content_tag(:ul, pubs_html.html_safe, class: 'list-unstyled icons-ul') end html.html_safe end |
#render_docs_by_sku(skus = [], hide_header = false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/helpers/www/publications_helper.rb', line 73 def render_docs_by_sku(skus = [], hide_header = false) html = '' pubs_html = '' skus.each do |sku| pub = find_publication(sku, locale_store) pubs_html << content_tag(:li, link_to(pub.public_name, www_publication_path(pub.sku, format: :pdf))) unless pub.nil? end html << content_tag(:h4, 'Documents', class: 'fw-light h5') unless hide_header html << content_tag(:ul, pubs_html.html_safe, class: 'list-unstyled icons-ul') html.html_safe end |
#render_publication_card_with_thumbnail(*publication_skus, thumbnail_image_id: nil, thumbnail_transform_options: {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/helpers/www/publications_helper.rb', line 95 def render_publication_card_with_thumbnail(*publication_skus, thumbnail_image_id: nil, thumbnail_transform_options: {}) publication = find_publication_by_locale(*publication_skus) return unless publication .reverse_merge({ width: 300, height: 300, thumbnail: true }) thumbnail_path = Image.friendly.find(thumbnail_image_id)&.image_url(**) if thumbnail_image_id thumbnail_path ||= publication_path(publication.sku, format: :jpeg) render partial: 'pages/publication_thumbnail', locals: { publication:, thumbnail_path: } end |