commento 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7cc261c10ba9619a4d6f78856bd6307fd02a6f9a448db2f0672aabaff4e038c
4
- data.tar.gz: c4efb2c2b2b5d9da69e3fa4a9f0555e8b6ebd712a03f7b8e3a4949d80da6a927
3
+ metadata.gz: a3088eb93d7f0b937169a65a8c95efc36eecb202be76a6cd7c98ec5adc5b945e
4
+ data.tar.gz: d1ce5f3eaad3ccc2fead78cecde406a20067f4304364948f09027d66ff26b789
5
5
  SHA512:
6
- metadata.gz: 5b62dd5d4983d649927fff20945286694af6ebebac4b7f07279b8a15026831220310eb6613e2a0930a936fe50296c97c343c8c17b25563dc48c61ebdb55cf93f
7
- data.tar.gz: 275e6b4fe545516a63539efd94254918dc564a222ba2023a5273e7460d5bfc9e1b5408464ac6878e8f5cf006d9940741be9bc261eba691c894f0c87b8ebb985f
6
+ metadata.gz: 47cd00bf0babcd8ab1c10db8d3edcd4be3330e99c5233b4241fe9552f5a65df96d6494349cc60530781f6d41cead7e05b74c76e7fccff191c9e02de4d78a2031
7
+ data.tar.gz: 3d7dbf40b8b2783a32dd86208b32e452d142b3d13b3e4ce5e9325472d5e04343d4f594b707aa8454eca2adf014b7e59fbb3f8dc9a72a42d557377f3c2d0d4783
@@ -8,8 +8,6 @@ module Commento
8
8
  # Public: The name of the adapter.
9
9
  attr_reader :name
10
10
 
11
- SKIP_COLUMN_NAME = %w[id uuid created_at updated_at].freeze
12
-
13
11
  # Public: Initialize a new ActiveRecord adapter instance.
14
12
  #
15
13
  # name - The Symbol name for this adapter. Optional (default :active_record)
@@ -46,24 +44,30 @@ module Commento
46
44
 
47
45
  # Public: Returns comments for tables and columns.
48
46
  def comments_for_database
49
- execute(tables_comments_sql).to_a.map! do |table_data|
50
- {
51
- table_name: table_data['table_name'],
52
- table_comment: table_data['obj_description'],
53
- columns: execute(columns_comments_sql(table_data['table_name'])).to_a.filter_map do |column_data|
54
- next if SKIP_COLUMN_NAME.include?(column_data['column_name'])
55
-
56
- {
57
- column_name: column_data['column_name'],
58
- column_comment: column_data['col_description']
59
- }
60
- end
61
- }
47
+ execute(tables_comments_sql).to_a.filter_map do |table_data|
48
+ next if configuration.skip_table_names.include?(table_data['table_name'])
49
+
50
+ parse_data(table_data)
62
51
  end
63
52
  end
64
53
 
65
54
  private
66
55
 
56
+ def parse_data(table_data)
57
+ {
58
+ table_name: table_data['table_name'],
59
+ table_comment: table_data['obj_description'],
60
+ columns: execute(columns_comments_sql(table_data['table_name'])).to_a.filter_map do |column_data|
61
+ next if configuration.skip_column_names.include?(column_data['column_name'])
62
+
63
+ {
64
+ column_name: column_data['column_name'],
65
+ column_comment: column_data['col_description']
66
+ }
67
+ end
68
+ }
69
+ end
70
+
67
71
  def tables_comments_sql
68
72
  <<-SQL.squish
69
73
  SELECT
@@ -94,6 +98,10 @@ module Commento
94
98
  def execute(sql)
95
99
  ::ActiveRecord::Base.connection.execute(sql)
96
100
  end
101
+
102
+ def configuration
103
+ Commento.configuration
104
+ end
97
105
  end
98
106
  end
99
107
  end
@@ -2,10 +2,14 @@
2
2
 
3
3
  module Commento
4
4
  class Configuration
5
- attr_accessor :adapter
5
+ attr_accessor :adapter, :include_folders, :exclude_folders, :skip_table_names, :skip_column_names
6
6
 
7
7
  def initialize
8
8
  @adapter = nil
9
+ @include_folders = %w[app lib]
10
+ @exclude_folders = ['app/assets']
11
+ @skip_table_names = %w[ar_internal_metadata schema_migrations]
12
+ @skip_column_names = %w[id uuid created_at updated_at]
9
13
  end
10
14
  end
11
15
  end
@@ -2,10 +2,17 @@
2
2
 
3
3
  module Commento
4
4
  class Report
5
+ COMMENTO_FOLDER_NAME = 'commento'
6
+
7
+ def initialize(data_collect_service: Commento::Services::DataCollect)
8
+ @data_collect_service = data_collect_service.new
9
+ end
10
+
5
11
  def create_report
6
- FileUtils.mkdir_p('commento')
12
+ @commento_data = @data_collect_service.call
13
+ FileUtils.mkdir_p(COMMENTO_FOLDER_NAME)
7
14
  File.write(
8
- "commento/index.#{template_format}",
15
+ "#{COMMENTO_FOLDER_NAME}/#{file_name}",
9
16
  main_template.gsub('%tables_placeholder%', tables_placeholder)
10
17
  )
