Class: Symphonia::FormBuilder

Inherits:
BootstrapForm::FormBuilder
  • Object
show all
Defined in:
lib/symphonia/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#calendar_field(name, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/symphonia/form_builder.rb', line 16

def calendar_field(name, options = {})
  options[:data] ||= {} #{'date-clear-btn': true, 'date-format': 'yyyy-mm-dd'}
  options[:data][:'date-clear-btn'] ||= true
  # options[:data][:'date-format'] ||= 'yyyy-mm-dd'
  text_field(name, options.merge({
                                     class: 'form-control datepicker',
                                     append: @template.fa_icon('calendar')
                                 }))
end

#error_classObject

def generate_label(id, name, options, custom_label_col, group_layout)

return if options.blank?
# id is the caller's options[:id] at the only place this method is called.
# The options argument is a small subset of the options that might have
# been passed to generate_label's caller, and definitely doesn't include
# :id.
options[:for] = id if acts_like_form_tag
classes = [options[:class]]

if layout_horizontal?(group_layout)
  classes << "col-form-label"
  classes << (custom_label_col || label_col)
elsif layout_inline?(group_layout)
  classes << "mr-sm-2"
end

xname = name.to_s.remove(/_id$/)

case options.delete(:required)
when true
  classes << "required"
when nil, :default
  classes << "required" if required_attribute?(object, name)
end

options[:class] = classes.compact.join(" ").strip
options.delete(:class) if options[:class].empty?

if label_errors && has_error?(name)
  error_messages = get_error_messages(name)
  label_text = (options[:text] || object.class.human_attribute_name(xname)).to_s.concat(" #{error_messages}")
  options[:class] = [options[:class], "text-danger"].compact.join(" ")
  label(name, label_text, options.except(:text))
else
  label(name, options[:text], options.except(:text))
end

end



132
133
134
# File 'lib/symphonia/form_builder.rb', line 132

def error_class
  'is-invalid'
end

#error_messagesObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/symphonia/form_builder.rb', line 4

def error_messages
  if object.respond_to?(:errors) && object.errors.any?
    list = (:p, I18n.t('activerecord.errors.template.body')).html_safe
    list += (:ul) do
      object.errors.full_messages.collect do |error|
        (:li, error)
      end.join.html_safe
    end
    (:div, list, class: 'error_explanation')
  end
end

#label_text(name, options) ⇒ Object

def form_group_builder(method, options, html_options = nil)

options.symbolize_keys!
html_options.symbolize_keys! if html_options

# Add control_class; allow it to be overridden by :control_class option
css_options = html_options || options
control_classes = css_options.delete(:control_class) { control_class }
css_options[:class] = [control_classes, css_options[:class]].compact.join(" ")
css_options[:class] << " is-invalid" if has_error?(method)

options = convert_form_tag_options(method, options) if acts_like_form_tag

wrapper_class = css_options.delete(:wrapper_class)
wrapper_options = css_options.delete(:wrapper)
help = options.delete(:help)
icon = options.delete(:icon)
label_col = options.delete(:label_col)
control_col = options.delete(:control_col)
layout = get_group_layout(options.delete(:layout))
form_group_options = {
  id: options[:id],
  help: help,
  icon: icon,
  label_col: label_col,
  control_col: control_col,
  layout: layout,
  class: wrapper_class
}

if wrapper_options.is_a?(Hash)
  form_group_options.merge!(wrapper_options)
end

unless options.delete(:skip_label)
  if options[:label].is_a?(Hash)
    label_text  = options[:label].delete(:text)
    label_class = options[:label].delete(:class)
    options.delete(:label)
  end
  label_class ||= options.delete(:label_class)
  label_class = hide_class if options.delete(:hide_label)

  if options[:label].is_a?(String)
    label_text ||= options.delete(:label)
  end

  form_group_options.merge!(label: {
    text: label_text,
    class: label_class,
    skip_required: options.delete(:skip_required),
    required: options.delete(:required)
  })
end

form_group(method, form_group_options) do
  yield
end

end



85
86
87
88
89
90
91
92
93
# File 'lib/symphonia/form_builder.rb', line 85

def label_text(name, options)
  name = name.to_s.remove(/_id$/)

  if label_errors && error?(name)
    (options[:text] || object.class.human_attribute_name(name)).to_s.concat(" #{get_error_messages(name)}")
  else
    options[:text]
  end
end