commento 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a65354f12eb0a8c66c0bc901f87bb35f1ddf2d9d18f2b399c8e13758bc779e7
4
- data.tar.gz: b0d88b4c72e2b6e6404b762b2113a6c61779c2d7a3cc4a7732c09452599d895c
3
+ metadata.gz: ec9a9477c941a8b246f869b1e157c99c9cf49476ff1708abc1a4c1b0f57c847e
4
+ data.tar.gz: ddfc8c0308bbfa0933e7d580e57fe7a2f157ad73a02d066681d21993901e6fa4
5
5
  SHA512:
6
- metadata.gz: def8375883745c14c5fa4b032bec57a3e4030299fc79c4a0a214e46f177d4e962c93d99a2f899d51e8db450cb4e14ca26d5805430f678b5489b85f14893a72f1
7
- data.tar.gz: b7fe5588c90a2c072efb614a43201220c443410ed149b3965ea5baf65e93276506e85fe61c4d859e2a15556e4d8232b379e6425f9b288013f48918594689c168
6
+ metadata.gz: b39355f42447cbcf9d1b55e5adc35e8793f61a3514b4bcfdfbb3352fa2f66a6b41a38bcc6d9f1d8517cf4fa2f462ef5b7bc33bc1699bdff1715ac6e0e3324c17
7
+ data.tar.gz: 7a70ad3732e253860cb17f1dcc57152651e6229b9ad815d097093755f844553c38ff48e0c48fe5febeacd2688034bfdd273023598e977891242f8bd5c1cf6aed
data/README.md CHANGED
@@ -27,31 +27,55 @@ require 'commento/adapters/active_record'
27
27
 
28
28
  Commento.configure do |config|
29
29
  config.adapter = Commento::Adapters::ActiveRecord.new
30
+ config.include_folders = %w[app lib] # folder for searching commento comments
31
+ config.exclude_folders = ['app/assets'] # folder for excluding searching commento comments
32
+ config.skip_table_names = %w[ar_internal_metadata schema_migrations] # ignoring tables
33
+ config.skip_column_names = %w[id uuid created_at updated_at] # ignoring columns
30
34
  end
31
35
  ```
32
36
 
33
- ### Models
37
+ ## Rake tasks
34
38
 
35
- Update your application model
39
+ ### Generating reports
36
40
 
37
- ```ruby
38
- class ApplicationRecord < ActiveRecord::Base
39
- include Commento::Helpers
40
- end
41
+ You can generate different types of reports (right now only html) with rake task
42
+
43
+ ```bash
44
+ rails "commento:generate_report[html]"
41
45
  ```
42
46
 
43
- ## Generating reports
47
+ ### Health check
44
48
 
45
- You can generate different types of reports
49
+ You can check amount of missing comments for tables and columns with console report
46
50
 
47
- ```ruby
48
- Commento::Reports::Html.new.create_report
51
+ ```bash
52
+ rails commento:health
53
+ ```
54
+
55
+ <img width="727" alt="Снимок экрана 2024-09-13 в 10 11 12" src="https://github.com/user-attachments/assets/f0b8fbf8-6a6a-4c7b-b6b6-2a0de6d19f45">
56
+
57
+ ### Generating migration for adding comments to tables and columns
58
+
59
+ You can generate migration with adding comments for table and columns
60
+
61
+ ```bash
62
+ rails g commento:active_record
49
63
  ```
50
64
 
51
65
  ## Usage
52
66
 
53
67
  Commento provides helpers for models for setting and getting comments.
54
68
 
69
+ ### Models configuration
70
+
71
+ Update your application model
72
+
73
+ ```ruby
74
+ class ApplicationRecord < ActiveRecord::Base
75
+ include Commento::Helpers
76
+ end
77
+ ```
78
+
55
79
  ### Set table's comment
56
80
 
57
81
  ```ruby
@@ -63,13 +87,13 @@ or reset comment by skiping value
63
87
  User.set_table_comment
64
88
  ```
65
89
 
66
- ### Read table's column comment
90
+ ### Read table's comment
67
91
 
68
92
  ```ruby
69
- User.get_table_comment
93
+ User.fetch_table_comment
70
94
  ```
71
95
 
72
- ### Set table's column comment
96
+ ### Set column's comment
73
97
 
74
98
  ```ruby
75
99
  User.set_column_comment(:email, 'Required field for user authentication')
@@ -80,10 +104,10 @@ or reset comment by skiping value
80
104
  User.set_column_comment(:email)
81
105
  ```
82
106
 
83
- ### Read table's column comment
107
+ ### Read column's comment
84
108
 
85
109
  ```ruby
