module CensorBear class ImageDataId attr_accessor :klass_name, :klass_id, :image_id, :user_id, :ip_address, :data_id def initialize(data_id) @klass_name, @klass_id, @image_id, @user_id, @ip_address = data_id.split("-") @data_id = data_id end def record @record ||= Object.const_get(@klass_name).find_by(id: @klass_id) end class << self def valid?(raw) arr = raw.split("-") return false if arr.length != 5 return false unless Object.const_defined?(arr.first) klass = Object.const_get(arr.first) record_id = arr[1] return false if record_id.blank? # 根据 ImageResult 来看 record 可以为空 # return false if klass.find_by(id: record_id).blank? true end def encode(obj, image_id: "", user_id: "", ip_address: "") # data_id 格式 "klass_name-klass_id-image_id-user_id-ip_adress" klass = obj.class.to_s id = obj.id "#{klass}-#{id}-#{image_id}-#{user_id}-#{ip_address}" end end end class TextDataId attr_accessor :klass_name, :klass_id, :user_id, :ip_address, :data_id def initialize(raw) @klass_name, @klass_id, @user_id, @ip_address = raw.split("-") @data_id = raw end def record @record ||= Object.const_get(@klass_name).find_by(id: @klass_id) end class << self def valid?(raw) # data_id 格式 "klass_name-klass_id-user_id-ip_adress" arr = raw.split("-") return false if arr.length != 4 return false unless Object.const_defined?(arr.first) klass = Object.const_get(arr.first) record_id = arr[1] return false if record_id.blank? # 根据 TextResult 来看 好像模型可以为空 # return false if klass.find_by(id: record_id).blank? true end def encode(obj, user_id: "", ip_address: "") # data_id 格式 "klass_name-klass_id-user_id-ip_adress" klass = obj.class.to_s id = obj.id "#{klass}-#{id}-#{user_id}-#{ip_address}" end end end end