# frozen_string_literal: true require 'ipaddr' # Provides a +request_attributes+ method for actions that handle # +UserAttributable+ objects. It returns a hash that can be inserted into params # or passed directly via assign_attributes/update_attributes. module UserAttribution def request_attributes { ip: _request_ip, user_agent: request.env['HTTP_USER_AGENT'], referrer: request.referer, user: current_user } end # apply a /64 mask to an IPv6 address if we have one, otherwise just use the v4 address as-is. def _request_ip ip = IPAddr.new(request.remote_ip) ip.ipv6? ? ip.mask(64) : ip end end