Module: ImportFile

Extended by:
ActiveSupport::Concern
Included in:
UserImportFile
Defined in:
app/models/concerns/import_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.expireObject

失敗したインポート処理を一括削除します。



5
6
7
8
9
# File 'app/models/concerns/import_file.rb', line 5

def self.expire
  self.stucked.find_each do |file|
    file.destroy
  end
end

Instance Method Details

#convert_encoding(line) ⇒ Object

インポートするファイルの文字コードをUTF-8に変換します。

Parameters:

  • line (String)

    変換する文字列



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/concerns/import_file.rb', line 26

def convert_encoding(line)
  if defined?(CharlockHolmes::EncodingDetector)
    begin
      case user_encoding
      when 'auto_detect'
        encoding = CharlockHolmes::EncodingDetector.detect(line)[:encoding]
      when nil
        encoding = CharlockHolmes::EncodingDetector.detect(line)[:encoding]
      else
        encoding = user_encoding
      end
      line.encode('UTF-8', encoding, universal_newline: true)
    rescue StandardError
      nkf_encode(line)
    end
  else
    nkf_encode(line)
  end
end

#import_startObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/concerns/import_file.rb', line 11

def import_start
  case edit_mode
  when 'create'
    import
  when 'update'
    modify
  when 'destroy'
    remove
  else
    import
  end
end

#send_message(mailer) ⇒ Object

インポート完了時のメッセージを送信します。



47
48
49
50
51
52
53
54
55
# File 'app/models/concerns/import_file.rb', line 47

def send_message(mailer)
  sender = User.find(1)
  message = Message.create!(
    recipient: user.username,
    sender: sender,
    body: mailer.body.raw_source,
    subject: mailer.subject
  )
end