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
333 def call(tag)
334   # All textual <input>, <textarea>, and <select> elements with .form-control
335   case tag
336   when Tag
337     case tag.type
338     when :input
339       # default to <input type="text"...> if not set
340       tag.attr[:type] = :text if tag.attr[:type].nil?
341       
342       case tag.attr[:type].to_sym
343       when :checkbox, :radio, :hidden
344         # .form-control class causes rendering problems, so remove if found
345         tag.attr[:class].gsub!(/\s*form-control\s*/,'') if tag.attr[:class]
346         tag.attr[:class] = nil if tag.attr[:class] && tag.attr[:class].empty?
347         
348       when :file
349         tag.attr[:class] = nil unless tag.attr[:class] && tag.attr[:class].strip != ''
350       
351       when :submit, :reset
352         klass = ['btn', 'btn-default']
353         if tag.attr[:class] && tag.attr[:class].strip != ''
354           tag.attr[:class].split(' ').each { |c| klass.push c }
355         end
356         tag.attr[:class] = klass.uniq
357         ['btn-primary','btn-success', 'btn-info', 'btn-warning','btn-danger',
358           'btn-outline','btn-link' 
359         ].each do |k|
360           tag.attr[:class].delete('btn-default') if tag.attr[:class].include?(k)
361         end
362         tag.attr[:class].join(' ')
363         
364       else
365         klass = tag.attr[:class] ? "form-control #{tag.attr[:class].to_s}" : ''
366         tag.attr[:class] = "form-control #{klass.gsub(/\s*form-control\s*/,'')}".strip
367       end
368       
369       return "<#{tag.type}#{attr_html(tag.attr)}/>"
370       
371     when :textarea, :select
372       klass = tag.attr[:class] ? "form-control #{tag.attr[:class].to_s}" : ''
373       tag.attr[:class] = "form-control #{klass.gsub(/\s*form-control\s*/,'')}".strip
374       return "#{serialize_open(tag)}#{call(tag.children)}#{serialize_close(tag)}"
375     else
376       super
377     end
378   else
379     super
380   end
381 end