Codeminer42/cm42-central

View on GitHub
app/validators/user_belongs_to_project_validator.rb

Summary

Maintainability
A
0 mins
Test Coverage
class UserBelongsToProjectValidator < ActiveModel::EachValidator
  # TODO: Change de name class to BelongsToProjectValidator when remove support gem
  # Checks that the parameter being validated represents a User#id that
  # is a member of the object.project
  def validate_each(record, attribute, value)
    if record.project && !value.nil?
      unless record.project.user_ids.include?(value)
        record.errors.add(attribute, message: 'user is not a member of this project')
      end
    end
  end
end