/
ip_address.rb
44 lines (32 loc) 路 943 Bytes
/
ip_address.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# == Schema Information
#
# Table name: ip_addresses
#
# id :integer not null, primary key
# ip_pool_id :integer
# ipv4 :string(255)
# ipv6 :string(255)
# created_at :datetime
# updated_at :datetime
# hostname :string(255)
# priority :integer
#
class IPAddress < ApplicationRecord
belongs_to :ip_pool
validates :ipv4, :presence => true, :uniqueness => true
validates :hostname, :presence => true
validates :ipv6, :uniqueness => {:allow_blank => true}
validates :priority, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100, only_integer: true }
scope :order_by_priority, -> { order(priority: :desc) }
before_validation :set_default_priority
private
def set_default_priority
return if priority.present?
self.priority = 100
end
class << self
def select_by_priority
order(Arel.sql("RAND() * priority DESC")).first
end
end
end