Sha256: 7a8493d967e88a85e733d3b4652984d0476c7aed12f39f6cc780bc46c7216eff

Contents?: true

Size: 741 Bytes

Versions: 3

Compression:

Stored size: 741 Bytes

Contents

module Volt
  class UniqueValidator
    def self.validate(model, field_name, args)
      errors = {}

      if RUBY_PLATFORM != 'opal'
        if args
          value  = model.read_attribute(field_name)

          query = {}
          # Check to see if any other documents have this value.
          query[field_name.to_s] = value
          query['_id'] = {'$ne' => model._id}

          # Check if the value is taken
          # TODO: need a way to handle scope for unique
          if $page.store.send(:"_#{model.path[-2]}").find(query).size > 0
            message = (args.is_a?(Hash) && args[:message]) || "is already taken"

            errors[field_name] = [message]
          end
        end
      end

      errors
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.8.21 lib/volt/models/validators/unique_validator.rb
volt-0.8.20 lib/volt/models/validators/unique_validator.rb
volt-0.8.19 lib/volt/models/validators/unique_validator.rb