annotaterb 4.3.1 → 4.4.1

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: 638be873328bb99a7c5b8196ee15749e0c40e36fc3660204ceec28af42678f09
4
- data.tar.gz: 0e88670fc1257d20e6282aaf4f510d8b2be29829983dce6c63bcfc4078deb996
3
+ metadata.gz: a337f543f32142efaffbfd396196692d42a98438352b3bdd406c104c25a9958a
4
+ data.tar.gz: 27d091e7fed5953ccdd26c4b4874e45a0fcff541630595fa467e010629efb9ba
5
5
  SHA512:
6
- metadata.gz: 2142be1d47c96ac81a3ab97cfbb6d852ea952be8a179785b5cf48eadf9eec3e9ceff3588353a2d3efa6efeed071589664776948b96eda8e1df5cd4dd7335d0c6
7
- data.tar.gz: e6cc12cc6d7110283ba23243cc27f355f594d71fc2f5ce5ee50f535f84bfb2e8e6a440546e86a4e2ffa1f300afd819321bb81968e7a235486605bd219c5284bb
6
+ metadata.gz: 44125b3f65246ad1b5a0dd0d354487462ea6584c6147165650268244fd8d3c9826cf337873f24c31e008efa1c560b37bcb71e36af7fc05c9bc381d48e2b1e4b0
7
+ data.tar.gz: 7979e2309e3c09ee11fc090a424e7006a94634c7a970fe34b61028b3a90d495b684cc2a4a6fa78221b288dfe5e6682567835fb6839ada3903dfaa599a4f8cfdc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # Changelog
2
2
 
