commento 0.1.0 → 0.1.1

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: f2558be497fdf71eca0c553371dc0fb09fdb348a04bc930289ad3696a571a4fa
4
- data.tar.gz: c4e41098d7c6ffcff077d5c18545e7c0d03fcb33b3b1273bb4cfac0406cabf38
3
+ metadata.gz: c7cc261c10ba9619a4d6f78856bd6307fd02a6f9a448db2f0672aabaff4e038c
4
+ data.tar.gz: c4efb2c2b2b5d9da69e3fa4a9f0555e8b6ebd712a03f7b8e3a4949d80da6a927
5
5
  SHA512:
6
- metadata.gz: f93c37134940512adbe7fd03600d7aeb0f4af032b9196c55bc5d011a310138c5f2fbefc4b8962abd9aa7cbc88ab41156cff8121eb80142df91e057146c60f779
7
- data.tar.gz: c66a4d0f5e6a626982a3842d743b712fe7023fd3303369426a9f911d4b4c3fbb7ebd00712b02de70f192857b4aa5adb0573bf7a782f4aa054ce3946c90e33da6
6
+ metadata.gz: 5b62dd5d4983d649927fff20945286694af6ebebac4b7f07279b8a15026831220310eb6613e2a0930a936fe50296c97c343c8c17b25563dc48c61ebdb55cf93f
7
+ data.tar.gz: 275e6b4fe545516a63539efd94254918dc564a222ba2023a5273e7460d5bfc9e1b5408464ac6878e8f5cf006d9940741be9bc261eba691c894f0c87b8ebb985f
data/README.md CHANGED
@@ -32,16 +32,42 @@ end
32
32
 
33
33
  ### Models
34
34
 
35
- Update you application model
35
+ Update your application model
36
+
36
37
  ```ruby
37
38
  class ApplicationRecord < ActiveRecord::Base
38
39
  include Commento::Helpers
39
40
  end
40
41
  ```
41
42
 
43
+ ## Generating reports
44
+
45
+ You can generate different types of reports
46
+
47
+ ```ruby
48
+ Commento::Reports::Html.new.create_report
49
+ ```
50
+
42
51
  ## Usage
43
52
 
44
- Commento provides helpers for models for setting and getting table's column comments.
53
+ Commento provides helpers for models for setting and getting comments.
54
+
55
+ ### Set table's comment
56
+
57
+ ```ruby
58
+ User.set_table_comment('Users table')
59
+ ```
60
+
61
+ or reset comment by skiping value
62
+ ```ruby
63
+ User.set_table_comment
64
+ ```
65
+
66
+ ### Read table's column comment
67
+
68
+ ```ruby
69
+ User.get_table_comment
70
+ ```
45
71
 
46
72
  ### Set table's column comment
47
73
 
@@ -8,6 +8,8 @@ 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
+
11
13
  # Public: Initialize a new ActiveRecord adapter instance.
12
14
  #
13
15
  # name - The Symbol name for this adapter. Optional (default :active_record)
@@ -15,6 +17,19 @@ module Commento
15
17
  @name = options.fetch(:name, :active_record)
16
18
  end
17
19
 
20
+ # Public: Sets comment for table.
21
+ def set_table_comment(table_name, value)
22
+ sql = "COMMENT ON TABLE #{table_name} IS "
23
+ sql += value ? "'#{value}'" : 'NULL'
24
+ execute(sql)
25
+ end
26
+
27
+ # Public: Returns comment for table.
28
+ def table_comment(table_name)
29
+ table_data = execute(tables_comments_sql).to_a.find { |data| data['table_name'] == table_name }
30
+ table_data.present? ? table_data['obj_description'] : nil
31
+ end
32
+
18
33
  # Public: Sets comment for table's column.
19
34
  def set_column_comment(table_name, column_name, value)
20
35
  sql = "COMMENT ON COLUMN #{table_name}.#{column_name} IS "
@@ -23,24 +38,59 @@ module Commento
23
38
  end
24
39
 
25
40
  # Public: Returns comment for table's column.
