<commit_before>require 'md5'

module MuckEngineHelper

  # Outputs the appropriate http protocol based on 
  # the request type.  if https is desired then 
  # pass true to override default behavior
  def http_protocol(use_ssl = request.ssl?)
    (use_ssl ? "https://" : "http://")
  end

  # Outputs a small bit of javascript that will enable message 
  # output to jgrowl or to the given message dom id
  #
  # message_dom_id: The dom id of the element that will hold messages.
  #                 This element can have display:none by default.
  def jquery_json_message(message_dom_id = nil)
    if MuckEngine.configuration.growl_enabled
      "jQuery.jGrowl.info(json.message);"
    else
      "jQuery('##{message_dom_id}').html(json.message);" +
      "jQuery('##{message_dom_id}').show();"
    end
  end
  
  # Outputs scripts that manipulate the country and state select controls
  def country_scripts
    return if defined?(@@country_scripts_included)
    @@country_scripts_included = true
    render :partial => 'scripts/country_scripts'
  end
  
  def muck_form_for(record_or_name_or_array, *args, &proc) 
    options = args.detect { |argument| argument.is_a?(Hash) } 
    if options.nil? 
      options = {:builder => MuckEngine::FormBuilder} 
      args << options 
    end 
    options[:builder] = MuckEngine::FormBuilder unless options.nil? 
    form_for(record_or_name_or_array, *args, &proc) 
  end
    
  # generates javascript that will hide a link or button
  # when click and then show a different dom object
  def show_hide_on_click(id_to_show, id_to_hide)
    %Q{jQuery(document).ready(function() {
      jQuery('##{id_to_hide}').click(function(){
        jQuery(this).hide();
        jQuery('##{id_to_show}').show();
      });
    });}
  end
  
  # Render a photo for the given object.  Note that the object will need a 'photo' method
  # provided by paperclip. If the object does not provide an image, but does have an 'email' method
  # this method will attempt to use gravatar.com to find a matching image.  
  # 
  # object:           Object to get icon for.
  # size:             Size to get. size is commonly one of:
  #                     :medium, :thumb, :icon or :tiny but can be any value provided by the photo object
  # default_image:    A default image should the photo not be found.
  # gravatar_size:    Size in pixels for the gravatar.  Can be from 1 to 512.  For reference the sizes from muck-profiles are:
  #                   medium: 300, thumb: 100, icon: 50, tiny: 24.  The default is set to 50 to match the default 'size' setting which is icon.
  # rating:           Default gravatar rating - g, pg, r, x.
  # gravatar_default: If a gravatar is used, but no image is found several defaults are available.  Leaving 
  #                   this value nil will result in the 'default_image' being used.  Other wise one of the following can be set:
  #                   identicon, monsterid, wavatar, 404
  def icon(object, size = :icon, default_image = '/images/profile_default.jpg', use_only_gravatar = false, gravatar_size = 50, rating = 'g', gravatar_default = nil)
    return "" if object.blank?
    
    if object.photo.original_filename && !use_only_gravatar
      image_url = object.photo.url(size) rescue nil
    end

    if image_url.blank? && object.email.present?
      gravatar_default = File.join(root_url, default_image) if gravatar_default.blank?
      image_url = gravatar(object.email, gravatar_default, gravatar_size, rating)
    else
      image_url ||= default_image
    end
    
    link_to(image_tag(image_url, :class => size), object, { :title => object.full_name })
  end
  
  # Gets a gravatar for a given email
  #
  # gravatar_default: If a gravatar is used, but no image is found several defaults are available.  Leaving 
  #                   this value nil will result in the 'default_image' being used.  Other wise one of the following can be set:
  #                   identicon, monsterid, wavatar, 404
  # size:             Size in pixels for the gravatar.  Can be from 1 to 512.
  # rating:           Default gravatar rating - g, pg, r, x.
  def gravatar(email, gravatar_default, size = 40, rating = 'g')
    hash = MD5::md5(email)
    image_url = "http://www.gravatar.com/avatar/#{hash}"
    image_url << "?d=#{CGI::escape(gravatar_default)}"
    image_url << "&s=#{size}"
    image_url << "&r=#{rating}"
  end
  
  # Generates a css style for the given service<commit_msg>Icon should attempt gravatar only if object responds_to email<commit_after>require 'md5'

