From 679a6ebebb506540a5c4709e9ce6f01cadaceda1 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Mon, 21 Dec 2015 15:19:14 -0500 Subject: [PATCH] Priority wasn't being set for new fields https://bugzilla.redhat.com/show_bug.cgi?id=1292791 The priority wasn't being set for the newly added fields, before being saved the fields were sorted and the fields with nil priority floated to the top. Set the fields to the highest priority if they are new --- app/controllers/miq_ae_class_controller.rb | 4 ++- .../miq_ae_class_controller_spec.rb | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/controllers/miq_ae_class_controller.rb b/app/controllers/miq_ae_class_controller.rb index fdea59cebaa..410684e67eb 100644 --- a/app/controllers/miq_ae_class_controller.rb +++ b/app/controllers/miq_ae_class_controller.rb @@ -2247,12 +2247,14 @@ def ns_set_record_vars(miqaens) # Set record variables to new values def set_field_vars(parent = nil) fields = parent_fields(parent) + highest_priority = fields.count @edit[:new][:fields].each_with_index do |fld, i| if fld['id'].nil? new_field = MiqAeField.new + highest_priority += 1 + new_field.priority = highest_priority if @ae_method new_field.method_id = @ae_method.id - new_field.priority = i + 1 else new_field.class_id = @ae_class.id end diff --git a/spec/controllers/miq_ae_class_controller_spec.rb b/spec/controllers/miq_ae_class_controller_spec.rb index a24ffa894d6..5b0e04866a8 100644 --- a/spec/controllers/miq_ae_class_controller_spec.rb +++ b/spec/controllers/miq_ae_class_controller_spec.rb @@ -388,6 +388,9 @@ set_user_privileges ns = FactoryGirl.create(:miq_ae_namespace) @cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id) + @cls.ae_fields << FactoryGirl.create(:miq_ae_field, :name => 'fred', + :class_id => @cls.id, :priority => 1) + @cls.save @method = FactoryGirl.create(:miq_ae_method, :name => "method01", :scope => "class", :language => "ruby", :class_id => @cls.id, :data => "exit MIQ_OK", :location => "inline") expect(controller).to receive(:render) @@ -504,4 +507,32 @@ expect(flash_messages.first[:message]).to include("Automate Namespace \"foo_namespace\": Delete successful") end end + + context "#set_field_vars" do + it "sets priority of new schema fields" do + ns = FactoryGirl.create(:miq_ae_namespace) + cls = FactoryGirl.create(:miq_ae_class, :namespace_id => ns.id) + field1 = FactoryGirl.create(:miq_ae_field, + :aetype => "attribute", + :datatype => "string", + :name => "name01", + :class_id => cls.id, + :priority => 1) + field2 = FactoryGirl.create(:miq_ae_field, + :aetype => "attribute", + :datatype => "string", + :name => "name02", + :class_id => cls.id, + :priority => 2) + field3 = {"aetype" => "attribute", + "datatype" => "string", + "name" => "name03"} + edit = {:fields_to_delete => [], :new => {:fields => [field2, field3, field1]}} + controller.instance_variable_set(:@edit, edit) + controller.instance_variable_set(:@ae_class, cls) + fields = controller.send(:set_field_vars, cls) + priorities = fields.collect(&:priority) + expect(priorities).to eq([1, 2, 3]) + end + end end