<commit_before>class DatasetsController < ApplicationController
  load_and_authorize_resource
  skip_authorize_resource :only => :show

  before_filter :authenticate_user!, only: :dashboard
  before_filter :authenticate_user_from_token!, only: [:create, :update_certificate, :import_status]
  before_filter(:only => [:show, :index]) { alternate_formats [:feed, :json] }

  def info
    respond_to do |format|
      format.json do
        render json: {
          published_certificate_count: Dataset.published_count
        }
      end
    end
  end

  def index

    @datasets = Dataset
                .where(removed: false)
                .includes(:response_set, :certificate)
                .joins(:response_set)
                .order('certificates.published_at DESC')

    @title = t('datasets.datasets')

    if params[:jurisdiction]
      @datasets = @datasets.joins(response_set: :survey)
                           .merge(Survey.where(title: params[:jurisdiction]))
    end

    if params[:publisher]
      @datasets = @datasets.joins(response_set: :certificate)
                           .merge(Certificate.where(curator: params[:publisher]))
    end

    if params[:search]
      @title = t('datasets.search_results')

      base = @datasets.joins(:certificate).joins({response_set: :survey}).reorder('')

      # this is far from ideal - loads in all matches then limits for pagination
      results = base.merge(Certificate.search(name_cont: params[:search]).result).all +
                base.merge(Certificate.search(curator_cont: params[:search]).result).all +
                base.merge(Survey.search(full_title_cont: params[:search]).result).all

      @datasets = Kaminari.paginate_array(results.flatten.uniq).page params[:page]
    else
      @datasets = @datasets.page params[:page]
    end

    respond_to do |format|<commit_msg>Use the features of ransack to filter in the database<commit_after>class DatasetsController < ApplicationController
  load_and_authorize_resource
  skip_authorize_resource :only => :show

  before_filter :authenticate_user!, only: :dashboard
  before_filter :authenticate_user_from_token!, only: [:create, :update_certificate, :import_status]
  before_filter(:only => [:show, :index]) { alternate_formats [:feed, :json] }

  def info
    respond_to do |format|
      format.json do
        render json: {
          published_certificate_count: Dataset.published_count
        }
      end
    end
  end

  def index

    datasets = Dataset
                .where(removed: false)
                .includes(:response_set, :certificate)
                .joins(:response_set)
                .order('certificates.published_at DESC')

    @title = t('datasets.datasets')

    if params[:jurisdiction]
      datasets = datasets.joins(response_set: :survey)
                           .merge(Survey.where(title: params[:jurisdiction]))
    end

    if params[:publisher]
      datasets = datasets.joins(response_set: :certificate)
                           .merge(Certificate.where(curator: params[:publisher]))
    end

    if term = params[:search]
      @title = t('datasets.search_results')

      datasets = datasets.joins(:certificate)
        .joins({response_set: :survey})
        .reorder('')
        .search(
          m: 'or',
          certificates_name_cont: term,
          certificates_curator_cont: term,
          survey_full_title_cont: term)
        .result
    end

    @datasets = datasets.page params[:page]

    respond_to do |format|
{'fraction_non_alphanumeric': 0.10635871114913031, 'fraction_numerical': 0.0, 'mean_word_length': 2.747863247863248, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7233747', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1012, 'n_words': 205}