<commit_before>class SourcesController < ApplicationController
  include WithComment
  actions_without_auth :existence, :index, :show, :datatable, :suggestions_datatable, :update_source_suggestion

  def index
    (sources, presenter) = if params[:detailed] == 'true'
         [
           Source.includes(authors_sources: [:author])
             .joins(:evidence_items)
             .order('sources.id asc')
             .page(params[:page])
             .per(params[:count])
             .uniq,
             SourceDetailPresenter
         ]
       else
         [
           Source.joins(:evidence_items)
             .order('sources.id asc')
             .page(params[:page])
             .per(params[:count])
             .uniq,
             SourcePresenter
         ]
       end

    sources = if params[:filter].present?
                description_search(pubmed_search(sources))
              else
                sources
              end

    render json: PaginatedCollectionPresenter.new(
      sources,
      request,
      presenter,
      PaginationPresenter,
      {
        filter: params[:filter] || {}
      },
      [:filter, :detailed]
    )
  end

  def show
    source = Source.includes(authors_sources: [:author])
      .find_by!(identifier_type => params[:id])

    render json: SourceDetailPresenter.new(source)
  end

  def create
    result = Source.propose(source_suggestion_params, comment_params, current_user)
    authorize result.source

    if result.succeeded?
      attach_comment(result.source)
      render json: SourceDetailPresenter.new(result.source), status: :created
    elsif result.errors.any? { |e| e =~ /already been submitted/ }
      render json: SourceDetailPresenter.new(result.source), status: :conflict
    else
      render json: result.errors, status: :internal_server_error
    end
  end

  def update
    status = params.permit(:status)
    source = Source.find_by(id: params[:id])
    authorize source
    if source.update_attributes(status: status)
      render json: SourceDetailPresenter.new(source)
    else
      render json: {errors: ['Failed to update status']}, status: :bad_request
    end
  end

  def update_source_suggestion
    status = params.permit(:status)
    suggestion = SourceSuggestion.find_by(id: params[:id])
    authorize suggestion
    if suggestion.update_attributes(status)
      render json: {}, status: :ok
    else
      render json: {errors: ['Failed to update status']}, status: :bad_request
    end
  end

  def existence
    proposed_pubmed_id = params[:pubmed_id]
    (to_render, status) = if source = Source.find_by(pubmed_id: proposed_pubmed_id)
      [{ description: source.description, pubmed_id: source.pubmed_id }, :ok]
    elsif (citation = Scrapers::PubMed.get_citation_from_pubmed_id(proposed_pubmed_id)).present?
      [{ description: citation, pubmed_id: proposed_pubmed_id }, :ok]
    else
      [{}, :not_found]
    end
    render json: to_render, status: status
  end

  def datatable
    render json: SourceBrowseTable.new(view_context)
  end

  def suggestions_datatable
    render json: SourceSuggestionBrowseTable.new(view_context)
  end
<commit_msg>include status with exist check<commit_after>class SourcesController < ApplicationController
  include WithComment
  actions_without_auth :existence, :index, :show, :datatable, :suggestions_datatable, :update_source_suggestion

  def index
    (sources, presenter) = if params[:detailed] == 'true'
         [
           Source.includes(authors_sources: [:author])
             .joins(:evidence_items)
             .order('sources.id asc')
             .page(params[:page])
             .per(params[:count])
             .uniq,
             SourceDetailPresenter
         ]
       else
         [
           Source.joins(:evidence_items)
             .order('sources.id asc')
             .page(params[:page])
             .per(params[:count])
             .uniq,
             SourcePresenter
         ]
       end

    sources = if params[:filter].present?
                description_search(pubmed_search(sources))
              else
                sources
              end

    render json: PaginatedCollectionPresenter.new(
      sources,
      request,
      presenter,
      PaginationPresenter,
      {
        filter: params[:filter] || {}
      },
      [:filter, :detailed]
    )
  end

  def show
    source = Source.includes(authors_sources: [:author])
      .find_by!(identifier_type => params[:id])

    render json: SourceDetailPresenter.new(source)
  end

  def create
    result = Source.propose(source_suggestion_params, comment_params, current_user)
    authorize result.source

    if result.succeeded?
      attach_comment(result.source)
      render json: SourceDetailPresenter.new(result.source), status: :created
    elsif result.errors.any? { |e| e =~ /already been submitted/ }
      render json: SourceDetailPresenter.new(result.source), status: :conflict
    else
      render json: result.errors, status: :internal_server_error
    end
  end

  def update
    status = params.permit(:status)
    source = Source.find_by(id: params[:id])
    authorize source
    if source.update_attributes(status)
      render json: SourceDetailPresenter.new(source)
    else
      render json: {errors: ['Failed to update status']}, status: :bad_request
    end
  end

  def update_source_suggestion
    status = params.permit(:status)
    suggestion = SourceSuggestion.find_by(id: params[:id])
    authorize suggestion
    if suggestion.update_attributes(status)
      render json: {}, status: :ok
    else
      render json: {errors: ['Failed to update status']}, status: :bad_request
    end
  end

  def existence
    proposed_pubmed_id = params[:pubmed_id]
    (to_render, status) = if source = Source.find_by(pubmed_id: proposed_pubmed_id)
      [{ description: source.description, pubmed_id: source.pubmed_id, status: source.status}, :ok]
    elsif (citation = Scrapers::PubMed.get_citation_from_pubmed_id(proposed_pubmed_id)).present?
      [{ description: citation, pubmed_id: proposed_pubmed_id, status: 'new' }, :ok]
    else
      [{}, :not_found]
    end
    render json: to_render, status: status
  end

  def datatable
    render json: SourceBrowseTable.new(view_context)
  end

  def suggestions_datatable
    render json: SourceSuggestionBrowseTable.new(view_context)
  end

{'fraction_non_alphanumeric': 0.09803613511390416, 'fraction_numerical': 0.0, 'mean_word_length': 2.7184579439252334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7042416', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1828, 'n_words': 409}