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

Reference only handling #196

Merged
merged 3 commits into from
Oct 2, 2024
Merged

Reference only handling #196

merged 3 commits into from
Oct 2, 2024

Conversation

chrisandreae
Copy link
Member

  • Skip validation on deserializing pure references

If a deserialization operation is only pointing to something, and making no assertions about it, then we don't need to demand that the referenced model be a valid ActiveRecord.

  • Allow trivial up-migration of reference-only views

Unless we want to admit migrations that mutate ids, there's by definition nothing for a migration of a pure reference to do.

This may change with #182, so we will want to make sure that that supports renaming in the case of pure references.

Copy link
Contributor

@thefloweringash thefloweringash left a comment

Choose a reason for hiding this comment

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

This introduces a new concept of fields that determine identity, and locks them in to exactly type and id. I don't think this is a problem for anything we do or plan to do. It fixes a real problem with our site. If this conflicts with something we decide to do later, we can revisit.

LGTM.

deserialize_context.run_callback(ViewModel::Callbacks::Hook::BeforeValidate, viewmodel)
viewmodel.validate!
# Validate, unless this is a id-only reference to an existing model
if !reference_only? || viewmodel.new_model?
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: comment is duplicating code, but using if instead of unless. If that's easier to understand, consider doing the same for the code.

Suggested change
if !reference_only? || viewmodel.new_model?
unless reference_only? && !viewmodel.new_model?

If it's worth a comment, consider adding a short description of why it is valuable to skip validations in some cases.

If the request is making no changes to this model, don't require the model to be valid. This permits edits to other models that refer to this model, when this model is invalid.

(Or your favorite explanation here).

Copy link
Member Author

@chrisandreae chrisandreae May 29, 2024

Choose a reason for hiding this comment

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

I like both the change and the improved comment

@chrisandreae chrisandreae force-pushed the reference-only-handling branch from da579f0 to 4f36198 Compare May 29, 2024 07:03
If a deserialization operation is only pointing to something, and making no
assertions about it, then we don't need to demand that the referenced model be a
valid ActiveRecord.
@chrisandreae chrisandreae force-pushed the reference-only-handling branch from 4f36198 to 2eea809 Compare October 2, 2024 03:11
@chrisandreae chrisandreae merged commit 60a7645 into master Oct 2, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants