Skip to content

Commit 3e15614

Browse files
authored
Merge pull request rails#51633 from taketo1113/fix-changed-cidr
Fix PostgreSQL `Cidr#change?` to compare with address prefix
2 parents 4dbf7e3 + acc277b commit 3e15614

File tree

2 files changed

+18
-0
lines changed
  • activerecord

2 files changed

+18
-0
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def serialize(value)
2828
end
2929
end
3030

31+
# TODO: Remove when IPAddr#== compares IPAddr#prefix. See
32+
# https://github.com/ruby/ipaddr/issues/21
33+
def changed?(old_value, new_value, _new_value_before_type_cast)
34+
super || old_value.prefix != new_value.prefix
35+
end
36+
3137
def cast_value(value)
3238
if value.nil?
3339
nil

activerecord/test/cases/adapters/postgresql/network_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ def test_schema_dump_with_shorthand
9494
assert_match %r{t\.macaddr\s+"mac_address",\s+default: "ff:ff:ff:ff:ff:ff"}, output
9595
end
9696

97+
def test_cidr_change_prefix
98+
model = PostgresqlNetworkAddress.create(cidr_address: "192.168.1.0/24")
99+
model.cidr_address = "192.168.1.0/24"
100+
assert_not_predicate model, :changed?
101+
102+
model.cidr_address = "192.168.2.0/24"
103+
assert_predicate model, :changed?
104+
105+
model.cidr_address = "192.168.1.0/25"
106+
assert_predicate model, :changed?
107+
end
108+
97109
def test_mac_address_change_case_does_not_mark_dirty
98110
model = PostgresqlNetworkAddress.create(mac_address: "Ab:Cd:Ef:01:02:03")
99111
model.mac_address = model.mac_address.swapcase

0 commit comments

Comments
 (0)