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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/rails/crud/tools/cli.rb', line 16
def generate_crud_file
load_application unless @application_loaded
routes_output = `bundle exec rails routes --expanded`
config = Rails::Crud::Tools::CrudConfig.instance.config
font_name = config.crud_file.font_name
routes_lines = routes_output.split("\n").reject(&:empty?)
routes_data = []
current_route = {}
routes_lines.each do |line|
if line.start_with?("--[ Route")
routes_data << current_route unless current_route.empty?
current_route = {}
else
key, value = line.split("|").map(&:strip)
current_route[key] = value
end
end
routes_data << current_route unless current_route.empty?
table_names = ActiveRecord::Base.connection.tables.sort
workbook = RubyXL::Workbook.new
sheet = workbook[0]
sheet.sheet_name = config.crud_file.sheet_name
= %w[Prefix Verb URI Controller#Action crud_count] + table_names
.each_with_index do |, index|
cell = sheet.add_cell(0, index, )
cell.change_fill("00FFCC")
cell.change_font_name(font_name)
cell.change_font_bold(true)
apply_borders(cell)
end
start_col = "F"
end_col = ("A".."ZZ").to_a[table_names.length + 4]
routes_data.each_with_index do |route, row_index|
.each_with_index do |, col_index|
cell = sheet.add_cell(row_index + 1, col_index, route[])
cell.change_font_name(font_name)
apply_borders(cell)
end
crud_count_formula = "=SUMPRODUCT(LEN(#{start_col}#{row_index + 2}:#{end_col}#{row_index + 2}))"
crud_count_cell = sheet.add_cell(row_index + 1, 4, "", crud_count_formula)
crud_count_cell.change_font_name(font_name)
apply_borders(crud_count_cell)
end
job_files = Dir.glob("app/jobs/**/*.rb")
job_classes = job_files.map do |file|
File.basename(file, ".rb").camelize
end.reject { |job| job == "ApplicationJob" }.sort
job_classes.each_with_index do |job, index|
.each_with_index do |, col_index|
if == "Controller#Action"
cell = sheet.add_cell(routes_data.length + 1 + index, col_index, job)
cell.change_font_name(font_name)
else
cell = sheet.add_cell(routes_data.length + 1 + index, col_index, nil)
end
apply_borders(cell)
end
crud_count_formula = "=SUMPRODUCT(LEN(#{start_col}#{routes_data.length + 2 + index}:#{end_col}#{routes_data.length + 2 + index}))"
crud_count_cell = sheet.add_cell(routes_data.length + 1 + index, 4, "", crud_count_formula)
crud_count_cell.change_font_name(font_name)
apply_borders(crud_count_cell)
end
(0...length - 1).each do |col_index|
sheet[0][col_index].change_fill(config.crud_file.)
end
.each_with_index do |, col_index|
max_length = .length
(1..routes_data.length).each do |row_index|
cell_value = sheet[row_index][col_index].value.to_s
max_length = [max_length, cell_value.length].max
end
sheet.change_column_width(col_index, max_length + 2)
end
crud_file = config.crud_file_path
base_dir = File.dirname(crud_file)
FileUtils.mkdir_p(base_dir) unless Dir.exist?(base_dir)
workbook.write(crud_file)
puts "Output: #{crud_file}"
end
|