require 'bh/helpers/glyphicon_helper' module Bh module Form module BaseHelper include GlyphiconHelper # for glyphicon private def base_field(method, field_type, options = {}, &block) errors = (object.errors.get method if object) || {} label = label_for method, errors, options field = field_container(options) do field_tags errors, field_type, &block end label_and_field = safe_join [label, field].compact label_and_field_container label_and_field, field_type, errors end def field_container(options = {}, &block) if horizontal_form? klass = [('col-sm-offset-3' if options[:offset]), 'col-sm-9'] content_tag :div, class: klass.compact.join(' '), &block else yield end end def field_tags(errors, field_type, &block) tags = [@template.capture(&block)] tags << error_icon_tag if errors.any? && show_error_icon?(field_type) tags << error_help_tag(errors) if errors.any? && show_error_help? safe_join tags end def label_and_field_container(label_and_field, field_type, errors = {}) klass = ['form-group'] if errors.any? klass << 'has-error' klass << 'has-feedback' if show_error_icon?(field_type) end content_tag :div, label_and_field, class: klass.join(' ') end def show_error_icon?(field_type) hide = [:checkbox, :number_field, :radio_button, :select, :legend].include? field_type @options.fetch(:errors, {}).fetch(:icons, true) && !hide end def error_icon_tag glyphicon :remove, class: 'form-control-feedback' end def error_help_tag(errors) klass = ['help-block', 'text-left'] klass << 'sr-only' if inline_form? content_tag :span, errors.to_sentence, class: klass.join(' ') end def label_for(method, errors, options) if options.delete(:use_label) { true } args = [method, options.delete(:label), label_options(errors)] label *args.compact end end def label_options(errors = {}) klass = [] klass << 'sr-only' if inline_form? klass << 'col-sm-3' if horizontal_form? klass << 'control-label' if horizontal_form? klass << 'control-label' if basic_form? && errors.any? {class: klass.join(' ')} if klass.any? end # Rails adds