Sha256: d395c91abf0ee50f01c7fb6cb0beb4c3e4ed06c367fa28c5267015c5027c34d7

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

class BootstrapBuilders::Box
  def initialize(args)
    @title = args.fetch(:title)
    @width = args.fetch(:width)
    @right = args[:right]
    @table = args[:table]
    @block = args.fetch(:block)
    @context = args.fetch(:context)
    @class = args[:class]
    @data = args[:data]

    if @width.is_a?(Fixnum) || @width.is_a?(Integer)
      @width = "#{@width}px"
    else
      @width = "100%"
    end
  end

  def html
    @panel = HtmlGen::Element.new(:div, inden: "  ", classes: container_classes, css: {width: @width}, data: @data)

    add_heading if @title || @right

    if @table
      add_table
    else
      add_body
    end

    html = @panel.html

    if html.respond_to?(:html_safe)
      html.html_safe
    else
      html
    end
  end

private

  def add_heading
    heading = @panel.add_ele(:div, classes: ["panel-heading", "clearfix"])
    heading.add_ele(:div, classes: ["panel-title", "pull-left"], str: @title)
    heading.add_ele(:div, classes: ["pull-right"], str_html: @right.to_s) if @right
  end

  def add_table
    @panel.add_html(@context.content_tag(:table, nil, class: ["table", "table-striped", "table-hover"], &@block))
  end

  def add_body
    @panel.add_html(@context.content_tag(:div, nil, class: ["panel-body"], &@block))
  end

  def container_classes
    classes = ["panel", "panel-default"]

    if @class.is_a?(String)
      classes += @class.split(/\s+/)
    elsif @class.is_a?(Array)
      classes += @class
    end

    classes
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bootstrap_builders-0.0.3 lib/bootstrap_builders/box.rb