Sha256: 88e63aa942c27068f67509f751e957a99b035eb2fe71ba810a35a047070a081a

Contents?: true

Size: 755 Bytes

Versions: 2

Compression:

Stored size: 755 Bytes

Contents

# frozen_string_literal: true

module ActiveFields
  # Model mix-in that adds array functionality to the active fields model
  module FieldArrayConcern
    extend ActiveSupport::Concern

    included do
      store_accessor :options, :min_size, :max_size

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

      %i[min_size max_size].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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_fields-1.1.0 app/models/concerns/active_fields/field_array_concern.rb
active_fields-1.0.0 app/models/concerns/active_fields/field_array_concern.rb