Class: ScopeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/scope_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/validators/scope_validator.rb', line 2

def validate_each(record, attribute, value)
  id_method = :"#{attribute}_id"
  valid_collection = :"valid_#{attribute.to_s.pluralize}"

  if record.send(id_method).present?
    # Don't allow users to assign the IDs of other teams' or users' resources to this attribute.
    unless record.send(valid_collection).exists?(id: record.send(id_method))
      record.errors.add(id_method, :invalid)
    end
  end
end