Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ordering in list of custom buttons #18049

Merged
merged 1 commit into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/custom_button_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions spec/models/custom_button_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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
Expand Down