Module: ControllerMixins::CsvInstanceMethods
- Defined in:
- lib/controller_mixins/csv_instance_methods.rb
Instance Method Summary collapse
- #generate_and_return_csv(csv_array, name = "export.csv", encoding = "UTF-8", delimiter = ";") ⇒ Object
- #get_data_for_csv_from_settings(settings, encoding = "UTF-8") ⇒ Object
- #make_import_csv(model_class, import_settings = nil) ⇒ Object
- #to_csv(objects, skip_attributes = [], delimiter = ",") ⇒ Object
Instance Method Details
#generate_and_return_csv(csv_array, name = "export.csv", encoding = "UTF-8", delimiter = ";") ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/controller_mixins/csv_instance_methods.rb', line 18 def generate_and_return_csv(csv_array, name = "export.csv", encoding = "UTF-8", delimiter = ";") csv = CSV.generate(encoding: encoding, col_sep: delimiter, force_quotes: true) do |csv| csv_array.each do |row| csv << row end end #Then spit out the csv with BOM as the response: csv = "\xEF\xBB\xBF".force_encoding("UTF-8") + csv if ["UTF-8"].include?(encoding) csv = "\xFF\xFE".force_encoding("UTF-16LE") + csv if ["UTF-16LE"].include?(encoding) send_data csv, :type => 'text/csv', :filename => name #,:disposition => 'attachment' end |
#get_data_for_csv_from_settings(settings, encoding = "UTF-8") ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/controller_mixins/csv_instance_methods.rb', line 34 def get_data_for_csv_from_settings(settings, encoding = "UTF-8") data_for_csv = [] header_of_names = [] header_of_labels = [] settings[:columns].each do |c| header_of_names << c[:name].to_s.encode(encoding, "UTF-8") header_of_labels << c[:label].to_s.encode(encoding, "UTF-8") end data_for_csv << header_of_names data_for_csv << header_of_labels settings[:data].each do |c| row = [] c.each_pair do |name, value| if value.kind_of?(Hash) val = value[:title].blank? ? "" : value[:title] row << val.to_s.encode(encoding, "UTF-8") else row << value.to_s.encode(encoding, "UTF-8") end end data_for_csv << row end data_for_csv end |
#make_import_csv(model_class, import_settings = nil) ⇒ Object
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/controller_mixins/csv_instance_methods.rb', line 62 def make_import_csv(model_class, import_settings = nil) if request.post? && params[:file].present? col_sep = params[:col_sep] col_sep = ";" if col_sep.blank? infile = params[:file].read # crapy crapper DELETE all spaces from beginning and the end infile = infile.encode("UTF-8", "Windows-1250") bom = "\xEF\xBB\xBF".force_encoding("UTF-8") infile.gsub!(/^#{bom}/, "") infile.gsub!(/#{bom}$/, "") # whether it should be updated is decided by user by checkbox update_existing = params[:update_existing] unique_attribute_for_update = import_settings[:unique_attribute_for_update] # additional row data will be added to each row additional_row_data = import_settings[:additional_row_data] @successful_updates = 0 @successful_creates = 0 @errors = [] @label_header = [] header = [] white_list = model_class.get_white_list(import_settings) row_number = 0 CSV.parse(infile, :encoding => "UTF-8", :col_sep => col_sep) do |row| row_number += 1 # SKIP: header if row_number == 1 header = model_class.build_header_from_csv(row, white_list) next end if row_number == 2 @label_header = model_class.build_header_from_csv(row, white_list) next end row_data = model_class.build_record_from_csv(row, header, white_list) # merging with additional data if there are some row_data.merge!(additional_row_data) unless additional_row_data.blank? import_operation = :create if update_existing # updating is allowed if (row_obj = model_class.where(unique_attribute_for_update => row_data[unique_attribute_for_update]).first) import_operation = :update row_obj.assign_attributes(row_data) else row_obj = model_class.new(row_data) end else row_obj = model_class.new(row_data) end if row_obj.save case import_operation when :create @successful_creates += 1 when :update @successful_updates += 1 end else = "" row_obj.errors..each do |msg| += ". " unless .blank? += msg end @errors << {:row_number => row_number, :row => row, :error_message => } end # build_from_csv method will map customer attributes & # build new customer record #customer = Customer.build_from_csv(row) ## Save upon valid ## otherwise collect error records to export #if customer.valid? # customer.save #else # errs << row #end end render :action => :import_csv else flash[:error] = "MusÃte vybrat soubor pro import " redirect_to :action => :import_csv end end |
#to_csv(objects, skip_attributes = [], delimiter = ",") ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/controller_mixins/csv_instance_methods.rb', line 6 def to_csv(objects, skip_attributes=[], delimiter= ",") return "" if objects.blank? objects_class = objects.first.class filtered_columns = objects_class.column_names - skip_attributes CSV.generate do |csv| csv << filtered_columns objects.each do |object| csv << filtered_columns.collect { |a| object.attributes[a].blank? ? '' : "'#{object.attributes[a]}'" } end end end |