require 'railsstrap/core_ext/rails/base_helper' require 'railsstrap/helpers/icon_helper' module Railsstrap module Form module BaseHelper include Railsstrap::Rails::BaseHelper include Railsstrap::Helpers private def base_field(method, field_type, options = {}, &block) errors = (object.errors[method] if object) || {} label = label_for method, errors, field_type, options field = field_container(options) do field_tags errors, field_type, options, &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) offset = options.delete :offset if horizontal_form? klass = [('col-sm-offset-3' if offset), 'col-sm-9'] content_tag :div, class: klass.compact.join(' '), &block else yield end end def field_tags(errors, field_type, options = {}, &block) prefix, suffix = options.delete(:prefix), options.delete(:suffix) help_text = options.delete(:help) field = @template.capture(&block) tags = [field_in_group(field, prefix, suffix)] if show_error_icon?(field_type, errors, suffix) && !basic_form? tags << error_icon_tag end if errors.any? && show_error_help? tags << help_tag(errors.to_sentence) elsif help_text tags << help_tag(help_text) end safe_join tags end def field_in_group(field, prefix, suffix) input_group_container(prefix || suffix) do safe_join [input_addon(prefix), field, input_addon(suffix)].compact end end def input_addon(addon) content_tag :span, addon, class: 'input-group-addon' if addon end def input_group_container(has_addons, &block) if has_addons content_tag :div, class: 'input-group', &block else yield end end def label_and_field_container(label_and_field, field_type, errors = {}) klass = ['form-group'] klass << 'has-error' if errors.any? klass << 'has-feedback' if show_error_icon?(field_type, errors) content_tag :div, label_and_field, class: klass.join(' ') end def show_error_icon?(field_type, errors, suffix = nil) no_icon = %w(checkbox number_field radio_button select legend) hide = no_icon.include?(field_type.to_s) errors.any? && @options.fetch(:errors, {}).fetch(:icons, true) && !hide && suffix.nil? end def error_icon_tag icon :remove, class: 'form-control-feedback' end def help_tag(help_text) klass = ['help-block', 'text-left'] klass << 'sr-only' if inline_form? content_tag :span, help_text, class: klass.join(' ') end # Rails wraps the label in