Sha256: 360548b31cebbbda97bc5eb2cdeef4a19985f012da4f8c25422d2e8da46832fb

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true
require 'railsstrap/classes/base'

module Railsstrap
  module Classes
    class Card < Base
      # Differently from other classes, Panel works with no content or block,
      # given that the options[:body] is passed, in which case it functions
      # as the content.
      def initialize(app = nil, *args, &block)
        if args.first.is_a?(Hash) && !block_given?
          args.unshift args.first.delete(:body)
        end

        super
      end

      # @return [#to_s] the content-related class to assign to the card.
      def variant_class
        Card.variants[@options[:variant]]
      end

      # @return [#to_s] the content-related class to assign to the card.
      def text_variant_class
        @options[:text_variant]
      end

      # @return [#to_s] the HTML tag to wrap the card in.
      def tag
        @options.fetch :tag, :div
      end

      # @return [#to_s] the text to display as the card header
      def header
        text = title || @options[:header]
        @app.content_tag :div, text, class: 'card-header' if text
      end

      def image_cap
        if @options[:img]
          @app.content_tag :img, nil, class: 'card-img-top', src: @options[:img]
        end
      end
      def body
        if @options[:body]
          @app.content_tag :div, @options[:body], class: 'card-body'
        end
      end


      def extract_content_from(*args, &block)
        if block_given?
          super
        else
          @app.content_tag :div, super, class: 'card-body'
        end
      end

      def title
        if @options[:title]
          @app.content_tag :h3, @options[:title], class: 'card-title'
        end
      end

      # @return [Hash<Symbol, String>] the class that Bootstrap requires to
      #   append to an card box based on its variant.
      def self.variants
        HashWithIndifferentAccess.new(:'bg-default').tap do |klass|
          variant_types.each { |variant| klass[variant.to_sym] = :"bg-#{variant}" }
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
railsstrap-4.0.0.beta3 lib/railsstrap/classes/card.rb