diff --git a/app/models/custom_button_set.rb b/app/models/custom_button_set.rb index 1bc9ad39e76..eb04f5914f1 100644 --- a/app/models/custom_button_set.rb +++ b/app/models/custom_button_set.rb @@ -56,7 +56,7 @@ def self.applies_to_all_instances(class_name) # - filtered custom_button_sets array when all visibilty expression custom buttons have been evaluated to false def self.filter_with_visibility_expression(custom_button_sets, object) custom_button_sets.each_with_object([]) do |custom_button_set, ret| - custom_button_from_set = CustomButton.where(:id => custom_button_set.custom_buttons.pluck(:id)).select(:id, :visibility_expression) + custom_button_from_set = CustomButton.where(:id => custom_button_set.custom_buttons.pluck(:id)).select(:id, :visibility_expression).order(:name) filtered_ids = custom_button_from_set.select { |x| x.evaluate_visibility_expression_for(object) }.pluck(:id) if filtered_ids.present? custom_button_set.set_data[:button_order] = filtered_ids diff --git a/spec/models/custom_button_set_spec.rb b/spec/models/custom_button_set_spec.rb index 0a192b8cb98..8f50af8a96b 100644 --- a/spec/models/custom_button_set_spec.rb +++ b/spec/models/custom_button_set_spec.rb @@ -25,7 +25,7 @@ let(:vm_1) { FactoryGirl.create(:vm_vmware, :name => 'vm_1') } let(:custom_button_1) { FactoryGirl.create(:custom_button, :applies_to => vm_1) } let(:miq_expression) { MiqExpression.new('EQUAL' => {'field' => 'Vm-name', 'value' => "vm_1"}) } - let(:custom_button_2) { FactoryGirl.create(:custom_button, :applies_to => vm_1, :visibility_expression => miq_expression) } + let(:custom_button_2) { FactoryGirl.create(:custom_button, :name => "DDD", :applies_to => vm_1, :visibility_expression => miq_expression) } let(:set_data) { {:applies_to_class => "Vm", :button_order => [custom_button_1.id, custom_button_2.id]} } let(:custom_button_set) { FactoryGirl.create(:custom_button_set, :name => "set_1", :set_data => set_data) } @@ -42,12 +42,20 @@ end context 'when any visibility_expression is evaluated to false and any to true' do - let(:miq_expression_false) { MiqExpression.new('EQUAL' => {'field' => 'Vm-name', 'value' => "vm_2"}) } - let(:custom_button_1) { FactoryGirl.create(:custom_button, :applies_to => vm_1, :visibility_expression => miq_expression_false) } + let(:miq_expression_false) { MiqExpression.new('EQUAL' => {'field' => 'Vm-name', 'value' => "vm_2"}) } + let(:custom_button_1) { FactoryGirl.create(:custom_button, :applies_to => vm_1, :visibility_expression => miq_expression_false) } + let(:custom_button_3) { FactoryGirl.create(:custom_button, :name => "ZZZ", :applies_to => vm_1, :visibility_expression => miq_expression) } + let(:custom_button_4) { FactoryGirl.create(:custom_button, :name => "BBBB", :applies_to => vm_1, :visibility_expression => miq_expression) } + let(:set_data) { {:applies_to_class => "Vm", :button_order => [custom_button_1.id, custom_button_2.id, custom_button_3.id]} } + let(:custom_button_set) { FactoryGirl.create(:custom_button_set, :name => "set_1", :set_data => set_data) } - it 'returns filtered array of CustomButtonSet and CustomButtons' do + before do + [custom_button_3, custom_button_4].each { |x| custom_button_set.add_member(x) } + end + + it 'returns filtered array of CustomButtonSet and CustomButtons ordered by name' do set = described_class.filter_with_visibility_expression([custom_button_set], vm_1).first - expect(set.set_data[:button_order]).to eq([custom_button_2.id]) + expect(set.set_data[:button_order]).to eq([custom_button_4.id, custom_button_2.id, custom_button_3.id]) end context 'all CustomButtons#visibility_expression are evaluated to false' do