Class: Resto::Validate::Presence

Inherits:
Object
  • Object
show all
Includes:
Resto::Validate
Defined in:
lib/resto/validate/presence.rb

Instance Method Summary collapse

Methods included from Resto::Validate

#if, #initialize, #message, #unless, #validate?

Instance Method Details

#attribute_value(resource, attribute_method) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/resto/validate/presence.rb', line 8

def attribute_value(resource, attribute_method)

  error_key = (attribute_method.to_s + "_presence").to_sym
  value_before_cast = resource.send("#{attribute_method}_without_cast")

  error =
    if (validate?(resource) && value_before_cast.to_s.strip.empty?)
      ":#{attribute_method} #{@message || "can’t be blank"}"
    else
      nil
    end

  resource.add_error(error_key, error)
end