<commit_before>class ProjectsController < ApplicationController
  WIDGET_CACHE_EXPIRATION = 15.minutes

  protect_from_forgery unless: -> { request.format.widget? }
  before_filter :allow_external_iframes, only: [:show]

  caches_action :observed_taxa_count, :contributors,
    :expires_in => WIDGET_CACHE_EXPIRATION,
    :cache_path => Proc.new {|c| c.params}, 
    :if => Proc.new {|c| c.request.format == :widget}

  before_action :doorkeeper_authorize!, 
    only: [ :by_login, :join, :leave, :members ],
    if: lambda { authenticate_with_oauth? }
  
  before_filter :return_here, :only => [:index, :show, :contributors, :members, :show_contributor, :terms, :invite]
  before_filter :authenticate_user!, 
    :unless => lambda { authenticated_with_oauth? },
    :except => [ :index, :show, :search, :map, :contributors, :observed_taxa_count,
      :browse, :calendar, :stats_slideshow ]
  load_except = [ :create, :index, :search, :new_traditional, :by_login, :map, :browse, :calendar, :new ]
  before_filter :load_project, :except => load_except
  blocks_spam :except => load_except, :instance => :project
  before_filter :ensure_current_project_url, :only => :show
  before_filter :load_project_user, :except => [:index, :search, :new_traditional, :by_login, :new]
  before_filter :load_user_by_login, :only => [:by_login]
  before_filter :ensure_can_edit, :only => [:edit, :update]
  before_filter :filter_params, :only => [:update, :create]
  
  ORDERS = %w(title created)
  ORDER_CLAUSES = {
    'title' => 'lower(title)',
    'created' => 'id'
  }

  def index
    respond_to do |format|
      format.html do
        if @site && (@site_place = @site.place)
          @place = @site.place unless params[:everywhere].yesish?
        end
        @projects = Project.recently_added_to(place: @place)
        @created = Project.not_flagged_as_spam.order("projects.id desc").limit(8)
        @created = @created.joins(:place).where(@place.self_and_descendant_conditions) if @place
        @featured = Project.featured
        @featured = @featured.joins(:place).where(@place.self_and_descendant_conditions) if @place
        @carousel = @featured.where( "project_type IN ('collection', 'umbrella')" ).limit( 3 )
        @carousel = @featured.limit( 3 ) if @carousel.count == 0
        @carousel = @projects.limit( 3 ) if @carousel.count == 0

        # Temporary for CNC
        if cnc_project = Project.find( "city-nature-challenge-2018" ) rescue nil
          @carousel = [cnc_project, @carousel.to_a].flatten.uniq[0..3]
        end

        @featured = @featured.limit( 30 ).to_a.reject{ |p| @carousel.include?( p )}[0..8]
        @recent = Project.joins(:posts).order( "posts.id DESC" ).limit( 20 )
        @recent = @recent.joins( :place ).where( @place.self_and_descendant_conditions ) if @place
        @recent = @recent.to_a.uniq[0..8]
        if logged_in?
          @started = current_user.projects.not_flagged_as_spam.
            order("projects.id desc").limit(5)
          @joined = current_user.project_users.joins(:project).
            merge(Project.not_flagged_as_spam).includes(:project).
            where( "projects.user_id != ?", current_user ).
            order("projects.id desc").limit(5).map(&:project)
          @followed = Project.
            joins( "JOIN subscriptions ON subscriptions.resource_type = 'Project' AND resource_id = projects.id" ).
            where( "subscriptions.user_id = ?", current_user ).
            where( "projects.user_id != ?", current_user ).
            order( "subscriptions.id DESC" ).limit( 15 ).select{ |p| !@joined.include?( p ) }
        end
        render layout: "bootstrap"
      end
      format.json do
        scope = Project.all
        if params[:featured] && params[:latitude]
          scope = scope.featured_near_point( params[:latitude], params[:longitude] )
        else
          scope = scope.featured if params[:featured]
          scope = scope.near_point(params[:latitude], params[:longitude]) if params[:latitude] && params[:longitude]
        end
        scope = scope.in_group(params[:group]) if params[:group]
        scope = scope.from_source_url(params[:source]) if params[:source]
        @projects = scope.paginate(:page => params[:page], :per_page => 100)
        opts = Project.default_json_options.merge(:include => [
          :project_list, 
          {:project_observation_fields => ProjectObservationField.default_json_options}
        ])
        opts[:methods] << :project_observations_count
        render :json => @projects.to_json(opts)
      end<commit_msg>Some preloading for projects/index, removed cnc-specific stuff.<commit_after>class ProjectsController < ApplicationController
  WIDGET_CACHE_EXPIRATION = 15.minutes

  protect_from_forgery unless: -> { request.format.widget? }
  before_filter :allow_external_iframes, only: [:show]

  caches_action :observed_taxa_count, :contributors,
    :expires_in => WIDGET_CACHE_EXPIRATION,
    :cache_path => Proc.new {|c| c.params}, 
    :if => Proc.new {|c| c.request.format == :widget}

  before_action :doorkeeper_authorize!, 
    only: [ :by_login, :join, :leave, :members ],
    if: lambda { authenticate_with_oauth? }
  
  before_filter :return_here, :only => [:index, :show, :contributors, :members, :show_contributor, :terms, :invite]
  before_filter :authenticate_user!, 
    :unless => lambda { authenticated_with_oauth? },
    :except => [ :index, :show, :search, :map, :contributors, :observed_taxa_count,
      :browse, :calendar, :stats_slideshow ]
  load_except = [ :create, :index, :search, :new_traditional, :by_login, :map, :browse, :calendar, :new ]
  before_filter :load_project, :except => load_except
  blocks_spam :except => load_except, :instance => :project
  before_filter :ensure_current_project_url, :only => :show
  before_filter :load_project_user, :except => [:index, :search, :new_traditional, :by_login, :new]
  before_filter :load_user_by_login, :only => [:by_login]
  before_filter :ensure_can_edit, :only => [:edit, :update]
  before_filter :filter_params, :only => [:update, :create]
  
  ORDERS = %w(title created)
  ORDER_CLAUSES = {
    'title' => 'lower(title)',
    'created' => 'id'
  }

  def index
    respond_to do |format|
      format.html do
        if @site && (@site_place = @site.place)
          @place = @site.place unless params[:everywhere].yesish?
        end
        @projects = Project.recently_added_to(place: @place).includes(:stored_preferences)
        @created = Project.not_flagged_as_spam.order("projects.id desc").limit(8).includes(:stored_preferences)
        @created = @created.joins(:place).where(@place.self_and_descendant_conditions) if @place
        @featured = Project.featured.includes(:stored_preferences)
        @featured = @featured.joins(:place).where(@place.self_and_descendant_conditions) if @place
        @carousel = @featured.where( "project_type IN ('collection', 'umbrella')" ).limit( 3 ).includes(:stored_preferences)
        @carousel = @featured.limit( 3 ) if @carousel.count == 0
        @carousel = @projects.limit( 3 ) if @carousel.count == 0
        @carousel = @carousel.to_a

        @featured = @featured.limit( 30 ).to_a.reject{ |p| @carousel.include?( p )}[0..8]
        @recent = Project.joins(:posts).order( "posts.id DESC" ).limit( 20 ).includes(:stored_preferences)
        @recent = @recent.joins( :place ).where( @place.self_and_descendant_conditions ) if @place
        @recent = @recent.to_a.uniq[0..7]
        if logged_in?
          @started = current_user.projects.not_flagged_as_spam.
            order("projects.id desc").limit(5).includes(:stored_preferences)
          @joined = current_user.project_users.joins(:project).
            merge(Project.not_flagged_as_spam).includes( project: :stored_preferences ).
            where( "projects.user_id != ?", current_user ).
            order("projects.id desc").limit(5).map(&:project)
          @followed = Project.
            includes(:stored_preferences).
            joins( "JOIN subscriptions ON subscriptions.resource_type = 'Project' AND resource_id = projects.id" ).
            where( "subscriptions.user_id = ?", current_user ).
            where( "projects.user_id != ?", current_user ).
            order( "subscriptions.id DESC" ).limit( 15 ).select{ |p| !@joined.include?( p ) }
        end
        render layout: "bootstrap"
      end
      format.json do
        scope = Project.all
        if params[:featured] && params[:latitude]
          scope = scope.featured_near_point( params[:latitude], params[:longitude] )
        else
          scope = scope.featured if params[:featured]
          scope = scope.near_point(params[:latitude], params[:longitude]) if params[:latitude] && params[:longitude]
        end
        scope = scope.in_group(params[:group]) if params[:group]
        scope = scope.from_source_url(params[:source]) if params[:source]
        @projects = scope.paginate(:page => params[:page], :per_page => 100)
        opts = Project.default_json_options.merge(:include => [
          :project_list, 
          {:project_observation_fields => ProjectObservationField.default_json_options}
        ])
        opts[:methods] << :project_observations_count
        render :json => @projects.to_json(opts)
      end
{'fraction_non_alphanumeric': 0.13834034746951548, 'fraction_numerical': 0.005611308945721377, 'mean_word_length': 3.417540514775977, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, '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': '5510611', 'n_tokens_mistral': 3051, 'n_tokens_neox': 2925, 'n_words': 587}