-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate existing dialog field association data to use new relationship
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
db/migrate/20170927135007_migrate_dialog_field_associations_to_use_new_relationship.rb
This file contains 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,32 @@ | ||
class MigrateDialogFieldAssociationsToUseNewRelationship < ActiveRecord::Migration[5.0] | ||
Rails.application.eager_load! | ||
|
||
def up | ||
dialogs = Dialog.includes(:dialog_fields) | ||
dialogs.each do |dialog| | ||
fields = dialog.dialog_fields | ||
dialog_fields_with_associations = fields.select { |df| df.auto_refresh || df.trigger_auto_refresh }.sort { |n| -n.position } | ||
triggers = dialog_fields_with_associations.select(&:trigger_auto_refresh) | ||
responders = dialog_fields_with_associations.select(&:auto_refresh) | ||
triggers.each_with_index do |t, index| | ||
responder_fields_for_t = if triggers[index + 1] | ||
responders.select { |r| r.position > t.position && r.position <= triggers[index + 1].position }.pluck(:id) | ||
else | ||
responders.select { |r| r.position > t.position }.pluck(:id) | ||
end | ||
responder_fields_for_t.each do |responder| | ||
DialogFieldAssociation.create(:trigger_id => t.id, | ||
:respond_id => responder) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def down | ||
Dialog.all.each do |dialog| | ||
dialog.dialog_fields.each do |field| | ||
field.dialog_field_associations.delete_all | ||
end | ||
end | ||
end | ||
end |