Skip to content

Commit 58894ab

Browse files
committed
add sti and polymorphysm
1 parent c36fc65 commit 58894ab

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

lib/rails_stats/console_formatter.rb

Lines changed: 10 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_sti_stats
22+
print_polymorphic_stats
2123
print_schema_stats
2224
end
2325

@@ -55,6 +57,14 @@ def print_code_test_stats
5557
puts ""
5658
end
5759

60+
def print_sti_stats
61+
puts " STI models count: #{calculator.sti} STI classes"
62+
end
63+
64+
def print_polymorphic_stats
65+
puts " Polymorphic models count: #{calculator.polymorphic} polymorphic associations"
66+
end
67+
5868
def print_schema_stats
5969
if File.exist?(calculator.schema_path)
6070
puts " Schema Stats: #{calculator.schema} `create_table` calls in schema.rb"

lib/rails_stats/json_formatter.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def result
1818
@result << stat_hash("Total", @grand_total).merge(code_test_hash) if @grand_total
1919

2020
@result << { "schema_stats" => schema_info }
21+
@result << { "sti_stats" => print_sti_stat }
22+
@result << { "polymorphic_stats" => print_polymorphic_stats }
2123

2224
@result
2325
end
@@ -53,6 +55,22 @@ def stat_hash(name, statistics)
5355
}
5456
end
5557

58+
def print_sti_stat
59+
if calculator.sti
60+
{
61+
"sti_models_count" => calculator.sti,
62+
}
63+
end
64+
end
65+
66+
def print_polymorphic_stats
67+
if calculator.polymorphic
68+
{
69+
"polymorphic_models_count" => calculator.polymorphic,
70+
}
71+
end
72+
end
73+
5674
def schema_info
5775
if File.exist?(calculator.schema_path)
5876
{

lib/rails_stats/stats_calculator.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ def initialize(root_directory)
1717
@code_loc = calculate_code
1818
@test_loc = calculate_tests
1919
@schema = calculate_create_table_calls
20+
@sti = calculate_sti
21+
@polymorphic = calculate_polymorphic
2022
@files_total, @code_total, @tests_total, @grand_total = calculate_totals
2123
end
2224

23-
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path
25+
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path, :sti, :polymorphic
2426

2527
private
2628

@@ -49,7 +51,6 @@ def calculate_projects
4951
out
5052
end
5153

52-
5354
def app_projects
5455
@app_projects ||= calculate_app_projects
5556
end
@@ -82,7 +83,6 @@ def calculate_test_projects
8283
end
8384
end
8485

85-
8686
def calculate_root_projects
8787
[RootStatistics.new(@root_directory)]
8888
end
@@ -154,5 +154,29 @@ def count_create_table_calls(file_path, keyword)
154154
end
155155
create_table_count
156156
end
157+
158+
def calculate_sti
159+
@sti = 0
160+
Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file|
161+
File.foreach(file) do |line|
162+
if line =~ /class\s+(\w+)\s*<\s*(\w+)/ && !(line =~ /class\s+(\w+)\s*<\s*(ActiveRecord::Base|ApplicationRecord)/)
163+
@sti += 1
164+
end
165+
end
166+
end
167+
@sti
168+
end
169+
170+
def calculate_polymorphic
171+
polymorphic_count = 0
172+
Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file|
173+
File.foreach(file) do |line|
174+
if line =~ /belongs_to\s+:\w+,\s+polymorphic:\s+true/
175+
polymorphic_count += 1
176+
end
177+
end
178+
end
179+
polymorphic_count
180+
end
157181
end
158182
end

0 commit comments

Comments
 (0)