techiferous/tabulous

View on GitHub
lib/tabulous/renderers/bootstrap_renderer.rb

Summary

Maintainability
A
2 hrs
Test Coverage
require 'active_support/core_ext/string'

module Tabulous
  class BootstrapRenderer < CombinedRenderer

    def tabs_html
      <<-HTML.strip_heredoc
        <ul class="nav nav-tabs">
          #{ tab_list_html }
        </ul>
      HTML
    end

  protected

    def tab_html(tab)
      return '' unless tab.visible?(@view)
      html = ''
      klass = ''
      klass << 'active' if tab.active?(@view)
      klass << ' disabled' unless tab.enabled?(@view)
      klass << ' dropdown' if tab.subtabs.any?
      klass.strip!
      if klass.empty?
        html << '<li>'
      else
        html << %Q{<li class="#{klass}">}
      end
      if tab.subtabs.empty?
        html << tab_link(tab)
      else
        html << tab_link_with_subtabs(tab)
      end
      html << "</li>"
      html
    end

    def tab_link(tab)
      html = ''
      if tab.clickable?(@view) && tab.enabled?(@view)
        html << %Q{<a href="#{tab_url(tab)}" #{tab_http_verb_attributes(tab)}>#{tab_text(tab)}</a>}
      else
        html << %Q{<a>#{tab_text(tab)}</a>}
      end
      html
    end

    def tab_link_with_subtabs(tab)
      html = ''
      html << %Q{<a class="dropdown-toggle" data-toggle="dropdown" href="#">#{tab_text(tab)}<b class="caret"></b></a>}
      html << %Q{<ul class="dropdown-menu">}
      for subtab in tab.subtabs
        next unless subtab.visible?(@view)
        klass = (subtab.enabled?(@view) ? '' : 'disabled')
        if klass.empty?
          html << '<li>'
        else
          html << %Q{<li class="#{klass}">}
        end
        html << tab_link(subtab)
        html << "</li>"
      end
      html << "</ul>"
      html
    end

  end
end