Sha256: dfd4398dfea74d2d3de77462928d222ab212594701e6527a6658d0a4eeba4dfb
Contents?: true
Size: 1.85 KB
Versions: 4
Compression:
Stored size: 1.85 KB
Contents
require 'glue/validation' module Glue # Extend the Validation methods defined in glue/validation.rb with # extra db related options. module Validation # Encapsulates a list of validation errors. class Errors cattr_accessor :not_unique, 'The value is already used' cattr_accessor :invalid_relation, 'Invalid relations' end module MetaLanguage # 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. #++ 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 # Validate the related object or objects. Works # with all relations. 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 alias_method :validate_associated, :validate_related end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
og-0.21.0 | lib/og/validation.rb |
og-0.21.2 | lib/og/validation.rb |
og-0.22.0 | lib/og/validation.rb |
og-0.23.0 | lib/og/validation.rb |