Class: Crm::PublicationCardComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/crm/publication_card_component.rb

Overview

Component for displaying a publication card in the publications index.
Optimized to avoid N+1 queries by accepting pre-loaded associations and
precomputed most_recent_revision data.

Usage:
render Crm::PublicationCardComponent.new(
publication: publication,
most_recent_revision: most_recent_revisions[publication.id]
)

Instance Attribute Summary collapse

Delegated Instance Attributes collapse

Instance Method Summary collapse

Constructor Details

#initialize(publication:, most_recent_revision: nil, cover_image_url: nil) ⇒ PublicationCardComponent

Returns a new instance of PublicationCardComponent.

Parameters:

  • publication (Publication)

    the publication to render

  • most_recent_revision (Publication, nil) (defaults to: nil)

    precomputed most recent
    revision, used to flag the card as superseded

  • cover_image_url (String, nil) (defaults to: nil)

    precomputed cover image URL



26
27
28
29
30
31
# File 'app/components/crm/publication_card_component.rb', line 26

def initialize(publication:, most_recent_revision: nil, cover_image_url: nil)
  super()
  @publication = publication
  @most_recent_revision = most_recent_revision
  @cover_image_url = cover_image_url
end

Instance Attribute Details

#cover_image_urlObject (readonly)

Returns the value of attribute cover_image_url.



20
# File 'app/components/crm/publication_card_component.rb', line 20

attr_reader :publication, :most_recent_revision, :cover_image_url

#most_recent_revisionPublication? (readonly)

Returns precomputed most recent revision of the
publication, if one exists.

Returns:

  • (Publication, nil)

    precomputed most recent revision of the
    publication, if one exists



20
# File 'app/components/crm/publication_card_component.rb', line 20

attr_reader :publication, :most_recent_revision, :cover_image_url

#publicationPublication (readonly)

Returns the publication being rendered.

Returns:



20
21
22
# File 'app/components/crm/publication_card_component.rb', line 20

def publication
  @publication
end

Instance Method Details

#category_nameString?

Uses pre-loaded product_category association to avoid N+1

Returns:

  • (String, nil)

    name of the publication's product category



60
61
62
# File 'app/components/crm/publication_card_component.rb', line 60

def category_name
  publication.product_category&.name
end

#fancybox_groupString

Unique group per publication so clicking a cover opens only that
publication's document in the lightbox, instead of grouping every cover
on the page into a single carousel. Mirrors Crm::UploadCardComponent.

Returns:

  • (String)

    Fancybox group identifier for this publication



93
94
95
# File 'app/components/crm/publication_card_component.rb', line 93

def fancybox_group
  "publication-#{publication.id}"
end

#fancybox_typeString?

Returns Fancybox content type for the uploaded document.

Returns:

  • (String, nil)

    Fancybox content type for the uploaded document



85
86
87
# File 'app/components/crm/publication_card_component.rb', line 85

def fancybox_type
  helpers.fancybox_type_for_upload(literature) if literature_present?
end

#has_newer_revision?Boolean

Returns whether a newer revision of this publication exists.

Returns:

  • (Boolean)

    whether a newer revision of this publication exists



65
66
67
# File 'app/components/crm/publication_card_component.rb', line 65

def has_newer_revision?
  most_recent_revision.present?
end

#literatureUpload?

Returns the publication's uploaded document.

Returns:

  • (Upload, nil)

    the publication's uploaded document



35
# File 'app/components/crm/publication_card_component.rb', line 35

delegate :literature, to: :publication

#literature_present?Boolean

Returns whether the publication has an uploaded document.

Returns:

  • (Boolean)

    whether the publication has an uploaded document



39
# File 'app/components/crm/publication_card_component.rb', line 39

delegate :present?, to: :literature, prefix: true

#most_recent_revision_pathString?

Returns URL path to the most recent revision's detail page.

Returns:

  • (String, nil)

    URL path to the most recent revision's detail page



75
76
77
# File 'app/components/crm/publication_card_component.rb', line 75

def most_recent_revision_path
  helpers.publication_path(most_recent_revision.id) if most_recent_revision
end

#most_recent_revision_skuString?

Returns SKU of the most recent revision, if any.

Returns:

  • (String, nil)

    SKU of the most recent revision, if any



70
71
72
# File 'app/components/crm/publication_card_component.rb', line 70

def most_recent_revision_sku
  most_recent_revision&.sku
end

#publication_nameString

Returns the publication's name.

Returns:

  • (String)

    the publication's name



43
# File 'app/components/crm/publication_card_component.rb', line 43

delegate :name, to: :publication, prefix: true

#publication_pathString

Returns URL path to this publication's detail page.

Returns:

  • (String)

    URL path to this publication's detail page



54
55
56
# File 'app/components/crm/publication_card_component.rb', line 54

def publication_path
  helpers.publication_path(publication.id)
end

#publication_skuString

Returns the publication's SKU.

Returns:

  • (String)

    the publication's SKU



47
# File 'app/components/crm/publication_card_component.rb', line 47

delegate :sku, to: :publication, prefix: true

#updated_atActiveSupport::TimeWithZone

Returns when the publication was last updated.

Returns:

  • (ActiveSupport::TimeWithZone)

    when the publication was last updated



51
# File 'app/components/crm/publication_card_component.rb', line 51

delegate :updated_at, to: :publication

#upload_pathString?

Returns URL path to the uploaded document, if one exists.

Returns:

  • (String, nil)

    URL path to the uploaded document, if one exists



80
81
82
# File 'app/components/crm/publication_card_component.rb', line 80

def upload_path
  helpers.upload_path(literature) if literature_present?
end