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)


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/workflow/adapters/active_record_validations.rb', line 94

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



106
107
108
109
# File 'lib/workflow/adapters/active_record_validations.rb', line 106

def write_attribute(attr_name, value)
  @changed_since_validation = true unless attr_name.to_sym == :workflow_state
  super
end