Module: ObjectAttorney::Validations

Defined in:
lib/object_attorney/validations.rb

Instance Method Summary collapse

Instance Method Details

#clear_imposed_errorsObject



19
20
21
# File 'lib/object_attorney/validations.rb', line 19

def clear_imposed_errors
  @imposed_errors = {}
end

#imposed_errorsObject



31
32
33
# File 'lib/object_attorney/validations.rb', line 31

def imposed_errors
  @imposed_errors ||= {}
end

#override_validations?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/object_attorney/validations.rb', line 15

def override_validations?
  Helpers.marked_for_destruction?(self)
end

#populate_imposed_errorsObject



23
24
25
26
27
28
29
# File 'lib/object_attorney/validations.rb', line 23

def populate_imposed_errors
  if respond_to?(:represented_object)
    represented_object.errors.each { |key, value| imposed_errors[key] = value } if Helpers.has_errors_method?(represented_object)
  else
    errors.each { |key, value| imposed_errors[key] = value } if Helpers.has_errors_method?(self)
  end
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
# File 'lib/object_attorney/validations.rb', line 4

def valid?(context = nil)
  return true if override_validations? || !Helpers.has_errors_method?(self)

  context ||= (new_record? ? :create : :update)
  output = super(context)

  load_errors_from imposed_errors

  errors.empty? && output
end