<commit_before># frozen_string_literal: true

# Controls viewing and modifying herbaria.
class HerbariaController < ApplicationController
  # filters
  before_action :login_required, except: [
    :index,
    :index_herbaria,
    :index_nonpersonal_herbaria,
    :next,
    :prev,
    :search,
    :show
  ]

  # Old MO Action Name       New "Normalized"Name
  # ------------------       --------------------
  # create_herbarium (get)   new
  # create_herbarium (post)  create
  # delete_curator           ? - in separate file or controller?
  # destroy_herbarium        destroy
  # edit_herbarium (get)     edit
  # edit_herbarium (post)    update
  # herbarium_search         search
  # index                    index_nonpersonal_herbaria
  # index_herbarium          index_herbaria
  # list_herbaria            index
  # merge_herbaria           merge - in separate file or controller?
  # next_herbarium           next
  # prev_herbarium           prev
  # request_to_be_curator    ? - in separate file or controller?
  # show_herbarium           show

  # ---------- Actions to Display data (index, show, etc.) ---------------------

  # List all herbaria
  def index
    store_location
    query = create_query(:Herbarium, :all, by: :name)
    show_selected_herbaria(query, always_index: true)
  end

  def show
    store_location
    pass_query_params
    @canonical_url = Herbarium.show_url(params[:id])
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return if request.method != "POST"
    return if !@user || !@herbarium.curator?(@user) && !in_admin_mode?

    login = params[:add_curator].to_s.sub(/ <.*/, "")
    user = User.find_by_login(login)
    if user
      @herbarium.add_curator(user)
    else
      flash_error(:show_herbarium_no_user.t(login: login))
    end
  end

  # ---------- Actions to Display forms -- (new, edit, etc.) -------------------

  def new
    store_location
    pass_query_params
    keep_track_of_referrer
    @herbarium = Herbarium.new
  end

  def edit
    store_location
    pass_query_params
    keep_track_of_referrer
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium
    return unless make_sure_can_edit!

    if request.method == "GET"
      @herbarium.place_name         = @herbarium.location.try(&:name)
      @herbarium.personal           = @herbarium.personal_user_id.present?
      @herbarium.personal_user_name = @herbarium.personal_user.try(&:login)
    elsif request.method == "POST"
      post_edit_herbarium
    else
      redirect_to_referrer || redirect_to_show_herbarium
    end
  end

  # ---------- Actions to Modify data: (create, update, destroy, etc.) ---------

  def create
    store_location
    pass_query_params
    keep_track_of_referrer
    post_create_herbarium
  end

  def update
    store_location
    pass_query_params
    keep_track_of_referrer
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium
    return unless make_sure_can_edit!
    post_edit_herbarium
  end

  def destroy
    pass_query_params
    keep_track_of_referrer
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium

    if in_admin_mode? ||
       @herbarium.curator?(@user) ||
       @herbarium.curators.empty? && @herbarium.owns_all_records?(@user)
      @herbarium.destroy
      redirect_to_referrer || redirect_to_herbarium_index
    else
      flash_error(:permission_denied.t)
      redirect_to_referrer || redirect_to_show_herbarium
    end
  end

  # ========== Non=standard REST Actions =======================================

  # ---------- Display data

  # Display selected Herbarium's (based on current Query).
  def index_herbaria
    query = find_or_create_query(:Herbarium, by: params[:by])
    show_selected_herbaria(query, id: params[:id].to_s, always_index: true)
  end

  # list nonpersonal herbaria
  def index_nonpersonal_herbaria
    store_location
    query = create_query(:Herbarium, :nonpersonal, by: :code_then_name)
    show_selected_herbaria(query, always_index: true)
  end

  # Display list of Herbaria whose text matches a string pattern.
  def search
    pattern = params[:pattern].to_s
    if pattern.match(/^\d+$/) && (herbarium = Herbarium.safe_find(pattern))
      redirect_to(herbarium_path(herbarium.id))
    else
      query = create_query(:Herbarium, :pattern_search, pattern: pattern)
      show_selected_herbaria(query)
    end
  end

  def next
    redirect_to_next_object(:next, Herbarium, params[:id].to_s)
  end

  def prev
    redirect_to_next_object(:prev, Herbarium, params[:id].to_s)
  end

  # ---------- Modify data

  def merge
    pass_query_params
    keep_track_of_referrer
    this = find_or_goto_index(Herbarium, params[:this]) || return
    that = find_or_goto_index(Herbarium, params[:that]) || return
    result = perform_or_request_merge(this, that) || return
    redirect_to_herbarium_index(result)
  end

  def delete_curator
    pass_query_params
    keep_track_of_referrer
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium

    user = User.safe_find(params[:user])
    if !@herbarium.curator?(@user) && !in_admin_mode?
      flash_error(:permission_denied.t)
    elsif user && @herbarium.curator?(user)
      @herbarium.delete_curator(user)
    end
    redirect_to_referrer || redirect_to_show_herbarium
  end

  # ---------- Other

  # linked from show page
  def request_to_be_curator
    pass_query_params
    keep_track_of_referrer
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium && request.method == "POST"
<commit_msg>Turn some method calls into filters<commit_after># frozen_string_literal: true

