forked from KingOfBrian/browsercms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_logging.rb
67 lines (55 loc) · 1.48 KB
/
test_logging.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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