| Class | FlexScaffold::Validations |
| In: |
lib/validations.rb
|
| Parent: | Object |
| VALIDATIONS_INFO | = | %w( message with on in maximum minimum too_short too_long ) |
| SCHEMA_INFO | = | %w( type limit default ) |
Adds a validation of an attribute (attr_name) to the record and returns the individual set that was added
# File lib/validations.rb, line 14
14: def add(attr_name, type, configuration={})
15: attribute = add_type_to_hash(attr_name, type)
16: VALIDATIONS_INFO.each do |rule| attribute.update(rule.to_s => configuration[rule.to_sym]) unless configuration[rule.to_sym].nil? end
17: @attrs
18: end
Removes all the validations that have been added
# File lib/validations.rb, line 40
40: def clear
41: @attrs = {}
42: end
Yields each attribute and associated details per validation added
# File lib/validations.rb, line 50
50: def each
51: @attrs.each_key { |attr| @attrs[attr].each { |msg| yield attr, msg } }
52: end
Returns true if no validations have been added
# File lib/validations.rb, line 55
55: def empty?
56: @attrs.empty?
57: end
Returns the schema-based validation information of the attributes. This information includes the type, limit, and default values. It is categorised by the attribute name
# File lib/validations.rb, line 28
28: def get_schema_info(class_name=@record)
29: klass = Object.const_get(class_name.to_s.camelize) rescue nil
30: if klass && klass < ActiveRecord::Base
31: klass.columns.each do |col|
32: attribute = add_schema_to_hash(col.name)
33: SCHEMA_INFO.each do |info| attribute.update(info => eval("col.#{info}.to_s")) end
34: end
35: end
36: @schema_attrs
37: end
Adds the schema information into the validation of the attributes. This information includes the type, limit, and default values.
# File lib/validations.rb, line 22
22: def load_schema_info(class_name=@record)
23: @attrs.merge(get_schema_info(class_name))
24: end
Returns the number of attributes with validations
# File lib/validations.rb, line 60
60: def size
61: @attrs.size
62: end
Returns the string of attributes with validations
# File lib/validations.rb, line 65
65: def to_s
66: @attrs.to_s
67: end
Returns an XML representation of the validation object
# File lib/validations.rb, line 70
70: def to_xml
71: @attrs.to_xml
72: end