<commit_before># frozen_string_literal: true

module ApplicationHelper

  def body_css_classes
    classes = []
    classes.push current_module == 'gobierto_participation' ?  'gobierto_participation theme-participation' : current_module
    classes.push controller_name
    classes.push action_name
    classes.push "#{controller_name}_#{action_name}"
    classes.join(" ")
  end

  def render_if_exists(partial_path, partial_params = {}, format = "html.erb")
    partial_path_name = Pathname.new(partial_path)
    partial_file_name = "#{partial_path_name.dirname}/_#{partial_path_name.basename}.#{format}"

    render(partial_path, partial_params) if lookup_context.exists?(partial_file_name)
  end

  # Example: translate_enum_value(GobiertoParticipation::ProcessStage, :stage_type, :information)
  def translate_enum_value(object, enum_name, enum_value_name)
    I18n.t("activerecord.attributes.#{object.model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value_name}")
  end

  def localized_enum(class_name, enum_name)
    return {} unless class_name.respond_to?(enum_name.to_s.pluralize)

    class_name.send(enum_name.to_s.pluralize).reduce({}) do |enum_options, (key, _)|
      enum_options.merge!(
        I18n.t("activerecord.attributes.#{class_name.model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{key}", default: key.capitalize) => key
      )
    end
  end

  def privacy_policy_page_link
    if current_site && current_site.configuration.privacy_page?
      link_to t("layouts.accept_privacy_policy_signup"), gobierto_cms_page_or_news_path(current_site.configuration.privacy_page)
    end
  end

  def tab_attributes(condition)
    { role: "tab", "tabindex" => condition ? 0 : -1, "aria-selected" => condition }
  end

  def show_social_links?
    !params[:controller].include?("user/")
  end

  def full_layout?
    (current_module == "gobierto_participation") &&
      ((controller_name == "contribution_containers" && action_name == "show") || (controller_name == "poll_answers" && action_name == "new"))
  end

  def filetype_icon(attachment)
    extension = if attachment.class == String
                  attachment.split(".").last
                else
                  attachment.extension
                end

    fontawesome_filetype = if ::GobiertoAttachments::Attachment.extension_fontawesome_matching.has_key?(extension.to_sym)
                             "-" + ::GobiertoAttachments::Attachment.extension_fontawesome_matching[extension.to_sym]
                           else
                             ""
                           end
    html = "<i class='fa fa-file" + fontawesome_filetype + "-o'></i>"
    html.html_safe
  end

  def current_parameters_with_year(year)
    params.except(:host, :port, :protocol).merge(year: year).permit!
  end

  def next_poll(poll_id = nil)
    poll = GobiertoParticipation::Poll.by_site(current_site).find(poll_id) if poll_id
    answerable_polls = GobiertoParticipation::Poll.by_site(current_site).answerable.order(ends_at: :asc)
    answerable_polls_by_user = answerable_polls.detect { |p| p.answerable_by?(current_user) } if current_user

    if current_user
      if poll && poll.answerable_by?(current_user)
        poll
      elsif answerable_polls_by_user
        answerable_polls_by_user
      end
    else
      answerable_polls.first
    end
  end

  def content_for_if(name, condition, &block)
    if condition
      content_for(name, &block)
    end
    yield
  end

  def attribute_indication_tag(params = {})
    text = if params[:required] && params[:max_length]
             I18n.t "views.forms.required_and_max_characters", length: parse_max_length(params[:max_length])
           elsif params[:required]
             I18n.t "views.forms.required"
           elsif params[:max_length]
             I18n.t "views.forms.max_characters", length: parse_max_length(params[:max_length])
           end
    content_tag(:span, class: "indication") { text }
  end

  def whom(entity_name)
    if I18n.locale == :ca
      if /\A[aeiou]/i =~ entity_name
        " l'#{entity_name}"
      else
        # TODO: define a setting with the genre of the entity
        "la #{entity_name}"
      end
    else
      " " + entity_name
    end
  end

  def meaningful_date_range?(date_range)
    date_range.is_a?(Array) && (date_range.first != date_range.last)
  end

  private

  def parse_max_length(params)
    if params.is_a? Integer
      params
    elsif params[:length]
      params[:length]
    else
      params[:f].object.class.max_length(params[:attr])
    end
  end

end<commit_msg>Suport date truncation in `meaningful_date_range?` helper<commit_after># frozen_string_literal: true

