Skip to content

Commit

Permalink
Order buttons by custom_button_set.set_data[:button_order] in CustomB…
Browse files Browse the repository at this point in the history
…uttonSet
  • Loading branch information
lpichler committed Oct 4, 2018
1 parent 2635d9b commit 30ba222
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
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).order(:name)
custom_button_from_set = CustomButton.where(:id => custom_button_set.custom_buttons.pluck(:id)).select(:id, :visibility_expression).with_array_order(custom_button_set.set_data[:button_order])
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
10 changes: 5 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, :name => "DDD", :applies_to => vm_1, :visibility_expression => miq_expression) }
let(:custom_button_2) { FactoryGirl.create(:custom_button, :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 @@ -44,16 +44,16 @@
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(: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_3) { FactoryGirl.create(:custom_button, :applies_to => vm_1, :visibility_expression => miq_expression) }
let(:custom_button_4) { FactoryGirl.create(:custom_button, :applies_to => vm_1, :visibility_expression => miq_expression) }
let(:set_data) { {:applies_to_class => "Vm", :button_order => [custom_button_4.id, custom_button_2.id, custom_button_1.id, custom_button_3.id]} }
let(:custom_button_set) { FactoryGirl.create(:custom_button_set, :name => "set_1", :set_data => set_data) }

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
it 'returns filtered array of CustomButtonSet and CustomButtons ordered by custom_button_set.set_data[:button_order]' do
set = described_class.filter_with_visibility_expression([custom_button_set], vm_1).first
expect(set.set_data[:button_order]).to eq([custom_button_4.id, custom_button_2.id, custom_button_3.id])
end
Expand Down

0 comments on commit 30ba222

Please sign in to comment.