From 7fc2dbecd572718421d000441ccb305241e7d5ff Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 6 Jan 2008 12:17:58 +0000 Subject: [PATCH] Migrate web site to use rails 2.0.1. --- app/views/site/index.rhtml | 2 +- config/boot.rb | 127 +- config/environment.rb | 122 +- config/environments/development.rb | 7 +- config/environments/production.rb | 2 +- config/environments/test.rb | 5 +- config/initializers/action_mailer.rb | 6 + config/initializers/assert_tag_helper.rb | 10 + config/initializers/buffered_logger.rb | 14 + config/initializers/inflections.rb | 10 + config/initializers/limits.rb | 6 + config/initializers/mime_types.rb | 5 + config/initializers/sql_session_store.rb | 2 + lib/daemons/gpx_import.rb | 2 +- public/javascripts/controls.js | 838 +++-- public/javascripts/dragdrop.js | 146 +- public/javascripts/effects.js | 760 +++-- public/javascripts/prototype.js | 3924 ++++++++++++++++------ 18 files changed, 3980 insertions(+), 2008 deletions(-) create mode 100644 config/initializers/action_mailer.rb create mode 100644 config/initializers/assert_tag_helper.rb create mode 100644 config/initializers/buffered_logger.rb create mode 100644 config/initializers/inflections.rb create mode 100644 config/initializers/limits.rb create mode 100644 config/initializers/mime_types.rb create mode 100644 config/initializers/sql_session_store.rb diff --git a/app/views/site/index.rhtml b/app/views/site/index.rhtml index 27bcd618e..604ecdb32 100644 --- a/app/views/site/index.rhtml +++ b/app/views/site/index.rhtml @@ -51,7 +51,7 @@ by the OpenStreetMap project and it's contributors. <% zoom = h(params['zoom'] || '12') %> <% layers = h(params['layers']) %> <% elsif cookies.key?("location") %> -<% lon,lat,zoom,layers = cookies["location"].value.first.split(",") %> +<% lon,lat,zoom,layers = cookies["location"].split(",") %> <% elsif @user and !@user.home_lon.nil? and !@user.home_lat.nil? %> <% lon = @user.home_lon %> <% lat = @user.home_lat %> diff --git a/config/boot.rb b/config/boot.rb index b7af0c35c..b71c198db 100644 --- a/config/boot.rb +++ b/config/boot.rb @@ -1,45 +1,108 @@ -# Don't change this file. Configuration is done in config/environment.rb and config/environments/*.rb +# Don't change this file! +# Configure your app in config/environment.rb and config/environments/*.rb -unless defined?(RAILS_ROOT) - root_path = File.join(File.dirname(__FILE__), '..') +RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT) - unless RUBY_PLATFORM =~ /(:?mswin|mingw)/ - require 'pathname' - root_path = Pathname.new(root_path).cleanpath(true).to_s - end +module Rails + class << self + def boot! + unless booted? + preinitialize + pick_boot.run + end + end - RAILS_ROOT = root_path -end + def booted? + defined? Rails::Initializer + end + + def pick_boot + (vendor_rails? ? VendorBoot : GemBoot).new + end + + def vendor_rails? + File.exist?("#{RAILS_ROOT}/vendor/rails") + end -unless defined?(Rails::Initializer) - if File.directory?("#{RAILS_ROOT}/vendor/rails") - require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer" - else - require 'rubygems' + def preinitialize + load(preinitializer_path) if File.exists?(preinitializer_path) + end - environment_without_comments = IO.readlines(File.dirname(__FILE__) + '/environment.rb').reject { |l| l =~ /^#/ }.join - environment_without_comments =~ /[^#]RAILS_GEM_VERSION = '([\d.]+)'/ - rails_gem_version = $1 + def preinitializer_path + "#{RAILS_ROOT}/config/preinitializer.rb" + end + end - if version = defined?(RAILS_GEM_VERSION) ? RAILS_GEM_VERSION : rails_gem_version - # Asking for 1.1.6 will give you 1.1.6.5206, if available -- makes it easier to use beta gems - rails_gem = Gem.cache.search('rails', "~>#{version}.0").sort_by { |g| g.version.version }.last + class Boot + def run + load_initializer + Rails::Initializer.run(:set_load_path) + end + end - if rails_gem - gem "rails", "=#{rails_gem.version.version}" - require rails_gem.full_gem_path + '/lib/initializer' + class VendorBoot < Boot + def load_initializer + require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer" + end + end + + class GemBoot < Boot + def load_initializer + self.class.load_rubygems + load_rails_gem + require 'initializer' + end + + def load_rails_gem + if version = self.class.gem_version + gem 'rails', version else - STDERR.puts %(Cannot find gem for Rails ~>#{version}.0: - Install the missing gem with 'gem install -v=#{version} rails', or - change environment.rb to define RAILS_GEM_VERSION with your desired version. - ) + gem 'rails' + end + rescue Gem::LoadError => load_error + $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.) + exit 1 + end + + class << self + def rubygems_version + Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion + end + + def gem_version + if defined? RAILS_GEM_VERSION + RAILS_GEM_VERSION + elsif ENV.include?('RAILS_GEM_VERSION') + ENV['RAILS_GEM_VERSION'] + else + parse_gem_version(read_environment_rb) + end + end + + def load_rubygems + require 'rubygems' + + unless rubygems_version >= '0.9.4' + $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.) + exit 1 + end + + rescue LoadError + $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org) exit 1 end - else - gem "rails" - require 'initializer' + + def parse_gem_version(text) + $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*'([!~<>=]*\s*[\d.]+)'/ + end + + private + def read_environment_rb + File.read("#{RAILS_ROOT}/config/environment.rb") + end end end - - Rails::Initializer.run(:set_load_path) end + +# All that for this: +Rails.boot! diff --git a/config/environment.rb b/config/environment.rb index 2bc029767..ed7bb6774 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,58 +1,63 @@ -# Be sure to restart your web server when you modify this file. +# Be sure to restart your server when you modify this file -# Limit each rails process to a 512Mb resident set size -Process.setrlimit Process::RLIMIT_AS, 640*1024*1024, Process::RLIM_INFINITY - -# Uncomment below to force Rails into production mode when +# Uncomment below to force Rails into production mode when # you don't control web/app server and can't set it the proper way ENV['RAILS_ENV'] ||= 'production' # Specifies gem version of Rails to use when vendor/rails is not present -RAILS_GEM_VERSION = '1.2.3' +RAILS_GEM_VERSION = '2.0.1' unless defined? RAILS_GEM_VERSION -# Bootstrap the Rails environment, frameworks, and default configuration -require File.join(File.dirname(__FILE__), 'boot') +# Set the server URL +SERVER_URL = ENV['OSM_SERVER_URL'] || 'www.openstreetmap.org' # Application constants needed for routes.rb - must go before Initializer call API_VERSION = ENV['OSM_API_VERSION'] || '0.5' -# Custom logger class to format messages sensibly -class OSMLogger < Logger - def format_message(severity, time, progname, msg) - "[%s.%06d #%d] %s\n" % [time.strftime("%Y-%m-%d %H:%M:%S"), time.usec, $$, msg.sub(/^\n+/, "")] - end -end +# Set to :readonly to put the API in read-only mode or :offline to +# take it completely offline +API_STATUS = :online + +# Bootstrap the Rails environment, frameworks, and default configuration +require File.join(File.dirname(__FILE__), 'boot') Rails::Initializer.run do |config| - # Settings in config/environments/* take precedence those specified here - - # Skip frameworks you're not going to use - # config.frameworks -= [ :action_web_service, :action_mailer ] + # Settings in config/environments/* take precedence over those specified here. + # Application configuration should go into files in config/initializers + # -- all .rb files in that directory are automatically loaded. + # See Rails::Configuration for more options. + + # Skip frameworks you're not going to use (only works if using vendor/rails). + # To use Rails without a database, you must remove the Active Record framework + # config.frameworks -= [ :active_record, :active_resource, :action_mailer ] + + # Only load the plugins named here, in the order given. By default, all plugins + # in vendor/plugins are loaded in alphabetical order. + # :all can be used as a placeholder for all plugins not explicitly named + # config.plugins = [ :exception_notification, :ssl_requirement, :all ] # Add additional load paths for your own custom dirs # config.load_paths += %W( #{RAILS_ROOT}/extras ) - # Force all environments to use the same logger level + # Force all environments to use the same logger level # (by default production uses :info, the others :debug) # config.log_level = :debug - # Use our custom logger - config.logger = OSMLogger.new(config.log_path) - config.logger.level = Logger.const_get(config.log_level.to_s.upcase) - - # Use the database for sessions instead of the file system + # Your secret key for verifying cookie session data integrity. + # If you change this key, all old sessions will become invalid! + # Make sure the secret is at least 30 characters and all random, + # no regular words or you'll be exposed to dictionary attacks. + config.action_controller.session = { + :session_key => '_osm_session', + :secret => 'd886369b1e709c61d1f9fcb07384a2b96373c83c01bfc98c6611a9fe2b6d0b14215bb360a0154265cccadde5489513f2f9b8d9e7b384a11924f772d2872c2a1f' + } + + # Use the database for sessions instead of the cookie-based default, + # which shouldn't be used to store highly confidential information # (create the session table with 'rake db:sessions:create') - # config.action_controller.session_store = :sql_session_store - - # Unfortunately SqlSessionStore is a plugin which has not been - # loaded yet, so we have to do things the hard way... - config.after_initialize do - ActionController::Base.session_store = :sql_session_store - SqlSessionStore.session_class = MysqlSession - end + config.action_controller.session_store = :sql_session_store # Use SQL instead of Active Record's schema dumper when creating the test database. - # This is necessary if your schema can't be completely dumped by the schema dumper, + # This is necessary if your schema can't be completely dumped by the schema dumper, # like if you have constraints or database-specific column types config.active_record.schema_format = :sql @@ -61,55 +66,4 @@ Rails::Initializer.run do |config| # Make Active Record use UTC-base instead of local time # config.active_record.default_timezone = :utc - - # See Rails::Configuration for more options end - -# Add new inflection rules using the following format -# (all these examples are active by default): -# Inflector.inflections do |inflect| -# inflect.plural /^(ox)$/i, '\1en' -# inflect.singular /^(ox)en/i, '\1' -# inflect.irregular 'person', 'people' -# inflect.uncountable %w( fish sheep ) -# end - -# Hack the AssetTagHelper to make asset tagging work better -module ActionView - module Helpers - module AssetTagHelper - private - alias :old_compute_public_path :compute_public_path - - def compute_public_path(source, dir, ext) - path = old_compute_public_path(source, dir, ext) - if path =~ /(.+)\?(\d+)\??$/ - path = "#{$1}/#{$2}" - end - path - end - end - end -end - -# Set to :readonly to put the API in read-only mode or :offline to -# take it completely offline -API_STATUS = :online - -# Include your application configuration below -SERVER_URL = ENV['OSM_SERVER_URL'] || 'www.openstreetmap.org' - -ActionMailer::Base.smtp_settings = { - :address => "localhost", - :port => 25, - :domain => 'localhost', -} - -#Taming FCGI -# -COUNT = 0 -MAX_COUNT = 10000 - - - - diff --git a/config/environments/development.rb b/config/environments/development.rb index 0589aa979..09a451f9a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -8,14 +8,11 @@ config.cache_classes = false # Log error messages when you accidentally call methods on nil. config.whiny_nils = true -# Enable the breakpoint server that script/breakpointer connects to -config.breakpoint_server = true - # Show full error reports and disable caching config.action_controller.consider_all_requests_local = true +config.action_view.debug_rjs = true config.action_controller.perform_caching = false config.action_view.cache_template_extensions = false -config.action_view.debug_rjs = true # Don't care if the mailer can't send -config.action_mailer.raise_delivery_errors = false +config.action_mailer.raise_delivery_errors = false \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb index 5a4e2b1c5..cb295b83f 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -14,5 +14,5 @@ config.action_controller.perform_caching = true # Enable serving of images, stylesheets, and javascripts from an asset server # config.action_controller.asset_host = "http://assets.example.com" -# Disable delivery errors if you bad email addresses should just be ignored +# Disable delivery errors, bad email addresses will be ignored # config.action_mailer.raise_delivery_errors = false diff --git a/config/environments/test.rb b/config/environments/test.rb index f0689b924..58850a797 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -13,7 +13,10 @@ config.whiny_nils = true config.action_controller.consider_all_requests_local = true config.action_controller.perform_caching = false +# Disable request forgery protection in test environment +config.action_controller.allow_forgery_protection = false + # Tell ActionMailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. -config.action_mailer.delivery_method = :test \ No newline at end of file +config.action_mailer.delivery_method = :test diff --git a/config/initializers/action_mailer.rb b/config/initializers/action_mailer.rb new file mode 100644 index 000000000..27030d541 --- /dev/null +++ b/config/initializers/action_mailer.rb @@ -0,0 +1,6 @@ +# Configure ActionMailer +ActionMailer::Base.smtp_settings = { + :address => "localhost", + :port => 25, + :domain => 'localhost', +} diff --git a/config/initializers/assert_tag_helper.rb b/config/initializers/assert_tag_helper.rb new file mode 100644 index 000000000..a5f2e5ac3 --- /dev/null +++ b/config/initializers/assert_tag_helper.rb @@ -0,0 +1,10 @@ +module ActionView + module Helpers + module AssetTagHelper + def rewrite_asset_path!(source) + asset_id = rails_asset_id(source) + source << "/#{asset_id}" if !asset_id.blank? + end + end + end +end diff --git a/config/initializers/buffered_logger.rb b/config/initializers/buffered_logger.rb new file mode 100644 index 000000000..03d88f69c --- /dev/null +++ b/config/initializers/buffered_logger.rb @@ -0,0 +1,14 @@ +# Hack BufferedLogger to add timestamps to messages +module ActiveSupport + class BufferedLogger + alias_method :old_add, :add + + def add(severity, message = nil, progname = nil, &block) + return if @level > severity + message = (message || (block && block.call) || progname).to_s + time = Time.now + message = "[%s.%06d #%d] %s\n" % [time.strftime("%Y-%m-%d %H:%M:%S"), time.usec, $$, message.sub(/^\n+/, "")] + old_add(severity, message) + end + end +end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb new file mode 100644 index 000000000..09158b865 --- /dev/null +++ b/config/initializers/inflections.rb @@ -0,0 +1,10 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format +# (all these examples are active by default): +# Inflector.inflections do |inflect| +# inflect.plural /^(ox)$/i, '\1en' +# inflect.singular /^(ox)en/i, '\1' +# inflect.irregular 'person', 'people' +# inflect.uncountable %w( fish sheep ) +# end diff --git a/config/initializers/limits.rb b/config/initializers/limits.rb new file mode 100644 index 000000000..f88d43956 --- /dev/null +++ b/config/initializers/limits.rb @@ -0,0 +1,6 @@ +# Limit each rails process to a 512Mb resident set size +Process.setrlimit Process::RLIMIT_AS, 640*1024*1024, Process::RLIM_INFINITY + +# Force a restart after every 10000 requests +COUNT = 0 +MAX_COUNT = 10000 diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb new file mode 100644 index 000000000..72aca7e44 --- /dev/null +++ b/config/initializers/mime_types.rb @@ -0,0 +1,5 @@ +# Be sure to restart your server when you modify this file. + +# Add new mime types for use in respond_to blocks: +# Mime::Type.register "text/richtext", :rtf +# Mime::Type.register_alias "text/html", :iphone diff --git a/config/initializers/sql_session_store.rb b/config/initializers/sql_session_store.rb new file mode 100644 index 000000000..fd6c11d44 --- /dev/null +++ b/config/initializers/sql_session_store.rb @@ -0,0 +1,2 @@ +# Use the MySQL interface for SqlSessionStore +SqlSessionStore.session_class = MysqlSession diff --git a/lib/daemons/gpx_import.rb b/lib/daemons/gpx_import.rb index 86bd2ac14..07f4cd6a4 100755 --- a/lib/daemons/gpx_import.rb +++ b/lib/daemons/gpx_import.rb @@ -55,5 +55,5 @@ while(true) do exit if terminated end - sleep 5.minutes + sleep 5.minutes.value end diff --git a/public/javascripts/controls.js b/public/javascripts/controls.js index 8c273f874..fbc4418b8 100644 --- a/public/javascripts/controls.js +++ b/public/javascripts/controls.js @@ -1,6 +1,6 @@ -// Copyright (c) 2005, 2006 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) -// (c) 2005, 2006 Ivan Krstic (http://blogs.law.harvard.edu/ivan) -// (c) 2005, 2006 Jon Tirsen (http://www.tirsen.com) +// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) +// (c) 2005-2007 Ivan Krstic (http://blogs.law.harvard.edu/ivan) +// (c) 2005-2007 Jon Tirsen (http://www.tirsen.com) // Contributors: // Richard Livsey // Rahul Bhargava @@ -37,22 +37,23 @@ if(typeof Effect == 'undefined') throw("controls.js requires including script.aculo.us' effects.js library"); -var Autocompleter = {} -Autocompleter.Base = function() {}; -Autocompleter.Base.prototype = { +var Autocompleter = { } +Autocompleter.Base = Class.create({ baseInitialize: function(element, update, options) { - this.element = $(element); + element = $(element) + this.element = element; this.update = $(update); this.hasFocus = false; this.changed = false; this.active = false; this.index = 0; this.entryCount = 0; + this.oldElementValue = this.element.value; if(this.setOptions) this.setOptions(options); else - this.options = options || {}; + this.options = options || { }; this.options.paramName = this.options.paramName || this.element.name; this.options.tokens = this.options.tokens || []; @@ -74,6 +75,9 @@ Autocompleter.Base.prototype = { if(typeof(this.options.tokens) == 'string') this.options.tokens = new Array(this.options.tokens); + // Force carriage returns as token delimiters anyway + if (!this.options.tokens.include('\n')) + this.options.tokens.push('\n'); this.observer = null; @@ -81,15 +85,14 @@ Autocompleter.Base.prototype = { Element.hide(this.update); - Event.observe(this.element, "blur", this.onBlur.bindAsEventListener(this)); - Event.observe(this.element, "keypress", this.onKeyPress.bindAsEventListener(this)); + Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this)); + Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this)); }, show: function() { if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update); if(!this.iefix && - (navigator.appVersion.indexOf('MSIE')>0) && - (navigator.userAgent.indexOf('Opera')<0) && + (Prototype.Browser.IE) && (Element.getStyle(this.update, 'position')=='absolute')) { new Insertion.After(this.update, '