Sha256: 704ad3ed20df35b1dcb08144c43f25b7aa5cd5430dd5d8f9235210ead0b4f209

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

module TestLogging
  def log(msg)
    Rails.logger.info(msg)
  end
  
  def log_array(obj, *columns)
    lengths = columns.map{|m| m.to_s.length }

    obj.each do |r|
      columns.each_with_index do |m, i|
        v = r.send(m)
        if v.to_s.length > lengths[i]
          lengths[i] = v.to_s.length
        end
      end
    end

    str = "  "
    columns.each_with_index do |m, i|
      str << "%#{lengths[i]}s" % m
      str << "  "
    end
    str << "\n  "

    columns.each_with_index do |m, i|
      str << ("-"*lengths[i])
      str << "  "
    end
    str << "\n  "

    obj.each do |r|
      columns.each_with_index do |m, i|
        str << "%#{lengths[i]}s" % r.send(m)
        str << "  "
      end
      str << "\n  "      
    end

    log str    
  end  
  
  def log_table(cls, options={})
    if options[:include_columns]
      columns = options[:include_columns]
    elsif options[:exclude_columns]
      columns = cls.column_names - options[:exclude_columns].map(&:to_s)
    else
      columns = cls.column_names      
    end
    log_array (cls.uses_soft_delete? ? cls.find_with_deleted(:all) : cls.all), *columns
  end
  
  def log_table_with(cls, *columns)
    log_table(cls, :include_columns => columns)
  end
  
  def log_table_without(cls, *columns)
    log_table(cls, :exclude_columns => columns)
  end

  def log_table_without_stamps(cls, *columns)
    log_table(cls, :exclude_columns => %w[created_at updated_at created_by_id updated_by_id] + columns)
  end    
  
  extend self
  
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bcms_fckeditor-1.2.0 test/test_logging.rb
bcms_fckeditor-1.1.0 test/test_logging.rb
bcms_fckeditor-1.0.4 test/test_logging.rb
bcms_fckeditor-1.0.3 test/test_logging.rb
bcms_fckeditor-1.0.2 test/test_logging.rb
bcms_fckeditor-1.0.1 test/test_logging.rb
bcms_fckeditor-1.0.0 test/test_logging.rb