class FoundationRailsHelper::FormBuilder

Public Instance Methods

autocomplete(attribute, url, options = {}) click to toggle source

rubocop:enable LineLength, ParameterLists

# File lib/foundation_rails_helper/form_builder.rb, line 103
def autocomplete(attribute, url, options = {})
  field attribute, options do |opts|
    opts.merge!(update_elements: opts[:update_elements],
                min_length: 0, value: object.send(attribute))
    autocomplete_field(attribute, url, opts)
  end
end
check_box(attribute, options = {}, checked_value = '1', unchecked_value = '0') click to toggle source

rubocop:disable LineLength

Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 31
def check_box(attribute, options = {}, checked_value = '1', unchecked_value = '0')
  custom_label(attribute, options[:label], options[:label_options]) do
    options.delete(:label)
    options.delete(:label_options)
    super(attribute, options, checked_value, unchecked_value)
  end + error_and_help_text(attribute, options)
end
collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {}) click to toggle source

rubocop:disable LineLength, ParameterLists

Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 86
def collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {})
  field attribute, options, html_options do |html_opts|
    html_options[:autocomplete] ||= :off
    super(attribute, collection, value_method, text_method, options,
          html_opts)
  end
end
date_select(attribute, options = {}, html_options = {}) click to toggle source
Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 63
def date_select(attribute, options = {}, html_options = {})
  field attribute, options, html_options do |html_opts|
    super(attribute, options, html_opts.merge(autocomplete: :off))
  end
end
datetime_select(attribute, options = {}, html_options = {}) click to toggle source
Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 57
def datetime_select(attribute, options = {}, html_options = {})
  field attribute, options, html_options do |html_opts|
    super(attribute, options, html_opts.merge(autocomplete: :off))
  end
end
error_for(attribute, options = {}) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 116
def error_for(attribute, options = {})
  return unless error?(attribute)

  class_name = 'form-error is-visible'
  class_name += " #{options[:class]}" if options[:class]

  error_messages = object.errors[attribute].join(', ')
  error_messages = error_messages.html_safe if options[:html_safe_errors]
  content_tag(:small, error_messages,
              class: class_name.sub('is-invalid-input', ''))
end
grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) click to toggle source
Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 94
def grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  field attribute, options, html_options do |html_opts|
    html_options[:autocomplete] ||= :off
    super(attribute, collection, group_method, group_label_method,
          option_key_method, option_value_method, options, html_opts)
  end
end
label(attribute, text = nil, options = {}) click to toggle source
Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 21
def label(attribute, text = nil, options = {})
  if error?(attribute)
    options[:class] ||= ''
    options[:class] += ' is-invalid-label'
  end

  super(attribute, (text || '').html_safe, options)
end
password_field(attribute, options = {}) click to toggle source
Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 51
def password_field(attribute, options = {})
  field attribute, options do |opts|
    super(attribute, opts.merge(autocomplete: :off))
  end
end
radio_button(attribute, tag_value, options = {}) click to toggle source

rubocop:enable LineLength

# File lib/foundation_rails_helper/form_builder.rb, line 40
def radio_button(attribute, tag_value, options = {})
  options[:label_options] ||= {}
  label_options = options.delete(:label_options).merge!(value: tag_value)
  label_text = options.delete(:label)
  l = label(attribute, label_text, label_options) unless label_text == false
  r = @template.radio_button(@object_name, attribute, tag_value,
                             objectify_options(options))

  "#{r}#{l}".html_safe
end
select(attribute, choices, options = {}, html_options = {}) click to toggle source

rubocop:enable LineLength

Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 78
def select(attribute, choices, options = {}, html_options = {})
  field attribute, options, html_options do |html_opts|
    html_options[:autocomplete] ||= :off
    super(attribute, choices, options, html_opts)
  end
end
submit(value = nil, options = {}) click to toggle source
Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 111
def submit(value = nil, options = {})
  options[:class] ||= FoundationRailsHelper.configuration.button_class
  super(value, options)
end
time_zone_select(attribute, priorities = nil, options = {}, html_options = {}) click to toggle source

