Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change update term to reorder
Browse files Browse the repository at this point in the history
akhilkr128 committed Apr 20, 2022
1 parent 1b7df80 commit 17ea939
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions app/models/custom_button_set.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/models/custom_button_set_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 17ea939

Please sign in to comment.