Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce model changes for v2v #16787

Merged
merged 1 commit into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bzwei You are using TransformationMappingItem.new in the spec, is this factory needed at this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec/models/factory_girl_spec.rb requires all models to have a factory defined.

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