Skip to content

Commit

Permalink
Introduce new models TransformationMapping and TransformationMappingItem
Browse files Browse the repository at this point in the history
Create a complete mapping through a virtual column.
  • Loading branch information
bzwei committed Feb 5, 2018
1 parent a07ec73 commit 805e445
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
61 changes: 61 additions & 0 deletions app/models/transformation_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class TransformationMapping < ApplicationRecord
has_many :transformation_mapping_items, :dependent => :destroy

validates :name, :presence => true, :uniqueness => true

virtual_column :mappings, :type => :string

# forward compatible: next release the table will be polymorphic
virtual_column :type, :type => :string

def self.inheritance_column
"none"
end

def type
"MigrationInfrastructureMapping"
end

def type=(type)
end

# Assumption: source and destination types are the same. Works for infrastructure mapping
# group mapping items first by destination_type, then by destination_id
# simplify records with IDs.
# Sample result (Hash)
#
# EmsCluster:
# - destination: "2":
# sources: ["100", "200"]
# - destination: "3"
# sources: ["301", "302"]
# Network:
# - destination: "2"
# sources: ["202", "204"]
def mappings
dst_type_groups = transformation_mapping_items.group_by(&:destination_type)
dst_type_groups.each_with_object({}) do |(dst_type, items), results|
dst_groups = items.group_by { |item| item.destination_id.to_s }
results[dst_type] = dst_groups.each_with_object({}) do |(dst_id, item_array), dst_id_groups|
dst_id_groups["destination"] = dst_id
dst_id_groups["sources"] = item_array.collect { |item| item.source_id.to_s }
end
end
end

def mappings=(options)
options.each do |dst_type, item_map_array|
item_map_array.each do |item_map|
destination_id = item_map['destination']
item_map['sources'].each do |source_id|
transformation_mapping_items << TransformationMappingItem.new(
:source_id => source_id,
:source_type => dst_type,
:destination_id => destination_id,
:destination_type => dst_type
)
end
end
end
end
end
7 changes: 7 additions & 0 deletions app/models/transformation_mapping_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class TransformationMappingItem < ApplicationRecord
belongs_to :transformation_mapping
belongs_to :source, :polymorphic => true
belongs_to :destination, :polymorphic => true

validates :source_id, :uniqueness => {:scope => [:transformation_mapping_id, :source_type]}
end

0 comments on commit 805e445

Please sign in to comment.