Sha256: 0c32810adfec3561c8f2f6aad32a4e49dee06a8a7045514a1854060014c1d526

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 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:)
          raise_error_if_not_supported(model_class, ar_column)

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

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

          VALUE_GENERATORS.keys.include?(ar_column.type)
        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
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_data_loader-1.0.1 lib/active_record_data_loader/active_record/column_configuration.rb