3
+ ## [v4.4.0](https://github.com/drwl/annotaterb/tree/v4.4.0) (2023-06-24)
4
+
5
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.3.1...v4.4.0)
6
+
7
+ **Merged pull requests:**
8
+
9
+ - Bump version to v4.4.0 [\#52](https://github.com/drwl/annotaterb/pull/52) ([drwl](https://github.com/drwl))
10
+ - Flatten tests in `annotation_builder_spec.rb` [\#51](https://github.com/drwl/annotaterb/pull/51) ([drwl](https://github.com/drwl))
11
+ - Add support for table comments [\#50](https://github.com/drwl/annotaterb/pull/50) ([drwl](https://github.com/drwl))
12
+ - Improve some model annotator tests [\#49](https://github.com/drwl/annotaterb/pull/49) ([drwl](https://github.com/drwl))
13
+ - Make tests that use `mock_column` more accurate [\#48](https://github.com/drwl/annotaterb/pull/48) ([drwl](https://github.com/drwl))
14
+ - Generate changelog for v4.3.1 [\#47](https://github.com/drwl/annotaterb/pull/47) ([drwl](https://github.com/drwl))
15
+
16
+ ## [v4.3.1](https://github.com/drwl/annotaterb/tree/v4.3.1) (2023-06-15)
17
+
18
+ [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.3.0...v4.3.1)
19
+
20
+ **Closed issues:**
21
+
22
+ - Column defaults change in migration [\#45](https://github.com/drwl/annotaterb/issues/45)
23
+
24
+ **Merged pull requests:**
25
+
26
+ - Bump version to v4.3.1 [\#46](https://github.com/drwl/annotaterb/pull/46) ([drwl](https://github.com/drwl))
27
+ - Prettify column defaults [\#44](https://github.com/drwl/annotaterb/pull/44) ([drwl](https://github.com/drwl))
28
+ - Generate changelog for v4.3.0 [\#42](https://github.com/drwl/annotaterb/pull/42) ([drwl](https://github.com/drwl))
29
+
3
30
  ## [v4.3.0](https://github.com/drwl/annotaterb/tree/v4.3.0) (2023-06-10)
4
31
 
5
32
  [Full Changelog](https://github.com/drwl/annotaterb/compare/v4.2.0...v4.3.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.3.1
1
+ 4.4.1
data/exe/annotaterb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- unless File.exist?("./Rakefile") || File.exist?("./Gemfile")
4
+ if !File.exist?("./Rakefile") && !File.exist?("./Gemfile")
5
5
  abort "Please run annotaterb from the root of the project."
6
6
  end
7
7
 
@@ -12,14 +12,14 @@ module AnnotateRb
12
12
  MD_NAMES_OVERHEAD = 6
13
13
  MD_TYPE_ALLOWANCE = 18
14
14
 
15
- def initialize(klass, options = {})
15
+ def initialize(klass, options)
16
16
  @model = ModelWrapper.new(klass, options)
17
17
  @options = options
18
18
  @info = "" # TODO: Make array and build string that way
19
19
  end
20
20
 
21
21
  def build
22
- @info = "# #{header}\n"
22
+ @info = "#{header}\n"
23
23
  @info += schema_header_text
24
24
 
25
25
  max_size = @model.max_schema_info_width
@@ -51,6 +51,7 @@ module AnnotateRb
51
51
 
52
52
  def header
53
53
  header = @options[:format_markdown] ? PREFIX_MD.dup : PREFIX.dup
54
+ header = "# #{header}"
54
55
  version = begin
55
56
  ActiveRecord::Migrator.current_version
56
57
  rescue
@@ -69,11 +70,11 @@ module AnnotateRb
69
70
  info << "#"
70
71
 
71
72
  if @options[:format_markdown]
72
- info << "# Table name: `#{@model.table_name}`"
73
+ info << "# Table name: `#{table_name}`"
73
74
  info << "#"
74
75
  info << "# ### Columns"
75
76
  else
76
- info << "# Table name: #{@model.table_name}"
77
+ info << "# Table name: #{table_name}"
77
78
  end
78
79
  info << "#\n" # We want the last line break
79
80
 
@@ -93,6 +94,20 @@ module AnnotateRb
93
94
 
94
95
  info.join("\n")
95
96
  end
97
+
98
+ private
99
+
100
+ def table_name
101
+ table_name = @model.table_name
102
+ display_table_comments = @options[:with_comment] && @options[:with_table_comments]
103
+
104
+ if display_table_comments && @model.has_table_comments?
105
+ table_comment = "(#{@model.table_comments.gsub(/\n/, "\\n")})"
106
+ table_name = "#{table_name}#{table_comment}"
107
+ end
108
+
109
+ table_name
110
+ end
96
111
  end
97
112
  end
98
113
  end
@@ -26,7 +26,8 @@ module AnnotateRb
26
26
  column_attributes = AttributesBuilder.new(@column, @options, is_primary_key, column_indices, column_defaults).build
27
27
  formatted_column_type = TypeBuilder.new(@column, @options, column_defaults).build
28
28
 
29
- col_name = if @model.with_comments? && @column.comment
29
+ display_column_comments = @options[:with_comment] && @options[:with_column_comments]
30
+ col_name = if display_column_comments && @model.with_comments? && @column.comment
30
31
  "#{@column.name}(#{@column.comment.gsub(/\n/, '\\n')})"
31
32
  else
32
33
  @column.name
@@ -88,17 +88,7 @@ module AnnotateRb
88
88
 
89
89
  # Simple quoting for the default column value
90
90
  def quote(value)
91
- case value
92
- when NilClass then "NULL"
93
- when TrueClass then "TRUE"
94
- when FalseClass then "FALSE"
95
- when Float, Integer then value.to_s
96
- # BigDecimals need to be output in a non-normalized form and quoted.
97
- when BigDecimal then value.to_s("F")
98
- when Array then value.map { |v| quote(v) }
99
- else
100
- value.inspect
101
- end
91
+ DefaultValueBuilder.new(value).build
102
92
  end
103
93
  end
104
94
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ module ColumnAnnotation
6
+ class DefaultValueBuilder
7
+ def initialize(value)
8
+ @value = value
9
+ end
10
+
11
+ # @return [String]
12
+ # Returns the value to get written to file by file.puts. Strings get written to file so escaped quoted strings
13
+ # get written as quoted. For example, if `value: "\"some_string\""` then "some_string" gets written.
14
+ # Same with arrays, if `value: "[\"a\", \"b\", \"c\"]"` then `["a", "b", "c"]` gets written.
15
+ #
16
+ # @example "\"some_string\""
17
+ # @example "NULL"
18
+ # @example "1.2"
19
+ def build
20
+ if @value.is_a?(Array)
21
+ quote_array(@value)
22
+ else
23
+ quote(@value)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def quote(value)
30
+ case value
31
+ when NilClass then "NULL"
32
+ when TrueClass then "TRUE"
33
+ when FalseClass then "FALSE"
34
+ when Float, Integer then value.to_s
35
+ # BigDecimals need to be output in a non-normalized form and quoted.
36
+ when BigDecimal then value.to_s("F")
37
+ when String then value.inspect
38
+ else
39
+ value.inspect
40
+ end
41
+ end
42
+
43
+ def quote_array(value)
44
+ content = value.map { |v| quote(v) }.join(", ")
45
+ "[" + content + "]"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -7,6 +7,7 @@ module AnnotateRb
7
7
  autoload :TypeBuilder, "annotate_rb/model_annotator/column_annotation/type_builder"
8
8
  autoload :ColumnWrapper, "annotate_rb/model_annotator/column_annotation/column_wrapper"
9
9
  autoload :AnnotationBuilder, "annotate_rb/model_annotator/column_annotation/annotation_builder"
10
+ autoload :DefaultValueBuilder, "annotate_rb/model_annotator/column_annotation/default_value_builder"
10
11
  end
11
12
  end
12
13
  end
@@ -4,13 +4,12 @@ module AnnotateRb
4
4
  module ModelAnnotator
5
5
  # Generates the text file content with annotations, these are then to be written to filesystem.
6
6
  class FileBuilder
7
- def initialize(file_components, new_annotations, annotation_position, options)
7
+ def initialize(file_components, annotation_position, options)
8
8
  @file_components = file_components
9
- @new_annotations = new_annotations
10
9
  @annotation_position = annotation_position
11
10
  @options = options
12
11
 
13
- @new_wrapped_annotations = wrapped_content(new_annotations)
12
+ @new_wrapped_annotations = wrapped_content(@file_components.new_annotations)
14
13
  end
15
14
 
16
15
  def generate_content_with_new_annotations
@@ -6,11 +6,14 @@ module AnnotateRb
6
6
  SKIP_ANNOTATION_STRING = "# -*- SkipSchemaAnnotations"
7
7
  SOME_PATTERN = /\A(?<start>\s*).*?\n(?<end>\s*)\z/m # Unsure what this pattern is
8
8
 
9
+ attr_reader :new_annotations
10
+
9
11
  def initialize(file_content, new_annotations, options)
10
12
  @file_content = file_content
11
13
  @diff = AnnotationDiffGenerator.new(file_content, new_annotations).generate
12
14
  @options = options
13
15
  @annotation_pattern = AnnotationPatternGenerator.call(options)
16
+ @new_annotations = new_annotations
14
17
  end
15
18
 
16
19
  def current_file_content
@@ -49,6 +49,15 @@ module AnnotateRb
49
49
  @klass.table_exists?
50
50
  end
51
51
 
52
+ def table_comments
53
+ @klass.connection.table_comment(@klass.table_name)
54
+ end
55
+
56
+ def has_table_comments?
57
+ @klass.connection.respond_to?(:table_comment) &&
58
+ @klass.connection.table_comment(@klass.table_name).present?
59
+ end
60
+
52
61
  def column_defaults
53
62
  @klass.column_defaults
54
63
  end
@@ -110,8 +119,7 @@ module AnnotateRb
110
119
  end
111
120
 
112
121
  def with_comments?
113
- @with_comments ||= @options[:with_comment] &&
114
- raw_columns.first.respond_to?(:comment) &&
122
+ @with_comments ||= raw_columns.first.respond_to?(:comment) &&
115
123
  raw_columns.map(&:comment).any? { |comment| !comment.nil? }
116
124
  end
117
125
 
@@ -26,7 +26,7 @@ module AnnotateRb
26
26
  old_content = File.read(file_name)
27
27
 
28
28
  file_components = FileComponents.new(old_content, annotation, options)
29
- builder = FileBuilder.new(file_components, annotation, annotation_position, options)
29
+ builder = FileBuilder.new(file_components, annotation_position, options)
30
30
 
31
31
  return false if file_components.has_skip_string?
32
32
  return false if !file_components.annotations_changed? && !options[:force]
@@ -51,7 +51,9 @@ module AnnotateRb
51
51
  sort: false, # ModelAnnotator
52
52
  timestamp: false, # RouteAnnotator
53
53
  trace: false, # ModelAnnotator, but is part of Core
54
- with_comment: true # ModelAnnotator
54
+ with_comment: true, # ModelAnnotator
55
+ with_column_comments: nil, # ModelAnnotator
56
+ with_table_comments: nil # ModelAnnotator
55
57
  }.freeze
56
58
 
57
59
  OTHER_OPTIONS = {
@@ -113,7 +115,9 @@ module AnnotateRb
113
115
  :sort,
114
116
  :timestamp,
115
117
  :trace,
116
- :with_comment
118
+ :with_comment,
119
+ :with_column_comments,
120
+ :with_table_comments
117
121
  ].freeze
118
122
 
119
123
  OTHER_OPTION_KEYS = [
@@ -187,6 +191,10 @@ module AnnotateRb
187
191
  @options[:wrapper_open] ||= @options[:wrapper]
188
192
  @options[:wrapper_close] ||= @options[:wrapper]
189
193
 
194
+ # Set column and table comments to default to :with_comment, if not set
195
+ @options[:with_column_comments] = @options[:with_comment] if @options[:with_column_comments].nil?
196
+ @options[:with_table_comments] = @options[:with_comment] if @options[:with_table_comments].nil?
197
+
190
198
  self
191
199
  end
192
200
 
@@ -217,6 +217,31 @@ module AnnotateRb
217
217
  "include database comments in model annotations") do
218
218
  @options[:with_comment] = true
219
219
  end
220
+
221
+ option_parser.on("--without-comment",
222
+ "include database comments in model annotations") do
223
+ @options[:with_comment] = false
224
+ end
225
+
226
+ option_parser.on("--with-column-comments",
227
+ "include column comments in model annotations") do
228
+ @options[:with_column_comments] = true
229
+ end
230
+
231
+ option_parser.on("--without-column-comments",
232
+ "exclude column comments in model annotations") do
233
+ @options[:with_column_comments] = false
234
+ end
235
+
236
+ option_parser.on("--with-table-comments",
237
+ "include table comments in model annotations") do
238
+ @options[:with_table_comments] = true
239
+ end
240
+
241
+ option_parser.on("--without-table-comments",
242
+ "exclude table comments in model annotations") do
243
+ @options[:with_table_comments] = false
244
+ end
220
245
  end
221
246
 
222
247
  def add_route_options_to_parser(option_parser)
data/lib/annotate_rb.rb CHANGED
@@ -10,8 +10,6 @@ require "active_support/core_ext/string/inflections"
10
10
 
11
11
  require "rake"
12
12
 
13
- require "annotate_rb/active_record_patch"
14
-
15
13
  require_relative "annotate_rb/helper"
16
14
  require_relative "annotate_rb/core"
17
15
  require_relative "annotate_rb/commands"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annotaterb
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.1
4
+ version: 4.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew W. Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-15 00:00:00.000000000 Z
11
+ date: 2023-09-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Annotates Rails/ActiveRecord Models, routes, fixtures, and others based
14
14
  on the database schema.
@@ -25,7 +25,6 @@ files:
25
25
  - VERSION
26
26
  - exe/annotaterb
27
27
  - lib/annotate_rb.rb
28
- - lib/annotate_rb/active_record_patch.rb
29
28
  - lib/annotate_rb/commands.rb
30
29
  - lib/annotate_rb/commands/annotate_models.rb
31
30
  - lib/annotate_rb/commands/annotate_routes.rb
@@ -48,6 +47,7 @@ files:
48
47
  - lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb
49
48
  - lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb
50
49
  - lib/annotate_rb/model_annotator/column_annotation/column_wrapper.rb
50
+ - lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb
51
51
  - lib/annotate_rb/model_annotator/column_annotation/type_builder.rb
52
52
  - lib/annotate_rb/model_annotator/file_builder.rb
53
53
  - lib/annotate_rb/model_annotator/file_components.rb
@@ -1,11 +0,0 @@
1
- # monkey patches
2
-
3
- # standard:disable Style/MissingRespondToMissing
4
- module ::ActiveRecord
5
- class Base
6
- def self.method_missing(_name, *_args)
7
- # ignore this, so unknown/unloaded macros won't cause parsing to fail
8
- end
9
- end
10
- end
11
- # standard:enable Style/MissingRespondToMissing