annotaterb 4.1.0 → 4.1.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: 1c77ca5a809b63c94e69b937732b8f8f531807cc332627791b649576e8ed583e
4
- data.tar.gz: c2fb3ac84bad2e0cd108c801ca162a6a17b34ba800af8f5e885e03c8338d636d
3
+ metadata.gz: 8888511eb71e6d0c45eadf9f562767868adf7cb9f2c5c58d6eb3458a65dfdc31
4
+ data.tar.gz: 51d25d56e5068979a09d4562e7018b0272f8f73e952941a4f66faece7408cc11
5
5
  SHA512:
6
- metadata.gz: fb88fa8b511e0ccbbadce0db7d0ae941234967eb7358fea1146e48309939b1b35e51f8db9f15d544a6af4b3827decb65e51f4546d73feb2ab2f404c67f3c0f05
7
- data.tar.gz: 562cd430feba785e8f53ab90f79dfa1c3765f46a8e048bbe379955c8f696dd4e01ce167245cf17e31de4704dc0e22da859cba2f6f00c87d10782e637c2faad65
6
+ metadata.gz: 9b878bfc48836e52fe762c547870b67619427772e54efd28835e8699371dc60fdb5511649d9f2bf39431541a9b07f8e32188469526dd109553f71badedd6ec45
7
+ data.tar.gz: df69b71c4c10cc046ebd6cf0a21391262b90e0bcffe19556b59214d7658623fa323a7adbf24e711726bb685377e47596d9b95fed9c2425f22f4877264eed97d8
data/README.md CHANGED
@@ -60,10 +60,7 @@ $ bin/rails g annotate_rb:install
60
60
  This will copy a rake task into your Rails project's `lib/tasks` directory that will hook into the Rails project rake tasks, automatically running AnnotateRb after database migration rake tasks.
61
61
 
62
62
  ## Migrating from the annotate gem
