Class: Crm::EmbeddedAssetsPanelComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Crm::EmbeddedAssetsPanelComponent
- Includes:
- ActionView::Helpers::UrlHelper
- Defined in:
- app/components/crm/embedded_assets_panel_component.rb
Overview
Displays a panel listing all embedded assets (images, videos, FAQs) for an article/post
Used on CRM show pages to show what assets are embedded in the content
Usage:
<%= render Crm::EmbeddedAssetsPanelComponent.new(parent: @article) %>
Instance Attribute Summary collapse
-
#parent ⇒ ApplicationRecord
readonly
The record whose embedded assets are displayed (e.g. an Article).
Instance Method Summary collapse
-
#asset_link(embedded_asset) ⇒ String?
CRM edit path for the given embedded asset, dispatching on its STI type.
-
#asset_thumbnail(embedded_asset) ⇒ String?
Thumbnail URL for the given embedded asset, when the asset type has one.
-
#asset_title(embedded_asset) ⇒ String
Display title for the given embedded asset, falling back to an ID label.
-
#asset_type_icon(type) ⇒ String
Font Awesome icon classes for an embedded asset STI type.
-
#asset_type_label(type) ⇒ String
Human-readable label for an embedded asset STI type.
-
#embedded_assets ⇒ ActiveRecord::Relation<EmbeddedAsset>
The parent's embedded assets in creation order.
-
#grouped_assets ⇒ Hash{String => Array<EmbeddedAsset>}
Embedded assets grouped by STI type.
-
#initialize(parent:) ⇒ EmbeddedAssetsPanelComponent
constructor
A new instance of EmbeddedAssetsPanelComponent.
-
#render? ⇒ Boolean
Only render the panel when the parent has at least one embedded asset.
Constructor Details
#initialize(parent:) ⇒ EmbeddedAssetsPanelComponent
Returns a new instance of EmbeddedAssetsPanelComponent.
15 16 17 18 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 15 def initialize(parent:) super() @parent = parent end |
Instance Attribute Details
#parent ⇒ ApplicationRecord (readonly)
Returns the record whose embedded assets are displayed (e.g. an Article).
12 13 14 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 12 def parent @parent end |
Instance Method Details
#asset_link(embedded_asset) ⇒ String?
CRM edit path for the given embedded asset, dispatching on its STI type.
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 65 def asset_link() case when EmbeddedImageAsset image_link() when EmbeddedVideoAsset video_link() when EmbeddedFaqAsset faq_links() when EmbeddedProductAsset product_link() end end |
#asset_thumbnail(embedded_asset) ⇒ String?
Thumbnail URL for the given embedded asset, when the asset type has one.
102 103 104 105 106 107 108 109 110 111 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 102 def asset_thumbnail() case when EmbeddedImageAsset .asset&.thumbnail_url when EmbeddedVideoAsset .asset&.thumbnail_url when EmbeddedProductAsset .asset&.primary_image&.thumbnail_url end end |
#asset_title(embedded_asset) ⇒ String
Display title for the given embedded asset, falling back to an ID label.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 81 def asset_title() case when EmbeddedImageAsset .asset&.title || .asset&.seo_title || "Image ##{.asset_id}" when EmbeddedVideoAsset .asset&.title || "Video ##{.asset_id}" when EmbeddedFaqAsset # Each FAQ has its own record - FAQs use 'subject' for the question article = .asset article&.subject || article&.title || "FAQ ##{.asset_id}" when EmbeddedProductAsset item = .asset item&.public_name || item&.name || item&.sku || "Product ##{.asset_id}" else "Asset ##{.asset_id}" end end |
#asset_type_icon(type) ⇒ String
Font Awesome icon classes for an embedded asset STI type.
52 53 54 55 56 57 58 59 60 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 52 def asset_type_icon(type) case type when 'EmbeddedImageAsset' then 'fa-regular fa-image' when 'EmbeddedVideoAsset' then 'fa-regular fa-video' when 'EmbeddedFaqAsset' then 'fa-regular fa-circle-question' when 'EmbeddedProductAsset' then 'fa-regular fa-tag' else 'fa-regular fa-file' end end |
#asset_type_label(type) ⇒ String
Human-readable label for an embedded asset STI type.
39 40 41 42 43 44 45 46 47 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 39 def asset_type_label(type) case type when 'EmbeddedImageAsset' then 'Images' when 'EmbeddedVideoAsset' then 'Videos' when 'EmbeddedFaqAsset' then 'FAQs' when 'EmbeddedProductAsset' then 'Products' else type.demodulize.titleize end end |
#embedded_assets ⇒ ActiveRecord::Relation<EmbeddedAsset>
Returns the parent's embedded assets in creation order.
27 28 29 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 27 def @embedded_assets ||= parent..includes(:asset).order(:created_at) end |
#grouped_assets ⇒ Hash{String => Array<EmbeddedAsset>}
Returns embedded assets grouped by STI type.
32 33 34 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 32 def grouped_assets @grouped_assets ||= .group_by(&:type) end |
#render? ⇒ Boolean
Only render the panel when the parent has at least one embedded asset.
22 23 24 |
# File 'app/components/crm/embedded_assets_panel_component.rb', line 22 def render? .any? end |