Sha256: 709f3dc25ae8b90681eb0a7053f3cfb69279e1b235ab67825318afbddce14ad3

Contents?: true

Size: 959 Bytes

Versions: 1

Compression:

Stored size: 959 Bytes

Contents

# frozen_string_literal: true

module Fields
  module Validations::Inclusion
    extend ActiveSupport::Concern

    included do
      embeds_one :inclusion, class_name: "Fields::Validations::Inclusion::InclusionOptions"
      accepts_nested_attributes_for :inclusion

      after_initialize do
        build_inclusion unless inclusion
      end
    end

    def interpret_to(model, field_name, accessibility, options = {})
      super
      inclusion&.interpret_to model, field_name, accessibility, options
    end

    class InclusionOptions < FieldOptions
      attribute :message, :string, default: ""
      attribute :in, :string, default: [], array: true

      def interpret_to(model, field_name, _accessibility, _options = {})
        return if self.in.empty?

        options = {in: self.in}
        options[:message] = message if message.present?

        model.validates field_name, inclusion: options, allow_blank: true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
script_core-0.1.0 spec/dummy/app/models/concerns/fields/validations/inclusion.rb