Class: Www::BlogPostCardComponent

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

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)

Parameters:

  • post (Post)

    The blog post to display

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

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



9
10
11
12
13
# File 'app/components/www/blog_post_card_component.rb', line 9

def initialize(post:, col_size: '')
  super()
  @post = post
  @col_size = col_size
end

Instance Method Details

#author_first_nameObject



30
31
32
# File 'app/components/www/blog_post_card_component.rb', line 30

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

#author_thumbnail?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/components/www/blog_post_card_component.rb', line 57

def author_thumbnail?
  author_thumbnail_url.present?
end

#author_thumbnail_urlObject

Author thumbnail URL (profile picture or Gravatar fallback)



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/www/blog_post_card_component.rb', line 43

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_dateObject

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



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

def effective_date
  date = @post.revised_at.presence || @post.published_at
  return '' unless date.present?

  date.strftime('%b %d, %Y')
end

#post_pathObject



15
16
17
# File 'app/components/www/blog_post_card_component.rb', line 15

def post_path
  helpers.post_path(@post)
end

#preview_imageObject

Alias for @post#preview_image

Returns:

  • (Object)

    @post#preview_image

See Also:



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

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

#reading_timeObject



25
26
27
28
# File 'app/components/www/blog_post_card_component.rb', line 25

def reading_time
  minutes = @post.reading_time_in_minutes
  "#{minutes} min"
end

#subjectObject

Alias for @post#subject

Returns:

  • (Object)

    @post#subject

See Also:



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

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

#truncated_subjectObject



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

def truncated_subject
  helpers.truncate(@post.subject, length: 60)
end