26
- def get_column_comment(table_name, column_name)
27
- sql = <<-SQL.squish
41
+ def column_comment(table_name, column_name)
42
+ sql = columns_comments_sql(table_name)
43
+ column_data = execute(sql).to_a.find { |data| data['column_name'].to_sym == column_name }
44
+ column_data.present? ? column_data['col_description'] : nil
45
+ end
46
+
47
+ # Public: Returns comments for tables and columns.
48
+ 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
+ }
62
+ end
63
+ end
64
+
65
+ private
66
+
67
+ def tables_comments_sql
68
+ <<-SQL.squish
69
+ SELECT
70
+ tables.table_name,
71
+ pg_catalog.obj_description(pgc.oid, 'pg_class')
72
+ FROM information_schema.tables tables
73
+ INNER JOIN pg_catalog.pg_class pgc ON tables.table_name = pgc.relname
74
+ WHERE
75
+ tables.table_type='BASE TABLE' AND
76
+ tables.table_schema='public'
77
+ SQL
78
+ end
79
+
80
+ def columns_comments_sql(table_name)
81
+ <<-SQL.squish
28
82
  SELECT
29
83
  cols.column_name,
30
- pg_catalog.col_description(c.oid, cols.ordinal_position::int)
31
- FROM pg_catalog.pg_class c, information_schema.columns cols
84
+ pg_catalog.col_description(pgc.oid, cols.ordinal_position::int)
85
+ FROM pg_catalog.pg_class pgc, information_schema.columns cols
32
86
  WHERE
33
87
  cols.table_catalog = '#{Rails.configuration.database_configuration[Rails.env]["database"]}' AND
34
88
  cols.table_schema = 'public' AND
35
89
  cols.table_name = '#{table_name}' AND
36
- cols.table_name = c.relname
90
+ cols.table_name = pgc.relname
37
91
  SQL
38
- column_data = execute(sql).to_a.find { |column| column['column_name'].to_sym == column_name }
39
- column_data.presence ? column_data['col_description'] : nil
40
92
  end
41
93
 
42
- private
43
-
44
94
  def execute(sql)
45
95
  ::ActiveRecord::Base.connection.execute(sql)
46
96
  end
data/lib/commento/dsl.rb CHANGED
@@ -11,14 +11,29 @@ module Commento
11
11
  @adapter = adapter
12
12
  end
13
13
 
14
+ # Public: Sets comment for table.
15
+ def set_table_comment(table_name, value)
16
+ adapter.set_table_comment(table_name, value)
17
+ end
18
+
19
+ # Public: Returns comment for table.
20
+ def table_comment(table_name)
21
+ adapter.table_comment(table_name)
22
+ end
23
+
14
24
  # Public: Sets comment for table's column.
15
25
  def set_column_comment(table_name, column_name, value)
16
26
  adapter.set_column_comment(table_name, column_name, value)
17
27
  end
18
28
 
19
29
  # Public: Returns comment for table's column.
20
- def get_column_comment(table_name, column_name)
21
- adapter.get_column_comment(table_name, column_name)
30
+ def column_comment(table_name, column_name)
31
+ adapter.column_comment(table_name, column_name)
32
+ end
33
+
34
+ # Public: Returns comments for tables and columns.
35
+ def comments_for_database
36
+ adapter.comments_for_database
22
37
  end
23
38
  end
24
39
  end
@@ -9,12 +9,20 @@ module Commento
9
9
  end
10
10
 
11
11
  module ClassMethods
12
+ def set_table_comment(value=nil)
13
+ instance.set_table_comment(table_name, value)
14
+ end
15
+
16
+ def table_comment
17
+ instance.table_comment(table_name)
18
+ end
19
+
12
20
  def set_column_comment(column_name, value=nil)
13
21
  instance.set_column_comment(table_name, column_name, value)
14
22
  end
15
23
 
16
- def get_column_comment(column_name)
17
- instance.get_column_comment(table_name, column_name)
24
+ def column_comment(column_name)
25
+ instance.column_comment(table_name, column_name)
18
26
  end
19
27
 
