Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/models/miq_ae_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ class MiqAeMethod < ApplicationRecord
AVAILABLE_SCOPES = ["class", "instance"]
validates_inclusion_of :scope, :in => AVAILABLE_SCOPES

scope :name_path_search, lambda { |search|
where('name ILIKE ? or relative_path ILIKE ?', "%#{search}%", "%#{search}%") if search.present?
Copy link
Member

@kbrock kbrock Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
where('name ILIKE ? or relative_path ILIKE ?', "%#{search}%", "%#{search}%") if search.present?
search.present? ? where('name ILIKE ? or relative_path ILIKE ?', "%#{search}%", "%#{search}%") : where({})

Alternatively, if we want no results, then you can do : none

Do this across these 3 methods

}

scope :domain_search, lambda { |domain_id|
where(:domain_id => domain_id) if domain_id.present?
}

scope :selected_methods, lambda { |method_ids|
if method_ids.present?
where(:id => method_ids.split(',').map(&:to_i))
else
where(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsure, but think a simple self would work here.

Copy link
Member Author

@jeffibm jeffibm May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @kbrock , Yes, self methods should work here.

I used scope since the query was not that complex.

but, while using the self I was not able to chain it like -

MiqAeMethod
              .name_path_search(params[:search])
              .domain_search(params[:domain_id])
              .selected_methods(params[:ids])
              .select("id, relative_path, name")
              .order('name')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then lets use where(nil)

end
}

def self.available_languages
AVAILABLE_LANGUAGES
end
Expand Down