rubocop:disable LineLength

Calls superclass method
# File lib/foundation_rails_helper/form_builder.rb, line 70
def time_zone_select(attribute, priorities = nil, options = {}, html_options = {})
  field attribute, options, html_options do |html_opts|
    super(attribute, priorities, options,
          html_opts.merge(autocomplete: :off))
  end
end

Private Instance Methods

auto_labels() click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 227
def auto_labels
  if @options[:auto_labels].nil?
    FoundationRailsHelper.configuration.auto_labels
  else
    @options[:auto_labels]
  end
end
calculate_input_size(prefix_options, postfix_options) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 171
def calculate_input_size(prefix_options, postfix_options)
  input_size =
    OpenStruct.new(changed?: false, small: 12, medium: 12, large: 12)
  %w(small medium large).each do |size|
    decrement_input_size(input_size, size.to_sym, prefix_options)
    decrement_input_size(input_size, size.to_sym, postfix_options)
  end

  input_size
end
column_classes(options) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 150
def column_classes(options)
  classes = SizeClassCalculator.new(options).classes
  classes + ' columns'
end
custom_label(attribute, text, options) { || ... } click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 142
def custom_label(attribute, text, options)
  return block_given? ? yield.html_safe : ''.html_safe if text == false

  text = default_label_text(object, attribute) if text.nil? || text == true
  text = safe_join([yield, text.html_safe]) if block_given?
  label(attribute, text, options || {})
end
decrement_input_size(input, column, options) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 163
def decrement_input_size(input, column, options)
  return unless options.present? && options.key?(column)

  input.send("#{column}=",
             (input.send(column) - options.fetch(column).to_i))
  input.send('changed?=', true)
end
default_label_text(object, attribute) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 134
def default_label_text(object, attribute)
  if object.class.respond_to?(:human_attribute_name)
    return object.class.human_attribute_name(attribute)
  end

  attribute.to_s.humanize
end
error?(attribute) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 130
def error?(attribute)
  object.respond_to?(:errors) && !object.errors[attribute].blank?
end
error_and_help_text(attribute, options = {}) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 194
def error_and_help_text(attribute, options = {})
  html = ''
  if options[:help_text]
    html += content_tag(:p, options[:help_text], class: 'help-text')
  end
  html += error_for(attribute, options) || ''
  html.html_safe
end
field(attribute, options, html_options = nil) { |class_options| ... } click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 208
def field(attribute, options, html_options = nil)
  html = field_label(attribute, options)
  class_options = html_options || options

  if error?(attribute)
    class_options[:class] = class_options[:class].to_s
    class_options[:class] += ' is-invalid-input'
  end

  options.delete(:label)
  options.delete(:label_options)
  help_text = options.delete(:help_text)
  prefix = options.delete(:prefix)
  postfix = options.delete(:postfix)

  html += wrap_prefix_and_postfix(yield(class_options), prefix, postfix)
  html + error_and_help_text(attribute, options.merge(help_text: help_text))
end
field_label(attribute, options) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 203
def field_label(attribute, options)
  return ''.html_safe unless auto_labels || options[:label]
  custom_label(attribute, options[:label], options[:label_options])
end
tag_from_options(name, options) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 155
def tag_from_options(name, options)
  return ''.html_safe unless options && options[:value].present?

  content_tag(:div,
              content_tag(:span, options[:value], class: name),
              class: column_classes(options).to_s)
end
wrap_prefix_and_postfix(block, prefix_options, postfix_options) click to toggle source
# File lib/foundation_rails_helper/form_builder.rb, line 182
def wrap_prefix_and_postfix(block, prefix_options, postfix_options)
  prefix = tag_from_options('prefix', prefix_options)
  postfix = tag_from_options('postfix', postfix_options)

  input_size = calculate_input_size(prefix_options, postfix_options)
  klass = column_classes(input_size.marshal_dump).to_s
  input = content_tag(:div, block, class: klass)

  return block unless input_size.changed?
  content_tag(:div, prefix + input + postfix, class: 'row collapse')
end