Sha256: dd513bd354d9035a80792a71f3455636a8ae165b0f9336109f629f1fab78581e

Contents?: true

Size: 1.9 KB

Versions: 10

Compression:

Stored size: 1.9 KB

Contents

module ActiveRecord::Validate
  def self.all(quiet=nil, &block)
    klasses(quiet).inject(0) do |count, klass|
      count += invalid_models(klass, quiet).length
    end
  end

  def self.purge
    klasses(true).each do |klass|
      invalids = invalid_models(klass, true)
      klass.delete_all [ "id IN (?)", invalids.map(&:id) ] unless invalids.empty?
    end
  end
  
  
  def self.set_klass_for(opts)
    @klass_for_table ||= {}
    @klass_for_table.update opts.with_indifferent_access
  end
  
  private

  def self.klass_for_table(table)
    @klass_for_table ||= {}
    if klass = @klass_for_table[table]
      klass.constantize
    else
      table.singularize.camelize.constantize?
    end
  end
  
  def self.klasses(quiet)
    ActiveRecord::Base.connection.tables.map do |table|
      klass = klass_for_table table
      if !klass
        STDERR.puts "* #{table}: table w/o class" unless quiet
      elsif !klass.respond_to?(:table_name) || klass.table_name != table
        STDERR.puts "Tablename mismatch #{table.inspect} vs #{klass.table_name.inspect}" unless quiet
      else
        klass
      end
    end.compact
  end
  
  def self.invalid_models(klass, quiet)
    validate_klass(klass) do |*args|
      msg, force = *args
      STDERR.puts(msg) if force || !quiet
    end
  end
  
  def self.validate_klass(klass)
    yield "#{klass}: #{sum=klass.count} models"

    invalid = []

    klass.find_each do |model|
      next if model.valid?
      invalid << model
      
      yield "\tInvalid model #{model.class}##{model.id}: #{model.errors.full_messages.join(", ")}"
    end

    yield "\r#{klass}: checked #{klass.count} models, #{invalid.length} invalid", :force

    return invalid if invalid.empty?
    
    invalid.group_by(&:class).to_a.
      sort_by do |klass, ms| klass.name end.
      each do |klass, ms| 
        yield "\t** #{klass}: Invalid models: #{ms.collect(&:id).inspect}"
      end
    
    invalid
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/active_record/validate.rb
vex-0.4.4 lib/vex/active_record/validate.rb
vex-0.4.2 lib/vex/active_record/validate.rb
vex-0.3.3 lib/vex/active_record/validate.rb
vex-0.2.9 lib/vex/active_record/validate.rb
vex-0.2.8 lib/vex/active_record/validate.rb
vex-0.2.7 lib/vex/active_record/validate.rb
vex-0.2.6 lib/vex/active_record/validate.rb
vex-0.2.5 lib/vex/active_record/validate.rb
vex-0.2.2 lib/vex/active_record/validate.rb