Skip to content

Commit e533d14

Browse files
authored
Merge pull request #37 from fastruby/print_schema_stats
Output number of table created from schema.rb or structure.sql, add polymorphic models count
2 parents f9ac0f9 + 2226bdf commit e533d14

File tree

11 files changed

+258
-153
lines changed

11 files changed

+258
-153
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [BUGFIX: Fix JSON output missing Code and Tests total count](https://github.com/fastruby/rails_stats/pull/40)
44
* Update README examples
5+
* [FEATURE: Output number of tables created from schema.rb or structure.sql, add polymorphic models count](https://github.com/fastruby/rails_stats/pull/37)
56

67
# v2.0.1 ([commits](https://github.com/fastruby/rails_stats/compare/v2.0.0...v2.0.1))
78

lib/rails_stats/console_formatter.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def to_s
1818
end
1919

2020
print_code_test_stats
21+
print_polymorphic_stats
22+
print_schema_stats
2123
end
2224

2325
private
@@ -53,5 +55,19 @@ def print_code_test_stats
5355
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}"
5456
puts ""
5557
end
58+
59+
def print_polymorphic_stats
60+
puts " Polymorphic models count: #{calculator.polymorphic} polymorphic associations"
61+
end
62+
63+
def print_schema_stats
64+
if File.exist?(calculator.schema_path)
65+
puts " Schema Stats: #{calculator.schema} `create_table` calls in schema.rb"
66+
elsif File.exist?(calculator.structure_path)
67+
puts " Schema Stats: #{calculator.schema} `CREATE TABLE` calls in structure.sql"
68+
else
69+
puts " Schema Stats: No schema.rb or structure.sql file found"
70+
end
71+
end
5672
end
5773
end

lib/rails_stats/json_formatter.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def result
1717
@result << stat_hash("Tests", @tests_total).merge(code_test_hash) if @tests_total
1818
@result << stat_hash("Total", @grand_total).merge(code_test_hash) if @grand_total
1919

20+
@result << { "schema_stats" => schema_info }
21+
@result << { "polymorphic_stats" => print_polymorphic_stats }
22+
2023
@result
2124
end
2225

@@ -50,5 +53,29 @@ def stat_hash(name, statistics)
5053
"loc_over_m" => loc_over_m.to_s
5154
}
5255
end
56+
57+
def print_polymorphic_stats
58+
if calculator.polymorphic
59+
{
60+
"polymorphic_models_count" => calculator.polymorphic,
61+
}
62+
end
63+
end
64+
65+
def schema_info
66+
if File.exist?(calculator.schema_path)
67+
{
68+
"schema_path" => calculator.schema_path,
69+
"create_table calls count" => calculator.schema,
70+
}
71+
elsif File.exist?(calculator.structure_path)
72+
{
73+
"structure_path" => calculator.structure_path,
74+
"create_table calls count" => calculator.schema
75+
}
76+
else
77+
{ "schema_stats" => "No schema.rb or structure.sql file found" }
78+
end
79+
end
5380
end
54-
end
81+
end

lib/rails_stats/stats_calculator.rb

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ class StatsCalculator
99

1010
def initialize(root_directory)
1111
@root_directory = root_directory
12+
@schema_path = File.join(@root_directory, "db", "schema.rb")
13+
@structure_path = File.join(@root_directory, "db", "structure.sql")
1214
@key_concepts = calculate_key_concepts
1315
@projects = calculate_projects
1416
@statistics = calculate_statistics
1517
@code_loc = calculate_code
1618
@test_loc = calculate_tests
19+
@schema = calculate_create_table_calls
20+
@polymorphic = calculate_polymorphic
1721
@files_total, @code_total, @tests_total, @grand_total = calculate_totals
1822
end
1923

20-
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total
24+
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path, :polymorphic
2125

2226
private
2327

@@ -46,7 +50,6 @@ def calculate_projects
4650
out
4751
end
4852

49-
5053
def app_projects
5154
@app_projects ||= calculate_app_projects
5255
end
@@ -79,7 +82,6 @@ def calculate_test_projects
7982
end
8083
end
8184

