<commit_before>class Api::V1::ProjectsController < Api::ApiController
  include FilterByOwner
  include FilterByCurrentUserRoles
  include TranslatableResource
  include IndexSearch
  include AdminAllowed
  include Versioned

  require_authentication :update, :create, :destroy, :create_classifications_export,
    :create_subjects_export, :create_aggregations_export,
    :create_workflows_export, :create_workflow_contents_export,
    scopes: [:project]
  resource_actions :show, :index, :create, :update, :deactivate
  schema_type :json_schema

  alias_method :project, :controlled_resource

  CONTENT_PARAMS = [:description,
                    :title,
                    :workflow_description,
                    :introduction]

  CONTENT_FIELDS = [:title,
                    :description,
                    :workflow_description,
                    :introduction,
                    :url_labels]

  before_action :eager_load_includes, only: :index
  before_action :filter_by_tags, only: :index
  before_action :downcase_slug, only: :index

  prepend_before_action :require_login,
    only: [:create, :update, :destroy, :create_classifications_export,
    :create_subjects_export, :create_aggregations_export,
    :create_workflows_export, :create_workflow_contents_export]

  search_by do |name, query|
    query.search_display_name(name.join(" "))
  end

  search_by :tag do |name, query|
    query.joins(:tags).merge(Tag.search_tags(name.first))
  end

  def index
    unless params.has_key?(:sort)
      @controlled_resources = case
                              when params.has_key?(:launch_approved)
                                controlled_resources.rank(:launched_row_order)
                              when params.has_key?(:beta_approved)
                                controlled_resources.rank(:beta_row_order)
                              else
                                controlled_resources
                              end
    end
    super
  end

  def create_classifications_export
    create_export(:classifications)
  end

  def create_subjects_export
    create_export(:subjects)
  end

  def create_aggregations_export
    create_export(:aggregations)
  end

  def create_workflows_export
    create_export(:workflows)
  end

  def create_workflow_contents_export
    create_export(:workflow_contents)
  end

  def create
    super { |project| TalkAdminCreateWorker.perform_async(project.id) }
  end

  private

  def downcase_slug
    if params.has_key? "slug"
      params[:slug] = params[:slug].downcase
    end
  end

  def filter_by_tags
    if tags = params.delete(:tags).try(:split, ",").try(:map, &:downcase)
      @controlled_resources = controlled_resources
      .joins(:tags).where(tags: {name: tags})
    end
  end

  def eager_load_includes
    return unless params.has_key?(:include)
    includes = params.fetch(:include, "").split(",").map(&:to_sym)
    eager_loads = includes.select { |inc| controlled_resource.respond_to?(inc) }
    unless eager_loads.empty?
      @controlled_resources = controlled_resources.eager_load(*eager_loads)
    end
  end

  def create_or_update_medium(type, media_create_params=media_params)<commit_msg>eager load defaults for project<commit_after>class Api::V1::ProjectsController < Api::ApiController
  include FilterByOwner
  include FilterByCurrentUserRoles
  include TranslatableResource
  include IndexSearch
  include AdminAllowed
  include Versioned

  require_authentication :update, :create, :destroy, :create_classifications_export,
    :create_subjects_export, :create_aggregations_export,
    :create_workflows_export, :create_workflow_contents_export,
    scopes: [:project]
  resource_actions :show, :index, :create, :update, :deactivate
  schema_type :json_schema

  alias_method :project, :controlled_resource

  CONTENT_PARAMS = [:description,
                    :title,
                    :workflow_description,
                    :introduction]

  CONTENT_FIELDS = [:title,
                    :description,
                    :workflow_description,
                    :introduction,
                    :url_labels]

  before_action :eager_load_includes, only: :index
  before_action :filter_by_tags, only: :index
  before_action :downcase_slug, only: :index

  prepend_before_action :require_login,
    only: [:create, :update, :destroy, :create_classifications_export,
    :create_subjects_export, :create_aggregations_export,
    :create_workflows_export, :create_workflow_contents_export]

  search_by do |name, query|
    query.search_display_name(name.join(" "))
  end

  search_by :tag do |name, query|
    query.joins(:tags).merge(Tag.search_tags(name.first))
  end

  def index
    unless params.has_key?(:sort)
      @controlled_resources = case
                              when params.has_key?(:launch_approved)
                                controlled_resources.rank(:launched_row_order)
                              when params.has_key?(:beta_approved)
                                controlled_resources.rank(:beta_row_order)
                              else
                                controlled_resources
                              end
    end
    super
  end

  def create_classifications_export
    create_export(:classifications)
  end

  def create_subjects_export
    create_export(:subjects)
  end

  def create_aggregations_export
    create_export(:aggregations)
  end

  def create_workflows_export
    create_export(:workflows)
  end

  def create_workflow_contents_export
    create_export(:workflow_contents)
  end

  def create
    super { |project| TalkAdminCreateWorker.perform_async(project.id) }
  end

  private

  def downcase_slug
    if params.has_key? "slug"
      params[:slug] = params[:slug].downcase
    end
  end

  def filter_by_tags
    if tags = params.delete(:tags).try(:split, ",").try(:map, &:downcase)
      @controlled_resources = controlled_resources
      .joins(:tags).where(tags: {name: tags})
    end
  end

  def eager_load_includes(include_loads=[])
    default_loads = !!params[:cards] ? [:avatar] : [:tags, :background, :avatar, :owner]
    if params.has_key?(:include)
      includes = params.fetch(:include, "").split(",").map(&:to_sym)
      include_loads = includes.select { |inc| controlled_resource.respond_to?(inc) }
    end
    eager_loads = default_loads | include_loads
    unless eager_loads.empty?
      @controlled_resources = controlled_resources.eager_load(*eager_loads.uniq)
    end
  end

  def create_or_update_medium(type, media_create_params=media_params)
{'fraction_non_alphanumeric': 0.08405664687071722, 'fraction_numerical': 0.00030455306837216383, 'mean_word_length': 2.9685800604229606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '968428', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1911, 'n_words': 359}