Sha256: ecc8fadb388b6d9d239b5558cd1d2a0c2f9389feb33f4402b8dea2f0e3d5df73

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module ActiveFields
  module Field
    class Date < ActiveFields.config.field_base_class
      acts_as_active_field(
        validator: {
          class_name: "ActiveFields::Validators::DateValidator",
          options: -> { { required: required?, min: min, max: max } },
        },
        caster: {
          class_name: "ActiveFields::Casters::DateCaster",
        },
      )

      store_accessor :options, :required, :min, :max

      validates :required, exclusion: [nil]
      validates :max, comparison: { greater_than_or_equal_to: :min }, allow_nil: true, if: :min

      %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 max].each do |column|
        define_method(column) do
          Casters::DateCaster.new.deserialize(super())
        end

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

      private

      def set_defaults
        self.required ||= false
      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/date.rb
active_fields-1.0.0 app/models/active_fields/field/date.rb