20
28
  def instance
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ class Report
5
+ def create_report
6
+ FileUtils.mkdir_p('commento')
7
+ File.write(
8
+ "commento/index.#{template_format}",
9
+ main_template.gsub('%tables_placeholder%', tables_placeholder)
10
+ )
11
+ end
12
+
13
+ private
14
+
15
+ def tables_placeholder
16
+ database_comments.map do |table_data|
17
+ table_template
18
+ .gsub('%table_name%', table_data[:table_name])
19
+ .gsub('%table_comment%', table_data[:table_comment].presence || '')
20
+ .gsub('%columns_placeholder%', columns_placeholder(table_data[:table_name], table_data[:columns]))
21
+ end.join
22
+ end
23
+
24
+ def columns_placeholder(table_name, columns)
25
+ columns.map do |column_data|
26
+ column_template
27
+ .gsub('%column_name%', "#{table_name}.#{column_data[:column_name]}")
28
+ .gsub('%column_comment%', column_data[:column_comment].presence || '')
29
+ end.join
30
+ end
31
+
32
+ def database_comments
33
+ adapter.comments_for_database
34
+ end
35
+
36
+ def main_template
37
+ raise NotImplementedError
38
+ end
39
+
40
+ def table_template
41
+ raise NotImplementedError
42
+ end
43
+
44
+ def column_template
45
+ raise NotImplementedError
46
+ end
47
+
48
+ def template_format
49
+ raise NotImplementedError
50
+ end
51
+
52
+ def adapter
53
+ Commento.adapter
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Commento
4
+ module Reports
5
+ class Html < Commento::Report
6
+ private
7
+
8
+ def main_template
9
+ [
10
+ '<!DOCTYPE html>',
11
+ '<html>',
12
+ ' <head>',
13
+ " #{styles}",
14
+ ' <meta charset="utf-8" />',
15
+ ' </head>',
16
+ ' <body>',
17
+ '%tables_placeholder%',
18
+ ' </body>',
19
+ '</html>'
20
+ ].join("\n")
21
+ end
22
+
23
+ def table_template
24
+ [
25
+ " <div class='table'>",
26
+ " <div class='table-header'>",
27
+ ' <h3>%table_name%</h3>',
28
+ ' <p>%table_comment%</p>',
29
+ ' </div>',
30
+ " <div class='table-body'>",
31
+ '%columns_placeholder%',
32
+ ' </div>',
33
+ " </div>\n"
34
+ ].join("\n")
35
+ end
36
+
37
+ def column_template
38
+ " <p>%column_name%: %column_comment%</p>\n"
39
+ end
40
+
41
+ def template_format
42
+ 'html'
43
+ end
44
+
45
+ def styles
46
+ [
47
+ '<style>',
48
+ 'h3 { margin: 0 0 .25rem }',
49
+ 'p { margin: 0 0 .5rem }',
50
+ '.table { padding: 0 .5rem; margin-bottom: 1rem }',
51
+ '.table-header { border-bottom: 1px solid #ddd; margin-bottom: .5rem }',
52
+ '.table-header h3 { padding: .25rem; background: #bbb }',
53
+ '.table-header p { padding: .25rem; margin: 0 }',
54
+ '.table-body { padding: 0 .25rem }',
55
+ '</style>'
56
+ ].join
57
+ end
58
+ end
59
+ end
60
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Commento
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
data/lib/commento.rb CHANGED
@@ -6,17 +6,19 @@ require 'commento/version'
6
6
  require 'commento/configuration'
7
7
  require 'commento/dsl'
8
8
  require 'commento/helpers'
9
+ require 'commento/report'
10
+ require 'commento/reports/html'
9
11
 
10
12
  module Commento
11
13
  extend self
12
14
  extend Forwardable
13
15
 
14
- # Public: Given an adapter returns a handy DSL to all the commentp goodness.
16
+ # Public: Given an adapter returns a handy DSL to all the commento goodness.
15
17
  def new(adapter)
16
18
  DSL.new(adapter)
17
19
  end
18
20
 
19
- # Public: Configure commentp.
21
+ # Public: Configure commento.
20
22
  #
21
23
  # Commento.configure do |config|
22
24
  # config.adapter = Commento::Adapters::ActiveRecord.new
@@ -31,7 +33,7 @@ module Commento
31
33
  @configuration ||= Configuration.new
32
34
  end
33
35
 
34
- # Public: Default per thread commentp instance if configured.
36
+ # Public: Default per thread commento instance if configured.
35
37
  # Returns Commento::DSL instance.
36
38
  def instance
37
39
  Thread.current[:commento_instance] ||= new(configuration.adapter)
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.0
4
+ version: 0.1.1
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-01 00:00:00.000000000 Z
11
+ date: 2023-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -80,6 +80,8 @@ files:
80
80
  - lib/commento/configuration.rb
81
81
  - lib/commento/dsl.rb
82
82
  - lib/commento/helpers.rb
83
+ - lib/commento/report.rb
84
+ - lib/commento/reports/html.rb
83
85
  - lib/commento/version.rb
84
86
  homepage: https://github.com/kortirso/commento
85
87
  licenses: