Skip to content

Commit

Permalink
Merge pull request #15911 from imtayadeway/custom-button-visability-e…
Browse files Browse the repository at this point in the history
…nablement

Expose Custom Button visability/enablement
  • Loading branch information
gtanzillo authored Sep 19, 2017
2 parents 07decc4 + 906d915 commit 68fc994
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app/models/mixins/custom_actions_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
104 changes: 104 additions & 0 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 68fc994

Please sign in to comment.