-
Notifications
You must be signed in to change notification settings - Fork 7
Migrate registration policies from JSONB to ActiveRecord (#11238) #11819
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
Open
nbudin
wants to merge
8
commits into
main
Choose a base branch
from
nbudin/issue11238
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf929b6
wip
nbudin fc09d8e
Migrate registration policies from JSONB to ActiveRecord (#11238)
nbudin b9ac283
Restore plain anything/not_counted readers on RegistrationPolicyBucket
nbudin 9bd74e9
Fix cloned event proposals sharing registration_policy row with source
nbudin 3c50aea
Add unique index on registration_policy_id for events and event_propo…
nbudin 54ea738
Fix createEvent/createFillerEvent crash and lost registration_policy …
nbudin 8a1da72
Replace HasRegistrationPolicy's block-based API with a plain return v…
nbudin 649ba58
Address PR review: trim overwritten comments, YARD-document HasRegist…
nbudin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,71 @@ | ||
| # frozen_string_literal: true | ||
| class Mutations::UpdateEventProposal < Mutations::BaseMutation | ||
| field :event_proposal, Types::EventProposalType, null: false | ||
| description "Update an event proposal" | ||
|
|
||
| argument :event_proposal, Types::EventProposalInputType, required: true, camelize: false | ||
| argument :id, ID, required: false | ||
| field :event_proposal, Types::EventProposalType, null: false, description: "The updated event proposal" | ||
|
|
||
| argument :event_proposal, | ||
| Types::EventProposalInputType, | ||
| required: true, | ||
| camelize: false, | ||
| description: "The event proposal attributes to update" | ||
| argument :id, ID, required: false, description: "The ID of the event proposal to update" | ||
|
|
||
| load_and_authorize_convention_associated_model :event_proposals, :id, :update | ||
|
|
||
| # rubocop:disable Metrics/MethodLength | ||
| def resolve(**args) | ||
| event_proposal_attrs = args[:event_proposal].to_h.stringify_keys | ||
| form_response_attrs = JSON.parse(event_proposal_attrs.delete("form_response_attrs_json")) | ||
| registration_policy_attributes = form_response_attrs.delete("registration_policy") | ||
|
|
||
| changes = apply_registration_policy(event_proposal, registration_policy_attributes) | ||
| changes.update(apply_form_response_attrs(event_proposal, form_response_attrs)) | ||
| event_proposal.assign_attributes(event_proposal_attrs) | ||
| event_proposal.save! | ||
|
|
||
| log_form_response_changes(event_proposal, changes) if event_proposal.status != "draft" | ||
|
|
||
| { event_proposal: } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def apply_registration_policy(event_proposal, registration_policy_attributes) | ||
| change = event_proposal.registration_policy_change_for(registration_policy_attributes) | ||
| return {} unless change | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| if event_proposal.registration_policy | ||
| event_proposal.registration_policy.update_from!(change.new_policy) | ||
| else | ||
| event_proposal.registration_policy = change.new_policy | ||
| event_proposal.save! | ||
| end | ||
| end | ||
|
|
||
| { "registration_policy" => [change.old_json, event_proposal.registration_policy.as_json] } | ||
| end | ||
|
|
||
| def apply_form_response_attrs(event_proposal, form_response_attrs) | ||
| event_proposal.assign_form_response_attributes( | ||
| event_proposal.filter_form_response_attributes_for_assignment( | ||
| JSON.parse(event_proposal_attrs.delete("form_response_attrs_json")), | ||
| form_response_attrs, | ||
| event_proposal.event_category.event_proposal_form.form_items, | ||
| context[:pundit_user] | ||
| ) | ||
| ) | ||
| event_proposal.assign_attributes(event_proposal_attrs) | ||
| event_proposal.save! | ||
| event_proposal.form_response_attribute_changes | ||
| end | ||
|
|
||
| if event_proposal.status != "draft" | ||
| event_proposal.form_response_attribute_changes.each do |(key, (previous_value, new_value))| | ||
| FormResponseChange.create!( | ||
| response: event_proposal, | ||
| user_con_profile:, | ||
| field_identifier: key, | ||
| previous_value:, | ||
| new_value: | ||
| ) | ||
| end | ||
| def log_form_response_changes(event_proposal, changes) | ||
| changes.each do |(key, (previous_value, new_value))| | ||
| FormResponseChange.create!( | ||
| response: event_proposal, | ||
| user_con_profile:, | ||
| field_identifier: key, | ||
| previous_value:, | ||
| new_value: | ||
| ) | ||
| end | ||
|
|
||
| { event_proposal: } | ||
| end | ||
| # rubocop:enable Metrics/MethodLength | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # frozen_string_literal: true | ||
| module HasRegistrationPolicy | ||
| extend ActiveSupport::Concern | ||
|
|
||
| # A proposed registration policy change, returned by #registration_policy_change_for. | ||
| class Change | ||
| # @!attribute [r] new_policy | ||
| # @return [RegistrationPolicy] a detached, unsaved policy for the caller to apply | ||
| # @!attribute [r] old_json | ||
| # @return [Hash, nil] the current policy's JSON, captured before applying the change | ||
| attr_reader :new_policy, :old_json | ||
|
|
||
| def initialize(new_policy:, old_json:) | ||
| @new_policy = new_policy | ||
| @old_json = old_json | ||
| end | ||
| end | ||
|
|
||
| # @param hash [Hash, nil] a raw registration policy hash (e.g. from form response attrs) | ||
| # @return [Change, nil] nil if hash is blank or describes a policy equivalent to the current one | ||
| def registration_policy_change_for(hash) | ||
| return nil unless hash | ||
|
|
||
| new_policy = RegistrationPolicy.build_from_hash(hash) | ||
| return nil if registration_policy&.equivalent_to?(new_policy) | ||
|
|
||
| Change.new(new_policy:, old_json: registration_policy&.as_json) | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.