Sha256: b23b0e26fdccb03fad256bc02f522c6e725ad7721eea4f98386feb5a34133e72

Contents?: true

Size: 1.06 KB

Versions: 37

Compression:

Stored size: 1.06 KB

Contents

class CustomFormBuilder < SimpleForm::FormBuilder
  def initialize(*)
    super
  end

  def association(association, options = {}, &block)
    resolve_custom_input_association(association, options)
    super(association, options, &block)
  end

  def resolve_custom_input_association(association, options)
    return if options[:as].present?

    [
      "#{object.class.name.demodulize.underscore}_#{association.to_s}".to_sym, association
    ].each do |key|
      camelized = "#{key.to_s.camelize}Input"
      mapping = attempt_mapping_with_custom_namespace(camelized) ||
                attempt_mapping(camelized, Object) ||
                attempt_mapping(camelized, self.class)

      next unless mapping.present?

      options[:as] = key
      break
    end
  end

  private

  def fetch_association_collection(reflection, options)
    options_method = "options_for_#{reflection.name}".to_sym
    if object.respond_to?(options_method) then
      options.fetch(:collection) do
        object.send(options_method)
      end
    else
      super(reflection, options)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
five-two-nw-olivander-0.2.0.18 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.17 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.16 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.15 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.14 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.13 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.12 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.11 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.10 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.9 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.8 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.7 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.6 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.5 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.4 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.3 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.2 app/inputs/custom_form_builder.rb