Class: AgentImportFile

Inherits:
ApplicationRecord
  • Object
show all
Includes:
ImportFile, Statesman::Adapters::ActiveRecordQueries
Defined in:
app/models/agent_import_file.rb,
app/models2/agent_import_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



35
36
37
# File 'app/models/agent_import_file.rb', line 35

def mode
  @mode
end

Class Method Details

.importObject



102
103
104
105
106
107
108
# File 'app/models/agent_import_file.rb', line 102

def self.import
  AgentImportFile.not_imported.each do |file|
    file.import_start
  end
rescue
  Rails.logger.info "#{Time.zone.now} importing agents failed!"
end

Instance Method Details

#importObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/agent_import_file.rb', line 57

def import
  transition_to!(:started)
  num = { agent_imported: 0, user_imported: 0, failed: 0 }
  rows = open_import_file
  field = rows.first
  row_num = 1
  if [field['first_name'], field['last_name'], field['full_name']].reject{|field| field.to_s.strip == ""}.empty?
    raise "You should specify first_name, last_name or full_name in the first line"
  end
  #rows.shift

  AgentImportResult.create!(agent_import_file_id: id, body: rows.headers.join("\t"))
  rows.each do |row|
    row_num += 1
    import_result = AgentImportResult.create!(agent_import_file_id: id, body: row.fields.join("\t"))
    next if row['dummy'].to_s.strip.present?

    agent = Agent.new
    agent = set_agent_value(agent, row)

    if agent.save!
      import_result.agent = agent
      num[:agent_imported] += 1
      if row_num % 50 == 0
        Sunspot.commit
        GC.start
      end
    end

    import_result.save!
  end
  Sunspot.commit
  rows.close
  transition_to!(:completed)
  mailer = AgentImportMailer.completed(self)
  send_message(mailer)
  return num
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  transition_to!(:failed)
  mailer = AgentImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#import_startObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/agent_import_file.rb', line 44

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

#modifyObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/agent_import_file.rb', line 110

def modify
  transition_to!(:started)
  rows = open_import_file
  rows.shift
  row_num = 1

  rows.each do |row|
    row_num += 1
    next if row['dummy'].to_s.strip.present?
    agent = Agent.find_by(id: row['id'])
    if agent
      agent.full_name = row['full_name'] if row['full_name'].to_s.strip.present?
      agent.full_name_transcription = row['full_name_transcription'] if row['full_name_transcription'].to_s.strip.present?
      agent.first_name = row['first_name'] if row['first_name'].to_s.strip.present?
      agent.first_name_transcription = row['first_name_transcription'] if row['first_name_transcription'].to_s.strip.present?
      agent.middle_name = row['middle_name'] if row['middle_name'].to_s.strip.present?
      agent.middle_name_transcription = row['middle_name_transcription'] if row['middle_name_transcription'].to_s.strip.present?
      agent.last_name = row['last_name'] if row['last_name'].to_s.strip.present?
      agent.last_name_transcription = row['last_name_transcription'] if row['last_name_transcription'].to_s.strip.present?
      agent.address_1 = row['address_1'] if row['address_1'].to_s.strip.present?
      agent.address_2 = row['address_2'] if row['address_2'].to_s.strip.present?
      agent.save!
    end
  end
  transition_to!(:completed)
  mailer = AgentImportMailer.completed(self)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  transition_to!(:failed)
  mailer = AgentImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#removeObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/agent_import_file.rb', line 145

def remove
  transition_to!(:started)
  rows = open_import_file
  rows.shift
  row_num = 1

  rows.each do |row|
    row_num += 1
    next if row['dummy'].to_s.strip.present?
    agent = Agent.find_by(id: row['id'].to_s.strip)
    if agent
      agent.picture_files.destroy_all
      agent.reload
      agent.destroy
    end
  end
  transition_to!(:completed)
  mailer = AgentImportMailer.completed(self)
  send_message(mailer)
rescue => e
  self.error_message = "line #{row_num}: #{e.message}"
  transition_to!(:failed)
  mailer = AgentImportMailer.failed(self)
  send_message(mailer)
  raise e
end

#state_machineObject



37
38
39
# File 'app/models/agent_import_file.rb', line 37

def state_machine
  AgentImportFileStateMachine.new(self, transition_class: AgentImportFileTransition)
end