Skip to content

Commit

Permalink
Merge pull request #23106 from kbrock/pr-22921
Browse files Browse the repository at this point in the history
Method List dialog
  • Loading branch information
Fryguy authored Jul 29, 2024
2 parents 1993fa8 + 5d2ddfe commit 2d788d1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 68 deletions.
6 changes: 6 additions & 0 deletions app/models/miq_ae_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ 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({})
}

def self.available_languages
AVAILABLE_LANGUAGES
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/miq_ae_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
sequence(:name) { |n| "miq_ae_method#{seq_padded_for_sorting(n)}" }
language { "ruby" }
location { "inline" }
scope { "instance" }

transient do
params { {} }
Expand Down
144 changes: 76 additions & 68 deletions spec/models/miq_ae_method_spec.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
RSpec.describe MiqAeMethod do
let(:user) { FactoryBot.create(:user_with_group) }
it "should return editable as false if the parent namespace/class is not editable" do
n1 = FactoryBot.create(:miq_ae_system_domain, :tenant => user.current_tenant)
c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo")
f1 = FactoryBot.create(:miq_ae_method,
:class_id => c1.id,
:name => "foo_method",
:scope => "instance",
:language => "ruby",
:location => "inline")
expect(f1.editable?(user)).to be_falsey
end
let(:sys_domain) { FactoryBot.create(:miq_ae_system_domain) }
let(:domain) { FactoryBot.create(:miq_ae_domain) }
let(:sub_domain) { FactoryBot.create(:miq_ae_namespace, :parent => domain) }

it "should return editable as true if the parent namespace/class is editable" do
n1 = FactoryBot.create(:miq_ae_domain, :tenant => user.current_tenant)
c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo")
f1 = FactoryBot.create(:miq_ae_method,
:class_id => c1.id,
:name => "foo_method",
:scope => "instance",
:language => "ruby",
:location => "inline")
expect(f1.editable?(user)).to be_truthy
let(:sys_class) { FactoryBot.create(:miq_ae_class, :namespace_id => sys_domain.id) }
let(:reg_class) { FactoryBot.create(:miq_ae_class, :namespace_id => domain.id) }
let(:sub_class) { FactoryBot.create(:miq_ae_class, :namespace_id => sub_domain.id) }

describe "#editable" do
it "should return editable as false if the parent namespace/class is not editable" do
f1 = FactoryBot.create(:miq_ae_method,
:class_id => sys_class.id,
:name => "foo_method")
expect(f1.editable?(user)).to be_falsey
end

it "should return editable as true if the parent namespace/class is editable" do
f1 = FactoryBot.create(:miq_ae_method,
:class_id => reg_class.id,
:name => "foo_method")
expect(f1.editable?(user)).to be_truthy
end
end

it "should lookup method" do
n1 = FactoryBot.create(:miq_ae_system_domain, :tenant => user.current_tenant)
c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo")
f1 = FactoryBot.create(:miq_ae_method,
:class_id => c1.id,
:name => "foo_method",
:scope => "instance",
:language => "ruby",
:location => "inline")
expect(f1.editable?(user)).to be_falsey

expect(MiqAeMethod.lookup_by_class_id_and_name(c1.id, "foo_method")).to eq(f1)
describe ".lookup_by_class_id_and_name" do
it "should lookup method" do
f1 = FactoryBot.create(:miq_ae_method,
:class_id => sys_class.id,
:name => "foo_method")
expect(f1.editable?(user)).to be_falsey

expect(MiqAeMethod.lookup_by_class_id_and_name(sys_class.id, "foo_method")).to eq(f1)
end
end

it "doesn’t access database when unchanged model is saved" do
n1 = FactoryBot.create(:miq_ae_system_domain, :tenant => user.current_tenant)
c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo")
f1 = FactoryBot.create(:miq_ae_method,
:class_id => c1.id,
:name => "foo_method",
:scope => "instance",
:language => "ruby",
:location => "inline")
:class_id => reg_class.id,
:name => "foo_method")
expect { f1.valid? }.not_to make_database_queries
end

