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.40 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.39 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.38 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.37 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.36 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.35 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.34 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.33 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.32 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.31 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.30 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.29 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.28 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.27 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.26 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.25 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.24 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.23 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.22 app/inputs/custom_form_builder.rb
five-two-nw-olivander-0.2.0.19 app/inputs/custom_form_builder.rb