Files
danbooru/app/models/ip_address.rb
BrokenEagle 63b3503bfc Add ability to use nested only parameter
- The only string works much the same as before with its comma separation
-- Nested includes are indicated with square brackets "[ ]"
-- The nested include is the value immediately preceding the square brackets
-- The only string is the comma separated string inside those brackets
- Default includes are split between format types when necessary
-- This prevents unnecessary includes from being added on page load
- Available includes are those items which are allowed to be accessible to the user
-- Some aren't because they are sensitive, such as the creator of a flag
-- Some aren't because the number of associated items is too large
- The amount of times the same model can be included to prevent recursions
-- One exception is the root model may include the same model once
--- e.g. the user model can include the inviter which is also the user model
-- Another exception is if the include is a has_many association
--- e.g. artist urls can include the artist, and then artist urls again
2020-02-12 23:58:53 +00:00

57 lines
1.4 KiB
Ruby

class IpAddress < ApplicationRecord
belongs_to :model, polymorphic: true
belongs_to :user
attribute :ip_addr, IpAddressType.new
def self.model_types
%w[Post User Comment Dmail ArtistVersion ArtistCommentaryVersion NoteVersion WikiPageVersion]
end
def self.visible(user)
CurrentUser.is_admin? ? all : where.not(model_type: "Dmail")
end
def self.search(params)
q = super
q = q.search_attributes(params, :user, :model_type, :model_id, :ip_addr)
q.order(created_at: :desc)
end
def self.group_by_ip_addr(ipv4_masklen = nil, ipv6_masklen = nil)
ipv4_masklen ||= 32
ipv6_masklen ||= 128
q = select(sanitize_sql([<<~SQL, ipv4_masklen, ipv6_masklen]))
CASE
WHEN family(ip_addr) = 4
THEN network(set_masklen(ip_addr, ?))
ELSE network(set_masklen(ip_addr, ?))
END AS ip_addr,
COUNT(*) AS count_all
SQL
q.group("1").reorder("count_all DESC, ip_addr")
end
def self.group_by_user
group(:user_id).select("user_id, COUNT(*) AS count_all").reorder("count_all DESC, user_id")
end
def to_s
# include the subnet mask only when the IP denotes a subnet.
(ip_addr.size > 1) ? ip_addr.to_string : ip_addr.to_s
end
def readonly?
true
end
def html_data_attributes
super & attributes.keys.map(&:to_sym)
end
def self.available_includes
[:user, :model]
end
end