From b07d5c6b53e460c3670a092d84461a342cdeb512 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 27 Aug 2019 10:04:22 -0400 Subject: [PATCH 1/2] Explicitly pass transformation_mapping when generating mapping items. --- .../api/transformation_mappings_controller.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb index f3216db3b3..6303ee02df 100644 --- a/app/controllers/api/transformation_mappings_controller.rb +++ b/app/controllers/api/transformation_mappings_controller.rb @@ -3,7 +3,7 @@ class TransformationMappingsController < BaseController def create_resource(_type, _id, data = {}) raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"] TransformationMapping.new(data.except("transformation_mapping_items")).tap do |mapping| - mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"]) + mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], mapping) mapping.save! end rescue StandardError => err @@ -17,7 +17,7 @@ def edit_resource(type, id, data) updated_data = data.except("transformation_mapping_items") transformation_mapping.update_attributes!(updated_data) if updated_data.present? - transformation_mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"]) + transformation_mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], transformation_mapping) transformation_mapping.save! transformation_mapping rescue StandardError => err @@ -51,7 +51,7 @@ def vm_flavor_fit_resource(_type, _id, data) def add_mapping_item_resource(type, id, data) resource_search(id, type, collection_class(type)).tap do |mapping| - mapping.transformation_mapping_items.append(create_mapping_items([data])) + mapping.transformation_mapping_items.append(create_mapping_items([data], mapping)) mapping.save! end rescue StandardError => err @@ -60,10 +60,14 @@ def add_mapping_item_resource(type, id, data) private - def create_mapping_items(items) + def create_mapping_items(items, mapping) items.collect do |item| raise "Must specify source and destination hrefs" unless item["source"] && item["destination"] - TransformationMappingItem.new(:source => fetch_mapping_resource(item["source"]), :destination => fetch_mapping_resource(item["destination"])) + TransformationMappingItem.new( + :transformation_mapping => mapping, + :source => fetch_mapping_resource(item["source"]), + :destination => fetch_mapping_resource(item["destination"]) + ) end end From bcd4777c151a119809c79e206d1f7be390868d52 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 27 Aug 2019 14:05:30 -0400 Subject: [PATCH 2/2] Replace TransformationMapping.new with TransformationMapping.create, and remove redundant save! call. --- app/controllers/api/transformation_mappings_controller.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/api/transformation_mappings_controller.rb b/app/controllers/api/transformation_mappings_controller.rb index 6303ee02df..902dd5f365 100644 --- a/app/controllers/api/transformation_mappings_controller.rb +++ b/app/controllers/api/transformation_mappings_controller.rb @@ -2,9 +2,8 @@ module Api class TransformationMappingsController < BaseController def create_resource(_type, _id, data = {}) raise "Must specify transformation_mapping_items" unless data["transformation_mapping_items"] - TransformationMapping.new(data.except("transformation_mapping_items")).tap do |mapping| + TransformationMapping.create(data.except("transformation_mapping_items")).tap do |mapping| mapping.transformation_mapping_items = create_mapping_items(data["transformation_mapping_items"], mapping) - mapping.save! end rescue StandardError => err raise BadRequestError, "Could not create Transformation Mapping - #{err}"