Sha256: 2f1f4ff8054b6f0b319a12656b7185e015912548f557b450c06060a4c8a7affd

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module ActiveFields
  module Field
    class TextArray < ActiveFields.config.field_base_class
      acts_as_active_field(
        array: true,
        validator: {
          class_name: "ActiveFields::Validators::TextArrayValidator",
          options: -> do
            { min_length: min_length, max_length: max_length, min_size: min_size, max_size: max_size }
          end,
        },
        caster: {
          class_name: "ActiveFields::Casters::TextArrayCaster",
        },
      )

      store_accessor :options, :min_length, :max_length

      validates :min_length, comparison: { greater_than_or_equal_to: 0 }, allow_nil: true
      validates :max_length, comparison: { greater_than_or_equal_to: ->(r) { r.min_length || 0 } }, allow_nil: true

      %i[min_length max_length].each do |column|
        define_method(column) do
          Casters::IntegerCaster.new.deserialize(super())
        end

        define_method(:"#{column}=") do |other|
          super(Casters::IntegerCaster.new.serialize(other))
        end
      end

      private

      def set_defaults; end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_fields-1.1.0 app/models/active_fields/field/text_array.rb
active_fields-1.0.0 app/models/active_fields/field/text_array.rb