82-
8385
def calculate_root_projects
8486
[RootStatistics.new(@root_directory)]
8587
end
@@ -133,5 +135,35 @@ def calculate_tests
133135
@statistics.each { |k, v| @test_loc += v.code_lines if v.test }
134136
@test_loc
135137
end
138+
139+
def calculate_create_table_calls
140+
if @schema_path && File.exist?(@schema_path)
141+
count_create_table_calls(schema_path, "create_table")
142+
elsif @structure_path && File.exist?(@structure_path)
143+
count_create_table_calls(structure_path, "CREATE TABLE")
144+
else
145+
0
146+
end
147+
end
148+
149+
def count_create_table_calls(file_path, keyword)
150+
create_table_count = 0
151+
File.foreach(file_path) do |line|
152+
create_table_count += 1 if line.strip.start_with?(keyword)
153+
end
154+
create_table_count
155+
end
156+
157+
def calculate_polymorphic
158+
polymorphic_count = 0
159+
Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file|
160+
File.foreach(file) do |line|
161+
if line =~ /belongs_to\s+:\w+,\s+polymorphic:\s+true/
162+
polymorphic_count += 1
163+
end
164+
end
165+
end
166+
polymorphic_count
167+
end
136168
end
137169
end

test/dummy/app/models/comment.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Comments < ApplicationRecord
2+
belongs_to :commentable, polymorphic: true
3+
end

test/dummy/app/models/pet.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Pets < User
2+
end

test/dummy/app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Users < ApplicationRecord
2+
end

test/dummy/db/schema.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
ActiveRecord::Schema[7.0].define(version: 2023_04_25_154701) do
3+
create_table "users", force: :cascade do |t|
4+
t.string "email"
5+
end
6+
create_table "comments", force: :cascade do |t|
7+
t.bigint :commentable_id
8+
t.string :commentable_type
9+
end
10+
end

test/fixtures/console-output.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121
| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |
2222
+----------------------+---------+---------+---------+---------+---------+-----+-------+
2323
| Channels | 2 | 8 | 8 | 2 | 0 | 0 | 0 |
24-
| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 |
24+
| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 |
2525
| Controllers | 1 | 7 | 6 | 1 | 1 | 1 | 4 |
2626
| Helpers | 1 | 3 | 3 | 0 | 0 | 0 | 0 |
2727
| Javascripts | 3 | 27 | 7 | 0 | 0 | 0 | 0 |
2828
| Jobs | 1 | 7 | 2 | 1 | 0 | 0 | 0 |
2929
| Libraries | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3030
| Mailers | 1 | 4 | 4 | 1 | 0 | 0 | 0 |
3131
| Model Tests | 2 | 5 | 4 | 2 | 0 | 0 | 0 |
32-
| Models | 1 | 3 | 3 | 1 | 0 | 0 | 0 |
32+
| Models | 4 | 10 | 10 | 4 | 0 | 0 | 0 |
3333
| Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3434
| Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3535
+----------------------+---------+---------+---------+---------+---------+-----+-------+
36-
| Code | 30 | 477 | 145 | 7 | 1 | 0 | 143 |
36+
| Code | 33 | 484 | 152 | 10 | 1 | 0 | 150 |
3737
| Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 |
38-
| Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 |
38+
| Total | 37 | 491 | 158 | 12 | 1 | 0 | 156 |
3939
+----------------------+---------+---------+---------+---------+---------+-----+-------+
40-
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34
40+
Code LOC: 152 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 37
41+
42+
Polymorphic models count: 1 polymorphic associations
43+
Schema Stats: 2 `create_table` calls in schema.rb

test/lib/rails_stats/code_statistics_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
RailsStats::CodeStatistics.new(root_directory).to_s
1515
end
1616

17-
assert_equal table, out
17+
assert_equal(
18+
table.lines.map(&:rstrip).join,
19+
out.lines.map(&:rstrip).join
20+
)
1821
end
1922
end
2023
end

0 commit comments

Comments
 (0)