Sha256: 7a3f690c6ef7fabb5dd1460cb3d6a3bebe1bf1d82b6fcca8ecbb3125c955c903

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

class CustomFormBuilder < SimpleForm::FormBuilder
  def initialize(*)
    Rails.logger.warn "instantiating this class"
    super
  end

  def association(association, options = {}, &block)
    Rails.logger.warn "making an association"
    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}#{association.to_s.titleize}", "#{association.to_s.titleize}"
    ].each do |key|
      Rails.logger.debug "trying for #{key}"
      next unless attempt_mapping_with_custom_namespace("#{key}Input").present?

      options[:as] = key.to_sym
      break
    end
  end

  private

  def fetch_association_collection(reflection, options)
    Rails.logger.warn "we got into here"

    options_method = "options_for_#{reflection.name}".to_sym
    if object.respond_to?(options_method) then
      Rails.logger.info "using specific method"
      options.fetch(:collection) do
        object.send(options_method)
      end
    else
      Rails.logger.info "doing it old school"
      super(reflection, options)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
five-two-nw-olivander-0.1.2.27 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.1.2.26 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.1.2.25 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.1.2.24 app/inputs/custom_form_builder.rb