Skip to content

Commit 8c2f07f

Browse files
adamruzickaofedoren
authored andcommitted
Fixes #38718 - Register feature scope in belongs_to_proxy
1 parent e07fa56 commit 8c2f07f

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

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/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/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)