annotato 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 003a8311576a66ee46f22d1b23d586d44225e0f162071a241cec1374ce80249f
4
+ data.tar.gz: a714326713db198ec79ca4df5f1f81d9157e859fc9e6822939d72251c9f05a83
5
+ SHA512:
6
+ metadata.gz: 8db71510d95ad8bf436688fe1c64292dda80dff2bda4b6580b97e0ecb65c6a47804c703e457326495b5ea83e6bb75828d6f6d32a0affda9fdc74a7353ce08d54
7
+ data.tar.gz: 388ffb2c750400a6b59add8be79a216e5f6ac0fec1819c96a7b10febb3e248abe06c43d55cd929027ef77459462eaaf99b6414fd9999822f8e6d6f3c2f088b54
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-05-16
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in annotato.gemspec
6
+ gemspec
7
+
8
+ gem "irb"
9
+ gem "rake", "~> 13.0"
10
+
11
+ gem "rspec", "~> 3.0"
12
+
13
+ group :development, :test do
14
+ gem "simplecov", require: false
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,222 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ annotato (0.1.0)
5
+ rails (>= 6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (8.0.2)
11
+ actionpack (= 8.0.2)
12
+ activesupport (= 8.0.2)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ zeitwerk (~> 2.6)
16
+ actionmailbox (8.0.2)
17
+ actionpack (= 8.0.2)
18
+ activejob (= 8.0.2)
19
+ activerecord (= 8.0.2)
20
+ activestorage (= 8.0.2)
21
+ activesupport (= 8.0.2)
22
+ mail (>= 2.8.0)
23
+ actionmailer (8.0.2)
24
+ actionpack (= 8.0.2)
25
+ actionview (= 8.0.2)
26
+ activejob (= 8.0.2)
27
+ activesupport (= 8.0.2)
28
+ mail (>= 2.8.0)
29
+ rails-dom-testing (~> 2.2)
30
+ actionpack (8.0.2)
31
+ actionview (= 8.0.2)
32
+ activesupport (= 8.0.2)
33
+ nokogiri (>= 1.8.5)
34
+ rack (>= 2.2.4)
35
+ rack-session (>= 1.0.1)
36
+ rack-test (>= 0.6.3)
37
+ rails-dom-testing (~> 2.2)
38
+ rails-html-sanitizer (~> 1.6)
39
+ useragent (~> 0.16)
40
+ actiontext (8.0.2)
41
+ actionpack (= 8.0.2)
42
+ activerecord (= 8.0.2)
43
+ activestorage (= 8.0.2)
44
+ activesupport (= 8.0.2)
45
+ globalid (>= 0.6.0)
46
+ nokogiri (>= 1.8.5)
47
+ actionview (8.0.2)
48
+ activesupport (= 8.0.2)
49
+ builder (~> 3.1)
50
+ erubi (~> 1.11)
51
+ rails-dom-testing (~> 2.2)
52
+ rails-html-sanitizer (~> 1.6)
53
+ activejob (8.0.2)
54
+ activesupport (= 8.0.2)
55
+ globalid (>= 0.3.6)
56
+ activemodel (8.0.2)
57
+ activesupport (= 8.0.2)
58
+ activerecord (8.0.2)
59
+ activemodel (= 8.0.2)
60
+ activesupport (= 8.0.2)
61
+ timeout (>= 0.4.0)
62
+ activestorage (8.0.2)
63
+ actionpack (= 8.0.2)
64
+ activejob (= 8.0.2)
65
+ activerecord (= 8.0.2)
66
+ activesupport (= 8.0.2)
67
+ marcel (~> 1.0)
68
+ activesupport (8.0.2)
69
+ base64
70
+ benchmark (>= 0.3)
71
+ bigdecimal
72
+ concurrent-ruby (~> 1.0, >= 1.3.1)
73
+ connection_pool (>= 2.2.5)
74
+ drb
75
+ i18n (>= 1.6, < 2)
76
+ logger (>= 1.4.2)
77
+ minitest (>= 5.1)
78
+ securerandom (>= 0.3)
79
+ tzinfo (~> 2.0, >= 2.0.5)
80
+ uri (>= 0.13.1)
81
+ base64 (0.2.0)
82
+ benchmark (0.4.0)
83
+ bigdecimal (3.1.9)
84
+ builder (3.3.0)
85
+ concurrent-ruby (1.3.5)
86
+ connection_pool (2.5.3)
87
+ crass (1.0.6)
88
+ date (3.4.1)
89
+ diff-lcs (1.6.1)
90
+ docile (1.4.1)
91
+ drb (2.2.1)
92
+ erubi (1.13.1)
93
+ globalid (1.2.1)
94
+ activesupport (>= 6.1)
95
+ i18n (1.14.7)
96
+ concurrent-ruby (~> 1.0)
97
+ io-console (0.8.0)
98
+ irb (1.15.2)
99
+ pp (>= 0.6.0)
100
+ rdoc (>= 4.0.0)
101
+ reline (>= 0.4.2)
102
+ logger (1.7.0)
103
+ loofah (2.24.0)
104
+ crass (~> 1.0.2)
105
+ nokogiri (>= 1.12.0)
106
+ mail (2.8.1)
107
+ mini_mime (>= 0.1.1)
108
+ net-imap
109
+ net-pop
110
+ net-smtp
111
+ marcel (1.0.4)
112
+ mini_mime (1.1.5)
113
+ minitest (5.25.5)
114
+ net-imap (0.5.8)
115
+ date
116
+ net-protocol
117
+ net-pop (0.1.2)
118
+ net-protocol
119
+ net-protocol (0.2.2)
120
+ timeout
121
+ net-smtp (0.5.1)
122
+ net-protocol
123
+ nio4r (2.7.4)
124
+ nokogiri (1.18.8-arm64-darwin)
125
+ racc (~> 1.4)
126
+ nokogiri (1.18.8-x86_64-linux-gnu)
127
+ racc (~> 1.4)
128
+ pp (0.6.2)
129
+ prettyprint
130
+ prettyprint (0.2.0)
131
+ psych (5.2.5)
132
+ date
133
+ stringio
134
+ racc (1.8.1)
135
+ rack (3.1.13)
136
+ rack-session (2.1.0)
137
+ base64 (>= 0.1.0)
138
+ rack (>= 3.0.0)
139
+ rack-test (2.2.0)
140
+ rack (>= 1.3)
141
+ rackup (2.2.1)
142
+ rack (>= 3)
143
+ rails (8.0.2)
144
+ actioncable (= 8.0.2)
145
+ actionmailbox (= 8.0.2)
146
+ actionmailer (= 8.0.2)
147
+ actionpack (= 8.0.2)
148
+ actiontext (= 8.0.2)
149
+ actionview (= 8.0.2)
150
+ activejob (= 8.0.2)
151
+ activemodel (= 8.0.2)
152
+ activerecord (= 8.0.2)
153
+ activestorage (= 8.0.2)
154
+ activesupport (= 8.0.2)
155
+ bundler (>= 1.15.0)
156
+ railties (= 8.0.2)
157
+ rails-dom-testing (2.2.0)
158
+ activesupport (>= 5.0.0)
159
+ minitest
160
+ nokogiri (>= 1.6)
161
+ rails-html-sanitizer (1.6.2)
162
+ loofah (~> 2.21)
163
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
164
+ railties (8.0.2)
165
+ actionpack (= 8.0.2)
166
+ activesupport (= 8.0.2)
167
+ irb (~> 1.13)
168
+ rackup (>= 1.0.0)
169
+ rake (>= 12.2)
170
+ thor (~> 1.0, >= 1.2.2)
171
+ zeitwerk (~> 2.6)
172
+ rake (13.2.1)
173
+ rdoc (6.13.1)
174
+ psych (>= 4.0.0)
175
+ reline (0.6.1)
176
+ io-console (~> 0.5)
177
+ rspec (3.13.0)
178
+ rspec-core (~> 3.13.0)
179
+ rspec-expectations (~> 3.13.0)
180
+ rspec-mocks (~> 3.13.0)
181
+ rspec-core (3.13.3)
182
+ rspec-support (~> 3.13.0)
183
+ rspec-expectations (3.13.3)
184
+ diff-lcs (>= 1.2.0, < 2.0)
185
+ rspec-support (~> 3.13.0)
186
+ rspec-mocks (3.13.2)
187
+ diff-lcs (>= 1.2.0, < 2.0)
188
+ rspec-support (~> 3.13.0)
189
+ rspec-support (3.13.2)
190
+ securerandom (0.4.1)
191
+ simplecov (0.22.0)
192
+ docile (~> 1.1)
193
+ simplecov-html (~> 0.11)
194
+ simplecov_json_formatter (~> 0.1)
195
+ simplecov-html (0.13.1)
196
+ simplecov_json_formatter (0.1.4)
197
+ stringio (3.1.7)
198
+ thor (1.3.2)
199
+ timeout (0.4.3)
200
+ tzinfo (2.0.6)
201
+ concurrent-ruby (~> 1.0)
202
+ uri (1.0.3)
203
+ useragent (0.16.11)
204
+ websocket-driver (0.7.7)
205
+ base64
206
+ websocket-extensions (>= 0.1.0)
207
+ websocket-extensions (0.1.5)
208
+ zeitwerk (2.7.2)
209
+
210
+ PLATFORMS
211
+ arm64-darwin-24
212
+ x86_64-linux
213
+
214
+ DEPENDENCIES
215
+ annotato!
216
+ irb
217
+ rake (~> 13.0)
218
+ rspec (~> 3.0)
219
+ simplecov
220
+
221
+ BUNDLED WITH
222
+ 2.6.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Serhii Bodnaruk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Annotato
2
+
3
+ > Automatically adds schema comments (columns, indexes, triggers, enums) to Rails models.
4
+
5
+ ![CI](https://github.com/boserh/annotato/actions/workflows/ci.yml/badge.svg)
6
+ [![Gem Version](https://badge.fury.io/rb/annotato.svg)](https://badge.fury.io/rb/annotato)
7
+
8
+ ---
9
+
10
+ ## Features
11
+
12
+ - Annotates Rails models with:
13
+ - Columns and types
14
+ - Default values and constraints
15
+ - Enums with values
16
+ - Indexes (only if present)
17
+ - Triggers (only if present)
18
+ - Skips unchanged annotations
19
+ - Replaces legacy `annotate`/`annotate_models` blocks
20
+ - Smart formatting with aligned columns
21
+
22
+ Example:
23
+
24
+ ```ruby
25
+ # == Annotato Schema Info
26
+ # Table: users
27
+ #
28
+ # Columns:
29
+ # id :bigint not null, primary key
30
+ # email :string not null, unique
31
+ # role :string default("user"), not null
32
+ #
33
+ # Enums:
34
+ # role: { user, admin }
35
+
36
+ class User < ApplicationRecord
37
+ end
38
+ ```
39
+
40
+ ---
41
+
42
+ ## Installation
43
+
44
+ Add this line to your Gemfile:
45
+
46
+ ```ruby
47
+ gem "annotato"
48
+ ```
49
+
50
+ Then run:
51
+
52
+ ```bash
53
+ bundle install
54
+ ```
55
+
56
+ ---
57
+
58
+ ## Usage
59
+
60
+ To annotate all models:
61
+
62
+ ```bash
63
+ bundle exec rake annotato:models
64
+ ```
65
+
66
+ ---
67
+
68
+ ## Rake Task
69
+
70
+ ```bash
71
+ rake annotato:models
72
+ ```
73
+
74
+ - Automatically loads all models via `Rails.application.eager_load!`
75
+ - Modifies model files in place
76
+ - Existing Annotato and Annotate blocks will be replaced
77
+
78
+ ---
79
+
80
+ ## Development
81
+
82
+ To run tests:
83
+
84
+ ```bash
85
+ bundle exec rspec
86
+ ```
87
+
88
+ To check code coverage:
89
+
90
+ ```bash
91
+ COVERAGE=true bundle exec rspec
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Contributing
97
+
98
+ Feel free to open issues or pull requests.
99
+
100
+ ---
101
+
102
+ ## License
103
+
104
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/annotato.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/annotato/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "annotato"
7
+ spec.version = Annotato::VERSION
8
+ spec.authors = ["Serhii Bodnaruk"]
9
+ spec.email = ["sergiwez@gmail.com"]
10
+
11
+
12
+ spec.summary = "Rails model annotator"
13
+ spec.description = "Annotato automatically adds or updates comments at the end of your Rails models to show schema details like columns, indexes, and PostgreSQL triggers."
14
+ spec.homepage = "https://github.com/boserh/annotato"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 3.1.0"
17
+
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/boserh/annotato"
21
+ spec.metadata["changelog_uri"] = "https://github.com/boserh/annotato/blob/main/CHANGELOG.md"
22
+
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/}) || f.start_with?(".github")
26
+ end
27
+ end
28
+
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "rails", ">= 6.0"
34
+ end
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "annotato"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "column_formatter"
4
+ require_relative "index_formatter"
5
+ require_relative "trigger_formatter"
6
+ require_relative "line_formatter"
7
+ require_relative "enum_formatter"
8
+
9
+ module Annotato
10
+ class AnnotationBuilder
11
+ def self.build(model)
12
+ conn = ActiveRecord::Base.connection
13
+ table_name = model.table_name
14
+
15
+ lines = []
16
+ lines << "== Annotato Schema Info"
17
+ lines << "Table: #{table_name}"
18
+ lines << ""
19
+
20
+ lines << "Columns:"
21
+ lines += ColumnFormatter.format(model, conn)
22
+
23
+ enums = EnumFormatter.format(model)
24
+ unless enums.empty?
25
+ lines << ""
26
+ lines << "Enums:"
27
+ lines += enums
28
+ end
29
+
30
+ indexes = IndexFormatter.format(conn, table_name)
31
+ unless indexes.empty?
32
+ lines << ""
33
+ lines << "Indexes:"
34
+ lines += indexes
35
+ end
36
+
37
+ triggers = TriggerFormatter.format(conn, table_name)
38
+ unless triggers.empty?
39
+ lines << ""
40
+ lines << "Triggers:"
41
+ lines += triggers
42
+ end
43
+
44
+ LineFormatter.format(lines)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotato
4
+ class ColumnFormatter
5
+ def self.format(model, connection)
6
+ table_name = model.table_name
7
+ primary_key = model.primary_key
8
+ unique_indexes = connection.indexes(table_name).select(&:unique)
9
+ enums = model.defined_enums
10
+
11
+ columns_raw = connection.columns(table_name).map do |col|
12
+ name = col.name
13
+ type = col.sql_type
14
+ options = []
15
+
16
+ options << "default(#{col.default.inspect})" unless col.default.nil?
17
+ options << "not null" unless col.null
18
+ options << "primary key" if name == primary_key
19
+ options << "is an Array" if type.end_with?("[]")
20
+ options << "unique" if unique_indexes.any? { |idx| idx.columns == [name] }
21
+ options << "enum" if enums.key?(name)
22
+
23
+ [name, type, options.join(", ")]
24
+ end
25
+
26
+ name_width = columns_raw.map { |name, _, _| name.length }.max
27
+ type_width = columns_raw.map { |_, type, _| type.length }.max
28
+
29
+ columns_raw.map do |name, type, opts|
30
+ line = "# %-#{name_width}s :%-#{type_width}s" % [name, type]
31
+ line += " #{opts}" unless opts.empty?
32
+ line.rstrip
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotato
4
+ class EnumFormatter
5
+ def self.format(model)
6
+ return [] if model.defined_enums.empty?
7
+
8
+ model.defined_enums.map do |attr, values|
9
+ # Check if all values equal keys (strings)
10
+ if values.values.all? { |v| v == v.to_s && values.keys.include?(v) }
11
+ enum_str = values.keys.join(", ")
12
+ else
13
+ enum_str = values.map { |k, v| "#{k} (#{v})" }.join(", ")
14
+ end
15
+
16
+ "# #{attr}: { #{enum_str} }"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotato
4
+ class IndexFormatter
5
+ def self.format(conn, table_name)
6
+ conn.indexes(table_name).map do |idx|
7
+ cols = Array(idx.columns)
8
+ "# #{idx.name} (#{cols.join(', ')})"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotato
4
+ class LineFormatter
5
+ def self.format(lines)
6
+ lines.map do |line|
7
+ line.strip.empty? ? "#" : (line.start_with?("#") ? line : "# #{line.rstrip}")
8
+ end.join("\n")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require_relative "column_formatter"
5
+ require_relative "annotation_builder"
6
+
7
+ module Annotato
8
+ class ModelAnnotator
9
+ def initialize(output: $stdout)
10
+ @output = output
11
+ end
12
+
13
+ def run
14
+ models.each do |model|
15
+ next unless model.table_exists?
16
+
17
+ annotation = AnnotationBuilder.build(model)
18
+ write_annotation(model, annotation)
19
+ end
20
+ @output.puts "✅ Annotato completed"
21
+ end
22
+
23
+ private
24
+
25
+ def models
26
+ Rails.application.eager_load!
27
+ ActiveRecord::Base.descendants.reject(&:abstract_class?)
28
+ end
29
+
30
+ def model_file(model)
31
+ file = model.instance_method(:initialize).source_location&.first
32
+
33
+ if file.nil? || file.include?("/gems/")
34
+ Object.const_source_location(model.name)&.first
35
+ else
36
+ file
37
+ end
38
+ end
39
+
40
+ def write_annotation(model, annotation)
41
+ file = model_file(model)
42
+ return unless file && File.exist?(file)
43
+
44
+ content = File.read(file).dup # <--- duplicate string to avoid FrozenError
45
+
46
+ # Extract the old Annotato annotation block if it exists
47
+ old_annotation = content[/^# == Annotato Schema Info.*?(?=^class|\z)/m]
48
+
49
+ # Skip writing if the annotation hasn't changed
50
+ if old_annotation && old_annotation.strip == annotation.strip
51
+ @output.puts "ℹ️ Skipped #{model.name} — annotation unchanged"
52
+ return
53
+ end
54
+
55
+ # Remove old Annotato blocks
56
+ content.gsub!(/^# == Annotato Schema Info.*?(?=^class|\z)/m, "")
57
+ content.gsub!(/^# == Schema Information.*?(?=^class|\z)/m, "")
58
+ content.rstrip!
59
+
60
+ # Verify the class definition exists before appending annotation
61
+ unless content.match?(/class\s+\w+/)
62
+ @output.puts "⚠️ Skipped #{model.name} — class not found in file"
63
+ return
64
+ end
65
+
66
+ # Append the new annotation at the end of the file
67
+ content += "\n\n#{annotation}\n"
68
+ File.write(file, content)
69
+
70
+ @output.puts "✍️ Annotated #{model.name}"
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module Annotato
6
+ class Railtie < ::Rails::Railtie
7
+ rake_tasks do
8
+ Dir[File.expand_path("../tasks/**/*.rake", __dir__)].each { |f| load f }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotato
4
+ class TriggerFormatter
5
+ def self.format(conn, table_name)
6
+ conn.exec_query(<<~SQL).map { |r| "# #{r['tgname']}" }
7
+ SELECT tgname FROM pg_trigger
8
+ WHERE tgrelid = '#{table_name}'::regclass
9
+ AND NOT tgisinternal;
10
+ SQL
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Annotato
4
+ VERSION = "0.1.0"
5
+ end
data/lib/annotato.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "annotato/version"
2
+ require "annotato/railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,8 @@
1
+ require "annotato/model_annotator"
2
+
3
+ namespace :annotato do
4
+ desc "Annotate models with schema info"
5
+ task models: :environment do
6
+ Annotato::ModelAnnotator.new.run
7
+ end
8
+ end
data/sig/annotato.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Annotato
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: annotato
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Serhii Bodnaruk
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-05-16 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ description: Annotato automatically adds or updates comments at the end of your Rails
27
+ models to show schema details like columns, indexes, and PostgreSQL triggers.
28
+ email:
29
+ - sergiwez@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - VERSION
44
+ - annotato.gemspec
45
+ - bin/console
46
+ - bin/setup
47
+ - lib/annotato.rb
48
+ - lib/annotato/annotation_builder.rb
49
+ - lib/annotato/column_formatter.rb
50
+ - lib/annotato/enum_formatter.rb
51
+ - lib/annotato/index_formatter.rb
52
+ - lib/annotato/line_formatter.rb
53
+ - lib/annotato/model_annotator.rb
54
+ - lib/annotato/railtie.rb
55
+ - lib/annotato/trigger_formatter.rb
56
+ - lib/annotato/version.rb
57
+ - lib/tasks/annotato.rake
58
+ - sig/annotato.rbs
59
+ homepage: https://github.com/boserh/annotato
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ allowed_push_host: https://rubygems.org
64
+ homepage_uri: https://github.com/boserh/annotato
65
+ source_code_uri: https://github.com/boserh/annotato
66
+ changelog_uri: https://github.com/boserh/annotato/blob/main/CHANGELOG.md
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.1.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.6.2
82
+ specification_version: 4
83
+ summary: Rails model annotator
84
+ test_files: []