Revision 35241dd6
Added by Marek Hulán over 8 years ago
app/models/nic/base.rb | ||
---|---|---|
after_validation :set_validated
|
||
before_destroy :not_required_interface
|
||
|
||
validates :mac, :uniqueness => {:scope => :virtual},
|
||
:if => Proc.new { |nic| nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? && !nic.virtual? }, :allow_blank => true
|
||
validate :mac_uniqueness,
|
||
:if => Proc.new { |nic| nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? && !nic.virtual? && nic.mac.present? }
|
||
validates :mac, :presence => true,
|
||
:if => Proc.new { |nic| nic.managed? && nic.host_managed? && !nic.host.compute? && !nic.virtual? }
|
||
validates :mac, :mac_address => true, :allow_blank => true
|
||
... | ... | |
errors.add(:subnet, _("is not defined for host's organization.")) unless include_or_empty?(self.subnet.organizations, self.host.organization)
|
||
end
|
||
|
||
def mac_uniqueness
|
||
interface_attribute_uniqueness(:mac)
|
||
end
|
||
|
||
private
|
||
|
||
def interface_attribute_uniqueness(attr, base = Nic::Base.scoped)
|
||
in_memory_candidates = self.host.present? ? self.host.interfaces.select { |i| i.persisted? && !i.marked_for_destruction? } : [self]
|
||
db_candidates = base.where(attr => self.public_send(attr))
|
||
db_candidates = db_candidates.select { |c| c.id != self.id && in_memory_candidates.map(&:id).include?(c.id) }
|
||
errors.add(attr, :taken) if db_candidates.present?
|
||
end
|
||
|
||
def include_or_empty?(list, item)
|
||
(list.empty? && item.nil?) || list.include?(item)
|
||
end
|
Also available in: Unified diff
Fixes #11034 - custom uniqueness validations for interface attributes