<commit_before>module AssetifyHelper
  
  def assetify_javascript_library_include_tag(library = "library", options = {})
    asset = "/assets/#{library}.js"
    fix_rails_asset_cache(asset)
    javascript_include_tag(asset)
  end
  
  def assetify_javascript_dynamic_include_tag(options = {})
    cont = options.delete(:controller) || params[:controller]
    act = options.delete(:action) || params[:action]
    
    asset = "/assets/dynamic/#{cont}/#{act}.js"
    fix_rails_asset_cache(asset)
    javascript_include_tag(asset)
  end
  
  def assetify_stylesheet_library_include_tag(library = "library", options = {})
    include_print = options.delete(:include_print)
    include_ie = options.delete(:include_ie)
    
    tags = []

    asset = "/assets/#{library}.css"
    fix_rails_asset_cache(asset)
    tags << stylesheet_link_tag(asset)
    
    if include_print
      asset = "/assets/#{library}/print.css"
      fix_rails_asset_cache(asset)
      tags << stylesheet_link_tag(asset, :media => "print")
    end
    
    if include_ie
      asset = "/assets/#{library}/ie.css"
      fix_rails_asset_cache(asset)
      tags << ("<!--[if lt IE 8]>\n" + stylesheet_link_tag(asset) + "\n<![endif]-->")
    end
    
    tags.join("\n")
  end
  
  def assetify_stylesheet_dynamic_include_tag(options = {})
    cont = options.delete(:controller) || params[:controller]
    act = options.delete(:action) || params[:action]
    include_print = options.delete(:include_print)
    include_ie = options.delete(:include_ie)
    
    tags = []
    
    asset = "/assets/dynamic/#{cont}/#{act}.css"
    fix_rails_asset_cache(asset)
    tags << stylesheet_link_tag(asset)
    
    if include_print
      asset = "/assets/dynamic/#{cont}/#{act}/print.css"
      fix_rails_asset_cache(asset)
      tags << stylesheet_link_tag(asset, :media => "print")
    end
    
    if include_ie
      asset = "/assets/dynamic/#{cont}/#{act}/ie.css"
      fix_rails_asset_cache(asset)
      tags << ("<!--[if lt IE 8]>\n" + stylesheet_link_tag(asset) + "\n<![endif]-->")
    end
    
    tags.join("\n")
  end
  
  def assetify_timestamp_header
    <<-JS_TIMESTAMP
      <script type="text/javascript">
          var $loadTimes = {domHeader: new Date().getTime()};
      </script>
    JS_TIMESTAMP
  end
  
  def assetify_timestamp_after_body
    <<-JS_TIMESTAMP
      <script type="text/javascript">
          $loadTimes.domMiddle = new Date().getTime();
      </script>
    JS_TIMESTAMP
  end
  
  def assetify_timestamp_bottom(options = {})
    library = options.delete(:library) || :mootools
    
    domready_function = {
      :mootools   =>  "window.addEvent('domready',function(){",
      :jquery     =>  "$(document).ready(function(){",
      :prototype  =>  "document.observe('dom:loaded',function(){"
    }[library]
    
    <<-JS_TIMESTAMP
      <script type="text/javascript">
          $loadTimes.domBottom = new Date().getTime();
        
          #{domready_function}
              $loadTimes.domReady = new Date().getTime();
            
              if (console && console.log) {
                console.log("domTop to domMiddle: ", ($loadTimes.domMiddle - $loadTimes.domHeader));
                console.log("domTop to domEnd: ", ($loadTimes.domBottom - $loadTimes.domHeader));
                console.log("domTop to domReady: ", ($loadTimes.domReady - $loadTimes.domHeader));
              }
        });
        
      </script>
    JS_TIMESTAMP
  end
  
  private
    
    # NOTE: this function fixes Rails caching of asset timestamps
    # Without this, in production, scripts tags will look like "/assets/library.js",
    # because the file won't exist the first time through.  It will stay like this throughout the life of the application,
    # unless the server is restarted.
    # This method will cause the first asset to be "/assets/library.js", but all subsequent requests after that will
    # have the correct timestamp cached (eg, "/assets/library.js?234235235").
    def fix_rails_asset_cache(asset_name)
      if ActionView::Helpers::AssetTagHelper.cache_asset_timestamps
        timestamps_cache = self.class.send(:class_variable_get, :@@asset_timestamps_cache)
        timestamps_guard = self.class.send(:class_variable_get, :@@asset_timestamps_cache_guard)

        timestamps_guard.synchronize do 
          asset_id = timestamps_cache[asset_name]
          timestamps_cache.delete(asset_name) if asset_id == ''
        end
      end
    end
