Class: DsfrComponent::CardComponent

Inherits:
Base
  • Object
show all
Defined in:
app/components/dsfr_component/card_component.rb

Constant Summary collapse

SIZES =
%i[sm lg].freeze
HORIZONTAL_RATIOS =
[true, :half, :tier].freeze

Constants inherited from Base

Base::HEADING_LEVELS

Instance Attribute Summary

Attributes inherited from Base

#html_attributes

Instance Method Summary collapse

Constructor Details

#initialize(title:, url:, description: nil, image_src: nil, image_alt: "", heading_level: 3, size: nil, horizontal: false, start_detail: nil, start_detail_icon: nil, end_detail: nil, end_detail_icon: nil, enlarge_link: true, html_attributes: {}) ⇒ CardComponent

Returns a new instance of CardComponent.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/components/dsfr_component/card_component.rb', line 9

def initialize(
  title:,
  url:,
  description: nil,
  image_src: nil,
  image_alt: "",
  heading_level: 3,
  size: nil,
  horizontal: false,
  start_detail: nil,
  start_detail_icon: nil,
  end_detail: nil,
  end_detail_icon: nil,
  enlarge_link: true,
  html_attributes: {}
)
  raise ArgumentError, "`size` should be one of #{SIZES}" if size && SIZES.exclude?(size)
  raise ArgumentError, "`heading_level` should be one of #{HEADING_LEVELS}" if HEADING_LEVELS.exclude?(heading_level)
  raise ArgumentError, "`horizontal` should be one of #{[false] + HORIZONTAL_RATIOS}" if horizontal != false && HORIZONTAL_RATIOS.exclude?(horizontal)

  @title = title
  @url = url
  @description = description
  @image_src = image_src
  @image_alt = image_alt
  @heading_level = heading_level
  @size = size
  @horizontal = horizontal
  @start_detail = start_detail
  @start_detail_icon = start_detail_icon
  @end_detail = end_detail
  @end_detail_icon = end_detail_icon
  @enlarge_link = enlarge_link

  super(html_attributes: html_attributes)
end

Instance Method Details

#before_renderObject

this guard is set at render-time because we cannot make the footer-slot distinction in the constructor

Raises:

  • (ArgumentError)


48
49
50
# File 'app/components/dsfr_component/card_component.rb', line 48

def before_render
  raise ArgumentError, "You cannot setup a footer on a card with `enlarge_link: true`" if footer? && enlarge_link
end