You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix classified_sort to group polymorphic association columns together (#238)
This PR fixes the `classified_sort` option to properly group polymorphic
association columns together. Previously, polymorphic `_type` columns
were sorted with regular columns while their corresponding `_id` columns
were in the associations section.
I modified the `classified_sort` method in `ModelWrapper` to:
1. Collect all column names in a first pass
2. Check if columns ending with `_type` have a corresponding `_id`
column
3. If found, treat the `_type` column as an association column
## Example
Before this fix:
```ruby
# id :uuid not null, primary key
# amount_cents :decimal(10, 6)
# artifact_type :string # <-- separated from artifact_id
# model :string not null
# provider :string not null
# usage :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :uuid not null
# artifact_id :uuid # <-- in associations section
# user_id :uuid
```
After this fix:
```ruby
# id :uuid not null, primary key
# amount_cents :decimal(10, 6)
# model :string not null
# provider :string not null
# usage :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :uuid not null
# artifact_id :uuid # <-- grouped together
# artifact_type :string # <-- grouped together
# user_id :uuid
```
Fixes#236
- Generates a new configuration file, `.annotaterb.yml`, using defaults from the gem.
87
94
88
95
`bin/rails g annotate_rb:hook`
96
+
89
97
- Installs the Rake file to automatically annotate Rails models on a database task (e.g. AnnotateRb will automatically run after running `bin/rails db:migrate`).
90
98
91
99
`bin/rails g annotate_rb:install`
100
+
92
101
- Runs the `config` and `hook` generator commands
93
102
94
103
`bin/rails g annotate_rb:update_config`
104
+
95
105
- Appends to `.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).
96
106
97
107
## Migrating from the annotate gem
108
+
98
109
Refer to the [migration guide](MIGRATION_GUIDE.md).
99
110
100
111
## Usage
@@ -103,7 +114,7 @@ AnnotateRb has a CLI that you can use to add or remove annotations.
103
114
104
115
```sh
105
116
# To show the CLI options
106
-
$ bundle exec annotaterb
117
+
$ bundle exec annotaterb
107
118
108
119
Usage: annotaterb [command] [options]
109
120
@@ -127,14 +138,23 @@ Annotate model options:
127
138
Complete foreign key names in the annotation
128
139
-i, --show-indexes List the table's database indexes in the annotation
129
140
-s, --simple-indexes Concat the column's related indexes in the annotation
141
+
-c, --show-check-constraints List the table's check constraints in the annotation
130
142
--hide-limit-column-types VALUES
131
143
don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)
132
144
--hide-default-column-types VALUES
133
145
don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)
134
146
--ignore-unknown-models don't display warnings for bad model files
135
147
-I, --ignore-columns REGEX don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`
136
148
--with-comment include database comments in model annotations
137
-
--with-column-comments include column comments in model annotations
149
+
--without-comment include database comments in model annotations
150
+
--with-column-comments include column comments in model annotations
151
+
--without-column-comments exclude column comments in model annotations
152
+
--position-of-column-comments VALUE
153
+
set the position, in the annotation block, of the column comment
154
+
--with-table-comments include table comments in model annotations
155
+
--without-table-comments exclude table comments in model annotations
156
+
--classes-default-to-s class Custom classes to be represented with `to_s`, may be used multiple times
157
+
--nested-position Place annotations directly above nested classes or modules instead of at the top of the file.
138
158
139
159
Annotate routes options:
140
160
Usage: annotaterb routes [options]
@@ -157,6 +177,7 @@ Additional options that work for annotating models and routes
157
177
--ignore-model-subdirects Ignore subdirectories of the models directory
158
178
--sort Sort columns alphabetically, rather than in creation order
159
179
--classified-sort Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns
180
+
--grouped-polymorphic Group polymorphic associations together in the annotation when using --classified-sort
160
181
-R, --require path Additional file to require before loading models, may be used multiple times
161
182
-e [tests,fixtures,factories,serializers],
162
183
--exclude Do not annotate fixtures, test files, factories, and/or serializers
@@ -176,6 +197,8 @@ Additional options that work for annotating models and routes
176
197
Place the annotations at the top (before) or the bottom (after) of the routes.rb file
Place the annotations at the top (before) or the bottom (after) of files captured in additional file patterns
179
202
--force Force new annotations even if there are no changes.
180
203
--debug Prints the options and outputs messages to make it easier to debug.
181
204
--frozen Do not allow to change annotations. Exits non-zero if there are going to be changes to files.
@@ -185,6 +208,7 @@ Additional options that work for annotating models and routes
185
208
## Configuration
186
209
187
210
### Storing default options
211
+
188
212
Previously in the [Annotate](https://github.com/ctran/annotate_models) 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.
189
213
190
214
```yml
@@ -198,6 +222,7 @@ Annotaterb reads first the configuration file, if it exists, passes its content
198
222
For further details visit the [section in the migration guide](MIGRATION_GUIDE.md#automatic-annotations-after-running-database-migration-commands).
199
223
200
224
### How to skip annotating a particular model
225
+
201
226
If you want to always skip annotations on a particular model, add this string
202
227
anywhere in the file:
203
228
@@ -211,6 +236,10 @@ migrations were run).
211
236
If you prefer to sort alphabetically so that the results of annotation are
212
237
consistent regardless of what order migrations are executed in, use `--sort`.
213
238
239
+
You can also sort columns by type, then alphabetically using `--classified-sort`
240
+
and `--grouped-polymorphic`: first goes id, then the rest columns, then the
241
+
timestamp columns and then the association columns.
242
+
214
243
## License
215
244
216
245
Released under the same license as Ruby. No Support. No Warranty.
0 commit comments