Class: Crm::FaqRenderedWhereComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Crm::FaqRenderedWhereComponent
- 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 =
Regex pattern matching landing page tag.
/\Afor-(.+)-page\z/
Instance Attribute Summary collapse
-
#article ⇒ Article
readonly
The FAQ article whose locations are being listed.
Instance Method Summary collapse
-
#before_render ⇒ void
Preloads canonical paths for the article's product lines before rendering.
-
#blog_embeds ⇒ ActiveRecord::Relation<EmbeddedFaqAsset>
Blog posts embedding this FAQ.
-
#crm_post_path(post) ⇒ String
CRM path to the post.
-
#derive_landing_path_from_tag(tag) ⇒ String
Fallback when DigitalAsset mapping unavailable: remove for- prefix and -page suffix, convert hyphens to slashes.
-
#initialize(article:) ⇒ FaqRenderedWhereComponent
constructor
A new instance of FaqRenderedWhereComponent.
-
#landing_page_paths ⇒ Array<String>
Landing page paths derived from the article's
for-*-pagetags. -
#post_title(post) ⇒ String
The post title, falling back to its subject.
-
#product_line_urls ⇒ Array<Hash>
Sales product line URLs (
:url,:path) where the FAQ appears. -
#public_post_url(post) ⇒ String?
Public URL of the post, or nil when unpublished or slugless.
-
#standalone_location ⇒ Hash?
Path and public URL of the standalone FAQ page, or nil when unpublished.
-
#support_urls ⇒ Array<Hash>
Support product line URLs (
:url,:path) where the FAQ appears. -
#total_locations ⇒ Integer
Total number of locations where the FAQ appears.
-
#word_count ⇒ Integer
Word count of the FAQ body.
Constructor Details
#initialize(article:) ⇒ FaqRenderedWhereComponent
Returns a new instance of FaqRenderedWhereComponent.
17 18 19 20 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 17 def initialize(article:) super() @article = article end |
Instance Attribute Details
#article ⇒ Article (readonly)
Returns the FAQ article whose locations are being listed.
14 15 16 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 14 def article @article end |
Instance Method Details
#before_render ⇒ void
This method returns an undefined value.
Preloads canonical paths for the article's product lines before rendering.
24 25 26 27 28 29 30 31 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 24 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_embeds ⇒ ActiveRecord::Relation<EmbeddedFaqAsset>
Returns blog posts embedding this FAQ.
41 42 43 44 45 46 47 48 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 41 def @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) ⇒ String
Returns CRM path to the post.
104 105 106 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 104 def crm_post_path(post) helpers.post_path(post) end |
#derive_landing_path_from_tag(tag) ⇒ String
Fallback when DigitalAsset mapping unavailable: remove for- prefix and -page suffix, convert hyphens to slashes
81 82 83 84 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 81 def derive_landing_path_from_tag(tag) page_slug = tag.sub(/\Afor-/, "").sub(/-page\z/, "") "/#{page_slug.tr('-', '/')}" end |
#landing_page_paths ⇒ Array<String>
Returns landing page paths derived from the article's for-*-page tags.
69 70 71 72 73 74 75 76 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 69 def landing_page_paths tag_to_path = defined?(DigitalAsset) ? DigitalAsset. : {} article..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) ⇒ String
Returns the post title, falling back to its subject.
118 119 120 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 118 def post_title(post) post.title.presence || post.subject end |
#product_line_urls ⇒ Array<Hash>
Returns sales product line URLs (:url, :path) where the FAQ appears.
51 52 53 54 55 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 51 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) ⇒ String?
Returns public URL of the post, or nil when unpublished or slugless.
110 111 112 113 114 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 110 def public_post_url(post) return nil unless post.published? && post.slug.present? "#{WEB_URL}/#{post.slug}" end |
#standalone_location ⇒ Hash?
Returns path and public URL of the standalone FAQ page, or nil when unpublished.
34 35 36 37 38 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 34 def standalone_location return nil unless article.slug.present? && article.published? { path: "/faqs/#{article.slug}", url: "#{WEB_URL}/faqs/#{article.slug}" } end |
#support_urls ⇒ Array<Hash>
Returns support product line URLs (:url, :path) where the FAQ appears.
58 59 60 61 62 63 64 65 66 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 58 def support_urls return [] unless article.support? && article.product_lines.any? article.product_lines.filter_map do |pl| next unless pl.canonical_path { url: pl.slug_ltree.to_s, path: "/#{pl.canonical_path}/support" } end.uniq { |h| h[:url] } end |
#total_locations ⇒ Integer
Returns total number of locations where the FAQ appears.
87 88 89 90 91 92 93 94 95 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 87 def total_locations count = 0 count += 1 if standalone_location.present? count += .size count += product_line_urls.size count += support_urls.size count += landing_page_paths.size count end |
#word_count ⇒ Integer
Returns word count of the FAQ body.
98 99 100 |
# File 'app/components/crm/faq_rendered_where_component.rb', line 98 def word_count article.faq_word_count end |