module ApplicationHelper

  def body_css_classes
    classes = []
    classes.push current_module == 'gobierto_participation' ?  'gobierto_participation theme-participation' : current_module
    classes.push controller_name
    classes.push action_name
    classes.push "#{controller_name}_#{action_name}"
    classes.join(" ")
  end

  def render_if_exists(partial_path, partial_params = {}, format = "html.erb")
    partial_path_name = Pathname.new(partial_path)
    partial_file_name = "#{partial_path_name.dirname}/_#{partial_path_name.basename}.#{format}"

    render(partial_path, partial_params) if lookup_context.exists?(partial_file_name)
  end

  # Example: translate_enum_value(GobiertoParticipation::ProcessStage, :stage_type, :information)
  def translate_enum_value(object, enum_name, enum_value_name)
    I18n.t("activerecord.attributes.#{object.model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value_name}")
  end

  def localized_enum(class_name, enum_name)
    return {} unless class_name.respond_to?(enum_name.to_s.pluralize)

    class_name.send(enum_name.to_s.pluralize).reduce({}) do |enum_options, (key, _)|
      enum_options.merge!(
        I18n.t("activerecord.attributes.#{class_name.model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{key}", default: key.capitalize) => key
      )
    end
  end

  def privacy_policy_page_link
    if current_site && current_site.configuration.privacy_page?
      link_to t("layouts.accept_privacy_policy_signup"), gobierto_cms_page_or_news_path(current_site.configuration.privacy_page)
    end
  end

  def tab_attributes(condition)
    { role: "tab", "tabindex" => condition ? 0 : -1, "aria-selected" => condition }
  end

  def show_social_links?
    !params[:controller].include?("user/")
  end

  def full_layout?
    (current_module == "gobierto_participation") &&
      ((controller_name == "contribution_containers" && action_name == "show") || (controller_name == "poll_answers" && action_name == "new"))
  end

  def filetype_icon(attachment)
    extension = if attachment.class == String
                  attachment.split(".").last
                else
                  attachment.extension
                end

    fontawesome_filetype = if ::GobiertoAttachments::Attachment.extension_fontawesome_matching.has_key?(extension.to_sym)
                             "-" + ::GobiertoAttachments::Attachment.extension_fontawesome_matching[extension.to_sym]
                           else
                             ""
                           end
    html = "<i class='fa fa-file" + fontawesome_filetype + "-o'></i>"
    html.html_safe
  end

  def current_parameters_with_year(year)
    params.except(:host, :port, :protocol).merge(year: year).permit!
  end

  def next_poll(poll_id = nil)
    poll = GobiertoParticipation::Poll.by_site(current_site).find(poll_id) if poll_id
    answerable_polls = GobiertoParticipation::Poll.by_site(current_site).answerable.order(ends_at: :asc)
    answerable_polls_by_user = answerable_polls.detect { |p| p.answerable_by?(current_user) } if current_user

    if current_user
      if poll && poll.answerable_by?(current_user)
        poll
      elsif answerable_polls_by_user
        answerable_polls_by_user
      end
    else
      answerable_polls.first
    end
  end

  def content_for_if(name, condition, &block)
    if condition
      content_for(name, &block)
    end
    yield
  end

  def attribute_indication_tag(params = {})
    text = if params[:required] && params[:max_length]
             I18n.t "views.forms.required_and_max_characters", length: parse_max_length(params[:max_length])
           elsif params[:required]
             I18n.t "views.forms.required"
           elsif params[:max_length]
             I18n.t "views.forms.max_characters", length: parse_max_length(params[:max_length])
           end
    content_tag(:span, class: "indication") { text }
  end

  def whom(entity_name)
    if I18n.locale == :ca
      if /\A[aeiou]/i =~ entity_name
        " l'#{entity_name}"
      else
        # TODO: define a setting with the genre of the entity
        "la #{entity_name}"
      end
    else
      " " + entity_name
    end
  end

  def meaningful_date_range?(date_range, options = {})
    return false unless date_range.is_a?(Array)

    date_range.map! { |d| d.change(hour: 0, min: 0, sec: 0) } if options[:only_date]
    date_range.first != date_range.last
  end

  private

  def parse_max_length(params)
    if params.is_a? Integer
      params
    elsif params[:length]
      params[:length]
    else
      params[:f].object.class.max_length(params[:attr])
    end
  end

end
{'fraction_non_alphanumeric': 0.10904398322400258, 'fraction_numerical': 0.00419399935476933, 'mean_word_length': 3.3135435992578848, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6253811', 'n_tokens_mistral': 3064, 'n_tokens_neox': 2994, 'n_words': 512}