Module: Glue::Validation::ClassMethods
- Defined in:
- lib/og/validation.rb
Instance Method Summary collapse
-
#validate_related(*params) ⇒ Object
(also: #validate_associated)
Validate the related object or objects.
-
#validate_unique(*params) ⇒ Object
Validates that the given field(s) contain unique values.
Instance Method Details
#validate_related(*params) ⇒ Object Also known as: validate_associated
Validate the related object or objects. Works with all relations.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/og/validation.rb', line 61 def (*params) c = { :msg => Glue::Validation::Errors.invalid_relation, :on => :save } c.update(params.pop) if params.last.is_a?(Hash) for name in params code = %{ unless (obj.#{name}.is_a?(Array) ? obj.#{name} : [obj.#{name}]).inject(true) { |memo, obj| (obj.nil? or obj.valid?) and memo } errors.add(:#{name}, '#{c[:msg]}') end; } validations! << [code, c[:on]] end end |
#validate_unique(*params) ⇒ Object
Validates that the given field(s) contain unique values. Ensures that if a record is found with a matching value, that it is the same record, allowing updates.
The Og libraries are required for this methdod to work. You can override this method if you want to use another OR mapping library.
Example
validate_unique :param, :msg => ‘Value is already in use’ – TODO: :unique should implicitly generate validate_unique. ++
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/og/validation.rb', line 35 def validate_unique(*params) c = { :on => :save } c.update(params.pop) if params.last.is_a?(Hash) for name in params do c[:msg] ||= "#{self.name} with this #{name} already exists" code = %{ others = obj.class.send(:find_by_#{name}, obj.send(:#{name})) unless others.empty? if (others.size != 1) or (others[0].oid != obj.oid) errors.add(:#{name}, '#{c[:msg]}') end end } validations! << [code, c[:on]] end end |