<commit_before>require 'fastercsv'

module Noodall
  class Admin::FormResponsesController < Noodall::Admin::BaseController
    include SortableTable::App::Controllers::ApplicationController
    before_filter :find_form,:set_title

    def index
      if @form.nil?
        @responses = Noodall::FormResponse.all
      else
        @responses = @form.responses
      end

      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @responses }


        format.csv do
          csv_string = FasterCSV.generate do |csv|
            header_row = @form.fields.map do |field|
              field.name
            end
            header_row += ['Date', 'IP', 'Form Location']
            csv << header_row

            for response in @responses
              response_row = @form.fields.map do |field|
                begin
                  response.send(field.underscored_name)
                rescue NoMethodError => e
                  nil
                end
              end
              response_row += [response.created_at.to_formatted_s(:long_dot), response.ip, response.referrer]
              csv << response_row
            end
          end
          send_data csv_string, :filename => "#{@form.title} responses - #{Time.now.to_formatted_s(:db)}.csv",
                                :type => 'text/csv',
                                :disposition => 'attachment'
        end

      end
    end
<commit_msg>use normal CSV lib in ruby 1.9<commit_after>
# in ruby 1.9, fastercsv was included as csv
if RUBY_VERSION =~ /1\.8/
  require 'fastercsv'
  Abstracted_CSV_Class = FasterCSV
else
  require 'csv'
  Abstracted_CSV_Class = CSV
end

module Noodall
  class Admin::FormResponsesController < Noodall::Admin::BaseController
    include SortableTable::App::Controllers::ApplicationController
    before_filter :find_form,:set_title

    def index
      if @form.nil?
        @responses = Noodall::FormResponse.all
      else
        @responses = @form.responses
      end

      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @responses }


        format.csv do
          csv_string = Abstracted_CSV_Class.generate do |csv|
            header_row = @form.fields.map do |field|
              field.name
            end
            header_row += ['Date', 'IP', 'Form Location']
            csv << header_row

            for response in @responses
              response_row = @form.fields.map do |field|
                begin
                  response.send(field.underscored_name)
                rescue NoMethodError => e
                  nil
                end
              end
              response_row += [response.created_at.to_formatted_s(:long_dot), response.ip, response.referrer]
              csv << response_row
            end
          end
          send_data csv_string, :filename => "#{@form.title} responses - #{Time.now.to_formatted_s(:db)}.csv",
                                :type => 'text/csv',
                                :disposition => 'attachment'
        end

      end
    end

{'fraction_non_alphanumeric': 0.0904716073147257, 'fraction_numerical': 0.0019249278152069298, 'mean_word_length': 1.6995670995670995, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1459925', 'n_tokens_mistral': 897, 'n_tokens_neox': 855, 'n_words': 198}