Class: Forme::Serializer::Bootstrap3

Inherits:
Forme::Serializer show all
Defined in:
lib/forme/bs3.rb

Overview

Serializer class that converts tags to BS3 bootstrap tags.

Registered at :bs3.

Constant Summary

Constants inherited from Forme::Serializer

ESCAPE_HTML, ESCAPE_HTML_PATTERN, SELF_CLOSING

Instance Method Summary collapse

Methods inherited from Forme::Serializer

#serialize_close, #serialize_open

Instance Method Details

#call(tag) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/forme/bs3.rb', line 333

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