From e40f7d4c3c5d5eee01bdca2d345c96e8f11fd384 Mon Sep 17 00:00:00 2001 From: Tim Wade Date: Wed, 30 Aug 2017 14:57:48 -0700 Subject: [PATCH 1/4] Do not show hidden buttons in #custom_actions --- app/models/mixins/custom_actions_mixin.rb | 10 ++++-- spec/models/service_template_spec.rb | 38 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/models/mixins/custom_actions_mixin.rb b/app/models/mixins/custom_actions_mixin.rb index 265f24df9d3..334d1a9ff70 100644 --- a/app/models/mixins/custom_actions_mixin.rb +++ b/app/models/mixins/custom_actions_mixin.rb @@ -10,9 +10,11 @@ module CustomActionsMixin def custom_actions { - :buttons => custom_buttons.collect(&:expanded_serializable_hash), + :buttons => filter_by_visibility(custom_buttons).collect(&:expanded_serializable_hash), :button_groups => custom_button_sets_with_generics.collect do |button_set| - button_set.serializable_hash.merge(:buttons => button_set.children.collect(&:expanded_serializable_hash)) + button_set.serializable_hash.merge( + :buttons => filter_by_visibility(button_set.children).collect(&:expanded_serializable_hash) + ) end } end @@ -37,6 +39,10 @@ def direct_custom_buttons CustomButton.buttons_for(self).select { |b| b.parent.nil? } end + def filter_by_visibility(buttons) + buttons.select { |b| b.evaluate_visibility_expression_for(self) } + end + def generic_custom_buttons raise "called abstract method generic_custom_buttons" end diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 11d521b7bb1..53047e93a24 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -43,6 +43,44 @@ } expect(service_template.custom_actions).to match(expected) end + + it "does not show hidden buttons" do + service_template = FactoryGirl.create(:service_template, :name => "foo") + true_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "foo"}) + false_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "bar"}) + FactoryGirl.create(:custom_button, + :name => "visible button", + :applies_to_class => "Service", + :visibility_expression => true_expression) + FactoryGirl.create(:custom_button, + :name => "hidden button", + :applies_to_class => "Service", + :visibility_expression => false_expression) + FactoryGirl.create(:custom_button_set).tap do |group| + group.add_member(FactoryGirl.create(:custom_button, + :name => "visible button in group", + :applies_to_class => "Service", + :visibility_expression => true_expression)) + group.add_member(FactoryGirl.create(:custom_button, + :name => "hidden button in group", + :applies_to_class => "Service", + :visibility_expression => false_expression)) + end + + expected = { + :buttons => [ + a_hash_including("name" => "visible button") + ], + :button_groups => [ + a_hash_including( + :buttons => [ + a_hash_including("name" => "visible button in group") + ] + ) + ] + } + expect(service_template.custom_actions).to match(expected) + end end context "#type_display" do From b8a6d9dc2245c411e2f68ec68d6ea6a0250878fc Mon Sep 17 00:00:00 2001 From: Tim Wade Date: Wed, 30 Aug 2017 15:38:19 -0700 Subject: [PATCH 2/4] Do not show hidden buttons in #custom_action_buttons --- app/models/mixins/custom_actions_mixin.rb | 2 +- spec/models/service_template_spec.rb | 26 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/models/mixins/custom_actions_mixin.rb b/app/models/mixins/custom_actions_mixin.rb index 334d1a9ff70..ea9c135a633 100644 --- a/app/models/mixins/custom_actions_mixin.rb +++ b/app/models/mixins/custom_actions_mixin.rb @@ -20,7 +20,7 @@ def custom_actions end def custom_action_buttons - custom_buttons + custom_button_sets_with_generics.collect(&:children).flatten + filter_by_visibility(custom_buttons + custom_button_sets_with_generics.collect(&:children).flatten) end def generic_button_group diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 53047e93a24..877cb96048c 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -83,6 +83,32 @@ end end + describe "#custom_action_buttons" do + it "does not show hidden buttons" do + service_template = FactoryGirl.create(:service_template, :name => "foo") + true_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "foo"}) + false_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "bar"}) + visible_button = FactoryGirl.create(:custom_button, + :applies_to_class => "Service", + :visibility_expression => true_expression) + _hidden_button = FactoryGirl.create(:custom_button, + :applies_to_class => "Service", + :visibility_expression => false_expression) + visible_button_in_group = FactoryGirl.create(:custom_button, + :applies_to_class => "Service", + :visibility_expression => true_expression) + hidden_button_in_group = FactoryGirl.create(:custom_button, + :applies_to_class => "Service", + :visibility_expression => false_expression) + FactoryGirl.create(:custom_button_set).tap do |group| + group.add_member(visible_button_in_group) + group.add_member(hidden_button_in_group) + end + + expect(service_template.custom_action_buttons).to contain_exactly(visible_button, visible_button_in_group) + end + end + context "#type_display" do before(:each) do @st1 = FactoryGirl.create(:service_template, :name => 'Service Template 1') From 018859e140284f1d1eba6da2362f86a1e40844f5 Mon Sep 17 00:00:00 2001 From: Tim Wade Date: Wed, 30 Aug 2017 15:18:27 -0700 Subject: [PATCH 3/4] Extract method #serialize_button --- app/models/mixins/custom_actions_mixin.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/mixins/custom_actions_mixin.rb b/app/models/mixins/custom_actions_mixin.rb index ea9c135a633..3ede2a000fe 100644 --- a/app/models/mixins/custom_actions_mixin.rb +++ b/app/models/mixins/custom_actions_mixin.rb @@ -10,10 +10,10 @@ module CustomActionsMixin def custom_actions { - :buttons => filter_by_visibility(custom_buttons).collect(&:expanded_serializable_hash), + :buttons => filter_by_visibility(custom_buttons).collect(&method(:serialize_button)), :button_groups => custom_button_sets_with_generics.collect do |button_set| button_set.serializable_hash.merge( - :buttons => filter_by_visibility(button_set.children).collect(&:expanded_serializable_hash) + :buttons => filter_by_visibility(button_set.children).collect(&method(:serialize_button)) ) end } @@ -43,6 +43,10 @@ def filter_by_visibility(buttons) buttons.select { |b| b.evaluate_visibility_expression_for(self) } end + def serialize_button(button) + button.expanded_serializable_hash + end + def generic_custom_buttons raise "called abstract method generic_custom_buttons" end From 906d9158539f46e9d564253c641ef0bd8bd3bd3b Mon Sep 17 00:00:00 2001 From: Tim Wade Date: Wed, 30 Aug 2017 15:38:51 -0700 Subject: [PATCH 4/4] Serialize the enablement of buttons in #custom_actions --- app/models/mixins/custom_actions_mixin.rb | 2 +- spec/models/service_template_spec.rb | 40 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/models/mixins/custom_actions_mixin.rb b/app/models/mixins/custom_actions_mixin.rb index 3ede2a000fe..b8bd515a262 100644 --- a/app/models/mixins/custom_actions_mixin.rb +++ b/app/models/mixins/custom_actions_mixin.rb @@ -44,7 +44,7 @@ def filter_by_visibility(buttons) end def serialize_button(button) - button.expanded_serializable_hash + button.expanded_serializable_hash.merge("enabled" => button.evaluate_enablement_expression_for(self)) end def generic_custom_buttons diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 877cb96048c..77e099b3a3b 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -81,6 +81,46 @@ } expect(service_template.custom_actions).to match(expected) end + + it "serializes the enablement" do + service_template = FactoryGirl.create(:service_template, :name => "foo") + true_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "foo"}) + false_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "bar"}) + FactoryGirl.create(:custom_button, + :name => "enabled button", + :applies_to_class => "Service", + :enablement_expression => true_expression) + FactoryGirl.create(:custom_button, + :name => "disabled button", + :applies_to_class => "Service", + :enablement_expression => false_expression) + FactoryGirl.create(:custom_button_set).tap do |group| + group.add_member(FactoryGirl.create(:custom_button, + :name => "enabled button in group", + :applies_to_class => "Service", + :enablement_expression => true_expression)) + group.add_member(FactoryGirl.create(:custom_button, + :name => "disabled button in group", + :applies_to_class => "Service", + :enablement_expression => false_expression)) + end + + expected = { + :buttons => a_collection_containing_exactly( + a_hash_including("name" => "enabled button", "enabled" => true), + a_hash_including("name" => "disabled button", "enabled" => false) + ), + :button_groups => [ + a_hash_including( + :buttons => a_collection_containing_exactly( + a_hash_including("name" => "enabled button in group", "enabled" => true), + a_hash_including("name" => "disabled button in group", "enabled" => false) + ) + ) + ] + } + expect(service_template.custom_actions).to match(expected) + end end describe "#custom_action_buttons" do