end<commit_msg>Refactor the assetify_dom_ready helper function for timestamp reporting.<commit_after>module AssetifyHelper
  
  def assetify_javascript_library_include_tag(library = "library", options = {})
    asset = "/assets/#{library}.js"
    fix_rails_asset_cache(asset)
    javascript_include_tag(asset)
  end
  
  def assetify_javascript_dynamic_include_tag(options = {})
    cont = options.delete(:controller) || params[:controller]
    act = options.delete(:action) || params[:action]
    
    asset = "/assets/dynamic/#{cont}/#{act}.js"
    fix_rails_asset_cache(asset)
    javascript_include_tag(asset)
  end
  
  def assetify_stylesheet_library_include_tag(library = "library", options = {})
    include_print = options.delete(:include_print)
    include_ie = options.delete(:include_ie)
    
    tags = []

    asset = "/assets/#{library}.css"
    fix_rails_asset_cache(asset)
    tags << stylesheet_link_tag(asset)
    
    if include_print
      asset = "/assets/#{library}/print.css"
      fix_rails_asset_cache(asset)
      tags << stylesheet_link_tag(asset, :media => "print")
    end
    
    if include_ie
      asset = "/assets/#{library}/ie.css"
      fix_rails_asset_cache(asset)
      tags << ("<!--[if lt IE 8]>\n" + stylesheet_link_tag(asset) + "\n<![endif]-->")
    end
    
    tags.join("\n")
  end
  
  def assetify_stylesheet_dynamic_include_tag(options = {})
    cont = options.delete(:controller) || params[:controller]
    act = options.delete(:action) || params[:action]
    include_print = options.delete(:include_print)
    include_ie = options.delete(:include_ie)
    
    tags = []
    
    asset = "/assets/dynamic/#{cont}/#{act}.css"
    fix_rails_asset_cache(asset)
    tags << stylesheet_link_tag(asset)
    
    if include_print
      asset = "/assets/dynamic/#{cont}/#{act}/print.css"
      fix_rails_asset_cache(asset)
      tags << stylesheet_link_tag(asset, :media => "print")
    end
    
    if include_ie
      asset = "/assets/dynamic/#{cont}/#{act}/ie.css"
      fix_rails_asset_cache(asset)
      tags << ("<!--[if lt IE 8]>\n" + stylesheet_link_tag(asset) + "\n<![endif]-->")
    end
    
    tags.join("\n")
  end
  
  def assetify_timestamp_header
    <<-JS_TIMESTAMP
      <script type="text/javascript">
          var $loadTimes = {domHeader: new Date().getTime()};
      </script>
    JS_TIMESTAMP
  end
  
  def assetify_timestamp_after_body
    <<-JS_TIMESTAMP
      <script type="text/javascript">
          $loadTimes.domMiddle = new Date().getTime();
      </script>
    JS_TIMESTAMP
  end
  
  def assetify_timestamp_bottom(options = {})
    library = options.delete(:library) || :mootools
    output = options.delete(:output) || :console
    
    domready_function = {
      :mootools   =>  "window.addEvent('domready',function(){",
      :jquery     =>  "$(document).ready(function(){",
      :prototype  =>  "document.observe('dom:loaded',function(){"
    }[library]
    
    <<-JS_TIMESTAMP
      <script type="text/javascript">
          $loadTimes.domBottom = new Date().getTime();
        
          #{domready_function}
              $loadTimes.domReady = new Date().getTime();
            
              var str = "DomTop to (Middle, Bottom, DomReady): (" +
                        ($loadTimes.domMiddle - $loadTimes.domHeader) + ", " +
                        ($loadTimes.domBottom - $loadTimes.domHeader) + ", " +
                        ($loadTimes.domReady - $loadTimes.domHeader) + ")" +
                        " DomReady: " + ($loadTimes.domReady - $loadTimes.domBottom);
              
              if (#{output == :console} && console && console.log) {
                  console.log(str);
              } else if (#{output == :function}) {
                  #{options[:function]}(str);
              }
        });
        
      </script>
    JS_TIMESTAMP
  end
  
  private
    
    # NOTE: this function fixes Rails caching of asset timestamps
    # Without this, in production, scripts tags will look like "/assets/library.js",
    # because the file won't exist the first time through.  It will stay like this throughout the life of the application,
    # unless the server is restarted.
    # This method will cause the first asset to be "/assets/library.js", but all subsequent requests after that will
    # have the correct timestamp cached (eg, "/assets/library.js?234235235").
    def fix_rails_asset_cache(asset_name)
      if ActionView::Helpers::AssetTagHelper.cache_asset_timestamps
        timestamps_cache = self.class.send(:class_variable_get, :@@asset_timestamps_cache)
        timestamps_guard = self.class.send(:class_variable_get, :@@asset_timestamps_cache_guard)

        timestamps_guard.synchronize do 
          asset_id = timestamps_cache[asset_name]
          timestamps_cache.delete(asset_name) if asset_id == ''
        end
      end
    end
end
{'fraction_non_alphanumeric': 0.12891799055388578, 'fraction_numerical': 0.0023615285530270503, 'mean_word_length': 2.7843216896831846, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 33, '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': '3790313', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2845, 'n_words': 546}