<commit_before>module ApplicationHelper
  include Admin::RegionsHelper

  def trusty_config
    TrustyCms::Config
  end

  def default_page_title
    title + ' - ' + subtitle
  end

  def title
    trusty_config['admin.title'] || 'Trusty CMS'
  end

  def subtitle
    trusty_config['admin.subtitle'] || 'Publishing for Small Teams'
  end

  def logged_in?
    !current_user.nil?
  end

  def onsubmit_status(model)
    model.new_record? ? t('creating_status', :model => t(model.class.name.downcase)) : "#{I18n.t('saving_changes')}&#8230;"
  end

  def save_model_button(model, options = {})
    model_name = model.class.name.underscore
    human_model_name = model_name.humanize.titlecase
    options[:label] ||= model.new_record? ?
      t('buttons.create', :name => t(model_name, :default => human_model_name), :default => 'Create ' + human_model_name) :
      t('buttons.save_changes', :default => 'Save Changes')
    options[:class] ||= "button"
    options[:accesskey] ||= 'S'
    submit_tag options.delete(:label), options
  end

  def save_model_and_continue_editing_button(model)
    submit_tag t('buttons.save_and_continue'), :name => 'continue', :class => 'button', :accesskey => "s"
  end

  def current_item?(item)
    if item.tab && item.tab.many? {|i| current_url?(i.relative_url) }
      # Accept only stricter URL matches if more than one matches
      current_page?(item.url)
    else
      current_url?(item.relative_url)
    end
  end

  def current_tab?(tab)
    @current_tab ||= tab if tab.any? {|item| current_url?(item.relative_url) }
    @current_tab == tab
  end

  def current_url?(options)
    url = case options
          when Hash
            url_for options
          else
            options.to_s
          end
    #TODO: look for other instances of request_uri
    request.original_fullpath =~ Regexp.new('^' + Regexp.quote(clean(url)))
  end

  def clean(url)
    uri = URI.parse(url)
    uri.path.gsub(%r{/+}, '/').gsub(%r{/$}, '')
  end

  def nav_link_to(name, options)
    if current_url?(options)
      %{<strong>#{ link_to translate_with_default(name), options }</strong>}
    else
      link_to translate_with_default(name), options
    end
  end

  def admin?
    current_user and current_user.admin?
  end

  def designer?
    current_user and (current_user.designer? or current_user.admin?)
  end

  def updated_stamp(model)
    unless model.new_record?
      updated_by = (model.updated_by || model.created_by)
      name = updated_by ? updated_by.name : nil
      time = (model.updated_at || model.created_at)
      if name or time
        html = %{<p class="updated_line">#{t('timestamp.last_updated')} }
        html << %{#{t('timestamp.by')} <strong>#{name}</strong> } if name
        html << %{#{t('timestamp.at')} #{timestamp(time)}} if time
        html << %{</p>}
        html.html_safe
      end
    end
  end

  def timestamp(time)
    # time.strftime("%I:%M %p on %B %e, %Y").sub("AM", 'am').sub("PM", 'pm')
    I18n.localize(time, :format => :timestamp)
  end

  def meta_visible(symbol)
    v = case symbol
    when :meta_more
      not meta_errors?
    when :meta, :meta_less
      meta_errors?
    end
    v ? {} : {:style => "display: none"}
  end

  def meta_errors?
    false
  end

  def meta_label
    meta_errors? ? 'Less' : 'More'
  end

  def image(name, options = {})
    image_tag(append_image_extension("admin/#{name}"), options)
  end

  def image_submit(name, options = {})
    image_submit_tag(append_image_extension("admin/#{name}"), options)
  end

  def admin
    TrustyCms::AdminUI.instance
  end

  def filter_options_for_select(selected=nil)
    options_for_select([[t('select.none'), '']] + TextFilter.descendants_names, selected)
  end

  def body_classes
    @body_classes ||= []
  end

  def nav_tabs
    admin.nav
  end

  def translate_with_default(name)
    t(name.underscore.downcase, :default => name)
  end

  def available_locales_select
    [[t('select.default'),'']] + TrustyCms::AvailableLocales.locales
  end

  def stylesheet_overrides
    overrides = []
    if File.exist?("#{Rails.root}/public/stylesheets/admin/overrides.css") || File.exist?("#{Rails.root}/public/stylesheets/sass/admin/overrides.sass")
      overrides << 'admin/overrides'
    end
    overrides
  end

  def javascript_overrides
    overrides = []
    if File.exist?("#{Rails.root}/public/javascripts/admin/overrides.js")
      overrides << 'admin/overrides'
    end
    overrides
  end

  # Returns a Gravatar URL associated with the email parameter.
  # See: http://douglasfshearer.com/blog/gravatar-for-ruby-and-ruby-on-rails
  def gravatar_url(email, options={})
    # Default to highest rating. Rating can be one of G, PG, R X.
    options[:rating] ||= "G"

    # Default size of the image.
    options[:size] ||= "32px"

    # Default image url to be used when no gravatar is found
    # or when an image exceeds the rating parameter.
    local_avatar_url = "/images/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
    default_avatar_url = "#{request.protocol}#{request.host_with_port}#{ActionController::Base.relative_url_root}#{local_avatar_url}"
    options[:default] ||= default_avatar_url

    unless email.blank?
      # Build the Gravatar url.
      url = '//gravatar.com/avatar/'
      url << "#{Digest::MD5.new.update(email)}?"
      url << "rating=#{options[:rating]}" if options[:rating]
      url << "&size=#{options[:size]}" if options[:size]<commit_msg>See Issue - 149 - Fix asset path for avatar image<commit_after>module ApplicationHelper
  include Admin::RegionsHelper

  def trusty_config
    TrustyCms::Config
  end

  def default_page_title
    title + ' - ' + subtitle
  end

  def title
    trusty_config['admin.title'] || 'Trusty CMS'
  end

  def subtitle
    trusty_config['admin.subtitle'] || 'Publishing for Small Teams'
  end

  def logged_in?
    !current_user.nil?
  end

  def onsubmit_status(model)
    model.new_record? ? t('creating_status', :model => t(model.class.name.downcase)) : "#{I18n.t('saving_changes')}&#8230;"
  end

  def save_model_button(model, options = {})
    model_name = model.class.name.underscore
    human_model_name = model_name.humanize.titlecase
    options[:label] ||= model.new_record? ?
      t('buttons.create', :name => t(model_name, :default => human_model_name), :default => 'Create ' + human_model_name) :
      t('buttons.save_changes', :default => 'Save Changes')
    options[:class] ||= "button"
    options[:accesskey] ||= 'S'
    submit_tag options.delete(:label), options
  end

  def save_model_and_continue_editing_button(model)
    submit_tag t('buttons.save_and_continue'), :name => 'continue', :class => 'button', :accesskey => "s"
  end

  def current_item?(item)
    if item.tab && item.tab.many? {|i| current_url?(i.relative_url) }
      # Accept only stricter URL matches if more than one matches
      current_page?(item.url)
    else
      current_url?(item.relative_url)
    end
  end

  def current_tab?(tab)
    @current_tab ||= tab if tab.any? {|item| current_url?(item.relative_url) }
    @current_tab == tab
  end

  def current_url?(options)
    url = case options
          when Hash
            url_for options
          else
            options.to_s
          end
    #TODO: look for other instances of request_uri
    request.original_fullpath =~ Regexp.new('^' + Regexp.quote(clean(url)))
  end

  def clean(url)
    uri = URI.parse(url)
    uri.path.gsub(%r{/+}, '/').gsub(%r{/$}, '')
  end

  def nav_link_to(name, options)
    if current_url?(options)
      %{<strong>#{ link_to translate_with_default(name), options }</strong>}
    else
      link_to translate_with_default(name), options
    end
  end

  def admin?
    current_user and current_user.admin?
  end

  def designer?
    current_user and (current_user.designer? or current_user.admin?)
  end

  def updated_stamp(model)
    unless model.new_record?
      updated_by = (model.updated_by || model.created_by)
      name = updated_by ? updated_by.name : nil
      time = (model.updated_at || model.created_at)
      if name or time
        html = %{<p class="updated_line">#{t('timestamp.last_updated')} }
        html << %{#{t('timestamp.by')} <strong>#{name}</strong> } if name
        html << %{#{t('timestamp.at')} #{timestamp(time)}} if time
        html << %{</p>}
        html.html_safe
      end
    end
  end

  def timestamp(time)
    # time.strftime("%I:%M %p on %B %e, %Y").sub("AM", 'am').sub("PM", 'pm')
    I18n.localize(time, :format => :timestamp)
  end

  def meta_visible(symbol)
    v = case symbol
    when :meta_more
      not meta_errors?
    when :meta, :meta_less
      meta_errors?
    end
    v ? {} : {:style => "display: none"}
  end

  def meta_errors?
    false
  end

  def meta_label
    meta_errors? ? 'Less' : 'More'
  end

  def image(name, options = {})
    image_tag(append_image_extension("admin/#{name}"), options)
  end

  def image_submit(name, options = {})
    image_submit_tag(append_image_extension("admin/#{name}"), options)
  end

  def admin
    TrustyCms::AdminUI.instance
  end

  def filter_options_for_select(selected=nil)
    options_for_select([[t('select.none'), '']] + TextFilter.descendants_names, selected)
  end

  def body_classes
    @body_classes ||= []
  end

  def nav_tabs
    admin.nav
  end

  def translate_with_default(name)
    t(name.underscore.downcase, :default => name)
  end

  def available_locales_select
    [[t('select.default'),'']] + TrustyCms::AvailableLocales.locales
  end

  def stylesheet_overrides
    overrides = []
    if File.exist?("#{Rails.root}/public/stylesheets/admin/overrides.css") || File.exist?("#{Rails.root}/public/stylesheets/sass/admin/overrides.sass")
      overrides << 'admin/overrides'
    end
    overrides
  end

  def javascript_overrides
    overrides = []
    if File.exist?("#{Rails.root}/public/javascripts/admin/overrides.js")
      overrides << 'admin/overrides'
    end
    overrides
  end

  # Returns a Gravatar URL associated with the email parameter.
  # See: http://douglasfshearer.com/blog/gravatar-for-ruby-and-ruby-on-rails
  def gravatar_url(email, options={})
    # Default to highest rating. Rating can be one of G, PG, R X.
    options[:rating] ||= "G"

    # Default size of the image.
    options[:size] ||= "32px"

    # Default image url to be used when no gravatar is found
    # or when an image exceeds the rating parameter.
    local_avatar_url = "/assets/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
    default_avatar_url = "#{request.protocol}#{request.host_with_port}#{ActionController::Base.relative_url_root}#{local_avatar_url}"
    options[:default] ||= default_avatar_url

    unless email.blank?
      # Build the Gravatar url.
      url = '//gravatar.com/avatar/'
      url << "#{Digest::MD5.new.update(email)}?"
      url << "rating=#{options[:rating]}" if options[:rating]
      url << "&size=#{options[:size]}" if options[:size]
{'fraction_non_alphanumeric': 0.13951362223441213, 'fraction_numerical': 0.0024684585847504115, 'mean_word_length': 3.332277227722772, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 37, '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': '6698041', 'n_tokens_mistral': 3772, 'n_tokens_neox': 3701, 'n_words': 795}