lib/hydra/validations/uniqueness.rb in hydra-validations-0.3.2 vs lib/hydra/validations/uniqueness.rb in hydra-validations-0.4.0

- old
+ new

@@ -1,7 +1,6 @@ -require 'hydra/validations' -require 'hydra/validations/cardinality' +require 'active_support/core_ext/array/wrap' module Hydra module Validations # # UniquenessValidator - an ActiveFedora model validator @@ -23,25 +22,29 @@ # False negatives (record invalid) may result if, for example, # querying a Solr field of type "text". # class UniquenessValidator < ActiveModel::EachValidator - include Cardinality - def check_validity! - raise ArgumentError, "UniquenessValidator accepts only a single attribute: #{attribues}" if attributes.length > 1 - raise ArgumentError, "UniquenessValidator requires the :solr_name option be present." unless options[:solr_name].present? + if attributes.length > 1 + raise ArgumentError, "UniquenessValidator accepts only a single attribute: #{attributes}" + end + unless options[:solr_name].present? + raise ArgumentError, "UniquenessValidator requires the :solr_name option be present." + end end def validate_each(record, attribute, value) - # TODO: i18n messages - validate_cardinality(:single, record, attribute, value) - # Validate uniqueness proper only if value is of single cardinality - return if record.errors.added?(attribute, "can't have more than one value") - value = value.first if value.respond_to?(:each) - conditions = {options[:solr_name] => value} - conditions.merge!("-id" => record.id) if record.persisted? - record.errors.add attribute, "has already been taken" if record.class.exists?(conditions) + wrapped_value = Array.wrap(value) + if wrapped_value.length > 1 + record.errors.add(attribute, "can't have more than one value") + elsif wrapped_value.empty? + record.errors.add(attribute, "can't be empty") unless options[:allow_empty] + else + conditions = {options[:solr_name] => wrapped_value.first} + conditions.merge!("-id" => record.id) if record.persisted? + record.errors.add attribute, "has already been taken" if record.class.exists?(conditions) + end end end module HelperMethods