Skip to content

Commit

Permalink
Merge pull request #16787 from bzwei/v2v_models
Browse files Browse the repository at this point in the history
Introduce model changes for v2v
  • Loading branch information
gmcculloug authored Feb 9, 2018
2 parents 2099b3e + f60db73 commit a6a868c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/transformation_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class TransformationMapping < ApplicationRecord
has_many :transformation_mapping_items, :dependent => :destroy

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

def destination(source)
transformation_mapping_items.find_by(:source => source).try(:destination)
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
5 changes: 5 additions & 0 deletions spec/factories/transformation_mapping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :transformation_mapping do
sequence(:name) { |n| "Transformation Mapping #{seq_padded_for_sorting(n)}" }
end
end
3 changes: 3 additions & 0 deletions spec/factories/transformation_mapping_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :transformation_mapping_item
end
21 changes: 21 additions & 0 deletions spec/models/transformation_mapping_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe TransformationMapping do
describe '#destination' do
let(:src) { FactoryGirl.create(:ems_cluster) }
let(:dst) { FactoryGirl.create(:ems_cluster) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src, :destination => dst)]
)
end

it "finds the destination" do
expect(mapping.destination(src)).to eq(dst)
end

it "returns nil for unmapped source" do
expect(mapping.destination(FactoryGirl.create(:ems_cluster))).to be_nil
end
end
end

0 comments on commit a6a868c

Please sign in to comment.