Class: Crm::FaqRenderedWhereComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
app/components/crm/faq_rendered_where_component.rb

Overview

Shows ALL locations where a FAQ appears: standalone page, blog embeds, product/support pages, landing pages.

Usage:
<%= render Crm::FaqRenderedWhereComponent.new(article: @article) %>

Constant Summary collapse

LANDING_PAGE_TAG_PATTERN =
/\Afor-(.+)-page\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(article:) ⇒ FaqRenderedWhereComponent

Returns a new instance of FaqRenderedWhereComponent.



15
16
17
18
# File 'app/components/crm/faq_rendered_where_component.rb', line 15

def initialize(article:)
  super()
  @article = article
end

Instance Attribute Details

#articleObject (readonly)

Returns the value of attribute article.



13
14
15
# File 'app/components/crm/faq_rendered_where_component.rb', line 13

def article
  @article
end

Instance Method Details

#before_renderObject



20
21
22
23
24
25
26
27
# File 'app/components/crm/faq_rendered_where_component.rb', line 20

def before_render
  super
  pls = article.product_lines.to_a
  return if pls.empty?

  paths = ProductLine.canonical_paths_for(pls)
  pls.each { |pl| pl.instance_variable_set(:@canonical_path, paths[pl.id]) }
end

#blog_embedsObject



35
36
37
38
39
40
41
42
# File 'app/components/crm/faq_rendered_where_component.rb', line 35

def blog_embeds
  @blog_embeds ||= EmbeddedFaqAsset
    .where(asset_id: article.id, asset_type: 'Article', parent_type: 'Article')
    .joins('INNER JOIN articles ON articles.id = embedded_assets.parent_id')
    .where(articles: { type: 'Post' })
    .includes(:parent)
    .order(:created_at)
end

#crm_post_path(post) ⇒ Object



88
89
90
# File 'app/components/crm/faq_rendered_where_component.rb', line 88

def crm_post_path(post)
  helpers.post_path(post)
end

#derive_landing_path_from_tag(tag) ⇒ Object

Fallback when DigitalAsset mapping unavailable: remove for- prefix and -page suffix, convert hyphens to slashes



69
70
71
72
# File 'app/components/crm/faq_rendered_where_component.rb', line 69

def derive_landing_path_from_tag(tag)
  page_slug = tag.sub(/\Afor-/, "").sub(/-page\z/, "")
  "/#{page_slug.tr('-', '/')}"
end

#landing_page_pathsObject



59
60
61
62
63
64
65
66
# File 'app/components/crm/faq_rendered_where_component.rb', line 59

def landing_page_paths
  tag_to_path = defined?(DigitalAsset) ? DigitalAsset.available_page_tags_with_paths : {}
  article.tags.filter_map do |tag|
    next unless tag.to_s.match?(LANDING_PAGE_TAG_PATTERN)

    tag_to_path[tag.to_s].presence || derive_landing_path_from_tag(tag.to_s)
  end.uniq
end

#post_title(post) ⇒ Object



98
99
100
# File 'app/components/crm/faq_rendered_where_component.rb', line 98

def post_title(post)
  post.title.presence || post.subject
end

#product_line_urlsObject



44
45
46
47
48
# File 'app/components/crm/faq_rendered_where_component.rb', line 44

def product_line_urls
  return [] unless article.sales? && article.product_lines.any?

  article.product_lines.map { |pl| { url: pl.slug_ltree.to_s, path: "/#{pl.canonical_path}" } }.uniq { |h| h[:url] }
end

#public_post_url(post) ⇒ Object



92
93
94
95
96
# File 'app/components/crm/faq_rendered_where_component.rb', line 92

def public_post_url(post)
  return nil unless post.published? && post.slug.present?

  "#{WEB_URL}/#{post.slug}"
end

#standalone_locationObject



29
30
31
32
33
# File 'app/components/crm/faq_rendered_where_component.rb', line 29

def standalone_location
  return nil unless article.slug.present? && article.published?

  { path: "/faqs/#{article.slug}", url: "#{WEB_URL}/faqs/#{article.slug}" }
end

#support_urlsObject



50
51
52
53
54
55
56
57
# File 'app/components/crm/faq_rendered_where_component.rb', line 50

def support_urls
  return [] unless article.support? && article.product_lines.any?

  article.product_lines.filter_map { |pl|
    next unless pl.canonical_path
    { url: pl.slug_ltree.to_s, path: "/#{pl.canonical_path}/support" }
  }.uniq { |h| h[:url] }
end

#total_locationsObject



74
75
76
77
78
79
80
81
82
# File 'app/components/crm/faq_rendered_where_component.rb', line 74

def total_locations
  count = 0
  count += 1 if standalone_location.present?
  count += blog_embeds.size
  count += product_line_urls.size
  count += support_urls.size
  count += landing_page_paths.size
  count
end

#word_countObject



84
85
86
# File 'app/components/crm/faq_rendered_where_component.rb', line 84

def word_count
  article.faq_word_count
end