Skip to content

Commit

Permalink
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
Browse files Browse the repository at this point in the history
d-m-u committed Oct 3, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 51001e8 commit 108a22a
Showing 1 changed file with 32 additions and 0 deletions.
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

0 comments on commit 108a22a

Please sign in to comment.