Module: Workflow::Adapters::ActiveRecordValidations::RevalidateOnlyAfterAttributesChange

Defined in:
lib/workflow/adapters/active_record_validations.rb

Overview

Override for ActiveRecord::Validations#valid? Since we are validating inside of a transition, We need to be able to maintain the errors list for the object through future valid? calls or the list will be cleared next time valid? is called.

Once any attributes have changed on the object, the following call to #valid? will cause revalidation.

Note that a change on an association will not trigger a reset, meaning that one should call errors.clear before #valid? will trigger validations to run anew.

Instance Method Summary collapse

Instance Method Details

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/workflow/adapters/active_record_validations.rb', line 98

def valid?(context = nil)
  if errors.any? && !@changed_since_validation
    false
  else
    begin
      return super
    ensure
      @changed_since_validation = false
    end
  end
end

#write_attribute(attr_name, value) ⇒ Object



110
111
112
113
# File 'lib/workflow/adapters/active_record_validations.rb', line 110

def write_attribute(attr_name, value)
  @changed_since_validation = true
  super
end