-
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
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
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,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 |
50 changes: 50 additions & 0 deletions
50
...grations/20170927135007_migrate_dialog_field_associations_to_use_new_relationship_spec.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,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 |