Module: Glue::Validation::MetaLanguage

Defined in:
lib/og/validation.rb

Instance Method Summary collapse

Instance Method Details

Validate the related object or objects. Works with all relations.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/og/validation.rb', line 54

def validate_related(*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;
		}

		__meta[:validations] << [code, c[:on]]
	end
end

#validate_unique(*params) ⇒ Object

Validate the value of this attribute is unique for this class.

The Og libraries are required for this methdod to work. You can override this method if you want to use another OR mapping library.

– TODO: :unique should implicitly generate validate_unique. ++



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/og/validation.rb', line 31

def validate_unique(*params)
	c = { 
		:msg => Glue::Validation::Errors.not_unique, 
		:on => :save 
	}
	c.update(params.pop) if params.last.is_a?(Hash)

	for name in params
		# FIXME: improve the generated code.
		code = %{
			others = obj.class.find_by_#{name}(obj.#{name})
			unless (others.is_a?(Array) and others.empty?)
				errors.add(:#{name}, '#{c[:msg]}')
			end;
		}

		__meta[:validations] << [code, c[:on]]
	end
end