class Asbru::FormBuilder

Constants

BASIC_FORM_FIELDS

Public Instance Methods

check_box(attribute, options = {}) click to toggle source
# File lib/asbru/form_builder.rb, line 204
def check_box(attribute, options = {})
  render_form_element(
      attribute, { type: 'hidden', value: 'false', label: false }
    ).concat(
    render_form_element(
        attribute, options.merge({ type: 'checkbox' })
      )
  )
end
collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}) click to toggle source
# File lib/asbru/form_builder.rb, line 148
def collection_check_boxes(attribute,
    collection,
    value_method,
    text_method,
    options = {},
    html_options = {})
  render_form_element(attribute, options.merge(html_options).merge(
    {
      type: 'checkbox',
      options: create_collection_options(attribute,
          collection,
          value_method,
          text_method,
          options,
          true)
    }
  ))
end
collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}) click to toggle source
# File lib/asbru/form_builder.rb, line 130
def collection_radio_buttons(attribute,
    collection,
    value_method,
    text_method,
    options = {},
    html_options = {})
  render_form_element(attribute, options.merge(html_options).merge(
    {
      type: 'radio',
      options: create_collection_options(attribute,
          collection,
          value_method,
          text_method,
          options, false)
    }
  ))
end
collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {}) click to toggle source
Calls superclass method
# File lib/asbru/form_builder.rb, line 182
def collection_select(attribute,
    collection,
    value_method,
    text_method,
    options = {},
    html_options = {})
  if options[:without_react].present?
    super
  else
    render_form_element(attribute, options.merge(html_options).merge(
      {
        type: 'select',
        options: create_collection_options(attribute,
          collection,
          value_method,
          text_method,
          options)
      }
    ))
  end
end
create_collection_options(attribute, collection, value_method, text_method, _options, multiple = false) click to toggle source
# File lib/asbru/form_builder.rb, line 99
def create_collection_options(attribute,
    collection,
    value_method,
    text_method,
    _options,
    multiple = false)
  collection.map do |item|
    {
      label: item.send(text_method),
      value: item.send(value_method),
      name: tag_name_for(attribute, multiple),
      checked: if @object.nil?
         false
       else
         if @object.send(attribute).is_a?(Array)
           @object.send(attribute).include?(item.send(value_method))
         else
           @object.send(attribute).to_s == item.send(value_method).to_s
         end
       end

    }
  end
end
create_options(attribute, choices, chosen) click to toggle source
# File lib/asbru/form_builder.rb, line 87
def create_options(attribute, choices, chosen)
  choices.to_h.map do |key, value|
    {
      label: key,
      value: value,
      name: tag_name_for(attribute),
      checked: @object.nil? ? false :
          @object.send(attribute) == chosen
    }
  end
end
errors(name) click to toggle source
# File lib/asbru/form_builder.rb, line 41
def errors(name)
  if object.respond_to?(:errors) &&
      !(name.nil? || object.errors[name].empty?)
    object.errors.messages.dig(name)
  end
end
render_form_element(attribute, options) click to toggle source
# File lib/asbru/form_builder.rb, line 56
def render_form_element(attribute, options)
  new_options = options.merge(
    {
      name: tag_name_for(attribute),
      errors: errors(attribute)
    }
  )

  if @object.present? && attribute.present?
    if options[:value].nil?
      # if type of column is date, use default date format
      if @object.class.try(:columns_hash).present? &&
          @object.class
            .columns_hash[attribute.to_s]
            .try(:type) == :date && @object.send(attribute).present?
        new_options[:value] = I18n.l(@object.send(attribute),
            format: I18n.t('date_format.default'))
      else
        new_options[:value] = @object.send(attribute)
      end
    end
    if options[:label].nil?
      new_options[:label] = I18n.t(attribute,
          scope: "activerecord.attributes.#{@object.class.name.downcase}")
    end
  end

  Asbru::Components::Savage.send "#{Asbru.config.form_builder_prefix}_element",
      **new_options
end
select(attribute, choices = nil, chosen = nil, options = {}, html_options = {}) click to toggle source
# File lib/asbru/form_builder.rb, line 167
def select(attribute,
    choices = nil,
    chosen = nil,
    options = {},
    html_options = {})
  render_form_element(
      attribute, options.merge(html_options).merge(
      {
        type: 'select',
        options: create_options(attribute, choices, chosen)
      }
    )
    )
end
tag_name_for(method, multiple = false) click to toggle source
# File lib/asbru/form_builder.rb, line 48
def tag_name_for(method, multiple = false)
  name = ActionView::Helpers::Tags::TextField.new(object_name,
      method, {})
    .send(:tag_name)
  name += '[]' if multiple
  name
end