Module: Wedge::Plugins::Form::Validations
- Includes:
- Methods
- Included in:
- Wedge::Plugins::Form
- Defined in:
- lib/wedge/plugins/form/validations.rb
Overview
Provides a base implementation for extensible validation routines. Scrivener::Validations currently only provides the following assertions:
-
assert
-
assert_present
-
assert_format
-
assert_numeric
-
assert_url
-
assert_email
-
assert_member
-
assert_length
-
assert_decimal
-
assert_equal
The core tenets that Scrivener::Validations advocates can be summed up in a few bullet points:
-
Validations are much simpler and better done using composition rather than macros.
-
Error messages should be kept separate and possibly in the view or presenter layer.
-
It should be easy to write your own validation routine.
Other validations are simply added on a per-model or per-project basis.
Instance Method Summary collapse
-
#_errors ⇒ Object
Hash of errors for each attribute in this model.
- #_model_errors ⇒ Object
- #error(key, value) ⇒ Object
- #errors ⇒ Object
-
#model_errors ⇒ Object
gives back errors using the model_alias keys.
- #valid(atts) ⇒ Object
-
#valid? ⇒ Boolean
Check if the current model state is valid.
-
#validate ⇒ Object
Base validate implementation.
Methods included from Methods
Instance Method Details
#_errors ⇒ Object
Hash of errors for each attribute in this model.
102 103 104 105 106 107 108 |
# File 'lib/wedge/plugins/form/validations.rb', line 102 def _errors @_errors ||= Hash.new do |hash, key| data = [key] && [key].key?(:form) ? {} : [] alias_key = _aliases[key] || key _model_errors[alias_key] = hash[key] = data end end |
#_model_errors ⇒ Object
110 111 112 |
# File 'lib/wedge/plugins/form/validations.rb', line 110 def _model_errors @_model_errors ||= {} end |
#error(key, value) ⇒ Object
87 88 89 90 |
# File 'lib/wedge/plugins/form/validations.rb', line 87 def error key, value value = [value] unless value.is_a? Array _errors[key] = value end |
#errors ⇒ Object
92 93 94 |
# File 'lib/wedge/plugins/form/validations.rb', line 92 def errors IndifferentHash.new(_errors) end |
#model_errors ⇒ Object
gives back errors using the model_alias keys
97 98 99 |
# File 'lib/wedge/plugins/form/validations.rb', line 97 def model_errors IndifferentHash.new(_model_errors) end |
#valid(atts) ⇒ Object
78 79 80 81 |
# File 'lib/wedge/plugins/form/validations.rb', line 78 def valid atts _set_atts atts valid? end |
#valid? ⇒ Boolean
72 73 74 75 76 |
# File 'lib/wedge/plugins/form/validations.rb', line 72 def valid? _errors.clear validate _errors.empty? end |
#validate ⇒ Object
Base validate implementation. Override this method in subclasses.
84 85 |
# File 'lib/wedge/plugins/form/validations.rb', line 84 def validate end |