Define column methods on the ColumnMethods module #51730
+30
−31
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
I was writing some code that parses our migrations and extracts information about newly added columns. It was missing some columns because they were of a less column datatype that we weren't explicitly handling. I went to see if I could just list the methods on a module to find the valid column method aliases, and was happy to see such a module exists. Except that due to how it's structured, none of the column method aliases actually exist on that module.
Detail
The way they were being defined inside the
included
hook meant they ended up being defined directly onTableDefinition
, contrary to the documentation:https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQL/ColumnMethods.html. By extending
ClassMethods
ontoColumnMethods
, the module can call the macro directly, and end up with the methods defined on theColumnMethods
module, instead of deferring their creation until they'reincluded
, and defining them on the including class.Additional information
I left
define_column_methods
as a class method that gets extended onto TableDefinition, since there's at least one gem that relies on being able to call it there to define their own column typesChecklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]