From 17ea939f95dc7713843f895c1b40171beac4ef75 Mon Sep 17 00:00:00 2001 From: Akhil K R Date: Wed, 20 Apr 2022 15:55:16 +0530 Subject: [PATCH] Change update term to reorder --- app/models/custom_button_set.rb | 7 +++++-- spec/models/custom_button_set_spec.rb | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/models/custom_button_set.rb b/app/models/custom_button_set.rb index 415dc8c80afe..199fa4cf250b 100644 --- a/app/models/custom_button_set.rb +++ b/app/models/custom_button_set.rb @@ -28,9 +28,12 @@ def update_children replace_children(children) end - def self.update_group_index(ids) + def self.reorder_group_index(ids) + sets = CustomButtonSet.where(:id => ids).index_by(&:id) + raise ArgumentError, "not all requested ids found" unless sets.keys.sort == ids.sort + ids.each.with_index(1) do |id, i| - button_set = CustomButtonSet.find(id) + button_set = sets[id] button_set.set_data[:group_index] = i button_set.save! end diff --git a/spec/models/custom_button_set_spec.rb b/spec/models/custom_button_set_spec.rb index a03ec8f3c891..5c43329208bf 100644 --- a/spec/models/custom_button_set_spec.rb +++ b/spec/models/custom_button_set_spec.rb @@ -81,7 +81,7 @@ expect(CustomButtonSet.count).to eq(2) end - it "#update_group_index" do + it "#reorder_group_index" do service_template1 = FactoryBot.create(:service_template) custom_button1 = FactoryBot.create(:custom_button, :applies_to => service_template1) custom_button2 = FactoryBot.create(:custom_button, :applies_to => service_template1) @@ -90,9 +90,9 @@ set_data2 = {:applies_to_class => "ServiceTemplate", :button_order => [custom_button2.id], :group_index => 2} custom_button_set2 = FactoryBot.create(:custom_button_set, :set_data => set_data2) - CustomButtonSet.update_group_index([custom_button_set2.id, custom_button_set1.id]) - expect(CustomButtonSet.find(custom_button_set1.id).set_data[:group_index]).to eq(2) - expect(CustomButtonSet.find(custom_button_set2.id).set_data[:group_index]).to eq(1) + CustomButtonSet.reorder_group_index([custom_button_set2.id, custom_button_set1.id]) + expect(custom_button_set1.reload.set_data[:group_index]).to eq(2) + expect(custom_button_set2.reload.set_data[:group_index]).to eq(1) end context '#update_children' do