Sha256: 6ec814cb6ca4f83e11ae31dc51c9889c05e2e6278b06ab47aba64474765580a1

Contents?: true

Size: 810 Bytes

Versions: 3

Compression:

Stored size: 810 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).fetch_first 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

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.9.1 lib/volt/models/validators/unique_validator.rb
volt-0.9.1.pre5 lib/volt/models/validators/unique_validator.rb
volt-0.9.1.pre4 lib/volt/models/validators/unique_validator.rb