Expand Down Expand Up @@ -85,15 +77,13 @@
end

context "#copy" do
let(:d2) { FactoryBot.create(:miq_ae_domain, :name => "domain2", :priority => 2) }
let(:ns1) { FactoryBot.create(:miq_ae_namespace, :name => "ns1", :parent => @d1) }
let(:m1) { FactoryBot.create(:miq_ae_method, :class_id => @cls1.id, :name => "foo_method1", :scope => "instance", :language => "ruby", :location => "inline") }
let(:m2) { FactoryBot.create(:miq_ae_method, :class_id => @cls1.id, :name => "foo_method2", :scope => "instance", :language => "ruby", :location => "inline") }
before do
@d1 = FactoryBot.create(:miq_ae_domain, :name => "domain1", :parent => nil, :priority => 1)
@cls1 = FactoryBot.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id)
@ns2 = FactoryBot.create(:miq_ae_namespace, :name => "ns2", :parent => d2)
end
let(:d1) { FactoryBot.create(:miq_ae_domain, :priority => 1) }
let(:d2) { FactoryBot.create(:miq_ae_domain, :priority => 2) }
let(:ns1) { FactoryBot.create(:miq_ae_namespace, :parent => d1) }
let(:ns2) { FactoryBot.create(:miq_ae_namespace, :parent => d2) }
let(:m1) { FactoryBot.create(:miq_ae_method, :class_id => cls1.id, :scope => "instance") }
let(:m2) { FactoryBot.create(:miq_ae_method, :class_id => cls1.id, :scope => "instance") }
let(:cls1) { FactoryBot.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id) }

it "copies instances under specified namespace" do
options = {
Expand All @@ -109,7 +99,7 @@

it "copy instances under same namespace raise error when class exists" do
options = {
:domain => @d1.name,
:domain => d1.name,
:namespace => ns1.fqname,
:overwrite_location => false,
:ids => [m1.id, m2.id]
Expand All @@ -120,7 +110,7 @@
it "replaces instances under same namespace when class exists" do
options = {
:domain => d2.name,
:namespace => @ns2.name,
:namespace => ns2.name,
:overwrite_location => true,
:ids => [m1.id, m2.id]
}
Expand Down Expand Up @@ -162,28 +152,16 @@
end

it "#domain" do
d1 = FactoryBot.create(:miq_ae_system_domain, :name => 'dom1', :priority => 10)
n1 = FactoryBot.create(:miq_ae_namespace, :name => 'ns1', :parent => d1)
c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo")
m1 = FactoryBot.create(:miq_ae_method,
:class_id => c1.id,
:name => "foo_method",
:scope => "instance",
:language => "ruby",
:location => "inline")
expect(m1.domain.name).to eql('dom1')
:class_id => sub_class.id,
:name => "foo_method")
expect(m1.domain).to eql(domain)
end

it "#to_export_yaml" do
d1 = FactoryBot.create(:miq_ae_system_domain, :name => 'dom1', :priority => 10)
n1 = FactoryBot.create(:miq_ae_namespace, :name => 'ns1', :parent => d1)
c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo")
m1 = FactoryBot.create(:miq_ae_method,
:class_id => c1.id,
:name => "foo_method",
:scope => "instance",
:language => "ruby",
:location => "inline")
:class_id => sub_class.id,
:name => "foo_method")
result = m1.to_export_yaml

expect(result['name']).to eql('foo_method')
Expand All @@ -192,4 +170,34 @@
expect(keys.exclude?('options')).to be_truthy
expect(keys.exclude?('embedded_methods')).to be_truthy
end

describe ".name_path_search" do
it "matches name" 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.name_path_search("match")).to eq([m1])
end

it "matches a name with a %" do
m1 = FactoryBot.create(:miq_ae_method, "name" => "10_is_bigger", :class_id => sub_class.id)
FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id)

expect(MiqAeMethod.name_path_search("10%_big")).to eq([m1])
end

it "matches path" 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.name_path_search(sub_domain.name)).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.name_path_search(nil)).to match_array([m1, m2])
end
end
end

0 comments on commit 2d788d1

Please sign in to comment.