Module: SoftValidation

Extended by:
ActiveSupport::Concern
Included in:
CollectingEvent, CollectionProfile, Container, Image, Serial, Source::Bibtex, TaxonName, TaxonNameClassification, TaxonNameRelationship, TypeMaterial
Defined in:
lib/soft_validation.rb

Overview

Vaguely inspired by concepts from by svn://rubyforge.org/var/svn/softvalidations, but not as elegant.

Soft validations are a means to tie warnings or suggestions to instances of data. Soft validations do not prevent an instance from being saved. They are not intended to be bound to AR callbacks, but this may be possible. They may be used to alert the user to data issues that need to be addressed, or alert the programmer who is batch parsing data as to the quality of the incoming data, etc..

For example, soft validations could be shown on #show controller methods.

Usage:

class Foo < ActiveRecord::Base
  include SoftValidation
  soft_validate(:a_soft_validation_method )

  # Validations can be assigned to a set (only one), and validations in a set
  # can be called individually.
  soft_validate(:other_soft_validation_method, set: :some_set)
  soft_validate(:yet_another_method, set: :some_other_set )

  $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, SoftValidations

Instance Method Summary collapse

Instance Method Details

#clear_soft_validationsObject



259
260
261
# File 'lib/soft_validation.rb', line 259

def clear_soft_validations 
  @soft_validation_result = nil 
end

#fix_soft_validations(scope = :automatic) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/soft_validation.rb', line 285

def fix_soft_validations(scope = :automatic)
  return false if !soft_validated?
  raise 'invalid scope passed to fix_soft_validations' if ![:all, :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)


311
312
313
# File 'lib/soft_validation.rb', line 311

def soft_fixed?
  soft_validations.fixes_run?
end

#soft_valid?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/soft_validation.rb', line 315

def soft_valid?
  soft_validations.complete?
end

#soft_validate(set = :all) ⇒ Object

Parameters:

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

    the set of soft validations to run



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/soft_validation.rb', line 264

def soft_validate(set = :all)
  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 |set| 
    self.class.soft_validation_methods[set].each do |s|
      self.send(s)
    end
  end
  soft_validations.validated = true
  true
end

#soft_validated?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/soft_validation.rb', line 307

def soft_validated?
  soft_validations.validated?
end

#soft_validationsObject



255
256
257
# File 'lib/soft_validation.rb', line 255

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