app/models/effective/csv_importer.rb in effective_developer-0.0.3 vs app/models/effective/csv_importer.rb in effective_developer-0.0.4
- old
+ new
@@ -68,13 +68,13 @@
value = value.to_s
if column.ends_with?('?') # Boolean
::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
elsif column.ends_with?('_at') # DateTime
- Time.zone.parse(value) rescue nil
+ parse_datetime(column, value)
elsif column.ends_with?('_on') # Date
- Time.zone.parse(value).beginning_of_day rescue nil
+ parse_datetime(column, value).beginning_of_day
else
value.presence || ''.freeze
end
end
@@ -123,9 +123,17 @@
when :green ; 32
when :red ; 31
end
"\e[#{code}m#{text}\e[0m"
+ end
+
+ def parse_datetime(col, value)
+ begin
+ Time.zone.parse(value)
+ rescue => e
+ error("Unable to Time.zone.parse('#{value}'). Override parse_datetime() to parse your own time, something like:\n#{' ' * 6}def parse_datetime(col, value)\n#{' ' * 8}Time.strptime(value, '%m/%d/%Y %H:%M:%S').in_time_zone\n#{' ' * 6}end")
+ end
end
end
end