Module: Gluttonberg::Content::ImportExportCSV::ClassMethods

Defined in:
lib/gluttonberg/content/import_export_csv.rb

Defined Under Namespace

Classes: GlosentryHelper

Instance Method Summary collapse

Instance Method Details

#exportCSV(all_records, local_options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/gluttonberg/content/import_export_csv.rb', line 156

def exportCSV(all_records , local_options = {})
  export_column_names = self.import_export_columns
  if local_options && local_options.has_key?(:export_columns)
    export_column_names = local_options[:export_columns]
  end

  if export_column_names.blank?
    raise "Please define export_column_names property"
  end

  export_column_names << "published_at"
  export_column_names << "updated_at"

  csv_class_name = nil
  if RUBY_VERSION >= "1.9"
    require 'csv'
    csv_class_name = CSV
  else
    csv_class_name = FasterCSV
  end

  csv_string = csv_class_name.generate do |csv|
      csv << export_column_names

      all_records.each do |record|
          row = []
          export_column_names.each do |column|
            row << record.send(column)
          end
          csv << row
      end
  end

  csv_string
end

#find_column_position(csv_table, 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



145
146
147
148
149
150
151
152
153
154
# File 'lib/gluttonberg/content/import_export_csv.rb', line 145

def find_column_position(csv_table  , 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
    nil
  else
    nil
  end
end

#helperObject



137
138
139
# File 'lib/gluttonberg/content/import_export_csv.rb', line 137

def helper
  @h ||= GlosentryHelper.new
end

#import_export_csv(import_export_columns = nil, wysiwyg_columns = nil) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/gluttonberg/content/import_export_csv.rb', line 24

def import_export_csv(import_export_columns=nil,wysiwyg_columns=nil)
  #@@wysiwyg_columns = wysiwyg_columns
  if import_export_columns.blank?
    self.import_export_columns = self.new.attributes.keys
  else
    self.import_export_columns = import_export_columns
  end
end

#importCSV(file_path, local_options = {}) ⇒ Object

takes complete path to csv file. if all records are created successfully then return true otherwise returns array of feedback. each value represents the feedback for respective row in csv sample feedback array : [true , true , [active_record error array…] , true]



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gluttonberg/content/import_export_csv.rb', line 38

def importCSV(file_path , local_options = {})
  begin
    if RUBY_VERSION >= "1.9"
      require 'csv'
      csv_table = CSV.read(file_path)
    else
      csv_table = FasterCSV.read(file_path)
    end
  rescue => e
    return "Please provide a valid CSV file with correct column names."
  end

  import_column_names = self.import_export_columns
  if local_options && local_options.has_key?(:import_columns)
    import_column_names = local_options[:import_columns]
  end

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


  if import_column_names.blank?
    raise "Please define import_export_columns property"
  end

  import_columns = {}

  import_column_names.each do |key|
    import_columns[key] = self.find_column_position(csv_table , key )
  end

  feedback = []
  records = []
  all_valid = true #assume all records are valid.

  csv_table.each_with_index do |row , index |
    if index > 0 # ignore first row because its meta data row
      record_info = {}
      import_columns.each do |key , val|
        if !val.blank? && val >= 0
          if row[val].blank? || !row[val].kind_of?(String)
            record_info[key] = row[val]
          else
            record_info[key] = row[val].force_encoding("UTF-8")
          end
        end
      end
      record = nil
      if local_options[:unique_key]
        record = self.where(local_options[:unique_key] => record_info[local_options[:unique_key].to_s]).first
      end
      if record.blank?
        # make object
        if self.respond_to?(:localized?) && self.localized?
          record = self.new_with_localization(record_info)
        else
          record = self.new(record_info)
        end
      else
        record.attributes = record.attributes.merge(record_info)
      end

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

      records << record
      if record.valid?
        feedback << true
      else
        feedback << record.errors
        all_valid = false
      end
    end # if csv row index > 0

  end #loop

  if all_valid
    records.each do |record|
      unless wysiwyg_columns_names.blank?

        wysiwyg_columns_names.each do |c|
          record.send("#{c}=",helper.simple_format(record.send(c)))
        end
      end
      record.save
    end
  end

  all_valid ? true : feedback
end