From 83d7e16f79b5bdf2d5f1599ac0c7b25dd25ffd06 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Thu, 25 Jul 2024 09:42:18 -0400 Subject: [PATCH] Add MiqAeMethod scopes for name, domain, and method search --- app/models/miq_ae_method.rb | 16 ++++++++++++++++ spec/models/miq_ae_method_spec.rb | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/models/miq_ae_method.rb b/app/models/miq_ae_method.rb index 6a4ea5d32950..4fbd4a197f9d 100644 --- a/app/models/miq_ae_method.rb +++ b/app/models/miq_ae_method.rb @@ -30,6 +30,22 @@ class MiqAeMethod < ApplicationRecord AVAILABLE_SCOPES = ["class", "instance"] validates_inclusion_of :scope, :in => AVAILABLE_SCOPES + # finds by name or namespace. not domain + # @param [nil,String] search + scope :name_path_search, lambda { |search| + search.present? ? where(arel_table[:relative_path].matches("%#{search}%")) : where({}) + } + + # @param [Integer,Array[Integer]] domain_ids + scope :domain_search, lambda { |domain_ids| + domain_ids.present? ? where(:domain_id => domain_ids) : where({}) + } + + # @param [Integer,Array[Integer]] method + scope :selected_methods, lambda { |method_ids| + method_ids.present? ? where(:id => method_ids) : where({}) + } + def self.available_languages AVAILABLE_LANGUAGES end diff --git a/spec/models/miq_ae_method_spec.rb b/spec/models/miq_ae_method_spec.rb index d67a7d317226..5bac1a5d3536 100644 --- a/spec/models/miq_ae_method_spec.rb +++ b/spec/models/miq_ae_method_spec.rb @@ -170,4 +170,21 @@ expect(keys.exclude?('options')).to be_truthy expect(keys.exclude?('embedded_methods')).to be_truthy end + + describe ".domain_search" do + it "matches by id" do + m1 = FactoryBot.create(:miq_ae_method, "name" => "match", :class_id => sub_class.id) + FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id) + + expect(MiqAeMethod.domain_search(domain.id)).to eq([m1]) + expect(MiqAeMethod.domain_search([domain.id])).to eq([m1]) + end + + it "searches all when blank" do + m1 = FactoryBot.create(:miq_ae_method, "name" => "match", :class_id => sub_class.id) + m2 = FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id) + + expect(MiqAeMethod.domain_search(nil)).to match_array([m1, m2]) + end + end end