Class: Gluttonberg::Content::ImportExportCSV::ImportUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/gluttonberg/content/import_export_csv.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, local_options = {}, klass, csv_table) ⇒ ImportUtils

Returns a new instance of ImportUtils.



160
161
162
163
164
165
166
167
168
# File 'lib/gluttonberg/content/import_export_csv.rb', line 160

def initialize(file_path , local_options = {}, klass, csv_table )
  self.file_path = file_path
  self.local_options = local_options
  self.klass = klass
  self.csv_table = csv_table
  self.records = []
  self.feedback = []
  self.all_valid = true #assume all records are valid.
end

Instance Attribute Details

#all_validObject

Returns the value of attribute all_valid.



157
158
159
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157

def all_valid
  @all_valid
end

#csv_tableObject

Returns the value of attribute csv_table.



156
157
158
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156

def csv_table
  @csv_table
end

#feedbackObject

Returns the value of attribute feedback.



157
158
159
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157

def feedback
  @feedback
end

#file_pathObject

Returns the value of attribute file_path.



156
157
158
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156

def file_path
  @file_path
end

#import_column_namesObject

Returns the value of attribute import_column_names.



158
159
160
# File 'lib/gluttonberg/content/import_export_csv.rb', line 158

def import_column_names
  @import_column_names
end

#import_columnsObject

Returns the value of attribute import_columns.



157
158
159
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157

def import_columns
  @import_columns
end

#klassObject

Returns the value of attribute klass.



156
157
158
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156

def klass
  @klass
end

#local_optionsObject

Returns the value of attribute local_options.



156
157
158
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156

def local_options
  @local_options
end

#recordsObject

Returns the value of attribute records.



157
158
159
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157

def records
  @records
end

#wysiwyg_columnsObject

Returns the value of attribute wysiwyg_columns.



157
158
159
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157

def wysiwyg_columns
  @wysiwyg_columns
end

#wysiwyg_columns_namesObject

Returns the value of attribute wysiwyg_columns_names.



158
159
160
# File 'lib/gluttonberg/content/import_export_csv.rb', line 158

def wysiwyg_columns_names
  @wysiwyg_columns_names
end

Class Method Details

.import(file_path, local_options = {}, klass, csv_table) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gluttonberg/content/import_export_csv.rb', line 170

def self.import(file_path , local_options = {}, klass, csv_table)
  import_utils = ImportUtils.new(file_path, local_options, klass, csv_table)
  import_utils.prepare_import_columns

  csv_table.each_with_index do |row , index |
    if index > 0 # ignore first row because its meta data row
      import_utils.import_row(row)
    end # if csv row index > 0

  end #loop
  import_utils.assign_wysiwyg_columns
  import_utils.all_valid ? true : import_utils.feedback
end

Instance Method Details

#_prepare_import_column_namesObject



212
213
214
215
216
217
218
219
220
# File 'lib/gluttonberg/content/import_export_csv.rb', line 212

def _prepare_import_column_names
  self.import_column_names = klass.import_export_columns.dup
  if local_options && local_options.has_key?(:import_columns)
    self.import_column_names = local_options[:import_columns].dup
  end
  if import_column_names.blank?
    raise "Please define import_export_columns property"
  end
end

#_prepare_wysiwyg_column_namesObject



222
223
224
225
226
227
# File 'lib/gluttonberg/content/import_export_csv.rb', line 222

def _prepare_wysiwyg_column_names
  self.wysiwyg_columns_names = klass.wysiwyg_columns
  if local_options && local_options.has_key?(:wysiwyg_columns)
    self.wysiwyg_columns_names = local_options[:wysiwyg_columns]
  end
end

#all_import_columnsObject



242
243
244
245
246
# File 'lib/gluttonberg/content/import_export_csv.rb', line 242

def all_import_columns
  temp_columns = self.import_columns.blank? ? {} : self.import_columns.dup
  temp_columns = temp_columns.merge(self.wysiwyg_columns.dup) unless self.wysiwyg_columns.blank?
  temp_columns
end

#assign_wysiwyg_column(record) ⇒ Object



301
302
303
304
305
306
307
308
# File 'lib/gluttonberg/content/import_export_csv.rb', line 301

def assign_wysiwyg_column(record)
  unless self.wysiwyg_columns_names.blank?
    self.wysiwyg_columns_names.each do |c|
      record.send("#{c}=", klass.helper.simple_format(record.send(c)))
    end
  end
  record.save
end

#assign_wysiwyg_columnsObject



293
294
295
296
297
298
299
# File 'lib/gluttonberg/content/import_export_csv.rb', line 293

def assign_wysiwyg_columns
  if self.all_valid
    records.each do |record|
      assign_wysiwyg_column(record)
    end
  end
end

#find_column_position(col_name) ⇒ Object

csv_table is two dimentional array col_name is a string. if structure is proper and column name found it returns column index from 0 to n-1 otherwise nil



188
189
190
191
192
193
194
195
# File 'lib/gluttonberg/content/import_export_csv.rb', line 188

def find_column_position(col_name)
  if csv_table.instance_of?(Array) && csv_table.count > 0 && csv_table.first.count > 0
    csv_table.first.each_with_index do |table_col , index|
      return index if table_col.to_s.upcase == col_name.to_s.upcase
    end
  end
  nil
end

#find_or_initialize_record(record_info) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/gluttonberg/content/import_export_csv.rb', line 267

def find_or_initialize_record(record_info)
  record = nil
  if local_options[:unique_key]
    val = record_info[local_options[:unique_key].to_s]
    val = record_info[local_options[:unique_key].to_sym] if val.blank?
    record = klass.where(local_options[:unique_key] => val).first
  end

  if record.blank?
    record = new_record(record_info)
  end

  record_info.each do |field,val|
    record.send("#{field}=",val) unless field.to_s == "id"
  end
  record
end

#import_row(row) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gluttonberg/content/import_export_csv.rb', line 229

def import_row(row)
  record_info = prepare_record_info(row)
  record = find_or_initialize_record(record_info)

  self.records << record
  if record.valid?
    self.feedback << true
  else
    feedback << record.errors
    self.all_valid = false
  end
end

#new_record(record_info) ⇒ Object



285
286
287
288
289
290
291
# File 'lib/gluttonberg/content/import_export_csv.rb', line 285

def new_record(record_info)
  if klass.respond_to?(:localized?) && klass.localized?
    klass.new_with_localization
  else
    klass.new
  end
end

#prepare_import_columnsObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/gluttonberg/content/import_export_csv.rb', line 197

def prepare_import_columns
  _prepare_import_column_names
  _prepare_wysiwyg_column_names

  self.import_columns = {}
  self.import_column_names.each do |key|
    self.import_columns[key] = find_column_position(key)
  end

  self.wysiwyg_columns = {} 
  self.wysiwyg_columns_names.each do |key|
    self.wysiwyg_columns[key] = find_column_position(key)
  end
end

#prepare_record_info(row) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/gluttonberg/content/import_export_csv.rb', line 248

def prepare_record_info(row)
  record_info = {}

  self.all_import_columns.each do |key , val|
    if !val.blank? && val >= 0
      record_info[key] = row[val]
      record_info[key] = record_info[key].force_encoding("UTF-8") if row[val].kind_of?(String)
    end
  end

  unless self.local_options[:additional_attributes].blank?
    self.local_options[:additional_attributes].each do |key, val|
      record_info[key] = (val.kind_of?(String) ? val.force_encoding("UTF-8") : val)
    end
  end

  record_info
end