63
- The old [annotate gem](https://github.com/ctran/annotate_models) relied on environment variables and hardcoded values in rake files for configuration. AnnotateRb is different. It reads from an optional configuration yml file and options from the CLI to function.
64
-
65
- * Remove the following files `lib/tasks/annotate_models.rake`, `lib/tasks/annotate_models_migrate.rake`, `lib/tasks/annotate_routes.rake`.
66
- * Run the generator install command above.
63
+ Refer to the [migration guide](MIGRATION_GUIDE.md).
67
64
 
68
65
  ## Usage
69
66
 
@@ -160,7 +157,9 @@ Previously in the [Annotate](https://github.com/ctran/annotate_models) you could
160
157
  position: after
161
158
  ```
162
159
 
163
- Annotaterb reads first from the configuration file, if it exists, then merges it with any options passed into the CLI.
160
+ Annotaterb reads first from the configuration file, if it exists, then merges it with any options passed into the CLI.
161
+
162
+ For further details visit the [section in the migration guide](MIGRATION_GUIDE.md#automatic-annotations-after-running-database-migration-commands).
164
163
 
165
164
  ### How to skip annotating a particular model
166
165
  If you want to always skip annotations on a particular model, add this string
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.1.0
1
+ 4.1.1
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ # Plain old Ruby object for holding the differences
6
+ class AnnotationDiff
7
+ attr_reader :current_columns, :new_columns
8
+
9
+ def initialize(current_columns, new_columns)
10
+ @current_columns = current_columns.dup.freeze
11
+ @new_columns = new_columns.dup.freeze
12
+ end
13
+
14
+ def changed?
15
+ @changed ||= @current_columns != @new_columns
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AnnotateRb
4
+ module ModelAnnotator
5
+ # Compares the current file content and new annotation block and generates the column annotation differences
6
+ class AnnotationDiffGenerator
7
+ HEADER_PATTERN = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/.freeze
8
+ COLUMN_PATTERN = /^#[\t ]+[\w\*\.`\[\]():]+[\t ]+.+$/.freeze
9
+
10
+ class << self
11
+ def call(file_content, annotation_block)
12
+ new(file_content, annotation_block).generate
13
+ end
14
+ end
15
+
16
+ # @param [String] file_content The current file content of the model file we intend to annotate
17
+ # @param [String] annotation_block The annotation block we intend to write to the model file
18
+ def initialize(file_content, annotation_block)
19
+ @file_content = file_content
20
+ @annotation_block = annotation_block
21
+ end
22
+
23
+ def generate
24
+ # Ignore the Schema version line because it changes with each migration
25
+ current_annotations = @file_content.match(HEADER_PATTERN).to_s
26
+ new_annotations = @annotation_block.match(HEADER_PATTERN).to_s
27
+
28
+ if current_annotations.present?
29
+ current_annotation_columns = current_annotations.scan(COLUMN_PATTERN).sort
30
+ else
31
+ current_annotation_columns = []
32
+ end
33
+
34
+ if new_annotations.present?
35
+ new_annotation_columns = new_annotations.scan(COLUMN_PATTERN).sort
36
+ else
37
+ new_annotation_columns = []
38
+ end
39
+
40
+ _result = AnnotationDiff.new(current_annotation_columns, new_annotation_columns)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -24,18 +24,12 @@ module AnnotateRb
24
24
  def call(file_name, info_block, position, options = {})
25
25
  return false unless File.exist?(file_name)
26
26
  old_content = File.read(file_name)
27
- return false if old_content =~ /#{Constants::SKIP_ANNOTATION_PREFIX}.*\n/
28
27
 
29
- # Ignore the Schema version line because it changes with each migration
30
- header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
31
- old_header = old_content.match(header_pattern).to_s
32
- new_header = info_block.match(header_pattern).to_s
28
+ return false if old_content =~ /#{Constants::SKIP_ANNOTATION_PREFIX}.*\n/
33
29
 
34
- column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
35
- old_columns = old_header && old_header.scan(column_pattern).sort
36
- new_columns = new_header && new_header.scan(column_pattern).sort
30
+ diff = AnnotationDiffGenerator.new(old_content, info_block).generate
37
31
 
38
- return false if old_columns == new_columns && !options[:force]
32
+ return false if !diff.changed? && !options[:force]
39
33
 
40
34
  abort "AnnotateRb error. #{file_name} needs to be updated, but annotaterb was run with `--frozen`." if options[:frozen]
41
35
 
@@ -26,5 +26,7 @@ module AnnotateRb
26
26
  autoload :RelatedFilesListBuilder, 'annotate_rb/model_annotator/related_files_list_builder'
27
27
  autoload :AnnotationDecider, 'annotate_rb/model_annotator/annotation_decider'
28
28
  autoload :FileAnnotatorInstruction, 'annotate_rb/model_annotator/file_annotator_instruction'
29
+ autoload :AnnotationDiffGenerator, 'annotate_rb/model_annotator/annotation_diff_generator'
30
+ autoload :AnnotationDiff, 'annotate_rb/model_annotator/annotation_diff'
29
31
  end
30
32
  end
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.1.0
4
+ version: 4.1.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-05-17 00:00:00.000000000 Z
11
+ date: 2023-05-20 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.
@@ -37,6 +37,8 @@ files:
37
37
  - lib/annotate_rb/eager_loader.rb
38
38
  - lib/annotate_rb/model_annotator.rb
39
39
  - lib/annotate_rb/model_annotator/annotation_decider.rb
40
+ - lib/annotate_rb/model_annotator/annotation_diff.rb
41
+ - lib/annotate_rb/model_annotator/annotation_diff_generator.rb
40
42
  - lib/annotate_rb/model_annotator/annotation_generator.rb
41
43
  - lib/annotate_rb/model_annotator/annotation_pattern_generator.rb
42
44
  - lib/annotate_rb/model_annotator/annotator.rb