Sha256: df001392fb48f1dd52626f6f95f41c4aedf6201f11e7353eeaf502bde3ad1603

Contents?: true

Size: 1.56 KB

Versions: 18

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module EacRailsUtils
  module CommonFormHelper
    class FormBuilder
      module AssociationSelectField
        def association_select_field(field_name, options = {})
          options = options.dup
          methods = extract_methods(options)
          select_options = extract_select_options(options)
          collection = extract_association_key(field_name, options, :collection, :all)
          foreign_key =  extract_association_key(field_name, options, :foreign_key,
                                                 :association_foreign_key)
          field(field_name, options) do
            form.collection_select(foreign_key, collection, methods[:value], methods[:text],
                                   select_options, class: 'form-control')
          end
        end

        private

        def extract_methods(options)
          { value: options.delete(:value_method) || :id, text:
              options.delete(:text_method) || :to_s }
        end

        def extract_select_options(options)
          options.extract!(:prompt, :include_blank)
        end

        def extract_association_key(field_name, options, key, method)
          return options.delete(key) if options.key?(key)
          if model_instance.class.respond_to?(:reflect_on_association)
            return model_instance.class.reflect_on_association(field_name).send(method)
          end

          raise "#{model_instance.class} não possui um método \"reflect_on_association\". " \
            "Defina explicitamente a opção :#{key}"
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
eac_rails_utils-0.14.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.13.5 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.13.4 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.13.3 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.13.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.13.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.13.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.12.3 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.12.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.12.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.12.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.6 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.5 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.4 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.3 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
eac_rails_utils-0.11.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb