Skip to content

Commit

Permalink
Custom button model change for API
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilkr128 committed Apr 8, 2022
1 parent 6554858 commit d61c49e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/custom_button_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def update_children
replace_children(children)
end

def self.update_group_index(ids)
ids.each.with_index(1) do |id, i|
button_set = CustomButtonSet.find(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 "#update_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.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)
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 d61c49e

Please sign in to comment.