Skip to content

Commit

Permalink
Merge pull request #21814 from akhilkr128/custom_button_reorder
Browse files Browse the repository at this point in the history
Custom button set model change for API
  • Loading branch information
kbrock authored May 10, 2022
2 parents 623b53a + 797b5d0 commit 915020b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/models/custom_button_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def update_children
replace_children(children)
end

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 = sets[id]
button_set.set_data[:group_index] = i
button_set.save!
end
end

def self.find_all_by_class_name(class_name, class_id = nil)
ordering = ->(o) { [o.set_data[:group_index].to_s, o.name] }

Expand Down
14 changes: 14 additions & 0 deletions spec/models/custom_button_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@
expect(CustomButtonSet.count).to eq(2)
end

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)
set_data1 = {:applies_to_class => "ServiceTemplate", :button_order => [custom_button1.id], :group_index => 1}
custom_button_set1 = FactoryBot.create(:custom_button_set, :set_data => set_data1)
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.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
let(:vm) { FactoryBot.create(:vm_vmware, :name => 'vm') }
let(:custom_button_1) { FactoryBot.create(:custom_button, :applies_to => vm) }
Expand Down

0 comments on commit 915020b

Please sign in to comment.