Sha256: ac9a4e3f9b9af391d921c6fc04bf69019523f52cef8b832d74811d1f92b9e09d

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module ActiveRecordDataLoader
  module ActiveRecord
    class ColumnConfiguration
      class << self
        VALUE_GENERATORS = {
          enum: EnumValueGenerator,
          integer: IntegerValueGenerator,
          string: TextValueGenerator,
          text: TextValueGenerator,
          datetime: DatetimeValueGenerator,
        }.freeze

        def config_for(model_class:, ar_column:, connection_factory:)
          raise_error_if_not_supported(model_class, ar_column)

          {
            ar_column.name.to_sym => VALUE_GENERATORS[column_type(ar_column)].generator_for(
              model_class: model_class,
              ar_column: ar_column,
              connection_factory: connection_factory
            ),
          }
        end

        def supported?(model_class:, ar_column:)
          return false if model_class.reflect_on_association(ar_column.name)

          VALUE_GENERATORS.keys.include?(column_type(ar_column))
        end

        private

        def raise_error_if_not_supported(model_class, ar_column)
          return if supported?(model_class: model_class, ar_column: ar_column)

          raise <<~ERROR
            Column '#{ar_column.name}' of type '#{ar_column.type}' in model '#{model_class.name}' not supported"
          ERROR
        end

        def column_type(ar_column)
          if ar_column.type == :string && ar_column.sql_type.to_s.downcase.start_with?("enum")
            :enum
          else
            ar_column.type
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_record_data_loader-1.2.0 lib/active_record_data_loader/active_record/column_configuration.rb
active_record_data_loader-1.1.0 lib/active_record_data_loader/active_record/column_configuration.rb
active_record_data_loader-1.0.2 lib/active_record_data_loader/active_record/column_configuration.rb