86
- User.get_column_comment(:email)
110
+ User.fetch_column_comment(:email)
87
111
  ```
88
112
 
89
113
  ## License
@@ -23,7 +23,7 @@ module Commento
23
23
  end
24
24
 
25
25
  # Public: Returns comment for table.
26
- def table_comment(table_name)
26
+ def fetch_table_comment(table_name)
27
27
  table_data = execute(tables_comments_sql).to_a.find { |data| data['table_name'] == table_name }
28
28
  table_data.present? ? table_data['obj_description'] : nil
29
29
  end
@@ -36,14 +36,14 @@ module Commento
36
36
  end
37
37
 
38
38
  # Public: Returns comment for table's column.
39
- def column_comment(table_name, column_name)
39
+ def fetch_column_comment(table_name, column_name)
40
40
  sql = columns_comments_sql(table_name)
41
41
  column_data = execute(sql).to_a.find { |data| data['column_name'].to_sym == column_name }
42
42
  column_data.present? ? column_data['col_description'] : nil
43
43
  end
44
44
 
45
45
  # Public: Returns comments for tables and columns.
46
- def comments_for_database
46
+ def fetch_comments_for_database
47
47
  execute(tables_comments_sql).to_a.filter_map do |table_data|
48
48
  next if configuration.skip_table_names.include?(table_data['table_name'])
49
49
 
data/lib/commento/dsl.rb CHANGED
@@ -17,8 +17,8 @@ module Commento
17
17
  end
18
18
 
19
19
  # Public: Returns comment for table.
20
- def table_comment(table_name)
21
- adapter.table_comment(table_name)
20
+ def fetch_table_comment(table_name)
21
+ adapter.fetch_table_comment(table_name)
22
22
  end
23
23
 
24
24
  # Public: Sets comment for table's column.
@@ -27,13 +27,13 @@ module Commento
27
27
  end
28
28
 
29
29
  # Public: Returns comment for table's column.
30
- def column_comment(table_name, column_name)
31
- adapter.column_comment(table_name, column_name)
30
+ def fetch_column_comment(table_name, column_name)
31
+ adapter.fetch_column_comment(table_name, column_name)
32
32
  end
33
33
 
34
34
  # Public: Returns comments for tables and columns.
35
- def comments_for_database
36
- adapter.comments_for_database
35
+ def fetch_comments_for_database
36
+ adapter.fetch_comments_for_database
37
37
  end
38
38
  end
39
39
  end
@@ -13,16 +13,16 @@ module Commento
13
13
  instance.set_table_comment(table_name, value)
14
14
  end
15
15
 
16
- def table_comment
17
- instance.table_comment(table_name)
16
+ def fetch_table_comment
17
+ instance.fetch_table_comment(table_name)
18
18
  end
19
19
 
20
20
  def set_column_comment(column_name, value=nil)
21
21
  instance.set_column_comment(table_name, column_name, value)
22
22
  end
23
23
 
24
- def column_comment(column_name)
25
- instance.column_comment(table_name, column_name)
24
+ def fetch_column_comment(column_name)
25
+ instance.fetch_column_comment(table_name, column_name)
26
26
  end
27
27
 
28
28
  def instance
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'commento'
4
+ require 'rails'
5
+
6
+ module Commento
7
+ class Railtie < Rails::Railtie
8
+ railtie_name :commento
9
+
10
+ rake_tasks do
11
+ path = File.expand_path(__dir__)
12
+ Dir.glob("#{path}/../tasks/**/*.rake").each { |f| load f }
13
+ end
14
+ end
15
+ end
@@ -32,7 +32,7 @@ module Commento
32
32
  columns.map do |column_data|
33
33
  full_column_name = "#{table_name}.#{column_data[:column_name]}"
34
34
  column_template
35
- .gsub('%column_name%', full_column_name)
35
+ .gsub('%column_name%', column_data[:column_name])
36
36
  .gsub('%column_comment%', column_data[:column_comment].presence || '')
37
37
  .gsub('%data_placeholder%', data_placeholder(full_column_name))
38
38
  end.join
@@ -47,7 +47,7 @@ module Commento
47
47
  end
48
48
 
49
49
  def database_comments
50
- adapter.comments_for_database
50
+ adapter.fetch_comments_for_database
51
51
  end
52
52
 
53
53
  def main_template
@@ -9,6 +9,7 @@ module Commento
9
9
  # {
10
10
  # 'fantasy_sports.points' => [
11
11
  # 'app/services/some_service.rb:11'
12
+ # 'app/services/some_another_service.rb:12'
12
13
  # ]
13
14
  # }
14
15
  # key - table_name.column_name
@@ -33,15 +34,15 @@ module Commento
33
34
  end
34
35
 
35
36
  # commento line looks like
36
- # commento: fantasy_sports.points, fantasy_sports.statistics
37
+ # commento:fantasy_sports:points,statistics
37
38
  def iterate_filelines(lines, filename)
38
39
  lines.each.with_index(1) do |line, index|
39
40
  next unless COMMENTO_MATCH_REGEXP.match?(line)
40
41
 
41
- fields = line.strip.split('# commento: ')[-1].split(',').map(&:strip)
42
- fields.each do |field|
43
- @data[field] ||= []
44
- @data[field] << "#{filename}:#{index}"
42
+ table_name, columns = line.strip.split('# commento:')[-1].split(':').map(&:strip)
43
+ columns.split(',').each do |column|
44
+ @data["#{table_name}.#{column}"] ||= []
45
+ @data["#{table_name}.#{column}"] << "#{filename}:#{index}"
45
46
  end
46
47
  end
47
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Commento
4
- VERSION = '0.1.3'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/commento.rb CHANGED
@@ -9,6 +9,7 @@ require 'commento/helpers'
9
9
  require 'commento/report'
10
10
  require 'commento/reports/html'
11
11
  require 'commento/scrapers/ruby'
12
+ require 'commento/railtie' if defined?(Rails)
12
13
 
13
14
  module Commento
14
15
  extend self
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/active_record'
4
+
5
+ module Commento
6
+ module Generators
7
+ class ActiveRecordGenerator < ::Rails::Generators::Base
8
+ include ::Rails::Generators::Migration
9
+
10
+ desc 'Generates migration for adding comments'
11
+
12
+ source_paths << File.join(File.dirname(__FILE__), 'templates')
13
+
14
+ attr_reader :tables_change_data, :columns_change_data
15
+
16
+ def self.next_migration_number(dirname)
17
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
18
+ end
19
+
20
+ def self.migration_version
21
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if requires_migration_number?
22
+ end
23
+
24
+ def self.requires_migration_number?
25
+ Rails::VERSION::MAJOR.to_i >= 5
26
+ end
27
+
28
+ def create_migration_file
29
+ generate_migration_data
30
+
31
+ migration_template 'migration.erb', 'db/migrate/commento_add_comments_to_tables_and_columns.rb'
32
+ end
33
+
34
+ def migration_version
35
+ self.class.migration_version
36
+ end
37
+
38
+ def generate_migration_data
39
+ @tables_without_comment = []
40
+ @table_report = {}
41
+
42
+ check_comments_for_database(Commento.adapter.fetch_comments_for_database)
43
+
44
+ @tables_change_data =
45
+ @tables_without_comment
46
+ .map { |table_name| "change_table_comment(:#{table_name}, from: nil, to: '')" }
47
+ .join("\n ")
48
+
49
+ @columns_change_data =
50
+ @table_report.map do |table_name, column_names|
51
+ column_names.map do |column_name|
52
+ "change_column_comment(:#{table_name}, :#{column_name}, from: nil, to: '')"
53
+ end
54
+ end.flatten.join("\n ")
55
+ end
56
+
57
+ private
58
+
59
+ def check_comments_for_database(comments_for_database)
60
+ comments_for_database.each do |table_data|
61
+ @tables_without_comment << table_data[:table_name] if table_data[:table_comment].nil?
62
+ table_data[:columns].each do |column_data|
63
+ next if column_data[:column_comment].present?
64
+
65
+ @table_report[table_data[:table_name]] ||= []
66
+ @table_report[table_data[:table_name]] << column_data[:column_name]
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ class CommentoAddCommentsToTablesAndColumns < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ <%= tables_change_data %>
4
+
5
+ <%= columns_change_data %>
6
+ end
7
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'commento'
4
+ require 'rainbow'
5
+ require 'terminal-table'
6
+
7
+ AVAILABLE_REPORT_FORMATS = %w[html].freeze
8
+
9
+ # rubocop: disable Metrics/BlockLength
10
+ namespace :commento do
11
+ desc 'Generates HTML report'
12
+ task :generate_report, [:format] => :environment do |_, args|
13
+ if args[:format].blank?
14
+ puts Rainbow('Please specify format').red.bright
15
+ next
16
+ end
17
+
18
+ unless args[:format].in?(AVAILABLE_REPORT_FORMATS)
19
+ puts Rainbow("Format is not supported, available options: #{AVAILABLE_REPORT_FORMATS.join(", ")}").red.bright
20
+ next
21
+ end
22
+
23
+ puts Rainbow('Generating report is started').green
24
+
25
+ case args[:format]
26
+ when 'html' then Commento::Reports::Html.new.create_report
27
+ end
28
+
29
+ puts Rainbow('✓ Good job! Report is generated').green.bright
30
+ end
31
+
32
+ desc 'Generates console report about missing table and column comments'
33
+ task health: :environment do
34
+ puts Rainbow("Starting commento health check\n").green
35
+
36
+ comments_for_database = Commento.adapter.fetch_comments_for_database
37
+
38
+ tables_with_comment_amount = 0
39
+ tables_without_comment = []
40
+ tables_without_comment_amount = 0
41
+ columns_with_comment_amount = 0
42
+ columns_without_comment_amount = 0
43
+
44
+ table_report_with_missing_comments = {}
45
+
46
+ comments_for_database.each do |table_data|
47
+ if table_data[:table_comment].nil?
48
+ tables_without_comment << table_data[:table_name]
49
+ tables_without_comment_amount += 1
50
+ table_report_with_missing_comments[table_data[:table_name]] = []
51
+ else
52
+ tables_with_comment_amount += 1
53
+ end
54
+ table_data[:columns].each do |column_data|
55
+ if column_data[:column_comment].nil?
56
+ columns_without_comment_amount += 1
57
+ table_report_with_missing_comments[table_data[:table_name]] ||= []
58
+ table_report_with_missing_comments[table_data[:table_name]] << column_data[:column_name]
59
+ else
60
+ columns_with_comment_amount += 1
61
+ end
62
+ end
63
+ end
64
+
65
+ total_tables_amount = tables_with_comment_amount + tables_without_comment_amount
66
+ tables_with_comment_ratio = total_tables_amount.zero? ? 0 : (tables_with_comment_amount * 100 / total_tables_amount)
67
+
68
+ if tables_with_comment_ratio == 100
69
+ puts Rainbow('✓ Amazing! All tables have comment').green.bright
70
+ elsif tables_with_comment_ratio > 50
71
+ puts Rainbow("✓ Good job! #{tables_with_comment_ratio}% of tables have comment").green.bright
72
+ elsif tables_with_comment_ratio.positive?
73
+ puts Rainbow("✓ Need to work! Only #{tables_with_comment_ratio}% of tables have comment").yellow.bright
74
+ else
75
+ puts Rainbow('✓ Something is missing! No single table have comment').red.bright
76
+ end
77
+
78
+ total_columns_amount = columns_with_comment_amount + columns_without_comment_amount
79
+ columns_ratio = total_columns_amount.zero? ? 0 : (columns_with_comment_amount * 100 / total_columns_amount)
80
+
81
+ if columns_ratio == 100
82
+ puts Rainbow('✓ Amazing! All columns have comment').green.bright
83
+ elsif columns_ratio > 50
84
+ puts Rainbow("✓ Good job! #{columns_ratio}% of columns have comment").green.bright
85
+ elsif columns_ratio.positive?
86
+ puts Rainbow("✓ Need to work! Only #{columns_ratio}% of columns have comment").yellow.bright
87
+ else
88
+ puts Rainbow('✓ Something is missing! No single column have comment').red.bright
89
+ end
90
+
91
+ if tables_without_comment_amount.positive? || columns_without_comment_amount.positive?
92
+ rows = []
93
+ table_report_with_missing_comments.each do |table_name, column_names|
94
+ rows << [
95
+ table_name.in?(tables_without_comment) ? Rainbow(table_name).cyan.bright : Rainbow(table_name).silver.bright,
96
+ column_names.map { |column_name| Rainbow(column_name).cyan.bright }.join("\n")
97
+ ]
98
+ rows << :separator
99
+ end
100
+ puts ''
101
+ puts Terminal::Table.new(
102
+ title: Rainbow('Missing comments report').silver.bright,
103
+ headings: [Rainbow('Table name').silver.bright, Rainbow('Column name').silver.bright],
104
+ rows: rows,
105
+ style: { width: 80, border_bottom: false }
106
+ )
107
+ end
108
+ end
109
+ end
110
+ # rubocop: enable Metrics/BlockLength
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commento
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdanov Anton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-01 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rainbow
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: terminal-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: rubocop
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,10 +108,14 @@ files:
80
108
  - lib/commento/configuration.rb
81
109
  - lib/commento/dsl.rb
82
110
  - lib/commento/helpers.rb
111
+ - lib/commento/railtie.rb
83
112
  - lib/commento/report.rb
84
113
  - lib/commento/reports/html.rb
85
114
  - lib/commento/scrapers/ruby.rb
86
115
  - lib/commento/version.rb
116
+ - lib/generators/commento/active_record_generator.rb
117
+ - lib/generators/commento/templates/migration.erb
118
+ - lib/tasks/commento.rake
87
119
  homepage: https://github.com/kortirso/commento
88
120
  licenses:
89
121
  - MIT