<commit_before>class TaxonConceptsController < ApplicationController
  include ActionController::MimeResponds

  def index
    @checklist = Checklist::Checklist.new(params)
    render :json => @checklist.generate(params[:page], params[:per_page])
  end

  def download_index
    send_checklist_index_file
  end

  def download_history
    send_checklist_history_file
  end

  def autocomplete
    taxon_concepts = MTaxonConcept.by_designation('CITES').
      without_nc.
      select("
        full_name, rank_name,
        ARRAY(
          SELECT * FROM UNNEST(synonyms_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        ) AS synonyms_ary,
        ARRAY(
          SELECT * FROM UNNEST(english_names_ary) name WHERE REGEXP_REPLACE(name, '(.+) (.+)', '\\2, \\1') ILIKE '#{params[:scientific_name]}%'
        ) AS english_names_ary,
        ARRAY(
          SELECT * FROM UNNEST(french_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        ) AS french_names_ary,
        ARRAY(
          SELECT * FROM UNNEST(spanish_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        ) AS spanish_names_ary"
      ).
      where("
        full_name ILIKE '#{params[:scientific_name]}%'
        OR
        EXISTS (
          SELECT * FROM UNNEST(synonyms_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
          UNION
          SELECT * FROM UNNEST(english_names_ary) name WHERE REGEXP_REPLACE(name, '(.+) (.+)', '\\2, \\1') ILIKE '#{params[:scientific_name]}%'
          UNION
          SELECT * FROM UNNEST(french_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
          UNION
          SELECT * FROM UNNEST(spanish_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        )
      ").
      limit(params[:per_page]).
      order("LENGTH(taxonomic_position), full_name")
    render :json => taxon_concepts.to_json(:methods => [:full_name, :rank_name, :matching_names])
  end

  def summarise_filters
    render :text => Checklist::Checklist.new(params).summarise_filters
  end

  private
  def send_checklist_index_file
    ch = if params[:format] == 'pdf'
      Checklist::Pdf::Index.new(params)
    elsif params[:format] == 'csv'
      Checklist::Csv::Index.new(params)
    elsif params[:format] == 'json'
      Checklist::Json::Index.new(params)
    end
    send_checklist_file(ch) unless ch.blank?
  end

  def send_checklist_history_file
    ch = if params[:format] == 'pdf'
      Checklist::Pdf::History.new(params)
    elsif params[:format] == 'csv'
      Checklist::Csv::History.new(params)
    elsif params[:format] == 'json'
      Checklist::Json::History.new(params)
    end
    send_checklist_file(ch) unless ch.blank?
  end

  def send_checklist_file(checklist)
    @download_path = checklist.generate
    send_file(@download_path,
      :filename => checklist.download_name,
      :type => checklist.ext)
    FileUtils.rm @download_path
  end

end<commit_msg>simplified controller code<commit_after>class TaxonConceptsController < ApplicationController
  include ActionController::MimeResponds

  def index
    @checklist = Checklist::Checklist.new(params)
    render :json => @checklist.generate(params[:page], params[:per_page])
  end

  def download_index
    send_checklist_index_file
  end

  def download_history
    send_checklist_history_file
  end

  def autocomplete
    taxon_concepts = MTaxonConcept.by_designation('CITES').
      without_nc.
      select("
        full_name, rank_name,
        ARRAY(
          SELECT * FROM UNNEST(synonyms_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        ) AS synonyms_ary,
        ARRAY(
          SELECT * FROM UNNEST(english_names_ary) name WHERE REGEXP_REPLACE(name, '(.+) (.+)', '\\2, \\1') ILIKE '#{params[:scientific_name]}%'
        ) AS english_names_ary,
        ARRAY(
          SELECT * FROM UNNEST(french_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        ) AS french_names_ary,
        ARRAY(
          SELECT * FROM UNNEST(spanish_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        ) AS spanish_names_ary"
      ).
      where("
        full_name ILIKE '#{params[:scientific_name]}%'
        OR
        EXISTS (
          SELECT * FROM UNNEST(synonyms_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
          UNION
          SELECT * FROM UNNEST(english_names_ary) name WHERE REGEXP_REPLACE(name, '(.+) (.+)', '\\2, \\1') ILIKE '#{params[:scientific_name]}%'
          UNION
          SELECT * FROM UNNEST(french_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
          UNION
          SELECT * FROM UNNEST(spanish_names_ary) name WHERE name ILIKE '#{params[:scientific_name]}%'
        )
      ").
      limit(params[:per_page]).
      order("LENGTH(taxonomic_position), full_name")
    render :json => taxon_concepts.to_json(:methods => [:full_name, :rank_name, :matching_names])
  end

  def summarise_filters
    render :text => Checklist::Checklist.new(params).summarise_filters
  end

  private
  def send_checklist_index_file
    klass = if params[:format] == 'pdf'
      Checklist::Pdf::Index
    elsif params[:format] == 'csv'
      Checklist::Csv::Index
    elsif params[:format] == 'json'
      Checklist::Json::Index
    end
    send_checklist_file(klass)
  end

  def send_checklist_history_file
    klass = if params[:format] == 'pdf'
      Checklist::Pdf::History
    elsif params[:format] == 'csv'
      Checklist::Csv::History
    elsif params[:format] == 'json'
      Checklist::Json::History
    end
    send_checklist_file(klass)
  end

  def send_checklist_file(klass)
    checklist = klass.new(params)
    @download_path = checklist.generate
    send_file(@download_path,
      :filename => checklist.download_name,
      :type => checklist.ext)
    FileUtils.rm @download_path
  end

end
{'fraction_non_alphanumeric': 0.11955593509820667, 'fraction_numerical': 0.0013663535439795047, 'mean_word_length': 3.246555474981871, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123376', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1877, 'n_words': 377}