unclesp1d3r/CipherSwarm

View on GitHub
app/helpers/application_helper.rb

Summary

Maintainability
A
0 mins
Test Coverage
F
48%
# frozen_string_literal: true

# SPDX-FileCopyrightText:  2024 UncleSp1d3r
# SPDX-License-Identifier: MPL-2.0

module ApplicationHelper
  include Pagy::Frontend

  # Returns the CSS class for the body element based on the current controller and action.
  # The CSS class is generated by combining the controller and action names, separated by hyphens.
  # If the controller name contains a slash ("/"), it is split and only the first part is used.
  # If the action name is present, it is appended to the controller name.
  # If the "page" parameter is present, it is appended to the controller and action names.
  #
  # @param params [Hash] The parameters hash containing the current controller, action, and page.
  # @return [String] The CSS class for the body element.
  def body_class(params)
    return unless params[:controller] && params[:action]

    controller = params[:controller].split("/").first
    action = params[:action]
    page = params[:page]

    classes = ["#{controller}", "#{controller}-#{action}"]
    classes << "#{controller}-#{action}-#{page}" if page

    classes.join(" ")
  end

  # Returns the content for the given name if it exists, otherwise returns the default value.
  #
  # @param name [Symbol] The name of the content block.
  # @param default [String] The default value to return if the content block does not exist.
  # @return [String] The content for the given name if it exists, otherwise the default value.
  def content_for_or(name, default = "")
    raise ArgumentError, "default must be a String" unless default.is_a?(String)

    if content_for?(name)
      content_for(name)
    else
      default
    end
  end

  # Returns the name of the current site.
  #
  # The current site name is derived from the name of the parent module of the Rails application,
  # converted to a human-readable format with capitalized words.
  #
  # Example:
  #   current_site #=> "Cipher Swarm"
  #
  # Returns a string representing the current site name.
  def current_site
    Rails.application.class.module_parent_name
         .underscore
         .humanize
         .split
         .map(&:capitalize)
         .join(" ")
  end

  # Returns the current title for the page.
  # The title is determined based on the current site, the content_for(:title) if present,
  # or the last part of the params[:controller] titleized.
  # Returns a string with the current title.
  def current_title(params)
    title = []
    title << current_site
    title << if content_for?(:title)
      content_for(:title)
    else
      params[:controller].split("/").last.titleize
    end
    title.uniq.join(" | ")
  end

  # Returns the current URL of the request.
  #
  # @return [String] The current URL.
  def current_url(request)
    "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
  end

  # Sets the description for the current page.
  #
  # @param description [String] The description to be set.
  # @return [void]
  def description(description = "")
    content_for :description, description
  end

  # Checks if a route exists for the given path.
  #
  # @param path [String] The path to check.
  # @return [Boolean] Returns true if the route exists, false otherwise.
  def route_exists?(path)
    Rails.application.routes.recognize_path(path, method: :get)
    true
  rescue ActionController::RoutingError, ActionController::UnknownController, ActionController::UnknownAction
    false
  end

  # Sanitizes the given content by removing any potentially harmful HTML tags.
  #
  # Parameters:
  # - content: The content to be sanitized.
  #
  # Returns:
  # The sanitized content.
  def sanitize_content(content)
    ActionController::Base.helpers.sanitize(content)
  end

  # Removes HTML tags and entities from a given string.
  #
  # Parameters:
  # - string: The string to be processed.
  #
  # Returns:
  # The processed string with HTML tags and entities removed.
  def strip_tags_and_entities(string)
    return if string.blank?

    stripped = strip_tags(string)
    decoded = HTMLEntities.new.decode(stripped)
    decoded.squish.gsub(%r{/</?[^>]*>/}, "")
  end
end