Module: CouchSurfer::Validations::InstanceMethods

Defined in:
lib/couch_surfer/validations.rb

Instance Method Summary collapse

Instance Method Details

#is_unique?(field, options) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/couch_surfer/validations.rb', line 18

def is_unique?(field, options)
  if options[:view]
    view_name = options[:view]
    query     = options[:query].is_a?(Proc) ? self.instance_eval(&options[:query]) : nil
  end
  view_name ||= "by_#{field}"
  query ||= {:key => self.send(field)}
  result = self.class.send(view_name, query)
  if result.blank?
    return true
  else
    return !id.blank? && (id == result.first.id)
  end
end

#validate_instanceObject



33
34
35
# File 'lib/couch_surfer/validations.rb', line 33

def validate_instance
  throw(:halt, false) unless valid?
end