From 406045f40ea084a5f72e8ffcf2d615809be3e750 Mon Sep 17 00:00:00 2001 From: drew uhlmann Date: Wed, 27 Sep 2017 09:57:43 -0400 Subject: [PATCH] Migrate existing dialog field association data to use new relationship --- ...ld_associations_to_use_new_relationship.rb | 46 +++++++++++++++++ ...sociations_to_use_new_relationship_spec.rb | 50 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 db/migrate/20170927135007_migrate_dialog_field_associations_to_use_new_relationship.rb create mode 100644 spec/migrations/20170927135007_migrate_dialog_field_associations_to_use_new_relationship_spec.rb diff --git a/db/migrate/20170927135007_migrate_dialog_field_associations_to_use_new_relationship.rb b/db/migrate/20170927135007_migrate_dialog_field_associations_to_use_new_relationship.rb new file mode 100644 index 000000000..c4a7a8466 --- /dev/null +++ b/db/migrate/20170927135007_migrate_dialog_field_associations_to_use_new_relationship.rb @@ -0,0 +1,46 @@ +class MigrateDialogFieldAssociationsToUseNewRelationship < ActiveRecord::Migration[5.0] + class DialogFieldAssociation < ActiveRecord::Base + include ReservedMigrationMixin + include MigrationStubHelper # NOTE: Must be included after other mixins + end + class DialogField < ActiveRecord::Base + include ReservedMigrationMixin + include MigrationStubHelper # NOTE: Must be included after other mixins + end + class Dialog < ActiveRecord::Base + include ReservedMigrationMixin + include MigrationStubHelper # NOTE: Must be included after other mixins + end + class DialogTab < ActiveRecord::Base + include ReservedMigrationMixin + include MigrationStubHelper # NOTE: Must be included after other mixins + end + class DialogGroup < ActiveRecord::Base + include ReservedMigrationMixin + include MigrationStubHelper # NOTE: Must be included after other mixins + end + + def up + dialog_fields_with_associations = DialogField.all.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 + trigger_field_dialog_id = DialogTab.find(DialogGroup.find(t.dialog_group_id).dialog_tab_id).dialog_id + responder_fields_for_t.each do |responder| + respond_field_dialog_id = DialogTab.find(DialogGroup.find(DialogField.find(responder).dialog_group_id).dialog_tab_id).dialog_id + if trigger_field_dialog_id == respond_field_dialog_id + DialogFieldAssociation.create(:trigger_id => t.id, :respond_id => responder) + end + end + end + end + + def down + DialogFieldAssociation.delete_all + end +end diff --git a/spec/migrations/20170927135007_migrate_dialog_field_associations_to_use_new_relationship_spec.rb b/spec/migrations/20170927135007_migrate_dialog_field_associations_to_use_new_relationship_spec.rb new file mode 100644 index 000000000..96aad4de2 --- /dev/null +++ b/spec/migrations/20170927135007_migrate_dialog_field_associations_to_use_new_relationship_spec.rb @@ -0,0 +1,50 @@ +require_migration + +describe MigrateDialogFieldAssociationsToUseNewRelationship do + let(:dialog_field_stub) { migration_stub(:DialogField) } + let(:dialog_group_stub) { migration_stub(:DialogGroup) } + let(:dialog_tab_stub) { migration_stub(:DialogTab) } + let(:dialog_stub) { migration_stub(:Dialog) } + let(:dialog_field_association_stub) { migration_stub(:DialogFieldAssociation) } + + migration_context :up do + before(:each) do + dialog_group_stub.create!(:id => 2, :dialog_tab_id => 8) + dialog_tab_stub.create!(:id => 8, :dialog_id => 10) + dialog_stub.create!(:id => 10) + dialog_field_stub.create!(:id => 4, :name => "dialog_field4", :auto_refresh => true, :trigger_auto_refresh => true, :position => 4, :dialog_group_id => 2) + end + + it "creates a dialog field association when one is present in dialog" do + dialog_group_stub.create!(:id => 3, :dialog_tab_id => 12) + dialog_tab_stub.create!(:id => 12, :dialog_id => 12) + dialog_stub.create!(:id => 12) + dialog_field_stub.create!(:id => 1, :name => "dialog_field1", :trigger_auto_refresh => true, :position => 2, :dialog_group_id => 2) + dialog_field_stub.create!(:id => 3, :name => "dialog_field3", :auto_refresh => true, :position => 3, :dialog_group_id => 3) + dialog_field_stub.create!(:id => 2, :name => "dialog_field2", :auto_refresh => true, :position => 5, :dialog_group_id => 2) + + expect(dialog_field_association_stub.count).to eq(0) + migrate + expect(dialog_field_association_stub.count).to eq(2) + expect(dialog_field_association_stub.first.trigger_id).to eq(1) + expect(dialog_field_association_stub.first.respond_id).to eq(4) + end + + it "does not create a circular reference" do + dialog_field_stub.create!(:id => 1, :name => "dialog_field1", :auto_refresh => true, :trigger_auto_refresh => true, :position => 2, :dialog_group_id => 2) + + expect(dialog_field_association_stub.count).to eq(0) + migrate + expect(dialog_field_association_stub.count).to eq(1) + expect(dialog_field_association_stub.first.trigger_id).to eq(1) + expect(dialog_field_association_stub.first.respond_id).to eq(4) + end + end + + migration_context :down do + it "should delete dialog field associations" do + migrate + expect(dialog_field_association_stub.count).to eq(0) + end + end +end