Skip to content

Commit

Permalink
Introduce new models TransformationMapping and TransformationMappingItem
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Feb 7, 2018
1 parent a07ec73 commit f60db73
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
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :transformation_mapping_item
end
21 changes: 21 additions & 0 deletions spec/models/transformation_mapping_spec.rb
@@ -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 f60db73

Please sign in to comment.