Class: Www::BlogPostCardComponent
- Inherits:
-
ApplicationComponent
- Object
- ViewComponent::Base
- ApplicationComponent
- Www::BlogPostCardComponent
- Defined in:
- app/components/www/blog_post_card_component.rb
Overview
ViewComponent: renders the blog post card block.
Delegated Instance Attributes collapse
-
#preview_image ⇒ Object
Alias for @post#preview_image.
-
#subject ⇒ Object
Alias for @post#subject.
Methods inherited from ApplicationComponent
#cms_link, #image_asset_tag, #image_tag, #number_to_currency, #number_with_delimiter, #post_url, #strip_tags
Instance Method Summary collapse
-
#author_first_name ⇒ String
Author's first name or fallback.
-
#author_thumbnail? ⇒ Boolean
Whether an author thumbnail is available.
-
#author_thumbnail_url ⇒ String?
Author thumbnail URL (profile picture or Gravatar fallback).
-
#effective_date ⇒ String
Returns the effective date: revised_at if present, otherwise published_at (short format).
-
#initialize(post:, col_size: '') ⇒ BlogPostCardComponent
constructor
A new instance of BlogPostCardComponent.
-
#post_path ⇒ String
Path to the blog post.
-
#reading_time ⇒ String
Estimated reading time.
-
#truncated_subject ⇒ String
Truncated post subject.
Methods inherited from ApplicationComponent
Constructor Details
#initialize(post:, col_size: '') ⇒ BlogPostCardComponent
To avoid N+1 queries, ensure posts are loaded with:
Post.includes(original_author: :profile_image)
Returns a new instance of BlogPostCardComponent.
10 11 12 13 14 |
# File 'app/components/www/blog_post_card_component.rb', line 10 def initialize(post:, col_size: '') super() @post = post @col_size = col_size end |
Instance Method Details
#author_first_name ⇒ String
Returns author's first name or fallback.
35 36 37 |
# File 'app/components/www/blog_post_card_component.rb', line 35 def @post.&.first_name || 'WarmlyYours' end |
#author_thumbnail? ⇒ Boolean
Returns whether an author thumbnail is available.
65 66 67 |
# File 'app/components/www/blog_post_card_component.rb', line 65 def .present? end |
#author_thumbnail_url ⇒ String?
Author thumbnail URL (profile picture or Gravatar fallback)
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/components/www/blog_post_card_component.rb', line 50 def return nil unless @post. # Try profile image first (consolidated storage for all party types) if (pp = @post..profile_image) pp.image_url(size: '40x40>') elsif (email = @post..email&.presence) # Gravatar fallback require 'digest/md5' hash = Digest::MD5.hexdigest(email.downcase) "https://www.gravatar.com/avatar/#{hash}.jpg?s=40" end end |
#effective_date ⇒ String
Returns the effective date: revised_at if present, otherwise published_at (short format)
41 42 43 44 45 46 |
# File 'app/components/www/blog_post_card_component.rb', line 41 def effective_date date = @post.revised_at.presence || @post.published_at return '' if date.blank? date.strftime('%b %d, %Y') end |
#post_path ⇒ String
Returns path to the blog post.
17 18 19 |
# File 'app/components/www/blog_post_card_component.rb', line 17 def post_path helpers.post_path(@post) end |
#preview_image ⇒ Object
Alias for @post#preview_image
21 |
# File 'app/components/www/blog_post_card_component.rb', line 21 delegate :preview_image, :subject, to: :@post |
#reading_time ⇒ String
Returns estimated reading time.
29 30 31 32 |
# File 'app/components/www/blog_post_card_component.rb', line 29 def reading_time minutes = @post.reading_time_in_minutes "#{minutes} min" end |
#subject ⇒ Object
Alias for @post#subject
21 |
# File 'app/components/www/blog_post_card_component.rb', line 21 delegate :preview_image, :subject, to: :@post |
#truncated_subject ⇒ String
Returns truncated post subject.
24 25 26 |
# File 'app/components/www/blog_post_card_component.rb', line 24 def truncated_subject helpers.truncate(@post.subject, length: 60) end |