Sha256: 4e1acf304c9dc9171acf7078544065b28507c62364575b969e196a29403d6f7e

Contents?: true

Size: 734 Bytes

Versions: 1

Compression:

Stored size: 734 Bytes

Contents

require 'active_model'

module LinkedVocabs::Validators
  class PropertyValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, values)
      values.each do |v|
        unless v.try(:in_vocab?)
          term = v.try(:rdf_subject) || v
          vocabularies = record.class.properties[attribute.to_s].class_name.vocabularies.keys
          record.errors.add :base, "value `#{term}' for `#{attribute}' property is not a term in a controlled vocabulary #{vocabularies.join(', ')}"
        end
      end
    end
  end
end

module ActiveModel::Validations::HelperMethods
  def validates_vocabulary_of(*attr_names)
    validates_with LinkedVocabs::Validators::PropertyValidator, :attributes => attr_names
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linked_vocabs-0.3.0 lib/linked_vocabs/validators/property_validator.rb