<commit_before># frozen_string_literal: true
class ExploreController < ApplicationController
  before_action :authorize_explore
  after_action :verify_authorized
  after_action :verify_policy_scoped

  respond_to :html, :json, :csv

  # TODO: remove?
  # autocomplete :map, :name, full: true, extra_data: [:user_id]

  # GET /explore/active
  def active
    page = params[:page].present? ? params[:page] : 1
    @maps = policy_scope(Map).order('updated_at DESC')
                             .page(page).per(20)

    respond_to do |format|
      format.html do
        # root url => main/home. main/home renders maps/activemaps view.
        redirect_to(root_url) && return if authenticated?
        respond_with(@maps, @user)
      end
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/featured
  def featured
    page = params[:page].present? ? params[:page] : 1
    @maps = policy_scope(
      Map.where('maps.featured = ? AND maps.permission != ?',
                true, 'private')
    ).order('updated_at DESC').page(page).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/mine
  def mine
    unless authenticated?
      skip_policy_scope
      return redirect_to explore_active_path
    end

    page = params[:page].present? ? params[:page] : 1
    @maps = policy_scope(
      Map.where('maps.user_id = ?', current_user.id)
    ).order('updated_at DESC').page(page).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/shared
  def shared
    unless authenticated?
      skip_policy_scope
      return redirect_to explore_active_path
    end

    page = params[:page].present? ? params[:page] : 1
    @maps = policy_scope(
      Map.where('maps.id IN (?)', current_user.shared_maps.map(&:id))
    ).order('updated_at DESC').page(page).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/starred
  def starred
    unless authenticated?
      skip_policy_scope
      return redirect_to explore_active_path
    end

    page = params[:page].present? ? params[:page] : 1
    stars = current_user.stars.map(&:map_id)
    @maps = policy_scope(
      Map.where('maps.id IN (?)', stars)
    ).order('updated_at DESC').page(page).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/mapper/:id
  def mapper
    page = params[:page].present? ? params[:page] : 1
    @user = User.find(params[:id])
    @maps = policy_scope(Map.where(user: @user))
            .order('updated_at DESC').page(page).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  private

  def authorize_explore
    authorize :Explore
  end
end<commit_msg>simplify explore controller a bit<commit_after># frozen_string_literal: true
class ExploreController < ApplicationController
  before_action :require_authentication, only: [:mine, :shared, :starred]
  before_action :authorize_explore
  after_action :verify_authorized
  after_action :verify_policy_scoped

  respond_to :html, :json, :csv

  # GET /explore/active
  def active
    @maps = policy_scope(Map).order(updated_at: :desc)
                             .page(params[:page]).per(20)

    respond_to do |format|
      format.html do
        # root url => main/home. main/home renders maps/activemaps view.
        redirect_to(root_url) && return if authenticated?
        respond_with(@maps, @user)
      end
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/featured
  def featured
    @maps = policy_scope(Map).where(featured: true).order(updated_at: :desc)
                             .page(params[:page]).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/mine
  def mine
    @maps = policy_scope(Map).where(user_id: current_user.id)
                             .order(updated_at: :desc).page(params[:page]).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/shared
  def shared
    @maps = policy_scope(Map).where(id: current_user.shared_maps.map(&:id))
                             .order(updated_at: :desc).page(params[:page]).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/starred
  def starred
    stars = current_user.stars.map(&:map_id)
    @maps = policy_scope(Map).where(id: stars).order(updated_at: :desc)
                             .page(params[:page]).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  # GET /explore/mapper/:id
  def mapper
    @user = User.find(params[:id])
    @maps = policy_scope(Map.where(user: @user))
            .order(updated_at: :desc).page(params[:page]).per(20)

    respond_to do |format|
      format.html { respond_with(@maps, @user) }
      format.json { render json: @maps.to_json }
    end
  end

  private

  def authorize_explore
    authorize :Explore
  end

  def require_authentication
    # skip_policy_scope
    redirect_to explore_active_path unless authenticated?
  end
end
{'fraction_non_alphanumeric': 0.12076788830715532, 'fraction_numerical': 0.005235602094240838, 'mean_word_length': 2.762967826657912, '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': '4424151', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1851, 'n_words': 410}