class Forme::Serializer::Bootstrap3

  1. lib/forme/bs3.rb
Superclass: Serializer

Serializer class that converts tags to BS3 bootstrap tags.

Registered at :bs3.

Methods

Public Instance

  1. call

Public Instance methods

call (tag)
[show source]
# File lib/forme/bs3.rb, line 337
def call(tag)
  # All textual <input>, <textarea>, and <select> elements with .form-control
  case tag
  when Tag
    case tag.type
    when :input
      # default to <input type="text"...> if not set
      tag.attr[:type] = :text if tag.attr[:type].nil?
      
      case tag.attr[:type].to_sym
      when :checkbox, :radio, :hidden
        # .form-control class causes rendering problems, so remove if found
        tag.attr[:class].gsub!(/\s*form-control\s*/,'') if tag.attr[:class]
        tag.attr[:class] = nil if tag.attr[:class] && tag.attr[:class].empty?
        
      when :file
        tag.attr[:class] = nil unless tag.attr[:class] && tag.attr[:class].strip != ''
      
      when :submit, :reset
        klass = ['btn', 'btn-default']
        if tag.attr[:class] && tag.attr[:class].strip != ''
          tag.attr[:class].split(' ').each { |c| klass.push c }
        end
        tag.attr[:class] = klass.uniq
        ['btn-primary','btn-success', 'btn-info', 'btn-warning','btn-danger',
          'btn-outline','btn-link' 
        ].each do |k|
          tag.attr[:class].delete('btn-default') if tag.attr[:class].include?(k)
        end
        tag.attr[:class].join(' ')
        
      else
        klass = tag.attr[:class] ? "form-control #{tag.attr[:class].to_s}" : ''
        tag.attr[:class] = "form-control #{klass.gsub(/\s*form-control\s*/,'')}".strip
      end
      
      return "<#{tag.type}#{attr_html(tag.attr)}/>"
      
    when :textarea, :select
      klass = tag.attr[:class] ? "form-control #{tag.attr[:class].to_s}" : ''
      tag.attr[:class] = "form-control #{klass.gsub(/\s*form-control\s*/,'')}".strip
      return "#{serialize_open(tag)}#{call(tag.children)}#{serialize_close(tag)}"
    else
      super
    end
  else
    super
  end
end