Sha256: ea199fb03175b5c47a01b759a95bcccf5d67ebaea46cf4efac382e1a9dcdccee

Contents?: true

Size: 809 Bytes

Versions: 7

Compression:

Stored size: 809 Bytes

Contents

module Volt
  class UniqueValidator
    def self.validate(model, field_name, args)
      if RUBY_PLATFORM != 'opal'
        if args
          value  = model.get(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
          return $page.store.get(model.path[-2]).where(query).first.then do |item|
            if item
              message = (args.is_a?(Hash) && args[:message]) || 'is already taken'

              # return the error
              next { field_name => [message] }
            end
          end
        end
      end

      # no errors
      {}
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
volt-0.9.4.pre3 lib/volt/models/validators/unique_validator.rb
volt-0.9.4.pre2 lib/volt/models/validators/unique_validator.rb
volt-0.9.4.pre1 lib/volt/models/validators/unique_validator.rb
volt-0.9.3 lib/volt/models/validators/unique_validator.rb
volt-0.9.3.pre6 lib/volt/models/validators/unique_validator.rb
volt-0.9.3.pre5 lib/volt/models/validators/unique_validator.rb
volt-0.9.3.pre4 lib/volt/models/validators/unique_validator.rb