module MuckEngineHelper

  # Outputs the appropriate http protocol based on 
  # the request type.  if https is desired then 
  # pass true to override default behavior
  def http_protocol(use_ssl = request.ssl?)
    (use_ssl ? "https://" : "http://")
  end

  # Outputs a small bit of javascript that will enable message 
  # output to jgrowl or to the given message dom id
  #
  # message_dom_id: The dom id of the element that will hold messages.
  #                 This element can have display:none by default.
  def jquery_json_message(message_dom_id = nil)
    if MuckEngine.configuration.growl_enabled
      "jQuery.jGrowl.info(json.message);"
    else
      "jQuery('##{message_dom_id}').html(json.message);" +
      "jQuery('##{message_dom_id}').show();"
    end
  end
  
  # Outputs scripts that manipulate the country and state select controls
  def country_scripts
    return if defined?(@@country_scripts_included)
    @@country_scripts_included = true
    render :partial => 'scripts/country_scripts'
  end
  
  def muck_form_for(record_or_name_or_array, *args, &proc) 
    options = args.detect { |argument| argument.is_a?(Hash) } 
    if options.nil? 
      options = {:builder => MuckEngine::FormBuilder} 
      args << options 
    end 
    options[:builder] = MuckEngine::FormBuilder unless options.nil? 
    form_for(record_or_name_or_array, *args, &proc) 
  end
    
  # generates javascript that will hide a link or button
  # when click and then show a different dom object
  def show_hide_on_click(id_to_show, id_to_hide)
    %Q{jQuery(document).ready(function() {
      jQuery('##{id_to_hide}').click(function(){
        jQuery(this).hide();
        jQuery('##{id_to_show}').show();
      });
    });}
  end
  
  # Render a photo for the given object.  Note that the object will need a 'photo' method
  # provided by paperclip. If the object does not provide an image, but does have an 'email' method
  # this method will attempt to use gravatar.com to find a matching image.  
  # 
  # object:           Object to get icon for.
  # size:             Size to get. size is commonly one of:
  #                     :medium, :thumb, :icon or :tiny but can be any value provided by the photo object
  # default_image:    A default image should the photo not be found.
  # gravatar_size:    Size in pixels for the gravatar.  Can be from 1 to 512.  For reference the sizes from muck-profiles are:
  #                   medium: 300, thumb: 100, icon: 50, tiny: 24.  The default is set to 50 to match the default 'size' setting which is icon.
  # rating:           Default gravatar rating - g, pg, r, x.
  # gravatar_default: If a gravatar is used, but no image is found several defaults are available.  Leaving 
  #                   this value nil will result in the 'default_image' being used.  Other wise one of the following can be set:
  #                   identicon, monsterid, wavatar, 404
  def icon(object, size = :icon, default_image = '/images/profile_default.jpg', use_only_gravatar = false, gravatar_size = 50, rating = 'g', gravatar_default = nil)
    return "" if object.blank?
    
    if object.photo.original_filename && !use_only_gravatar
      image_url = object.photo.url(size) rescue nil
    end

    if image_url.blank? && object.respond_to?(:email) && object.email.present?
      gravatar_default = File.join(root_url, default_image) if gravatar_default.blank?
      image_url = gravatar(object.email, gravatar_default, gravatar_size, rating)
    else
      image_url ||= default_image
    end
    
    link_to(image_tag(image_url, :class => size), object, { :title => object.full_name })
  end
  
  # Gets a gravatar for a given email
  #
  # gravatar_default: If a gravatar is used, but no image is found several defaults are available.  Leaving 
  #                   this value nil will result in the 'default_image' being used.  Other wise one of the following can be set:
  #                   identicon, monsterid, wavatar, 404
  # size:             Size in pixels for the gravatar.  Can be from 1 to 512.
  # rating:           Default gravatar rating - g, pg, r, x.
  def gravatar(email, gravatar_default, size = 40, rating = 'g')
    hash = MD5::md5(email)
    image_url = "http://www.gravatar.com/avatar/#{hash}"
    image_url << "?d=#{CGI::escape(gravatar_default)}"
    image_url << "&s=#{size}"
    image_url << "&r=#{rating}"
  end
  
  # Generates a css style for the given service
{'fraction_non_alphanumeric': 0.09383348174532502, 'fraction_numerical': 0.007346393588601959, 'mean_word_length': 2.9270104895104896, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4311510', 'n_tokens_mistral': 2805, 'n_tokens_neox': 2681, 'n_words': 946}