Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to validate which models are allowed to be associated to a polymorphic belongs_to #51738

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

natematykiewicz
Copy link
Contributor

Motivation / Background

When using a polymorphic belongs_to, I usually have a list of allowed classes that I expect this association to use. Oftentimes there's even certain views that need to handle each expected option. Unexpected types in the table would cause problems and potentially pose a security risk.

I've manually added an inclusion validator before, but wished I could just tell the association what the allowed options are.

Detail

This Pull Request changes belongs_to's polymorphic option to allow a String, Symbol, or array of Strings/Symbols of allowed model names. When provided, an inclusion validator is added to the model to ensure the _type field is in the list of allowed class names.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@natematykiewicz natematykiewicz force-pushed the nate/polymorphic_belong_to_validation branch from 7a2078c to 5116d51 Compare May 5, 2024 05:19
@flavorjones flavorjones added the ready PRs ready to merge label May 5, 2024
@flavorjones flavorjones added this to the 7.2.0 milestone May 5, 2024
@flavorjones flavorjones removed the ready PRs ready to merge label May 5, 2024
Copy link
Member

@flavorjones flavorjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this feature and the code and tests look good to me.

Presuming a Core team member will also approve, can you also add something to the "Polymorphic Associations" section in guides/source/association_basics.md?

@rails-bot rails-bot bot added the docs label May 5, 2024
@natematykiewicz natematykiewicz force-pushed the nate/polymorphic_belong_to_validation branch from 250ddc9 to e22524b Compare May 5, 2024 22:07
@natematykiewicz
Copy link
Contributor Author

I like this feature and the code and tests look good to me.

Presuming a Core team member will also approve, can you also add something to the "Polymorphic Associations" section in guides/source/association_basics.md?

Done! https://github.com/rails/rails/pull/51738/files#diff-d977574b785ce95fe656c58211c668992bf95214d52d3ffadb409ef8f69ad287

I've not done many references in markdown. I tried to follow other parts of the guide. I think I linked the text to https://edgeguides.rubyonrails.org/active_record_validations.html#inclusion correctly. I'm not quite sure of the best way to preview the guides, so I'm sort of flying blind on that.

@natematykiewicz natematykiewicz force-pushed the nate/polymorphic_belong_to_validation branch from e22524b to 018fcb1 Compare May 5, 2024 22:09
Copy link
Member

@flavorjones flavorjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Needs to be rebased but as soon as you do that, I'll tag it for review by one of the core team.

@natematykiewicz natematykiewicz force-pushed the nate/polymorphic_belong_to_validation branch from 018fcb1 to 317773f Compare May 9, 2024 16:12
@natematykiewicz
Copy link
Contributor Author

@flavorjones I've rebased the branch

@natematykiewicz natematykiewicz force-pushed the nate/polymorphic_belong_to_validation branch from 317773f to b8688b6 Compare May 9, 2024 17:52
@natematykiewicz
Copy link
Contributor Author

Just fixed another merge conflict on the changelog. The ActiveRecord changelog is a hot file. I guess this is why the Rubocop repo has PRs add a new file to a changelogs folder, and then they generate a CHANGELOG.md at each release.

@flavorjones flavorjones added the ready PRs ready to merge label May 13, 2024
@flavorjones
Copy link
Member

@natematykiewicz Thanks! I've poked the core team to review.

@@ -121,6 +121,13 @@ def self.define_validations(model, reflection)
required = !reflection.options[:optional]
end

case reflection.options[:polymorphic]
when Array, String, Symbol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we accept String and Symbol? If there is only one possible value people should just pass an array with one element

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you thinking we simply support arrays of strings? I was trying to be flexible, but only supporting an array of strings is certainly easier to document and explain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or are you saying I just support an array, and then map the values to string still (which would allow an array of strings/symbols)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Rafael is suggesting the latter: just support Array[String|Symbol] and not support passing a single string or symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the changes, but I'm not sure what to do about the changelog. I see @rafaelfranca changed the milestone to 8.0.0. So I need to rebase this to main's recently wiped changelog instead of 7.2.0's changelog?

@rafaelfranca rafaelfranca modified the milestones: 7.2.0, 8.0.0 May 17, 2024
@natematykiewicz natematykiewicz force-pushed the nate/polymorphic_belong_to_validation branch from b8688b6 to c1f6bf4 Compare May 19, 2024 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants