Sha256: ead27e0bef7e42301f80d69dd74b3599447685545d48dfa6f140ee47ded44180

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module RailsBootstrapForm
  class BootstrapFormBuilder < ActionView::Helpers::FormBuilder

    include RailsBootstrapForm::FieldWrapperBuilder
    include RailsBootstrapForm::Helpers
    include RailsBootstrapForm::InputGroupBuilder
    include RailsBootstrapForm::Inputs

    delegate :capture, :concat, :tag, to: :@template

    attr_accessor :bootstrap_form_options

    def initialize(object_name, object, template, options)
      @bootstrap_form_options = RailsBootstrapForm::BootstrapFormOptions.new(options[:bootstrap_form])
      apply_default_form_options(options)
      apply_default_form_classes(options)
      super(object_name, object, template, options)
    end

    def fields_for(record_name, record_object = nil, fields_options = {}, &block)
      fields_options = fields_for_options(record_object, fields_options)
      record_object = nil if record_object.is_a?(Hash) && record_object.extractable_options?
      super(record_name, record_object, fields_options, &block)
    end

    def apply_default_form_options(options)
      options[:html] ||= {}
      options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
    end

    def apply_default_form_classes(options)
      return unless @bootstrap_form_options.layout_inline?

      options[:html][:class] =
        ([*options[:html][:class]&.split(/\s+/)] + %w[row row-cols-lg-auto g-3 align-items-center])
        .compact.uniq.join(" ")
    end

    def fields_for_options(record_object, fields_options)
      field_options = record_object if record_object.is_a?(Hash) && record_object.extractable_options?
      field_options = {bootstrap_form: options.fetch(:bootstrap_form, {})}
      field_options.deep_merge!(field_options) if field_options.respond_to?(:deep_merge!)
      field_options
    end

    private :apply_default_form_options, :fields_for_options, :apply_default_form_classes
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_bootstrap_form-0.8.2 lib/rails_bootstrap_form/bootstrap_form_builder.rb