msg7086
2017-09-29 04:25:11 +08:00
同推 Rails 的 Active Record。
随便贴一段 ORM 中保存前验证的代码感受下。
before_save :check_rules
def check_rules
conflicts = port_forward_rules.where(exposed_ip: exposed_ip, exposed_port: exposed_port).where.not(id: id)
# "UDP" only conflicts with "UDP" and "Both"
if udp?
conflicts = conflicts.where(protocol: [:udp, :both])
# "TCP" family never conflicts with "UDP"
elsif !both?
conflicts = conflicts.where.not(protocol: :udp)
end
# No conflicts at all? Great!
return if conflicts.empty?
conflict = conflicts.first
# If they are both HTTP or both HTTPS rule.
return if ( http? || https?) && conflict.protocol == protocol
throw(:abort)
end