11
18
  end
@@ -23,9 +30,19 @@ module Commento
23
30
 
24
31
  def columns_placeholder(table_name, columns)
25
32
  columns.map do |column_data|
33
+ full_column_name = "#{table_name}.#{column_data[:column_name]}"
26
34
  column_template
27
- .gsub('%column_name%', "#{table_name}.#{column_data[:column_name]}")
35
+ .gsub('%column_name%', full_column_name)
28
36
  .gsub('%column_comment%', column_data[:column_comment].presence || '')
37
+ .gsub('%data_placeholder%', data_placeholder(full_column_name))
38
+ end.join
39
+ end
40
+
41
+ def data_placeholder(full_column_name)
42
+ return '' if @commento_data[full_column_name].blank?
43
+
44
+ @commento_data[full_column_name].map do |filename|
45
+ data_template.gsub('%commento_data%', filename)
29
46
  end.join
30
47
  end
31
48
 
@@ -45,7 +62,11 @@ module Commento
45
62
  raise NotImplementedError
46
63
  end
47
64
 
48
- def template_format
65
+ def data_template
66
+ raise NotImplementedError
67
+ end
68
+
69
+ def file_name
49
70
  raise NotImplementedError
50
71
  end
51
72
 
@@ -35,11 +35,22 @@ module Commento
35
35
  end
36
36
 
37
37
  def column_template
38
- " <p>%column_name%: %column_comment%</p>\n"
38
+ [
39
+ " <div class='column-body'>",
40
+ ' <p>%column_name%: %column_comment%</p>',
41
+ " <div class='column-data'>",
42
+ '%data_placeholder%',
43
+ ' </div>',
44
+ " </div>\n"
45
+ ].join("\n")
46
+ end
47
+
48
+ def data_template
49
+ ' <span>%commento_data%</span>'
39
50
  end
40
51
 
41
- def template_format
42
- 'html'
52
+ def file_name
53
+ 'index.html'
43
54
  end
44
55
 
45
56
  def styles
@@ -52,6 +63,10 @@ module Commento
52
63
  '.table-header h3 { padding: .25rem; background: #bbb }',
53
64
  '.table-header p { padding: .25rem; margin: 0 }',
54
65
  '.table-body { padding: 0 .25rem }',
66
+ '.column-body { margin-bottom: .5rem; }',
67
+ '.column-body p { margin: 0 }',
68
+ '.column-data { display: flex; flex-direction: column; margin: .5rem 0 }',
69
+ '.column-data span { font-style: italic; padding-left: 1rem }',
55
70
  '</style>'
56
71
  ].join
57
72
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ module Services
5
+ class DataCollect
6
+ COMMENTO_MATCH_REGEXP = /^[ \t]*# commento:.+$/.freeze
7
+
8
+ # @data contains data with next format
9
+ # {
10
+ # 'fantasy_sports.points' => [
11
+ # 'app/services/some_service.rb:11'
12
+ # ]
13
+ # }
14
+ # key - table_name.column_name
15
+ # values - lines of files where these columns are changed in the code
16
+ def initialize
17
+ @data = {}
18
+ end
19
+
20
+ def call
21
+ configuration.include_folders.each { |include_folder| iterate_folder(include_folder) }
22
+ @data
23
+ end
24
+
25
+ private
26
+
27
+ def iterate_folder(include_folder)
28
+ Dir.glob("#{include_folder}/**/*.rb").each do |filename|
29
+ next if exclude_folders.include?(filename.split('/')[0..-2].join('/'))
30
+
31
+ File.open(filename) { |lines| iterate_filelines(lines, filename) }
32
+ end
33
+ end
34
+
35
+ def iterate_filelines(lines, filename)
36
+ lines.each.with_index(1) do |line, index|
37
+ next unless COMMENTO_MATCH_REGEXP.match?(line)
38
+
39
+ field = line.strip.split('# commento: ')[-1]
40
+ @data[field] ||= []
41
+ @data[field] << "#{filename}:#{index}"
42
+ end
43
+ end
44
+
45
+ def exclude_folders
46
+ @exclude_folders ||= configuration.exclude_folders
47
+ end
48
+
49
+ def configuration
50
+ Commento.configuration
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Commento
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/commento.rb CHANGED
@@ -8,6 +8,7 @@ require 'commento/dsl'
8
8
  require 'commento/helpers'
9
9
  require 'commento/report'
10
10
  require 'commento/reports/html'
11
+ require 'commento/services/data_collect'
11
12
 
12
13
  module Commento
13
14
  extend self
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commento
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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-08-14 00:00:00.000000000 Z
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -82,6 +82,7 @@ files:
82
82
  - lib/commento/helpers.rb
83
83
  - lib/commento/report.rb
84
84
  - lib/commento/reports/html.rb
85
+ - lib/commento/services/data_collect.rb
85
86
  - lib/commento/version.rb
86
87
  homepage: https://github.com/kortirso/commento
87
88
  licenses: