119627029-modifications_for_model_and rake

This commit is contained in:
Stas 2016-06-30 11:15:56 +03:00
parent 8963c1d304
commit 7ef8ad5a46
3 changed files with 85 additions and 10 deletions

View file

@ -32,18 +32,28 @@ class LegalDocument < ActiveRecord::Base
def save_to_filesystem
loop do
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775)
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
digest = Digest::SHA1.new
ld = LegalDocument.where(checksum: digest.update(Base64.decode64(body)))
if !ld
loop do
rand = SecureRandom.random_number.to_s.last(4)
next if rand.to_i == 0 || rand.length < 4
dir = "#{ENV['legal_documents_dir']}/#{Time.zone.now.strftime('%Y/%m/%d')}"
FileUtils.mkdir_p(dir, mode: 0775)
self.path = "#{dir}/#{Time.zone.now.to_formatted_s(:number)}_#{rand}.#{document_type}"
break unless File.file?(path)
end
File.open(path, 'wb') { |f| f.write(Base64.decode64(body)) } unless Rails.env.test?
self.path = path
else
self.path = ld.first.path
end
File.open(path, 'wb') { |f| f.write(Base64.decode64(body)) } unless Rails.env.test?
self.path = path
end
def add_creator