<commit_before># frozen_string_literal: true

# private methods shared by HerbariaController and subcontrollers
module Herbaria::SharedPrivateMethods
  private

  # ---------- Filters ---------------------------------------------------------

  def keep_track_of_referrer
    @back = params[:back] || request.referer
  end

  def redirect_to_referrer
    return false if @back.blank?

    redirect_to(@back)
    true
  end

  # ---------- Indices ---------------------------------------------------------

  def show_selected_herbaria(query, args = {})
    args = show_index_args(args)

    # Clean up display by removing user-related stuff from nonpersonal index.
    if query.flavor == :nonpersonal
      args[:sorting_links].reject! { |x| x[0] == "user" }
      @no_user_column = true
    end

    # If user clicks "merge" on an herbarium, it reloads the page and asks
    # them to click on the destination herbarium to merge it with.
    @merge = Herbarium.safe_find(params[:merge])
    @links = right_tab_links(query, @links)
    show_index_of_objects(query, args)
  end

  def show_index_args(args)
    { # default args
      letters: "herbaria.name",
      num_per_page: 100,
      include: [:curators, :herbarium_records, :personal_user]
    }.merge(args,
            template: "/herbaria/index.html.erb", # render with this template
            sorting_links: [ # Add some alternate sorting criteria.
              ["records",     :sort_by_records.t],
              ["user",        :sort_by_user.t],
              ["code",        :sort_by_code.t],
              ["name",        :sort_by_name.t],
              ["created_at",  :sort_by_created_at.t],
              ["updated_at",  :sort_by_updated_at.t]
            ])
  end

  def right_tab_links(query, links)
    links ||= []
    unless query.flavor == :all
      links << [:herbarium_index_list_all_herbaria.l, herbaria_path]
    end
    unless query.flavor == :nonpersonal
      links << [:herbarium_index_nonpersonal_herbaria.l,
                nonpersonal_herbaria_path]
    end
    links << [:create_herbarium.l, herbaria_path(method: :post)]
  end

  # ---------- Merges ----------------------------------------------------------

  # Used by create, edit and HerbariaMerges

  def perform_or_request_merge(this, that)
    if in_admin_mode? || this.can_merge_into?(that)
      perform_merge(this, that)
    else
      request_merge(this, that)
    end
  end

  def perform_merge(this, that)
    old_name = this.name_was
    result = this.merge(that)
    flash_notice(
      :runtime_merge_success.t(
        type: :herbarium, this: old_name, that: result.name
      )
    )
    result
  end<commit_msg>Fix link causing errors in prior commit<commit_after># frozen_string_literal: true

# private methods shared by HerbariaController and subcontrollers
module Herbaria::SharedPrivateMethods
  private

  # ---------- Filters ---------------------------------------------------------

  def keep_track_of_referrer
    @back = params[:back] || request.referer
  end

  def redirect_to_referrer
    return false if @back.blank?

    redirect_to(@back)
    true
  end

  # ---------- Indices ---------------------------------------------------------

  def show_selected_herbaria(query, args = {})
    args = show_index_args(args)

    # Clean up display by removing user-related stuff from nonpersonal index.
    if query.flavor == :nonpersonal
      args[:sorting_links].reject! { |x| x[0] == "user" }
      @no_user_column = true
    end

    # If user clicks "merge" on an herbarium, it reloads the page and asks
    # them to click on the destination herbarium to merge it with.
    @merge = Herbarium.safe_find(params[:merge])
    @links = right_tab_links(query, @links)
    show_index_of_objects(query, args)
  end

  def show_index_args(args)
    { # default args
      letters: "herbaria.name",
      num_per_page: 100,
      include: [:curators, :herbarium_records, :personal_user]
    }.merge(args,
            template: "/herbaria/index.html.erb", # render with this template
            sorting_links: [ # Add some alternate sorting criteria.
              ["records",     :sort_by_records.t],
              ["user",        :sort_by_user.t],
              ["code",        :sort_by_code.t],
              ["name",        :sort_by_name.t],
              ["created_at",  :sort_by_created_at.t],
              ["updated_at",  :sort_by_updated_at.t]
            ])
  end

  def right_tab_links(query, links)
    links ||= []
    unless query.flavor == :all
      links << [:herbarium_index_list_all_herbaria.l, herbaria_path]
    end
    unless query.flavor == :nonpersonal
      links << [:herbarium_index_nonpersonal_herbaria.l,
                herbaria_nonpersonal_path]
    end
    links << [:create_herbarium.l, herbaria_path(method: :post)]
  end

  # ---------- Merges ----------------------------------------------------------

  # Used by create, edit and HerbariaMerges

  def perform_or_request_merge(this, that)
    if in_admin_mode? || this.can_merge_into?(that)
      perform_merge(this, that)
    else
      request_merge(this, that)
    end
  end

  def perform_merge(this, that)
    old_name = this.name_was
    result = this.merge(that)
    flash_notice(
      :runtime_merge_success.t(
        type: :herbarium, this: old_name, that: result.name
      )
    )
    result
  end
{'fraction_non_alphanumeric': 0.1673499813641446, 'fraction_numerical': 0.0014908684308609765, 'mean_word_length': 2.0827110855829982, 'pattern_counts': {'":': 0, '<': 15, '<?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': '365665', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1632, 'n_words': 375}