class Bootstrap4Helper::Card

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

Public Class Methods

new(template, opts = {}, &block) click to toggle source

Used to initialize a new Card component.

@param [ActionView] template @param [Hash] opts @option opts [String] :id @option opts [String] :class @option opts [Hash] :data @return [Card]

Calls superclass method
# File lib/bootstrap4_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

Public Instance Methods

body(args = {}, &block) click to toggle source

Builds the Body component.

@param [Hash] args @option args [String] :id @option args [String] :class @option args [Hash] :data @return [String]

# File lib/bootstrap4_helper/card.rb, line 44
def body(args = {}, &block)
  build_base_component :body, args, &block
end
header(args = {}, &block) click to toggle source

Builds the Header component.

@param [Hash] args @option args [String] :id @option args [String] :class @option args [Hash] :data @return [String]

# File lib/bootstrap4_helper/card.rb, line 32
def header(args = {}, &block)
  build_base_component :header, args, &block
end
image_overlay(args = {}, &block) click to toggle source

Builds a Img Overlay component.

@param [Hash] args @option args [String] :id @option args [String] :class @option args [Hash] :data @return [String]

# File lib/bootstrap4_helper/card.rb, line 92
def image_overlay(args = {}, &block)
  build_base_component 'img-overlay', args, &block
end
text(args = {}, &block) click to toggle source

Builds a Text component.

@param [Hash] args @option args [String] :id @option args [String] :class @option args [Hash] :data @return [String]

# File lib/bootstrap4_helper/card.rb, line 80
def text(args = {}, &block)
  build_sub_component config(:card_text, :p), :text, args, &block
end
title(args = {}, &block) click to toggle source

Builds a Title component.

@param [Hash] args @option args [String] :id @option args [String] :class @option args [Hash] :data @return [String]

# File lib/bootstrap4_helper/card.rb, line 68
def title(args = {}, &block)
  build_sub_component config(:card_title, :h5), :title, args, &block
end
to_s() click to toggle source

Outputs the Object in its String representation.

@return [String]

# File lib/bootstrap4_helper/card.rb, line 100
def to_s
  content_tag :div, id: @id, class: "card #{@class}", data: @data do
    @content.call(self)
  end
end

Private Instance Methods

build_base_component(type, args, &block) click to toggle source

Used to build basic DIV components.

@param [String] type @param [Mixed] args @return [String]

# File lib/bootstrap4_helper/card.rb, line 114
def build_base_component(type, args, &block)
  build_sub_component :div, type, args, &block
end
build_sub_component(tag, type, args, &block) click to toggle source

Used to build various DOM components.

@param [Symbol] tag @param [String] type @param [Hash] args @option args [String] :id @option args [String] :class @option args [Hash] :data @return [String]

# File lib/bootstrap4_helper/card.rb, line 128
def build_sub_component(tag, type, args, &block)
  id    = args.fetch(:id,    '')
  klass = args.fetch(:class, '')
  data  = args.fetch(:data,  {})

  content_tag(
    tag,
    id:    id,
    class: "card-#{type} #{klass}",
    data:  data,
    &block
  )
end