复制功能

dev_home
daiao 5 years ago
parent a88ca6e8f9
commit e80280a9a8

@ -2,8 +2,26 @@ module Util
module UUID
module_function
DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
def time_uuid(format: '%Y%m%d%H%M%S', suffix: 8)
"#{Time.zone.now.strftime(format)}#{Random.rand(10**suffix).to_i}"
end
# 随机生成字符
def generate_identifier(container, num, pre='')
code = DCODES.sample(num).join
if container == User
while container.exists?(login: pre+code) do
code = DCODES.sample(num).join
end
else
while container.exists?(identifier: code) do
code = DCODES.sample(num).join
end
end
code
end
end
end

@ -60,6 +60,7 @@ class Subjects::CopySubjectService < ApplicationService
to_shixun = Shixun.new
to_shixun.attributes = shixun.attributes.dup.except('id', 'user_id', 'identifier', 'homepage_show',
'use_scope', 'averge_star', 'myshixuns_count')
to_shixun.identifier = Util::UUID.generate_identifier(Shixun, 8)
to_shixun.user_id = user_id
to_shixun.save!

Loading…
Cancel
Save