Module: SoftValidation

Extended by:
ActiveSupport::Concern
Included in:
AssertedDistribution, BiologicalAssociation, CharacterState, CollectingEvent, CollectionObject, CollectionProfile, Container, Descriptor, Document, Documentation, Identifier::Global, Image, Loan, Otu, RangedLotCategory, Serial, Source, TaxonNameClassification, TaxonNameRelationship, TypeMaterial
Defined in:
lib/soft_validation.rb,
lib/soft_validation/soft_validation.rb,
lib/soft_validation/soft_validations.rb,
lib/soft_validation/soft_validation_method.rb

Overview

soft_validate(:a_fourth_example, has_fix: false) # there are no fix methods assigned in :a_fourth_example

  $hungry = true

  def a_soft_validation_method
    soft_validations.add(:base, 'hungry!',                          # :base or a model attribute (column)
      fix: :cook_cheezburgers,
      success_message: 'no longer hungry, cooked a cheezeburger',
      failure_message: 'oh no, cat ate your cheezeburger'
    ) if $hungry
  end

  def cook_cheezburgers
    $hungry = false
  end
end

f = Foo.new

f.soft_validations.validated?             # => false
f.soft_validations.fixes_run?             # => false
f.soft_validations.fixed?                 # => false
f.soft_validations.complete?              # => false

f.soft_validate                           # => true
f.soft_validated?                         # => true
f.soft_fixed?                             # => false
f.soft_valid?                             # => false  # true if there are no SoftValidations produced

f.soft_validations.soft_validations                        # => [soft_validation, soft_validation1 ... ]
f.soft_validations.soft_validations.size                   # => 1
f.soft_validations.soft_validations.first                  # => A SoftValidation instance

# SoftValidation attributes
f.soft_validations.soft_validations.first.attribute          # => :base
f.soft_validations.soft_validations.first.message            # => 'hungry!'
f.soft_validations.soft_validations.first.success_message    # => 'no longer hungry, cooked a cheezeburger'
f.soft_validations.soft_validations.first.failure_message    # => 'oh no, cat ate your cheezeburger'

f.soft_validations.soft_validations.first.fixed?           # => false
f.soft_validations.soft_validations.first.result_message     # => 'fix not yet run'

f.fix_soft_validations                    # => true
f.soft_fixed?                             # => true
f.soft_valid?                             # => false !! There is still a SoftValidation generated, will be true next time it's run

f.soft_validations.fixes_run                               # => true
f.soft_validations.soft_validations.first.fixed?           # => true
f.soft_validations.soft_validations.first.result_message   # => 'no longer hungry, cooked a cheezeburger'
f.soft_validations.on(:base)               # => [soft_validation, ... ]
f.soft_validations.messages                # => ['hungry!']
f.soft_validations.messages_on(:base)      # => ['hungry!']

f.clear_soft_validations

f.soft_validate(:some_other_set)          # only run this set of validations

Defined Under Namespace

Modules: ClassMethods Classes: SoftValidation, SoftValidationError, SoftValidationMethod, SoftValidations

Constant Summary collapse

ANCESTORS_WITH_SOFT_VALIDATIONS =
Hash.new do |h, klass|
  h[klass.name] = (klass.ancestors.select {|a| a.respond_to?(:soft_validates?) && a.soft_validates?} - [klass]) # a < ApplicationRecord && would be faster but requires AR in spec
end

Instance Method Summary collapse

Instance Method Details

#clear_soft_validationsNil

Returns:

  • (Nil)


220
221
222
# File 'lib/soft_validation.rb', line 220

def clear_soft_validations
  @soft_validation_result = nil
end

#fix_soft_validations(scope = :automatic) ⇒ Boolean

Parameters:

  • scope (Symbol) (defaults to: :automatic)

Returns:

  • (Boolean)


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/soft_validation.rb', line 250

def fix_soft_validations(scope = :automatic)
  return false if !soft_validated?
  raise 'invalid scope passed to fix_soft_validations' if ![:automatic, :requested].include?(scope)

  soft_validations.soft_validations.each do |v|
    if v.fix
      if v.fix_trigger == scope
        if self.send(v.fix)
          v.fixed = :fixed
        else
          v.fixed = :fix_error
        end
      else
        v.fixed = :fix_not_triggered
      end
    else
      v.fixed = :no_fix_available
    end
  end
  soft_validations.fixes_run = scope
  true
end

#soft_fixed?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/soft_validation.rb', line 279

def soft_fixed?
  soft_validations.fixes_run?
end

#soft_valid?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/soft_validation.rb', line 284

def soft_valid?
  soft_validations.complete?
end

#soft_validate(set = :all, include_ancestors = true, only_fixable = false) ⇒ Boolean

Returns always true

Parameters:

  • set (Symbol) (defaults to: :all)

    the set of soft validations to run

  • ancestors (Boolean)

    whether to also validate ancestors soft validations

Returns:

  • (Boolean)

    always true



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/soft_validation.rb', line 227

def soft_validate(set = :all, include_ancestors = true, only_fixable = false)
  clear_soft_validations
  soft_validations
  sets = case set.class.name
         when 'Array'
           set
         when 'Symbol'
           [set]
         when 'String'
           [set.to_sym]
         end

  sets.each do |s|
    self.class.soft_validators(set: s, include_ancestors: include_ancestors, fixable_only: only_fixable).each do |m|
      self.send(m)
    end
  end
  soft_validations.validated = true
  true
end

#soft_validated?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/soft_validation.rb', line 274

def soft_validated?
  soft_validations.validated?
end

#soft_validationsSoftValidations

Returns:



215
216
217
# File 'lib/soft_validation.rb', line 215

def soft_validations
  @soft_validation_result ||= SoftValidations.new(self)
end