Sha256: 91412943742de8ded7cac8525c222fc011efd131a58890e6e47f925bdb0c2c8b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module ActiveFields
  module Field
    class Text < ActiveFields.config.field_base_class
      store_accessor :options, :required, :min_length, :max_length

      # attribute :required, :boolean, default: false
      # attribute :min_length, :integer
      # attribute :max_length, :integer

      validates :required, exclusion: [nil]
      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[required].each do |column|
        define_method(column) do
          Casters::BooleanCaster.new.deserialize(super())
        end

        define_method(:"#{column}?") do
          !!public_send(column)
        end

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

      %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
        self.required ||= false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_fields-0.2.0 app/models/active_fields/field/text.rb