# Controls viewing and modifying herbaria.
class HerbariaController < ApplicationController
  # filters
  before_action :login_required, except: [
    :index,
    :index_herbaria,
    :index_nonpersonal_herbaria,
    :next,
    :prev,
    :search,
    :show
  ]
  before_action :store_location, only: [
    :create,
    :edit,
    :index,
    :index_nonpersonal_herbaria,
    :new,
    :show,
    :update
  ]
  before_action :pass_query_params, only: [
    :create,
    :delete_curator,
    :destroy,
    :edit,
    :merge,
    :new,
    :request_to_be_curator,
    :show,
    :update
  ]
  before_action :keep_track_of_referrer, only: [
    :create,
    :delete_curator,
    :destroy,
    :edit,
    :merge,
    :new,
    :request_to_be_curator,
    :update
  ]

  # Old MO Action Name       New "Normalized"Name
  # ------------------       --------------------
  # create_herbarium (get)   new
  # create_herbarium (post)  create
  # delete_curator           ? - in separate file or controller?
  # destroy_herbarium        destroy
  # edit_herbarium (get)     edit
  # edit_herbarium (post)    update
  # herbarium_search         search
  # index                    index_nonpersonal_herbaria
  # index_herbarium          index_herbaria
  # list_herbaria            index
  # merge_herbaria           merge - in separate file or controller?
  # next_herbarium           next
  # prev_herbarium           prev
  # request_to_be_curator    ? - in separate file or controller?
  # show_herbarium           show

  # ---------- Actions to Display data (index, show, etc.) ---------------------

  # List all herbaria
  def index
    query = create_query(:Herbarium, :all, by: :name)
    show_selected_herbaria(query, always_index: true)
  end

  def show
    @canonical_url = Herbarium.show_url(params[:id])
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return if request.method != "POST"
    return if !@user || !@herbarium.curator?(@user) && !in_admin_mode?

    login = params[:add_curator].to_s.sub(/ <.*/, "")
    user = User.find_by_login(login)
    if user
      @herbarium.add_curator(user)
    else
      flash_error(:show_herbarium_no_user.t(login: login))
    end
  end

  # ---------- Actions to Display forms -- (new, edit, etc.) -------------------

  def new
    @herbarium = Herbarium.new
  end

  def edit
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium
    return unless make_sure_can_edit!

    if request.method == "GET"
      @herbarium.place_name         = @herbarium.location.try(&:name)
      @herbarium.personal           = @herbarium.personal_user_id.present?
      @herbarium.personal_user_name = @herbarium.personal_user.try(&:login)
    elsif request.method == "POST"
      post_edit_herbarium
    else
      redirect_to_referrer || redirect_to_show_herbarium
    end
  end

  # ---------- Actions to Modify data: (create, update, destroy, etc.) ---------

  def create
    post_create_herbarium
  end

  def update
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium
    return unless make_sure_can_edit!
    post_edit_herbarium
  end

  def destroy
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium

    if in_admin_mode? ||
       @herbarium.curator?(@user) ||
       @herbarium.curators.empty? && @herbarium.owns_all_records?(@user)
      @herbarium.destroy
      redirect_to_referrer || redirect_to_herbarium_index
    else
      flash_error(:permission_denied.t)
      redirect_to_referrer || redirect_to_show_herbarium
    end
  end

  # ========== Non=standard REST Actions =======================================

  # ---------- Display data

  # Display selected Herbarium's (based on current Query).
  def index_herbaria
    query = find_or_create_query(:Herbarium, by: params[:by])
    show_selected_herbaria(query, id: params[:id].to_s, always_index: true)
  end

  # list nonpersonal herbaria
  def index_nonpersonal_herbaria
    query = create_query(:Herbarium, :nonpersonal, by: :code_then_name)
    show_selected_herbaria(query, always_index: true)
  end

  # Display list of Herbaria whose text matches a string pattern.
  def search
    pattern = params[:pattern].to_s
    if pattern.match(/^\d+$/) && (herbarium = Herbarium.safe_find(pattern))
      redirect_to(herbarium_path(herbarium.id))
    else
      query = create_query(:Herbarium, :pattern_search, pattern: pattern)
      show_selected_herbaria(query)
    end
  end

  def next
    redirect_to_next_object(:next, Herbarium, params[:id].to_s)
  end

  def prev
    redirect_to_next_object(:prev, Herbarium, params[:id].to_s)
  end

  # ---------- Modify data

  def merge
    this = find_or_goto_index(Herbarium, params[:this]) || return
    that = find_or_goto_index(Herbarium, params[:that]) || return
    result = perform_or_request_merge(this, that) || return
    redirect_to_herbarium_index(result)
  end

  def delete_curator
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium

    user = User.safe_find(params[:user])
    if !@herbarium.curator?(@user) && !in_admin_mode?
      flash_error(:permission_denied.t)
    elsif user && @herbarium.curator?(user)
      @herbarium.delete_curator(user)
    end
    redirect_to_referrer || redirect_to_show_herbarium
  end

  # ---------- Other

  # linked from show page
  def request_to_be_curator
    @herbarium = find_or_goto_index(Herbarium, params[:id])
    return unless @herbarium && request.method == "POST"

{'fraction_non_alphanumeric': 0.12100780695528744, 'fraction_numerical': 0.0, 'mean_word_length': 2.954051210101719, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4808242', 'n_tokens_mistral': 3767, 'n_tokens_neox': 3699, 'n_words': 752}