Skip to content

Commit 28bf8d2

Browse files
committed
Fixes #38718 - Register feature scope in belongs_to_proxy
1 parent a271812 commit 28bf8d2

File tree

10 files changed

+25
-16
lines changed

10 files changed

+25
-16
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ gem 'rest-client', '>= 2.0.0', '< 3', :require => 'rest_client'
88
gem 'audited', '~> 5.0', '!= 5.1.0'
99
gem 'will_paginate', '~> 3.3'
1010
gem 'ancestry', '~> 4.0'
11-
gem 'scoped_search', '>= 4.1.10', '< 5'
11+
gem 'scoped_search', '>= 4.3.0', '< 5'
1212
gem 'ldap_fluff', '>= 0.9.0', '< 1.0'
1313
gem 'apipie-rails', '>= 0.8.0', '< 2'
1414
gem 'apipie-dsl', '>= 2.6.2'

app/controllers/api/base_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def parent_scope
8585
association ||= resource_class.reflect_on_all_associations.detect { |assoc| assoc.class_name == parent_name.camelize }
8686
if association.nil? && parent_name == 'host'
8787
association = resource_class.reflect_on_all_associations.detect { |assoc| assoc.class_name == 'Host::Base' }
88+
association ||= resource_class.reflect_on_all_associations.detect { |assoc| assoc.class_name == 'Host::Managed' }
8889
end
8990
return resource_class.all if association.nil? && Taxonomy.types.include?(resource_class_for(resource_name(parent_name)))
9091
raise "Association not found for #{parent_name}" unless association

app/models/concerns/audit_search.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ module AuditSearch
44
included do
55
belongs_to :user, :class_name => 'User'
66
belongs_to :search_users, :class_name => 'User', :foreign_key => :user_id
7-
belongs_to :search_hosts, -> { where(:audits => { :auditable_type => 'Host::Base' }) },
8-
:class_name => 'Host::Base', :foreign_key => :auditable_id
7+
belongs_to :search_hosts, -> { joins(:audits).where(:audits => { :auditable_type => 'Host::Base' }) },
8+
:class_name => 'Host::Managed', :foreign_key => :auditable_id
99
belongs_to :search_hostgroups, :class_name => 'Hostgroup', :foreign_key => :auditable_id
1010
belongs_to :search_parameters, :class_name => 'Parameter', :foreign_key => :auditable_id
1111
belongs_to :search_templates, :class_name => 'ProvisioningTemplate', :foreign_key => :auditable_id
1212
belongs_to :search_ptables, :class_name => 'Ptable', :foreign_key => :auditable_id
1313
belongs_to :search_os, :class_name => 'Operatingsystem', :foreign_key => :auditable_id
1414
belongs_to :search_class, :class_name => 'Puppetclass', :foreign_key => :auditable_id
15-
belongs_to :search_nics, -> { where('audits.auditable_type LIKE ?', "Nic::%") }, :class_name => 'Nic::Base', :foreign_key => :auditable_id
15+
belongs_to :search_nics, -> { joins(:audits).where('audits.auditable_type LIKE ?', "Nic::%") }, :class_name => 'Nic::Base', :foreign_key => :auditable_id
1616
belongs_to :search_settings, :class_name => 'Setting', :foreign_key => :auditable_id
1717

1818
scoped_search :on => :id, :complete_value => false

app/models/concerns/belongs_to_proxies.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def belongs_to_proxy(name, options)
1616

1717
def register_smart_proxy(name, options)
1818
self.registered_smart_proxies = registered_smart_proxies.merge(name => options)
19-
belongs_to name, :class_name => 'SmartProxy'
19+
belongs_to name, -> { with_features(options[:feature]) }, :class_name => 'SmartProxy'
2020
validates name, :proxy_features => { :feature => options[:feature], :required => options[:required] }
2121
end
2222

app/validators/proxy_features_validator.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ def initialize(args)
55
end
66

