Class: Bootstrap5Helper::Card

Inherits:
Component show all
Defined in:
lib/bootstrap5_helper/card.rb

Overview

Used to build Bootstrap Card components. Cards are wildly used through Bootstrap 4.

Instance Method Summary collapse

Methods inherited from Component

#capture, #concat, #config, #content_tag, #parse_arguments, #parse_context_or_options, #parse_tag_or_options, #parse_text_or_options, #uuid

Constructor Details

#initialize(template, opts = {}, &block) ⇒ Card

Used to initialize a new Card component.

Parameters:

  • template (ActionView)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)


15
16
17
18
19
20
21
22
# File 'lib/bootstrap5_helper/card.rb', line 15

def initialize(template, opts = {}, &block)
  super(template)

  @id      = opts.fetch(:id,    '')
  @class   = opts.fetch(:class, '')
  @data    = opts.fetch(:data,  nil)
  @content = block || proc { '' }
end

Instance Method Details

#body(tag, opts) ⇒ String #body(opts) ⇒ String

Builds the Body component.

Overloads:

  • #body(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #body(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


68
69
70
71
72
73
74
75
76
# File 'lib/bootstrap5_helper/card.rb', line 68

def body(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})
  build_base_component(
    tag || config({ cards: :body }, :div),
    :body,
    args,
    &block
  )
end

Builds the Footer component.

Overloads:

  • #footer(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #footer(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


96
97
98
99
100
101
102
103
104
# File 'lib/bootstrap5_helper/card.rb', line 96

def footer(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})
  build_base_component(
    tag || config({ cards: :footer }, :div),
    :footer,
    args,
    &block
  )
end

#header(tag, opts) ⇒ String #header(opts) ⇒ String

Builds the Header component.

Overloads:

  • #header(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #header(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
# File 'lib/bootstrap5_helper/card.rb', line 41

def header(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})
  build_base_component(
    tag || config({ cards: :header }, :h5),
    :header,
    args,
    &block
  )
end

#image(src, opts = {}) ⇒ String

Simple image element for cards.

Parameters:

  • src (String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :id (String)
  • :class (String)
  • :data (Hash)
  • :aria (Hash)

Returns:

  • (String)


197
198
199
200
# File 'lib/bootstrap5_helper/card.rb', line 197

def image(src, opts = {})
  (opts[:class] ||= '') << ' card-img'
  @template.image_tag(src, opts)
end

#image_cap(src, type = :top, opts = {}) ⇒ String

Builds an Image Cap component.

Parameters:

  • src, (String)
  • type (Symbol|String) (defaults to: :top)
  • opts (Hash) (defaults to: {})

Returns:

  • (String)


209
210
211
212
# File 'lib/bootstrap5_helper/card.rb', line 209

def image_cap(src, type = :top, opts = {})
  (opts[:class] ||= '') << " card-img-#{type}"
  @template.image_tag(src, opts)
end

#image_overlay(args = {}, &block) ⇒ String

Builds a Img Overlay component.

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :id (String)
  • :class (String)
  • :data (Hash)

Returns:

  • (String)


222
223
224
# File 'lib/bootstrap5_helper/card.rb', line 222

def image_overlay(args = {}, &block)
  build_base_component(:div, 'img-overlay', args, &block)
end

#subtitle(tag, opts) ⇒ String #subtitle(opts) ⇒ String

Builds a Title component.

Overloads:

  • #subtitle(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #subtitle(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
# File 'lib/bootstrap5_helper/card.rb', line 150

def subtitle(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})
  build_sub_component(
    tag || config({ cards: :subtitle }, :h6),
    :subtitle,
    args,
    &block
  )
end

#header(tag, opts) ⇒ String #header(opts) ⇒ String

Builds a Text component.

Overloads:

  • #header(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #header(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


177
178
179
180
181
182
183
184
185
# File 'lib/bootstrap5_helper/card.rb', line 177

def text(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})
  build_sub_component(
    tag || config(:card_text, :p),
    :text,
    args,
    &block
  )
end

#title(tag, opts) ⇒ String #title(opts) ⇒ String

Builds a Title component.

Overloads:

  • #title(tag, opts) ⇒ String

    Parameters:

    • tag (Symbol|String)
      • The HTML element to use.

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)
  • #title(opts) ⇒ String

    Parameters:

    • options (Hash)

    Options Hash (opts):

    • :id (String)
    • :class (String)
    • :data (Hash)

Returns:

  • (String)


123
124
125
126
127
128
129
130
131
# File 'lib/bootstrap5_helper/card.rb', line 123

def title(*tag_or_options, &block)
  tag, args = parse_tag_or_options(*tag_or_options, {})
  build_sub_component(
    tag || config({ cards: :title }, :h5),
    :title,
    args,
    &block
  )
end

#to_sString

Outputs the Object in its String representation.

Returns:

  • (String)


230
231
232
233
234
# File 'lib/bootstrap5_helper/card.rb', line 230

def to_s
   :div, id: @id, class: "card #{@class}", data: @data do
    @content.call(self)
  end
end