Module: ImportFile::InstanceMethods

Defined in:
lib/enju_leaf/import_file.rb

Instance Method Summary collapse

Instance Method Details

#convert_encoding(line) ⇒ Object

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

Parameters:

  • line (String)

    変換する文字列



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/enju_leaf/import_file.rb', line 35

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
      string = line.encode('UTF-8', user_encoding, universal_newline: true)
    rescue StandardError
      string = nkf_encode(line)
    end
  else
    string = nkf_encode(line)
  end
end

#import_startObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/enju_leaf/import_file.rb', line 20

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

#send_messageObject

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



56
57
58
59
60
61
62
63
64
# File 'lib/enju_leaf/import_file.rb', line 56

def send_message
  sender = User.find(1)
  locale = user.profile.try(:locale) || I18n.default_locale.to_s
  message_template = MessageTemplate.localized_template('import_completed', locale)
  request = MessageRequest.new
  request.assign_attributes({sender: sender, receiver: user, message_template: message_template})
  request.save_message_body
  request.transition_to!(:sent)
end