77
def validate_each(record, attribute, value)
8-
if !value && @options[:required]
8+
id = record.public_send("#{attribute}_id")
9+
if !id && @options[:required]
910
record.errors.add("#{attribute}_id", _('was not found'))
1011
end
1112

12-
if value && !value.has_feature?(@options[:feature])
13+
# Due to scope being set on the association, it can happen that the id is present but the association is nil
14+
if (id || value) && !value.try(:has_feature?, @options[:feature])
1315
if @options[:message].nil?
1416
message = _('does not have the %s feature') % @options[:feature]
1517
else

test/controllers/concerns/auto_complete_search_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class AutoCompleteSearchTest < ActionController::TestCase
1313
assert_predicate response, :successful?
1414
suggestions = ActiveSupport::JSON.decode(response.body)
1515
assert_equal 1, suggestions.length
16-
assert_equal suggestions.first['part'], "name = #{domain1.name}"
16+
assert_equal suggestions.first['label'], "name = \"#{domain1.name}\""
1717
end
1818
end

test/controllers/subnets_controller_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class SubnetsControllerTest < ActionController::TestCase
1616
context 'three similar subnets exists' do
1717
def setup
1818
as_admin do
19-
@s1 = FactoryBot.create(:subnet_ipv4, :network => '100.20.100.100', :cidr => '24', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
20-
@s3 = FactoryBot.create(:subnet_ipv4, :network => '200.100.100.100', :cidr => '24', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
21-
@s2 = FactoryBot.create(:subnet_ipv4, :network => '100.100.100.100', :cidr => '24', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
22-
@s4 = FactoryBot.create(:subnet_ipv6, :network => 'beef::', :cidr => '64', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
23-
@s5 = FactoryBot.create(:subnet_ipv6, :network => 'ffee::', :cidr => '64', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
19+
@s1 = FactoryBot.create(:subnet_ipv4, :dhcp, :network => '100.20.100.100', :cidr => '24', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
20+
@s3 = FactoryBot.create(:subnet_ipv4, :dhcp, :network => '200.100.100.100', :cidr => '24', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
21+
@s2 = FactoryBot.create(:subnet_ipv4, :dhcp, :network => '100.100.100.100', :cidr => '24', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
22+
@s4 = FactoryBot.create(:subnet_ipv6, :dhcp, :network => 'beef::', :cidr => '64', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
23+
@s5 = FactoryBot.create(:subnet_ipv6, :dhcp, :network => 'ffee::', :cidr => '64', :organization_ids => [taxonomies(:organization1).id], :location_ids => [taxonomies(:location1).id])
2424
end
2525
end
2626

test/models/concerns/audit_search_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def setup
1111
end
1212

1313
test "host autocomplete works in audit search" do
14-
FactoryBot.create(:host, :managed)
14+
FactoryBot.create(:host, :managed, :with_auditing)
1515
hosts = Audit.complete_for("host = ", {:controller => 'audits'})
1616
assert hosts.count > 0
1717
end

test/models/concerns/belongs_to_proxies_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SampleModel
55
include BelongsToProxies
66

77
class << self
8-
def belongs_to(name, options = {})
8+
def belongs_to(name, scope, options = {})
99
end
1010

1111
def validates(name, options = {})

test/unit/validators/proxy_features_validator_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ProxyFeaturesValidatorTest < ActiveSupport::TestCase
44
class Validatable
55
include ActiveModel::Validations
66
validates :proxy, :proxy_features => { :feature => 'DNS' }
7-
attr_accessor :proxy
7+
attr_accessor :proxy, :proxy_id
88
end
99

1010
def setup
@@ -21,4 +21,10 @@ def setup
2121
refute_valid @validatable
2222
assert_equal ['does not have the DNS feature'], @validatable.errors[:proxy_id]
2323
end
24+
25+
test 'should fail when id is present but association returns nil' do
26+
@validatable.proxy_id = FactoryBot.create(:dhcp_smart_proxy).id
27+
refute_valid @validatable
28+
assert_equal ['does not have the DNS feature'], @validatable.errors[:proxy_id]
29+
end
2430
end

0 commit comments

Comments
 (0)