Class: Gluttonberg::Content::ImportExportCSV::ImportUtils
- Inherits:
-
Object
- Object
- Gluttonberg::Content::ImportExportCSV::ImportUtils
- Defined in:
- lib/gluttonberg/content/import_export_csv.rb
Instance Attribute Summary collapse
-
#all_valid ⇒ Object
Returns the value of attribute all_valid.
-
#csv_table ⇒ Object
Returns the value of attribute csv_table.
-
#feedback ⇒ Object
Returns the value of attribute feedback.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#import_column_names ⇒ Object
Returns the value of attribute import_column_names.
-
#import_columns ⇒ Object
Returns the value of attribute import_columns.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#local_options ⇒ Object
Returns the value of attribute local_options.
-
#records ⇒ Object
Returns the value of attribute records.
-
#wysiwyg_columns ⇒ Object
Returns the value of attribute wysiwyg_columns.
-
#wysiwyg_columns_names ⇒ Object
Returns the value of attribute wysiwyg_columns_names.
Class Method Summary collapse
Instance Method Summary collapse
- #_prepare_import_column_names ⇒ Object
- #_prepare_wysiwyg_column_names ⇒ Object
- #all_import_columns ⇒ Object
- #assign_wysiwyg_column(record) ⇒ Object
- #assign_wysiwyg_columns ⇒ Object
-
#find_column_position(col_name) ⇒ Object
csv_table is two dimentional array col_name is a string.
- #find_or_initialize_record(record_info) ⇒ Object
- #import_row(row) ⇒ Object
-
#initialize(file_path, local_options = {}, klass, csv_table) ⇒ ImportUtils
constructor
A new instance of ImportUtils.
- #new_record(record_info) ⇒ Object
- #prepare_import_columns ⇒ Object
- #prepare_record_info(row) ⇒ Object
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 , = {}, klass, csv_table ) self.file_path = file_path self. = 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_valid ⇒ Object
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_table ⇒ Object
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 |
#feedback ⇒ Object
Returns the value of attribute feedback.
157 158 159 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157 def feedback @feedback end |
#file_path ⇒ Object
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_names ⇒ Object
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_columns ⇒ Object
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 |
#klass ⇒ Object
Returns the value of attribute klass.
156 157 158 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156 def klass @klass end |
#local_options ⇒ Object
Returns the value of attribute local_options.
156 157 158 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156 def @local_options end |
#records ⇒ Object
Returns the value of attribute records.
157 158 159 |
# File 'lib/gluttonberg/content/import_export_csv.rb', line 157 def records @records end |
#wysiwyg_columns ⇒ Object
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_names ⇒ Object
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 , = {}, klass, csv_table) import_utils = ImportUtils.new(file_path, , 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_names ⇒ Object
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 && .has_key?(:import_columns) self.import_column_names = [:import_columns].dup end if import_column_names.blank? raise "Please define import_export_columns property" end end |
#_prepare_wysiwyg_column_names ⇒ Object
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 && .has_key?(:wysiwyg_columns) self.wysiwyg_columns_names = [:wysiwyg_columns] end end |
#all_import_columns ⇒ Object
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_columns ⇒ Object
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 [:unique_key] val = record_info[[:unique_key].to_s] val = record_info[[:unique_key].to_sym] if val.blank? record = klass.where([: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_columns ⇒ Object
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.[:additional_attributes].blank? self.[:additional_attributes].each do |key, val| record_info[key] = (val.kind_of?(String) ? val.force_encoding("UTF-8") : val) end end record_info end |