Skip to content

Commit

Permalink
Migrate existing dialog field association data to use new relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Oct 16, 2017
1 parent 027e309 commit 406045f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 406045f

Please sign in to comment.