Use a dynamic module to include the writing methods that are generated from using belongs_to#24
Merged
doliveirakn merged 2 commits intomasterfrom Dec 9, 2019
Conversation
doliveirakn
commented
Dec 9, 2019
| mapping = self.class.send("#{foreign_type}_mapping") | ||
| enum = mapping.key(klass.to_s) | ||
| if klass.kind_of?(Class) && klass <= ActiveRecord::Base | ||
| enum ||= mapping.key(klass.sti_name) |
Contributor
Author
There was a problem hiding this comment.
This line is also new.
Previously, we checked klass.to_s, then klass.base_class.to_s, then klass.base_class.sti_name. This ensures that we check klass.sti_name before we move to the base class.
doliveirakn
commented
Dec 9, 2019
| component_extension = Module.new do | ||
| define_method foreign_type do | ||
| t = super() | ||
| self.class.send("#{foreign_type}_mapping")[t] |
Contributor
Author
There was a problem hiding this comment.
mapping is a a local variable in the context of the belongs_to method. It was captured in the context of the dynamic methods. We have the ability to access the mapping through a class variable, so we can use that method instead of capturing the local variable.
959c681 to
3fdfb3e
Compare
mctaylorpants
approved these changes
Dec 9, 2019
…errides a method, we can still use the super behaviour
3fdfb3e to
967dbf2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Prior to this, if we have something like
the
source=will override the method defined by thebelongs_toand what will end up happening is that thesource_typewill not end up being created properly.By created an anonymous module, we can prepend it to the class and then the
superbehaviour will work as expected.