#!/usr/bin/env ruby # # USAGE:: # # script/refresh_name_lister_cache # # DESCRIPTION:: # # This is a nightly cronjob that updates the name_lister data cache. # It just writes the file NAME_LISTER_CACHE_FILE: # # RAILS_ROOT/public/javascripts/name_list_data.js # ################################################################################ require File.expand_path('../../config/boot.rb', __FILE__) require File.expand_path('../../config/environment.rb', __FILE__) # Tired of fighting with ActionView to get it to let me use the one in # helpers/javascript_helper.rb, so I just copied it verbatim. def escape(str) str.to_s.gsub('\\','\0\0').gsub(' 1 n += '|' + a if !a.blank? && need_author if s.to_i > 0 && d.to_i != 1 l = valid[s] ||= [] l.push(n) if !l.include?(n) end end # Now insert valid synonyms after each deprecated name. Stick a "*" after # all accepted names (including, of course, the accepted synonyms). # Include author after names, using a "|" to help make it easy for # javascript to parse it correctly. species = species.map do |rec| n, a, d, s = rec.values_at('n', 'a', 'd', 's') need_author = occurs[n] > 1 n += '|' + a if !a.blank? && need_author n += '*' if d.to_i != 1 d.to_i == 1 && valid[s] ? ([n] + valid[s].map {|x| "= #{x}"}) : n end.flatten # Write cache. File.open(NAME_LISTER_CACHE_FILE, 'w') do |fh| fh.write %( var NL_GENERA = [#{genera.map {|n| "'#{escape(n)}'"}.join(",")}]; var NL_SPECIES = [#{species.map {|n| "'#{escape(n)}'"}.join(",")}]; var NL_NAMES = []; ) end exit 0