Class: Crm::EmbeddedAssetsPanelComponent

Inherits:
ViewComponent::Base
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(parent:) ⇒ EmbeddedAssetsPanelComponent

Returns a new instance of EmbeddedAssetsPanelComponent.

Parameters:



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

#parentApplicationRecord (readonly)

Returns the record whose embedded assets are displayed (e.g. an Article).

Returns:

  • (ApplicationRecord)

    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

CRM edit path for the given embedded asset, dispatching on its STI type.

Parameters:

  • embedded_asset (EmbeddedAsset)

    the embedded asset to link to

Returns:

  • (String, nil)

    the edit path, or nil for unknown asset types



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(embedded_asset)
  case embedded_asset
  when EmbeddedImageAsset
    image_link(embedded_asset)
  when EmbeddedVideoAsset
    video_link(embedded_asset)
  when EmbeddedFaqAsset
    faq_links(embedded_asset)
  when EmbeddedProductAsset
    product_link(embedded_asset)
  end
end

#asset_thumbnail(embedded_asset) ⇒ String?

Thumbnail URL for the given embedded asset, when the asset type has one.

Parameters:

  • embedded_asset (EmbeddedAsset)

    the embedded asset to thumbnail

Returns:

  • (String, nil)

    the thumbnail URL, or nil when none is available



102
103
104
105
106
107
108
109
110
111
# File 'app/components/crm/embedded_assets_panel_component.rb', line 102

def asset_thumbnail(embedded_asset)
  case embedded_asset
  when EmbeddedImageAsset
    embedded_asset.asset&.thumbnail_url
  when EmbeddedVideoAsset
    embedded_asset.asset&.thumbnail_url
  when EmbeddedProductAsset
    embedded_asset.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.

Parameters:

Returns:

  • (String)

    the display title



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(embedded_asset)
  case embedded_asset
  when EmbeddedImageAsset
    embedded_asset.asset&.title || embedded_asset.asset&.seo_title || "Image ##{embedded_asset.asset_id}"
  when EmbeddedVideoAsset
    embedded_asset.asset&.title || "Video ##{embedded_asset.asset_id}"
  when EmbeddedFaqAsset
    # Each FAQ has its own record - FAQs use 'subject' for the question
    article = embedded_asset.asset
    article&.subject || article&.title || "FAQ ##{embedded_asset.asset_id}"
  when EmbeddedProductAsset
    item = embedded_asset.asset
    item&.public_name || item&.name || item&.sku || "Product ##{embedded_asset.asset_id}"
  else
    "Asset ##{embedded_asset.asset_id}"
  end
end

#asset_type_icon(type) ⇒ String

Font Awesome icon classes for an embedded asset STI type.

Parameters:

  • type (String)

    the embedded asset class name (e.g. 'EmbeddedImageAsset')

Returns:

  • (String)

    the icon CSS classes for the asset 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.

Parameters:

  • type (String)

    the embedded asset class name (e.g. 'EmbeddedImageAsset')

Returns:

  • (String)

    the display label for the asset 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_assetsActiveRecord::Relation<EmbeddedAsset>

Returns the parent's embedded assets in creation order.

Returns:

  • (ActiveRecord::Relation<EmbeddedAsset>)

    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
  @embedded_assets ||= parent.embedded_assets.includes(:asset).order(:created_at)
end

#grouped_assetsHash{String => Array<EmbeddedAsset>}

Returns embedded assets grouped by STI type.

Returns:

  • (Hash{String => Array<EmbeddedAsset>})

    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 ||= embedded_assets.group_by(&:type)
end

#render?Boolean

Only render the panel when the parent has at least one embedded asset.

Returns:

  • (Boolean)

    whether the component should render



22
23
24
# File 'app/components/crm/embedded_assets_panel_component.rb', line 22

def render?
  embedded_assets.any?
end