From d4bb30997867c788c172816a07f3369542f34253 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Mon, 12 May 2025 12:40:43 -0300 Subject: [PATCH 01/13] Adjust stats, formatter and calculator to print schema/structure file information --- lib/rails_stats/console_formatter.rb | 11 +++++++++++ lib/rails_stats/json_formatter.rb | 13 +++++++++++++ lib/rails_stats/stats_calculator.rb | 23 ++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/rails_stats/console_formatter.rb b/lib/rails_stats/console_formatter.rb index 990b726..0a3e5fc 100644 --- a/lib/rails_stats/console_formatter.rb +++ b/lib/rails_stats/console_formatter.rb @@ -18,6 +18,7 @@ def to_s end print_code_test_stats + print_schema_stats end private @@ -53,5 +54,15 @@ def print_code_test_stats puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}" puts "" end + + def print_schema_stats + if File.exist?(calculator.schema_path) + puts " Schema Stats: #{calculator.schema} `create_table` calls in schema.rb" + elsif File.exist?(calculator.structure_path) + puts " Schema Stats: #{calculator.schema} `CREATE TABLE` calls in structure.sql" + else + puts " Schema Stats: No schema.rb or structure.sql file found" + end + end end end \ No newline at end of file diff --git a/lib/rails_stats/json_formatter.rb b/lib/rails_stats/json_formatter.rb index a0ad8b0..8c1a3e8 100644 --- a/lib/rails_stats/json_formatter.rb +++ b/lib/rails_stats/json_formatter.rb @@ -17,6 +17,8 @@ def result @result << stat_hash("Tests", @tests_total).merge(code_test_hash) if @tests_total @result << stat_hash("Total", @grand_total).merge(code_test_hash) if @grand_total + @result << { "schema_stats" => schema_info } + @result end @@ -50,5 +52,16 @@ def stat_hash(name, statistics) "loc_over_m" => loc_over_m.to_s } end + + def schema_info + if File.exist?(calculator.schema_path) + { + "schema_stats" => calculator.schema, + "create_table_calls" => calculator.create_table_calls + } + else + { "schema_stats" => "No schema.rb file found" } + end + end end end \ No newline at end of file diff --git a/lib/rails_stats/stats_calculator.rb b/lib/rails_stats/stats_calculator.rb index 773ec14..054798d 100644 --- a/lib/rails_stats/stats_calculator.rb +++ b/lib/rails_stats/stats_calculator.rb @@ -9,15 +9,18 @@ class StatsCalculator def initialize(root_directory) @root_directory = root_directory + @schema_path = File.join(@root_directory, "db", "schema.rb") + @structure_path = File.join(@root_directory, "db", "structure.sql") @key_concepts = calculate_key_concepts @projects = calculate_projects @statistics = calculate_statistics @code_loc = calculate_code @test_loc = calculate_tests + @schema = calculate_create_table_calls @files_total, @code_total, @tests_total, @grand_total = calculate_totals end - attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total + attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path private @@ -133,5 +136,23 @@ def calculate_tests @statistics.each { |k, v| @test_loc += v.code_lines if v.test } @test_loc end + + def calculate_create_table_calls + if @schema_path && File.exist?(@schema_path) + count_create_table_calls(schema_path, "create_table") + elsif @structure_path && File.exist?(@structure_path) + count_create_table_calls(structure_path, "CREATE TABLE") + else + 0 + end + end + + def count_create_table_calls(file_path, keyword) + create_table_count = 0 + File.foreach(file_path) do |line| + create_table_count += 1 if line.strip.start_with?(keyword) + end + create_table_count + end end end \ No newline at end of file From c36fc658db7a216f4e546c1d1ed0d5330b5c5771 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 13 May 2025 11:16:10 -0300 Subject: [PATCH 02/13] fix json schema path information --- lib/rails_stats/json_formatter.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/rails_stats/json_formatter.rb b/lib/rails_stats/json_formatter.rb index 8c1a3e8..8cfdab5 100644 --- a/lib/rails_stats/json_formatter.rb +++ b/lib/rails_stats/json_formatter.rb @@ -56,11 +56,16 @@ def stat_hash(name, statistics) def schema_info if File.exist?(calculator.schema_path) { - "schema_stats" => calculator.schema, - "create_table_calls" => calculator.create_table_calls + "schema_path" => calculator.schema_path, + "create_table calls count" => calculator.schema, + } + elsif File.exist?(calculator.structure_path) + { + "structure_path" => calculator.structure_path, + "create_table calls count" => calculator.schema } else - { "schema_stats" => "No schema.rb file found" } + { "schema_stats" => "No schema.rb or structure.sql file found" } end end end From 58894abba356f51ba4dd9a4283110e164dedba9f Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 13 May 2025 11:39:29 -0300 Subject: [PATCH 03/13] add sti and polymorphysm --- lib/rails_stats/console_formatter.rb | 10 ++++++++++ lib/rails_stats/json_formatter.rb | 18 +++++++++++++++++ lib/rails_stats/stats_calculator.rb | 30 +++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/rails_stats/console_formatter.rb b/lib/rails_stats/console_formatter.rb index 0a3e5fc..bd4b170 100644 --- a/lib/rails_stats/console_formatter.rb +++ b/lib/rails_stats/console_formatter.rb @@ -18,6 +18,8 @@ def to_s end print_code_test_stats + print_sti_stats + print_polymorphic_stats print_schema_stats end @@ -55,6 +57,14 @@ def print_code_test_stats puts "" end + def print_sti_stats + puts " STI models count: #{calculator.sti} STI classes" + end + + def print_polymorphic_stats + puts " Polymorphic models count: #{calculator.polymorphic} polymorphic associations" + end + def print_schema_stats if File.exist?(calculator.schema_path) puts " Schema Stats: #{calculator.schema} `create_table` calls in schema.rb" diff --git a/lib/rails_stats/json_formatter.rb b/lib/rails_stats/json_formatter.rb index 8cfdab5..028112b 100644 --- a/lib/rails_stats/json_formatter.rb +++ b/lib/rails_stats/json_formatter.rb @@ -18,6 +18,8 @@ def result @result << stat_hash("Total", @grand_total).merge(code_test_hash) if @grand_total @result << { "schema_stats" => schema_info } + @result << { "sti_stats" => print_sti_stat } + @result << { "polymorphic_stats" => print_polymorphic_stats } @result end @@ -53,6 +55,22 @@ def stat_hash(name, statistics) } end + def print_sti_stat + if calculator.sti + { + "sti_models_count" => calculator.sti, + } + end + end + + def print_polymorphic_stats + if calculator.polymorphic + { + "polymorphic_models_count" => calculator.polymorphic, + } + end + end + def schema_info if File.exist?(calculator.schema_path) { diff --git a/lib/rails_stats/stats_calculator.rb b/lib/rails_stats/stats_calculator.rb index 054798d..80be9fc 100644 --- a/lib/rails_stats/stats_calculator.rb +++ b/lib/rails_stats/stats_calculator.rb @@ -17,10 +17,12 @@ def initialize(root_directory) @code_loc = calculate_code @test_loc = calculate_tests @schema = calculate_create_table_calls + @sti = calculate_sti + @polymorphic = calculate_polymorphic @files_total, @code_total, @tests_total, @grand_total = calculate_totals end - attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path + attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path, :sti, :polymorphic private @@ -49,7 +51,6 @@ def calculate_projects out end - def app_projects @app_projects ||= calculate_app_projects end @@ -82,7 +83,6 @@ def calculate_test_projects end end - def calculate_root_projects [RootStatistics.new(@root_directory)] end @@ -154,5 +154,29 @@ def count_create_table_calls(file_path, keyword) end create_table_count end + + def calculate_sti + @sti = 0 + Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file| + File.foreach(file) do |line| + if line =~ /class\s+(\w+)\s*<\s*(\w+)/ && !(line =~ /class\s+(\w+)\s*<\s*(ActiveRecord::Base|ApplicationRecord)/) + @sti += 1 + end + end + end + @sti + end + + def calculate_polymorphic + polymorphic_count = 0 + Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file| + File.foreach(file) do |line| + if line =~ /belongs_to\s+:\w+,\s+polymorphic:\s+true/ + polymorphic_count += 1 + end + end + end + polymorphic_count + end end end \ No newline at end of file From 7d76a7a0652adb87a1c7a71f6e4b055acab17512 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 13 May 2025 13:30:02 -0300 Subject: [PATCH 04/13] adjust fixture of dummy app --- test/fixtures/console-output.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt index 5069d28..90f7b12 100644 --- a/test/fixtures/console-output.txt +++ b/test/fixtures/console-output.txt @@ -37,4 +37,8 @@ | Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 | | Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 | +----------------------+---------+---------+---------+---------+---------+-----+-------+ - Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34 \ No newline at end of file + Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34 + + STI models count: 0 STI classes + Polymorphic models count: 0 polymorphic associations + Schema Stats: No schema.rb or structure.sql file found \ No newline at end of file From 92ca317c26480335432ca1db0f341bac7bab4f49 Mon Sep 17 00:00:00 2001 From: Julio Lucero Date: Wed, 14 May 2025 10:03:45 -0300 Subject: [PATCH 05/13] Updates test and dummy app with models and schema.rb --- lib/rails_stats/json_formatter.rb | 2 +- test/dummy/app/models/comment.rb | 3 +++ test/dummy/app/models/pet.rb | 2 ++ test/dummy/app/models/user.rb | 2 ++ test/dummy/db/schema.rb | 10 +++++++++ test/fixtures/console-output.txt | 24 ++++++++++----------- test/lib/rails_stats/json_formatter_test.rb | 2 +- 7 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 test/dummy/app/models/comment.rb create mode 100644 test/dummy/app/models/pet.rb create mode 100644 test/dummy/app/models/user.rb create mode 100644 test/dummy/db/schema.rb diff --git a/lib/rails_stats/json_formatter.rb b/lib/rails_stats/json_formatter.rb index 028112b..7c9ba77 100644 --- a/lib/rails_stats/json_formatter.rb +++ b/lib/rails_stats/json_formatter.rb @@ -87,4 +87,4 @@ def schema_info end end end -end \ No newline at end of file +end diff --git a/test/dummy/app/models/comment.rb b/test/dummy/app/models/comment.rb new file mode 100644 index 0000000..46abe97 --- /dev/null +++ b/test/dummy/app/models/comment.rb @@ -0,0 +1,3 @@ +class Comments < ApplicationRecord + belongs_to :commentable, polymorphic: true +end diff --git a/test/dummy/app/models/pet.rb b/test/dummy/app/models/pet.rb new file mode 100644 index 0000000..667f211 --- /dev/null +++ b/test/dummy/app/models/pet.rb @@ -0,0 +1,2 @@ +class Pets < User +end diff --git a/test/dummy/app/models/user.rb b/test/dummy/app/models/user.rb new file mode 100644 index 0000000..6537292 --- /dev/null +++ b/test/dummy/app/models/user.rb @@ -0,0 +1,2 @@ +class Users < ApplicationRecord +end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb new file mode 100644 index 0000000..e61cdad --- /dev/null +++ b/test/dummy/db/schema.rb @@ -0,0 +1,10 @@ + +ActiveRecord::Schema[7.0].define(version: 2023_04_25_154701) do + create_table "users", force: :cascade do |t| + t.string "email" + end + create_table "comments", force: :cascade do |t| + t.bigint :commentable_id + t.string :commentable_type + end +end diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt index 90f7b12..140efbf 100644 --- a/test/fixtures/console-output.txt +++ b/test/fixtures/console-output.txt @@ -4,12 +4,12 @@ | simplecov-console | 8 | 3 | | codecov | 4 | 1 | | rails_stats | 4 | 2 | -| simplecov | 3 | 3 | -| minitest-around | 1 | 1 | -| bundler | 0 | 0 | +| listen | 3 | 2 | +| bootsnap | 1 | 1 | +| puma | 1 | 1 | | byebug | 0 | 0 | -| minitest | 0 | 0 | -| minitest-spec-context | 0 | 0 | +| spring | 0 | 0 | +| tzinfo-data | 0 | 0 | +-----------------------|------------|----------------+ Declared Gems 9 @@ -29,16 +29,16 @@ | Libraries | 1 | 1 | 1 | 0 | 0 | 0 | 0 | | Mailers | 1 | 4 | 4 | 1 | 0 | 0 | 0 | | Model Tests | 2 | 5 | 4 | 2 | 0 | 0 | 0 | -| Models | 1 | 3 | 3 | 1 | 0 | 0 | 0 | +| Models | 4 | 10 | 10 | 4 | 0 | 0 | 0 | | Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 | | Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 | +----------------------+---------+---------+---------+---------+---------+-----+-------+ -| Code | 30 | 477 | 145 | 7 | 1 | 0 | 143 | +| Code | 33 | 486 | 154 | 10 | 1 | 0 | 152 | | Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 | -| Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 | +| Total | 37 | 493 | 160 | 12 | 1 | 0 | 158 | +----------------------+---------+---------+---------+---------+---------+-----+-------+ - Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34 + Code LOC: 154 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 37 - STI models count: 0 STI classes - Polymorphic models count: 0 polymorphic associations - Schema Stats: No schema.rb or structure.sql file found \ No newline at end of file + STI models count: 1 STI classes + Polymorphic models count: 1 polymorphic associations + Schema Stats: 2 `create_table` calls in schema.rb diff --git a/test/lib/rails_stats/json_formatter_test.rb b/test/lib/rails_stats/json_formatter_test.rb index f47e228..8abf637 100644 --- a/test/lib/rails_stats/json_formatter_test.rb +++ b/test/lib/rails_stats/json_formatter_test.rb @@ -284,4 +284,4 @@ assert_equal expectation, result end end -end \ No newline at end of file +end From 7cc20a50309e705df123be911ee44a8e3301a698 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Wed, 14 May 2025 16:56:22 -0300 Subject: [PATCH 06/13] fix identation --- lib/rails_stats/stats_calculator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rails_stats/stats_calculator.rb b/lib/rails_stats/stats_calculator.rb index 80be9fc..b0e9ed1 100644 --- a/lib/rails_stats/stats_calculator.rb +++ b/lib/rails_stats/stats_calculator.rb @@ -18,7 +18,7 @@ def initialize(root_directory) @test_loc = calculate_tests @schema = calculate_create_table_calls @sti = calculate_sti - @polymorphic = calculate_polymorphic + @polymorphic = calculate_polymorphic @files_total, @code_total, @tests_total, @grand_total = calculate_totals end From 8174700d53a3998e73d291a63bcd08c245429abc Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Fri, 23 May 2025 10:44:33 -0300 Subject: [PATCH 07/13] Adjust tests --- test/fixtures/console-output.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt index 140efbf..34dcf89 100644 --- a/test/fixtures/console-output.txt +++ b/test/fixtures/console-output.txt @@ -4,12 +4,12 @@ | simplecov-console | 8 | 3 | | codecov | 4 | 1 | | rails_stats | 4 | 2 | -| listen | 3 | 2 | -| bootsnap | 1 | 1 | -| puma | 1 | 1 | +| simplecov | 3 | 3 | +| minitest-around | 1 | 1 | +| bundler | 0 | 0 | | byebug | 0 | 0 | -| spring | 0 | 0 | -| tzinfo-data | 0 | 0 | +| minitest | 0 | 0 | +| minitest-spec-context | 0 | 0 | +-----------------------|------------|----------------+ Declared Gems 9 @@ -33,12 +33,12 @@ | Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 | | Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 | +----------------------+---------+---------+---------+---------+---------+-----+-------+ -| Code | 33 | 486 | 154 | 10 | 1 | 0 | 152 | +| Code | 33 | 484 | 152 | 10 | 1 | 0 | 150 | | Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 | -| Total | 37 | 493 | 160 | 12 | 1 | 0 | 158 | +| Total | 37 | 491 | 158 | 12 | 1 | 0 | 156 | +----------------------+---------+---------+---------+---------+---------+-----+-------+ - Code LOC: 154 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 37 + Code LOC: 152 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 37 STI models count: 1 STI classes Polymorphic models count: 1 polymorphic associations - Schema Stats: 2 `create_table` calls in schema.rb + Schema Stats: 2 `create_table` calls in schema.rb \ No newline at end of file From 3a87be4686143137623a54a762bc71d4c499099a Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 27 May 2025 13:28:08 -0300 Subject: [PATCH 08/13] fix json test order --- test/lib/rails_stats/json_formatter_test.rb | 289 ++++++++++---------- 1 file changed, 147 insertions(+), 142 deletions(-) diff --git a/test/lib/rails_stats/json_formatter_test.rb b/test/lib/rails_stats/json_formatter_test.rb index 8abf637..cdd9943 100644 --- a/test/lib/rails_stats/json_formatter_test.rb +++ b/test/lib/rails_stats/json_formatter_test.rb @@ -110,148 +110,153 @@ "transitive_dependencies": [] } ] - },{ - "name": "Mailers", - "files": "1", - "lines": "4", - "loc": "4", - "classes": "1", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Helpers", - "files": "1", - "lines": "3", - "loc": "3", - "classes": "0", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Jobs", - "files": "1", - "lines": "7", - "loc": "2", - "classes": "1", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Controllers", - "files": "1", - "lines": "7", - "loc": "6", - "classes": "1", - "methods": "1", - "m_over_c": "1", - "loc_over_m": "4" - }, { - "name": "Models", - "files": "1", - "lines": "3", - "loc": "3", - "classes": "1", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Channels", - "files": "2", - "lines": "8", - "loc": "8", - "classes": "2", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Javascripts", - "files": "3", - "lines": "27", - "loc": "7", - "classes": "0", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Libraries", - "files": "1", - "lines": "1", - "loc": "1", - "classes": "0", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Configuration", - "files": "19", - "lines": "417", - "loc": "111", - "classes": "1", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Model Tests", - "files": "2", - "lines": "5", - "loc": "4", - "classes": "2", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Spec Support", - "files": "1", - "lines": "1", - "loc": "1", - "classes": "0", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Test Support", - "files": "1", - "lines": "1", - "loc": "1", - "classes": "0", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Code", - "files": "30", - "lines": "477", - "loc": "145", - "classes": "7", - "methods": "1", - "m_over_c": "0", - "loc_over_m": "143", - "code_to_test_ratio": "0.0", - "total": true - }, { - "name": "Tests", - "files": "4", - "lines": "7", - "loc": "6", - "classes": "2", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0", - "code_to_test_ratio": "0.0", - "total": true - }, { - "name": "Total", - "files": "34", - "lines": "484", - "loc": "151", - "classes": "9", - "methods": "1", - "m_over_c": "0", - "loc_over_m": "149", - "code_to_test_ratio": "0.0", - "total": true - }] + },{ + "name": "Mailers", + "files": "1", + "lines": "4", + "loc": "4", + "classes": "1", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Models", + "files": "4", + "lines": "10", + "loc": "10", + "classes": "4", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Javascripts", + "files": "3", + "lines": "27", + "loc": "7", + "classes": "0", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Jobs", + "files": "1", + "lines": "7", + "loc": "2", + "classes": "1", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Controllers", + "files": "1", + "lines": "7", + "loc": "6", + "classes": "1", + "methods": "1", + "m_over_c": "1", + "loc_over_m": "4" + }, { + "name": "Helpers", + "files": "1", + "lines": "3", + "loc": "3", + "classes": "0", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Channels", + "files": "2", + "lines": "8", + "loc": "8", + "classes": "2", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Libraries", + "files": "1", + "lines": "1", + "loc": "1", + "classes": "0", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Configuration", + "files": "19", + "lines": "417", + "loc": "111", + "classes": "1", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Model Tests", + "files": "2", + "lines": "5", + "loc": "4", + "classes": "2", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Spec Support", + "files": "1", + "lines": "1", + "loc": "1", + "classes": "0", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Test Support", + "files": "1", + "lines": "1", + "loc": "1", + "classes": "0", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" + }, { + "name": "Code", + "files": "33", + "lines": "484", + "loc": "152", + "classes": "10", + "methods": "1", + "m_over_c": "0", + "loc_over_m": "150", + "code_to_test_ratio": "0.0", + "total": true + }, { + "name": "Tests", + "files": "4", + "lines": "7", + "loc": "6", + "classes": "2", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0", + "code_to_test_ratio": "0.0", + "total": true + }, { + "name": "Total", + "files": "37", + "lines": "491", + "loc": "158", + "classes": "12", + "methods": "1", + "m_over_c": "0", + "loc_over_m": "156", + "code_to_test_ratio": "0.0", + "total": true}, + {"schema_stats": + {"schema_path": + "/Users/hmdros/code/hmdros/miles_notifier_app/rails_stats/test/dummy/db/schema.rb", + "create_table calls count": 2}}, + {"sti_stats": {"sti_models_count": 1}}, + {"polymorphic_stats": {"polymorphic_models_count": 1}}] EOS it "outputs useful stats for a Rails project" do From 4b93f2c3a1a0068f35345e2cbd80e8f74a66bf18 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 27 May 2025 13:43:03 -0300 Subject: [PATCH 09/13] adjust schema path --- test/fixtures/console-output.txt | 6 ++- test/lib/rails_stats/json_formatter_test.rb | 48 +++++++++++---------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt index 34dcf89..ddbb37c 100644 --- a/test/fixtures/console-output.txt +++ b/test/fixtures/console-output.txt @@ -12,16 +12,18 @@ | minitest-spec-context | 0 | 0 | +-----------------------|------------|----------------+ + Declared Gems 9 Total Gems 18 Unpinned Versions 6 Github Refs 0 + +----------------------+---------+---------+---------+---------+---------+-----+-------+ | Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+---------+---------+---------+---------+---------+-----+-------+ | Channels | 2 | 8 | 8 | 2 | 0 | 0 | 0 | -| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 | +| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 | | Controllers | 1 | 7 | 6 | 1 | 1 | 1 | 4 | | Helpers | 1 | 3 | 3 | 0 | 0 | 0 | 0 | | Javascripts | 3 | 27 | 7 | 0 | 0 | 0 | 0 | @@ -41,4 +43,4 @@ STI models count: 1 STI classes Polymorphic models count: 1 polymorphic associations - Schema Stats: 2 `create_table` calls in schema.rb \ No newline at end of file + Schema Stats: 2 `create_table` calls in schema.rb diff --git a/test/lib/rails_stats/json_formatter_test.rb b/test/lib/rails_stats/json_formatter_test.rb index cdd9943..4b8841c 100644 --- a/test/lib/rails_stats/json_formatter_test.rb +++ b/test/lib/rails_stats/json_formatter_test.rb @@ -120,19 +120,10 @@ "m_over_c": "0", "loc_over_m": "0" }, { - "name": "Models", - "files": "4", - "lines": "10", - "loc": "10", - "classes": "4", - "methods": "0", - "m_over_c": "0", - "loc_over_m": "0" - }, { - "name": "Javascripts", - "files": "3", - "lines": "27", - "loc": "7", + "name": "Helpers", + "files": "1", + "lines": "3", + "loc": "3", "classes": "0", "methods": "0", "m_over_c": "0", @@ -156,11 +147,11 @@ "m_over_c": "1", "loc_over_m": "4" }, { - "name": "Helpers", - "files": "1", - "lines": "3", - "loc": "3", - "classes": "0", + "name": "Models", + "files": "4", + "lines": "10", + "loc": "10", + "classes": "4", "methods": "0", "m_over_c": "0", "loc_over_m": "0" @@ -173,6 +164,15 @@ "methods": "0", "m_over_c": "0", "loc_over_m": "0" + }, { + "name": "Javascripts", + "files": "3", + "lines": "27", + "loc": "7", + "classes": "0", + "methods": "0", + "m_over_c": "0", + "loc_over_m": "0" }, { "name": "Libraries", "files": "1", @@ -252,11 +252,13 @@ "code_to_test_ratio": "0.0", "total": true}, {"schema_stats": - {"schema_path": - "/Users/hmdros/code/hmdros/miles_notifier_app/rails_stats/test/dummy/db/schema.rb", - "create_table calls count": 2}}, - {"sti_stats": {"sti_models_count": 1}}, - {"polymorphic_stats": {"polymorphic_models_count": 1}}] + {"schema_path": + "#{Dir.pwd}/test/dummy/db/schema.rb", + "create_table calls count": 2}}, + {"sti_stats": {"sti_models_count": 1}}, + {"polymorphic_stats": {"polymorphic_models_count": 1} + } + ] EOS it "outputs useful stats for a Rails project" do From 05a2bc31cb7bef26807bd0bb104706ee055af1c2 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 27 May 2025 13:55:34 -0300 Subject: [PATCH 10/13] update assertion --- test/fixtures/console-output.txt | 2 -- test/lib/rails_stats/code_statistics_test.rb | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt index ddbb37c..7a1d69b 100644 --- a/test/fixtures/console-output.txt +++ b/test/fixtures/console-output.txt @@ -12,13 +12,11 @@ | minitest-spec-context | 0 | 0 | +-----------------------|------------|----------------+ - Declared Gems 9 Total Gems 18 Unpinned Versions 6 Github Refs 0 - +----------------------+---------+---------+---------+---------+---------+-----+-------+ | Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M | +----------------------+---------+---------+---------+---------+---------+-----+-------+ diff --git a/test/lib/rails_stats/code_statistics_test.rb b/test/lib/rails_stats/code_statistics_test.rb index 042bc09..2af4d7c 100644 --- a/test/lib/rails_stats/code_statistics_test.rb +++ b/test/lib/rails_stats/code_statistics_test.rb @@ -14,7 +14,10 @@ RailsStats::CodeStatistics.new(root_directory).to_s end - assert_equal table.delete(" \n"), out.delete(" \n") + assert_equal( + table.lines.map(&:rstrip).join, + out.lines.map(&:rstrip).join + ) end end end From 11b360c2e792d890078e3cd2a8c2a0ff43cf02e5 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Tue, 27 May 2025 14:01:19 -0300 Subject: [PATCH 11/13] add changelong entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e39a04e..b51474d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * [BUGFIX: Fix JSON output missing Code and Tests total count](https://github.com/fastruby/rails_stats/pull/40) * Update README examples +* [FEATURE: Output number of table created from schema.rb or structure.sql, add STI count add polymorphic models count](https://github.com/fastruby/rails_stats/pull/37) # v2.0.1 ([commits](https://github.com/fastruby/rails_stats/compare/v2.0.0...v2.0.1)) From 6e27cbc42823a826a31d47fbb3f2e20f5f3c32a7 Mon Sep 17 00:00:00 2001 From: Henrique Medeiros Date: Thu, 29 May 2025 13:39:10 -0300 Subject: [PATCH 12/13] Rmove STI count changes --- CHANGELOG.md | 2 +- lib/rails_stats/console_formatter.rb | 5 ----- lib/rails_stats/json_formatter.rb | 9 --------- lib/rails_stats/stats_calculator.rb | 15 +-------------- test/fixtures/console-output.txt | 1 - test/lib/rails_stats/json_formatter_test.rb | 1 - 6 files changed, 2 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b51474d..e059b6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ * [BUGFIX: Fix JSON output missing Code and Tests total count](https://github.com/fastruby/rails_stats/pull/40) * Update README examples -* [FEATURE: Output number of table created from schema.rb or structure.sql, add STI count add polymorphic models count](https://github.com/fastruby/rails_stats/pull/37) +* [FEATURE: Output number of table created from schema.rb or structure.sql, add polymorphic models count](https://github.com/fastruby/rails_stats/pull/37) # v2.0.1 ([commits](https://github.com/fastruby/rails_stats/compare/v2.0.0...v2.0.1)) diff --git a/lib/rails_stats/console_formatter.rb b/lib/rails_stats/console_formatter.rb index bd4b170..e12fae1 100644 --- a/lib/rails_stats/console_formatter.rb +++ b/lib/rails_stats/console_formatter.rb @@ -18,7 +18,6 @@ def to_s end print_code_test_stats - print_sti_stats print_polymorphic_stats print_schema_stats end @@ -57,10 +56,6 @@ def print_code_test_stats puts "" end - def print_sti_stats - puts " STI models count: #{calculator.sti} STI classes" - end - def print_polymorphic_stats puts " Polymorphic models count: #{calculator.polymorphic} polymorphic associations" end diff --git a/lib/rails_stats/json_formatter.rb b/lib/rails_stats/json_formatter.rb index 7c9ba77..cb1f1d2 100644 --- a/lib/rails_stats/json_formatter.rb +++ b/lib/rails_stats/json_formatter.rb @@ -18,7 +18,6 @@ def result @result << stat_hash("Total", @grand_total).merge(code_test_hash) if @grand_total @result << { "schema_stats" => schema_info } - @result << { "sti_stats" => print_sti_stat } @result << { "polymorphic_stats" => print_polymorphic_stats } @result @@ -55,14 +54,6 @@ def stat_hash(name, statistics) } end - def print_sti_stat - if calculator.sti - { - "sti_models_count" => calculator.sti, - } - end - end - def print_polymorphic_stats if calculator.polymorphic { diff --git a/lib/rails_stats/stats_calculator.rb b/lib/rails_stats/stats_calculator.rb index b0e9ed1..f8c283e 100644 --- a/lib/rails_stats/stats_calculator.rb +++ b/lib/rails_stats/stats_calculator.rb @@ -17,12 +17,11 @@ def initialize(root_directory) @code_loc = calculate_code @test_loc = calculate_tests @schema = calculate_create_table_calls - @sti = calculate_sti @polymorphic = calculate_polymorphic @files_total, @code_total, @tests_total, @grand_total = calculate_totals end - attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path, :sti, :polymorphic + attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total, :schema, :schema_path, :structure_path, :polymorphic private @@ -155,18 +154,6 @@ def count_create_table_calls(file_path, keyword) create_table_count end - def calculate_sti - @sti = 0 - Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file| - File.foreach(file) do |line| - if line =~ /class\s+(\w+)\s*<\s*(\w+)/ && !(line =~ /class\s+(\w+)\s*<\s*(ActiveRecord::Base|ApplicationRecord)/) - @sti += 1 - end - end - end - @sti - end - def calculate_polymorphic polymorphic_count = 0 Dir.glob(File.join(@root_directory, "app", "models", "*.rb")).each do |file| diff --git a/test/fixtures/console-output.txt b/test/fixtures/console-output.txt index 7a1d69b..cda0301 100644 --- a/test/fixtures/console-output.txt +++ b/test/fixtures/console-output.txt @@ -39,6 +39,5 @@ +----------------------+---------+---------+---------+---------+---------+-----+-------+ Code LOC: 152 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 37 - STI models count: 1 STI classes Polymorphic models count: 1 polymorphic associations Schema Stats: 2 `create_table` calls in schema.rb diff --git a/test/lib/rails_stats/json_formatter_test.rb b/test/lib/rails_stats/json_formatter_test.rb index 4b8841c..0f5b0f6 100644 --- a/test/lib/rails_stats/json_formatter_test.rb +++ b/test/lib/rails_stats/json_formatter_test.rb @@ -255,7 +255,6 @@ {"schema_path": "#{Dir.pwd}/test/dummy/db/schema.rb", "create_table calls count": 2}}, - {"sti_stats": {"sti_models_count": 1}}, {"polymorphic_stats": {"polymorphic_models_count": 1} } ] From 2226bdfd454aa65ebf6a4f35fc204dd9d09adb75 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Thu, 29 May 2025 21:43:55 -0400 Subject: [PATCH 13/13] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e059b6e..e144329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ * [BUGFIX: Fix JSON output missing Code and Tests total count](https://github.com/fastruby/rails_stats/pull/40) * Update README examples -* [FEATURE: Output number of table created from schema.rb or structure.sql, add polymorphic models count](https://github.com/fastruby/rails_stats/pull/37) +* [FEATURE: Output number of tables created from schema.rb or structure.sql, add polymorphic models count](https://github.com/fastruby/rails_stats/pull/37) # v2.0.1 ([commits](https://github.com/fastruby/rails_stats/compare/v2.0.0...v2.0.1))