一个 Ruby Gem,可为您的 Rails 模型和路由文件添加注释。
A Ruby Gem that adds annotations to your Rails models and route files.
Adds comments summarizing the model schema or routes in your:
routes.rb file (for Rails projects)The schema comment looks like this:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string
# email :string
# sign_in_count :integer default(0), not null
# admin :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ApplicationRecord
...$ gem install annotaterbOr install it into your Rails project through the Gemfile:
group :development do
...
gem "annotaterb"
...For Rails projects, model files can get automatically annotated after migration tasks. To do this, run the following command:
$ bin/rails g annotate_rb:installThis 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.
$ bin/rails db:migrate
# ...
# Annotating models
# Annotated (1): app/models/user.rbTo skip the automatic annotation that happens after a db task, pass the environment variable ANNOTATERB_SKIP_ON_DB_TASKS=1 before your command.
$ ANNOTATERB_SKIP_ON_DB_TASKS=1 bin/rails db:migrateThe following Rails generator commands get added.
$ bin/rails generate --help
...
AnnotateRb:
annotate_rb:config
annotate_rb:hook
annotate_rb:install
annotate_rb:update_config
...
bin/rails g annotate_rb:config
.annotaterb.yml, using defaults from the gem.bin/rails g annotate_rb:hook
bin/rails db:migrate).bin/rails g annotate_rb:install
config and hook generator commandsbin/rails g annotate_rb:update_config
.annotaterb.yml any configuration key-value pairs that are used by the Gem. This is useful when there's a drift between the config file values and the gem defaults (i.e. when new features get added).Refer to the migration guide.
AnnotateRb has a CLI that you can use to add or remove annotations.
…Previously in the Annotate you could pass options through the CLI or store them as environment variables. Annotaterb removes dependency on the environment variables and instead can read values from a .annotaterb.yml file stored in the Rails project root.
AnnotateRb also supports other configuration file locations, and are searched for in the following locations (in order of precedence):
.annotaterb.ymlconfig/annotaterb.yml.config/.annotaterb.yml.config/annotaterb/config.yml# .annotaterb.yml
position: afterAnnotaterb reads first the configuration file, if it exists, passes its content through ERB, and merges the result with any options passed into the CLI.
For further details visit the section in the migration guide.
Keys use snake_case and match the gem defaults in AnnotateRb::Options. CLI flags override values from the config file.
| Option | Default | Description |
|---|---|---|
position |
before |
Fallback position for all position_in_* options. One of before, top, after, bottom, before_doc. |
position_in_class |
before |
Position in model files. before_doc keeps class documentation adjacent to the class. |
position_in_factory |
before |
Position in FactoryBot factory files. |
position_in_fixture |
before |
Position in fixture files. |
position_in_test |
before |
Position in test/spec files. |
position_in_routes |
before |
Position in config/routes.rb. |
position_in_serializer |
before |
Position in serializer files. |
position_in_additional_file_patterns |
before |
Position in files matched by additional_file_patterns. |
nested_position |
false |
Place annotations directly above nested classes/modules instead of at the top of the file. |
| Option | Default | Description |
|---|---|---|
show_foreign_keys |
true |
List foreign key constraints. |
show_complete_foreign_keys |
false |
Use complete foreign key names. |
show_indexes |
true |
List table indexes. |
show_indexes_comments |
false |
Include index comments. |
show_indexes_include |
false |
Include INCLUDE columns on indexes. |
simple_indexes |
false |
Concat related indexes onto each column line. |
show_check_constraints |
false |
List check constraints. |
show_enums |
false |
Show PostgreSQL enum types. |
enum_default_format |
label |
Default shown for enum backed columns: label (default("idnow")), raw (default(0)) or both (default(0: "idnow")). |
show_virtual_columns |
false |
Show virtual/generated columns. |
include_version |
false |
Include the migration version number. |
with_comment |
true |
Include database comments (fallback for column/table comment flags). |
with_column_comments |
true |
Include column comments. |
with_table_comments |
true |
Include table comments. |
position_of_column_comment |
with_name |
Column comment placement: with_name or rightmost_column. |
hide_default_column_types |
"" |
Comma-separated column types that omit defaults (e.g. json,jsonb,hstore). |
hide_limit_column_types |
"" |
Comma-separated column types that omit limits (e.g. integer,boolean,text). |
ignore_columns |
null |
Regex of column names to skip. |
ignore_database_name |
false |
Omit the database name from annotations. |
ignore_multi_database_name |
false |
Omit the database name in multi-database setups. |
timestamp_columns |
[created_at, updated_at] |
Column names treated as timestamps for classified sorting. |
classes_default_to_s |
[] |
Class names whose default values are rendered with to_s. |
| Option | Default | Description |
|---|---|---|
sort |
false |
Sort columns alphabetically. |
classified_sort |
true |
Sort as id → other columns → timestamps → associations. |
grouped_polymorphic |
false |
Group polymorphic associations when using classified_sort. |
format_markdown |
false |
Render annotations as Markdown. |
format_rdoc |
false |
Render annotations as RDoc. |
format_yard |
false |
Render annotations as YARD. |
| Option | Default | Description |
|---|---|---|
active_admin |
false |
Annotate ActiveAdmin models. |
exclude_controllers |
true |
Skip controller files. |
exclude_factories |
false |
Skip factory files. |
exclude_fixtures |
false |
Skip fixture files. |
exclude_helpers |
true |
Skip helper files. |
exclude_scaffolds |
true |
Skip scaffold files. |
exclude_serializers |
false |
Skip serializer files. |
exclude_sti_subclasses |
false |
Skip STI subclasses. |
exclude_tests |
false |
Skip test/spec files. Can also be an array of symbols. |
ignore_model_sub_dir |
false |
Ignore subdirectories under model_dir. |
ignore_unknown_models |
false |
Suppress warnings for bad model files. |
| Option | Default | Description |
|---|---|---|
model_dir |
[app/models] |
Directories containing models. |
root_dir |
[""] |
Root directories for multi-project layouts. |
additional_file_patterns |
[] |
Extra paths/globs to annotate (supports %MODEL_NAME%). |
require |
[] |
Extra files to require before loading models. |
| Option | Default | Description |
|---|---|---|
ignore_routes |
null |
Regex of routes to skip. |
timestamp |
false |
Include a timestamp in route annotations. |
auto_annotate_routes_after_migrate |
false |
Also annotate routes after DB migrate tasks. |
| Option | Default | Description |
|---|---|---|
wrapper |
null |
Text used for both opening and closing wrappers. |
wrapper_open |
null |
Opening wrapper text (falls back to wrapper). |
wrapper_close |
null |
Closing wrapper text (falls back to wrapper). |
force |
false |
Rewrite annotations even when unchanged. |
frozen |
false |
Exit non-zero if annotations would change. |
skip_on_db_migrate |
false |
Skip automatic annotation after DB migrate tasks. |
debug |
false |
Print resolved options for debugging. |
trace |
false |
Print full stack traces when annotation fails. |
Example:
# .annotaterb.yml
position: after
show_foreign_keys: true
show_indexes: true
show_enums: true
classified_sort: true
model_dir:
- app/models
- app/models/concerns
skip_on_db_migrate: falseBy default, when position_in_class is before (or top), AnnotateRb places the schema annotation immediately before the class declaration line. Any human-written documentation comment that was directly above the class is therefore pushed above the annotation.
If you prefer to keep the documentation comment adjacent to the class, set position_in_class to before_doc. The schema annotation is then inserted above the documentation comment block, leaving the comment directly before the class.
# Source file:
# Doc about User
class User < ApplicationRecord
end
# With position_in_class: before (default)
# Doc about User
# == Schema Information
# ...
class User < ApplicationRecord
end
# With position_in_class: before_doc
# == Schema Information
# ...
# Doc about User
class User < ApplicationRecord
endA "documentation comment" is the contiguous comment block immediately above the class declaration, with no blank line between the comments and the class. Recognized magic comments (encoding, frozen_string_literal, shareable_constant_value, warn_indent, typed, rbs_inline, plus Emacs/Vim style modelines) are excluded so the annotation can still be inserted between magic comments and the class doc.
If you want to always skip annotations on a particular model, add this string anywhere in the file:
# -*- SkipSchemaAnnotationsBy default, columns will be sorted in database order (i.e. the order in which migrations were run).
If you prefer to sort alphabetically so that the results of annotation are
consistent regardless of what order migrations are executed in, use --sort.
You can also sort columns by type, then alphabetically using --classified-sort
and --grouped-polymorphic: first goes id, then the rest columns, then the
timestamp columns and then the association columns.
Released under the same license as Ruby. No Support. No Warranty.
暂无开放 Issues,或尚未同步最近议题。