Module: Gourami::Validations
Constant Summary
Constants included from FormattingConstants
FormattingConstants::EMAIL_FORMAT, FormattingConstants::HEX_COLOR_FORMAT, FormattingConstants::ISRC_FORMAT
Instance Method Summary collapse
-
#any_errors? ⇒ Boolean
Return true if there are any errors.
-
#append_error(attribute_name, error) ⇒ Object
Append an error to the given attribute.
-
#attribute_has_errors?(attribute_name) ⇒ Boolean
Return true if there given attribute has any errors.
-
#clear_and_set_errors(new_errors) ⇒ Object
Replace the existing errors with the provided errors Hash.
-
#errors ⇒ Hash<Symbol, nil>, Array<Symbol, String>
Get the current form errors Hash.
- #handle_validation_error(error) ⇒ Object
-
#perform! ⇒ Object
Validate and perform the form actions.
- #raise_validate_errors ⇒ Object
-
#valid? ⇒ Boolean
Determine if current form instance is valid by running the validations specified on #validate.
- #validate ⇒ Object
-
#validate_any(attribute_name, message = nil) ⇒ Object
Validate the value of the given attribute is not empty.
- #validate_color_format(attribute_name, message = nil) ⇒ Object
-
#validate_email_format(attribute_name, message = nil) ⇒ Object
Validate the email format.
-
#validate_filetype(attribute_name, filetypes, message = nil) ⇒ Object
Validate the type of the file sent if included in the list.
-
#validate_format(attribute_name, format, message = nil) ⇒ Object
Validate the format of the attribute value.
-
#validate_inclusion(attribute_name, list, message = nil) ⇒ Object
Validate the value of the given attribute is included in the list.
-
#validate_inclusion_of_each(attribute_name, list, message = nil) ⇒ Object
Validate the presence of each object in attribute name within list.
- #validate_isrc_format(attribute_name, message = nil) ⇒ Object
-
#validate_length(attribute_name, options = {}) ⇒ Object
Validate the length of a String, Array or any other form attribute which responds to #size.
-
#validate_presence(attribute_name, message = nil) ⇒ Object
Validate the presence of the attribute value.
-
#validate_range(attribute_name, options = {}) ⇒ Object
TODO: Spec return value in spec/validations_spec.rb Validate the range in which the attribute can be.
-
#validate_uniqueness(attribute_name, message = nil, &block) ⇒ Object
Validate the uniqueness of the attribute value.
Instance Method Details
#any_errors? ⇒ Boolean
Return true if there are any errors.
79 80 81 |
# File 'lib/gourami/validations.rb', line 79 def any_errors? errors.reject{ |k,v| v.empty? }.any? end |
#append_error(attribute_name, error) ⇒ Object
Append an error to the given attribute.
88 89 90 |
# File 'lib/gourami/validations.rb', line 88 def append_error(attribute_name, error) errors[attribute_name] << error end |
#attribute_has_errors?(attribute_name) ⇒ Boolean
Return true if there given attribute has any errors.
74 75 76 |
# File 'lib/gourami/validations.rb', line 74 def attribute_has_errors?(attribute_name) errors[attribute_name.to_sym].any? end |
#clear_and_set_errors(new_errors) ⇒ Object
Replace the existing errors with the provided errors Hash.
57 58 59 60 61 62 63 |
# File 'lib/gourami/validations.rb', line 57 def clear_and_set_errors(new_errors) new_errors = new_errors.dup errors.clear errors.merge!(new_errors) errors end |
#errors ⇒ Hash<Symbol, nil>, Array<Symbol, String>
Get the current form errors Hash.
38 39 40 |
# File 'lib/gourami/validations.rb', line 38 def errors @errors ||= Hash.new { |hash, key| hash[key] = [] } end |
#handle_validation_error(error) ⇒ Object
69 70 71 |
# File 'lib/gourami/validations.rb', line 69 def handle_validation_error(error) clear_and_set_errors(error.errors) unless error.errors.nil? end |
#perform! ⇒ Object
Validate and perform the form actions. If any errors come up during the
validation or the #perform method, raise an exception with the errors.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gourami/validations.rb', line 16 def perform! if valid? begin returned = perform rescue Gourami::ValidationError => error handle_validation_error(error) raise end end if any_errors? raise_validate_errors end returned end |
#raise_validate_errors ⇒ Object
65 66 67 |
# File 'lib/gourami/validations.rb', line 65 def raise_validate_errors raise ValidationError.new(errors) end |
#valid? ⇒ Boolean
Determine if current form instance is valid by running the validations
specified on #validate.
46 47 48 49 50 |
# File 'lib/gourami/validations.rb', line 46 def valid? errors.clear validate !any_errors? end |
#validate ⇒ Object
8 9 10 |
# File 'lib/gourami/validations.rb', line 8 def validate # Override to add custom validations end |
#validate_any(attribute_name, message = nil) ⇒ Object
Validate the value of the given attribute is not empty. Appends :cant_be_empty error.
210 211 212 213 214 215 |
# File 'lib/gourami/validations.rb', line 210 def validate_any(attribute_name, = nil) value = send(attribute_name) if value && value.empty? append_error(attribute_name, || :cant_be_empty) end end |
#validate_color_format(attribute_name, message = nil) ⇒ Object
130 131 132 |
# File 'lib/gourami/validations.rb', line 130 def validate_color_format(attribute_name, = nil) validate_format(attribute_name, HEX_COLOR_FORMAT, ) end |
#validate_email_format(attribute_name, message = nil) ⇒ Object
Validate the email format. If the value does not match the email format,
append the :is_invalid error to the attribute.
122 123 124 |
# File 'lib/gourami/validations.rb', line 122 def validate_email_format(attribute_name, = nil) validate_format(attribute_name, EMAIL_FORMAT, ) end |
#validate_filetype(attribute_name, filetypes, message = nil) ⇒ Object
Validate the type of the file sent if included in the list. If it’s not,
append an :invalid_file error to the attribute.
222 223 224 225 226 227 |
# File 'lib/gourami/validations.rb', line 222 def validate_filetype(attribute_name, filetypes, = nil) value = send(attribute_name) if value && !filetypes.include?(value[:type].to_s.split("/").first) append_error(attribute_name, || :is_invalid) end end |
#validate_format(attribute_name, format, message = nil) ⇒ Object
Validate the format of the attribute value. If the value does not match
the regexp given, append :is_invalid error to the attribute.
139 140 141 142 143 144 |
# File 'lib/gourami/validations.rb', line 139 def validate_format(attribute_name, format, = nil) value = send(attribute_name) if value && !(format =~ value) append_error(attribute_name, || :is_invalid) end end |
#validate_inclusion(attribute_name, list, message = nil) ⇒ Object
Validate the value of the given attribute is included in the list. If
the value is not included in the list, append the :not_listed error to
the attribute.
187 188 189 190 191 192 |
# File 'lib/gourami/validations.rb', line 187 def validate_inclusion(attribute_name, list, = nil) value = send(attribute_name) if value && !list.include?(value) append_error(attribute_name, || :isnt_listed) end end |
#validate_inclusion_of_each(attribute_name, list, message = nil) ⇒ Object
Validate the presence of each object in attribute name within list. If the object
is not included in the list, append the :not_listed error to the attribute.
196 197 198 199 200 201 202 203 204 |
# File 'lib/gourami/validations.rb', line 196 def validate_inclusion_of_each(attribute_name, list, = nil) value = send(attribute_name) value && value.each do |obj| unless list.include?(obj) append_error(attribute_name, || "#{obj} isn't listed") break end end end |
#validate_isrc_format(attribute_name, message = nil) ⇒ Object
126 127 128 |
# File 'lib/gourami/validations.rb', line 126 def validate_isrc_format(attribute_name, = nil) validate_format(attribute_name, ISRC_FORMAT, ) end |
#validate_length(attribute_name, options = {}) ⇒ Object
Validate the length of a String, Array or any other form attribute which
responds to #size. If the value is too short, append the :too_short
error to the attribute. If the value is too long append the :too_long
error to the attribute.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/gourami/validations.rb', line 154 def validate_length(attribute_name, = {}) # TODO: Support :unless_already_invalid => true in more validators. return if .fetch(:unless_already_invalid, false) && attribute_has_errors?(attribute_name) min = .fetch(:min, nil) max = .fetch(:max, nil) value = send(attribute_name) return if [:allow_blank] && value.blank? if value length = value.size did_append_error = false if min && length < min did_append_error = true append_error(attribute_name, .fetch(:min_message, nil) || :is_too_short) end if max && length > max did_append_error = true append_error(attribute_name, .fetch(:max_message, nil) || :is_too_long) end errors[attribute_name] if did_append_error end end |
#validate_presence(attribute_name, message = nil) ⇒ Object
Validate the presence of the attribute value. If the value is nil or
false append the :cant_be_empty error to the attribute.
96 97 98 99 100 101 |
# File 'lib/gourami/validations.rb', line 96 def validate_presence(attribute_name, = nil) value = send(attribute_name) if !value || value.to_s.strip.empty? append_error(attribute_name, || :cant_be_empty) end end |
#validate_range(attribute_name, options = {}) ⇒ Object
TODO: Spec return value in spec/validations_spec.rb Validate the range in which the attribute can be. If the value is less
than the min a :less_than_min error will be appended. If the value is
greater than the max a :greater_than_max error will be appended.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/gourami/validations.rb', line 239 def validate_range(attribute_name, = {}) value = send(attribute_name) return unless value min = .fetch(:min, nil) max = .fetch(:max, nil) append_error(attribute_name, .fetch(:min_message, nil) || :less_than_min) if min && value < min append_error(attribute_name, .fetch(:max_message, nil) || :greater_than_max) if max && value > max end |
#validate_uniqueness(attribute_name, message = nil, &block) ⇒ Object
Validate the uniqueness of the attribute value. The uniqueness is
determined by the block given. If the value is not unique, append the
:is_duplicated error to the attribute.
111 112 113 114 115 116 |
# File 'lib/gourami/validations.rb', line 111 def validate_uniqueness(attribute_name, = nil, &block) value = send(attribute_name) unless block.call(value) append_error(attribute_name, || :is_duplicated) end end |