Class: Www::BlogPostCardComponent

Inherits:
ApplicationComponent show all
Defined in:
app/components/www/blog_post_card_component.rb

Overview

ViewComponent: renders the blog post card block.

Delegated Instance Attributes collapse

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

Methods inherited from ApplicationComponent

#fetch_or_fallback

Constructor Details

#initialize(post:, col_size: '') ⇒ BlogPostCardComponent

Note:

To avoid N+1 queries, ensure posts are loaded with:
Post.includes(original_author: :profile_image)

Returns a new instance of BlogPostCardComponent.

Parameters:

  • post (Post)

    The blog post to display

  • col_size (String) (defaults to: '')

    Bootstrap column classes (empty string for carousel/slider usage)



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_nameString

Returns author's first name or fallback.

Returns:

  • (String)

    author's first name or fallback



35
36
37
# File 'app/components/www/blog_post_card_component.rb', line 35

def author_first_name
  @post.author&.first_name || 'WarmlyYours'
end

#author_thumbnail?Boolean

Returns whether an author thumbnail is available.

Returns:

  • (Boolean)

    whether an author thumbnail is available



65
66
67
# File 'app/components/www/blog_post_card_component.rb', line 65

def author_thumbnail?
  author_thumbnail_url.present?
end

#author_thumbnail_urlString?

Author thumbnail URL (profile picture or Gravatar fallback)

Returns:

  • (String, nil)

    thumbnail URL



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 author_thumbnail_url
  return nil unless @post.author

  # Try profile image first (consolidated storage for all party types)
  if (pp = @post.author.profile_image)
    pp.image_url(size: '40x40>')
  elsif (email = @post.author.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_dateString

Returns the effective date: revised_at if present, otherwise published_at (short format)

Returns:

  • (String)

    formatted date



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_pathString

Returns path to the blog post.

Returns:

  • (String)

    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_imageObject

Alias for @post#preview_image

Returns:

  • (Object)

    @post#preview_image

See Also:



21
# File 'app/components/www/blog_post_card_component.rb', line 21

delegate :preview_image, :subject, to: :@post

#reading_timeString

Returns estimated reading time.

Returns:

  • (String)

    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

#subjectObject

Alias for @post#subject

Returns:

  • (Object)

    @post#subject

See Also:



21
# File 'app/components/www/blog_post_card_component.rb', line 21

delegate :preview_image, :subject, to: :@post

#truncated_subjectString

Returns truncated post subject.

Returns:

  • (String)

    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