Skip to content

Commit

Permalink
[CustomActionsMixin] Fixes for MiqSetMembership
Browse files Browse the repository at this point in the history
Mostly converting the previous queries that existed to an AR based
version.

Notably:  The `uniq.flatten` needed to be switched out because previous
an array was returned by `.generic_button_group`, but now an
ActiveRecord::CollectionProxy is returned and if `uniq` is called first,
it doesn't do anything since each `CollectionProxy` is a uniq instance.
Doing a `flatten` first will convert the `CollectionProxy` instances to
Arrays, then the elements can properly have `.uniq` called on them.
  • Loading branch information
NickLaMuro committed Jul 13, 2021
1 parent 5470da3 commit 01443d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 7 additions & 4 deletions app/models/mixins/custom_actions_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ def custom_action_buttons(applies_to = self)
end

def generic_button_group
generic_custom_buttons.select { |button| !button.parent.nil? }
generic_custom_buttons.includes(:custom_button_sets)
.select { |button| button.custom_button_sets.present? }
end

def custom_button_sets_with_generics
custom_button_sets + generic_button_group.map(&:parent).uniq.flatten
custom_button_sets + generic_button_group.flat_map(&:custom_button_sets).uniq
end

def custom_buttons
generic_custom_buttons.select { |button| button.parent.nil? } + direct_custom_buttons
generic_custom_buttons.includes(:custom_button_sets)
.select { |b| b.custom_button_sets.blank? } + direct_custom_buttons
end

def direct_custom_buttons
CustomButton.buttons_for(self).select { |b| b.parent.nil? }
CustomButton.buttons_for(self).includes(:custom_button_sets)
.select { |b| b.custom_button_sets.blank? }
end

def filter_by_visibility(buttons, applies_to = self)
Expand Down
6 changes: 4 additions & 2 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
)
]
}
expect(service_template.custom_actions(service)).to match(expected)
actual = service_template.custom_actions(service)
expect(actual).to match(expected)
end

context "expression evaluation" do
Expand Down Expand Up @@ -183,7 +184,8 @@
)
]
}
expect(service_template.custom_actions(service)).to match(expected)
actual = service_template.custom_actions(service)
expect(actual).to match(expected)
end
end

Expand Down

0 comments on commit 01443d2

Please sign in to comment.