class SimpleBootstrapForm::HorizontalForm::Fields::BaseField

Attributes

input_type[RW]

Public Class Methods

new(form_builder, template, name, options) click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 10
def initialize(form_builder, template, name, options)
  @form_builder = form_builder
  @template = template
  @name = name
  process_options(options)
end

Public Instance Methods

to_s() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 17
def to_s
@template.content_tag :div, group_options do
    field_label +
    input_tag +
    errors_block
  end
end

Private Instance Methods

errors() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 103
def errors
  return [] unless model
  @cached_errors ||= model.errors[@name.to_sym]
end
errors_block() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 93
def errors_block
  return '' unless errors.any?
  @template.content_tag :span, errors.join(', '),
                               class: 'help-block col-sm-3'
end
field_label() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 52
def field_label
  @form_builder.label @name, label_text, label_options
end
group_class() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 44
def group_class # a class for the form group to make it more testable
  case @group_class
  when false; nil
  when nil; "#{@form_builder.object_name.to_s.underscore}_#{@name}_group"
  else @group_class
  end
end
group_options() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 38
def group_options
  css_classes = CssClassList.new 'form-group', group_class
  css_classes << 'has-error' if has_error?
  { class: css_classes }
end
has_error?() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 116
def has_error?
  errors.any?
end
input_options() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 81
def input_options
  @options.merge! class: 'form-control',
                  placeholder: placeholder,
                  type: self.class.input_type
  @options.merge! required: 'required' if required?
  @options
end
input_tag() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 75
def input_tag
  @template.content_tag(:div, class: @input_size) do
    @form_builder.text_field @name, input_options
  end
end
label_options() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 69
def label_options
  css_classes = CssClassList.new 'control-label'
  css_classes << @label_size
  { class: css_classes.to_s }
end
label_text() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 56
def label_text
  text = @label_text || @name.to_s.humanize.capitalize
  required_asterisk + text
end
model() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 99
def model
  @form_builder.object
end
placeholder() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 89
def placeholder
  @options[:placeholder] || @name.to_s.humanize
end
process_options(options) click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 27
def process_options(options)
  @options = options.dup
  @label_size  = @options.delete :label_size
  @input_size  = @options.delete :input_size
  @label_text  = @options.delete :label
  @group_class = @options.delete :group_class
  unless @label_size && @input_size
    raise "label_size and input_size are required options"
  end
end
required?() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 108
def required?
  return false unless model
  return @options[:required] if @options.has_key?(:required)
  model.class.validators_on(@name).any? do |validator|
    validator.kind_of? ActiveModel::Validations::PresenceValidator
  end
end
required_asterisk() click to toggle source
# File lib/simple_bootstrap_form/horizontal_form/fields/base_field.rb, line 61
def required_asterisk
  if required?
    @template.content_tag(:abbr, '*', title: 'required') + ' '
  else
    ''
  end.html_safe
end