diff --git a/app/models/mixins/custom_actions_mixin.rb b/app/models/mixins/custom_actions_mixin.rb index 265f24df9d3..b8bd515a262 100644 --- a/app/models/mixins/custom_actions_mixin.rb +++ b/app/models/mixins/custom_actions_mixin.rb @@ -10,15 +10,17 @@ module CustomActionsMixin def custom_actions { - :buttons => 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 => button_set.children.collect(&:expanded_serializable_hash)) + button_set.serializable_hash.merge( + :buttons => filter_by_visibility(button_set.children).collect(&method(:serialize_button)) + ) end } 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 @@ -37,6 +39,14 @@ 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 serialize_button(button) + button.expanded_serializable_hash.merge("enabled" => button.evaluate_enablement_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..77e099b3a3b 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -43,6 +43,110 @@ } 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 + + 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 + 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