Welcome to mirror list, hosted at ThFree Co, Russian Federation.

any_field_validation.rb « concerns « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 987c4e7800e71cdaf88dfa08c6f04cc7c14fd69e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# frozen_string_literal: true

# This module enables a record to be valid if any field is present
#
# Overwrite one_of_required_fields to set one of which fields must be present
module AnyFieldValidation
  extend ActiveSupport::Concern

  included do
    validate :any_field_present
  end

  private

  def any_field_present
    return unless one_of_required_fields.all? { |field| self[field].blank? }

    errors.add(:base, _("At least one field of %{one_of_required_fields} must be present") %
      { one_of_required_fields: one_of_required_fields })
  end

  def one_of_required_fields
    raise NotImplementedError
  end
end