From c06aff0a7e42868e9788d6cefe5ce49e93cbf45b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 20 Dec 2009 14:33:13 -0800 Subject: Added cookies.permanent, cookies.signed, and cookies.permanent.signed accessor for common cookie actions [DHH] --- actionpack/lib/action_controller/metal/cookies.rb | 168 ++++++++++++++++----- actionpack/test/controller/cookie_test.rb | 35 +++++ railties/CHANGELOG | 18 +++ .../initializers/cookie_verification_secret.rb.tt | 7 + 4 files changed, 190 insertions(+), 38 deletions(-) create mode 100644 railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt diff --git a/actionpack/lib/action_controller/metal/cookies.rb b/actionpack/lib/action_controller/metal/cookies.rb index 6855ca1478..8db665e929 100644 --- a/actionpack/lib/action_controller/metal/cookies.rb +++ b/actionpack/lib/action_controller/metal/cookies.rb @@ -50,56 +50,148 @@ module ActionController #:nodoc: included do helper_method :cookies + cattr_accessor :cookie_verifier_secret end - protected - # Returns the cookie container, which operates as described above. - def cookies - @cookies ||= CookieJar.build(request, response) + protected + # Returns the cookie container, which operates as described above. + def cookies + @cookies ||= CookieJar.build(request, response) + end end - end - class CookieJar < Hash #:nodoc: - def self.build(request, response) - new.tap do |hash| - hash.update(request.cookies) - hash.response = response + class CookieJar < Hash #:nodoc: + def self.build(request, response) + new.tap do |hash| + hash.update(request.cookies) + hash.response = response + end end - end - attr_accessor :response + attr_accessor :response - # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. - def [](name) - super(name.to_s) - end + # Returns the value of the cookie by +name+, or +nil+ if no such cookie exists. + def [](name) + super(name.to_s) + end + + # Sets the cookie named +name+. The second argument may be the very cookie + # value, or a hash of options as documented above. + def []=(key, options) + if options.is_a?(Hash) + options.symbolize_keys! + value = options[:value] + else + value = options + options = { :value => value } + end - # Sets the cookie named +name+. The second argument may be the very cookie - # value, or a hash of options as documented above. - def []=(key, options) - if options.is_a?(Hash) + super(key.to_s, value) + + options[:path] ||= "/" + response.set_cookie(key, options) + end + + # Removes the cookie on the client machine by setting the value to an empty string + # and setting its expiration date into the past. Like []=, you can pass in + # an options hash to delete cookies with extra data such as a :path. + def delete(key, options = {}) options.symbolize_keys! - value = options[:value] - else - value = options - options = { :value => value } + options[:path] ||= "/" + value = super(key.to_s) + response.delete_cookie(key, options) + value end - super(key.to_s, value) - - options[:path] ||= "/" - response.set_cookie(key, options) + # Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now. Example: + # + # cookies.permanent[:prefers_open_id] = true + # # => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT + # + # This jar is only meant for writing. You'll read permanent cookies through the regular accessor. + # + # This jar allows chaining with the signed jar as well, so you can set permanent, signed cookies. Examples: + # + # cookies.permanent.signed[:remember_me] = current_user.id + # # => Set-Cookie: discount=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT + def permanent + @permanent ||= PermanentCookieJar.new(self) + end + + # Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from + # the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed + # cookie was tampered with by the user (or a 3rd party), an ActiveSupport::MessageVerifier::InvalidSignature exception will + # be raised. + # + # This jar requires that you set a suitable secret for the verification on ActionController::Base.cookie_verifier_secret. + # + # Example: + # + # cookies.signed[:discount] = 45 + # # => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/ + # + # cookies.signed[:discount] # => 45 + def signed + @signed ||= SignedCookieJar.new(self) + end end - - # Removes the cookie on the client machine by setting the value to an empty string - # and setting its expiration date into the past. Like []=, you can pass in - # an options hash to delete cookies with extra data such as a :path. - def delete(key, options = {}) - options.symbolize_keys! - options[:path] ||= "/" - value = super(key.to_s) - response.delete_cookie(key, options) - value + + class PermanentCookieJar < CookieJar #:nodoc: + def initialize(parent_jar) + @parent_jar = parent_jar + end + + def []=(key, options) + if options.is_a?(Hash) + options.symbolize_keys! + else + options = { :value => options } + end + + options[:expires] = 20.years.from_now + @parent_jar[key] = options + end + + def signed + @signed ||= SignedCookieJar.new(self) + end + + def controller + @parent_jar.controller + end + + def method_missing(method, *arguments, &block) + @parent_jar.send(method, *arguments, &block) + end end + + class SignedCookieJar < CookieJar #:nodoc: + def initialize(parent_jar) + unless ActionController::Base.cookie_verifier_secret + raise "You must set ActionController::Base.cookie_verifier_secret to use signed cookies" + end + + @parent_jar = parent_jar + @verifier = ActiveSupport::MessageVerifier.new(ActionController::Base.cookie_verifier_secret) + end + + def [](name) + @verifier.verify(@parent_jar[name]) + end + + def []=(key, options) + if options.is_a?(Hash) + options.symbolize_keys! + options[:value] = @verifier.generate(options[:value]) + else + options = { :value => @verifier.generate(options) } + end + + @parent_jar[key] = options + end + + def method_missing(method, *arguments, &block) + @parent_jar.send(method, *arguments, &block) + end end end diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb index 53d4364576..84d5ce6ad4 100644 --- a/actionpack/test/controller/cookie_test.rb +++ b/actionpack/test/controller/cookie_test.rb @@ -1,5 +1,7 @@ require 'abstract_unit' +ActionController::Base.cookie_verifier_secret = "thisISverySECRET123" + class CookieTest < ActionController::TestCase class TestController < ActionController::Base def authenticate @@ -47,6 +49,21 @@ class CookieTest < ActionController::TestCase cookies["user_name"] = { :value => "david", :httponly => true } head :ok end + + def set_permanent_cookie + cookies.permanent[:user_name] = "Jamie" + head :ok + end + + def set_signed_cookie + cookies.signed[:user_id] = 45 + head :ok + end + + def set_permanent_signed_cookie + cookies.permanent.signed[:remember_me] = 100 + head :ok + end end tests TestController @@ -134,6 +151,24 @@ class CookieTest < ActionController::TestCase response = get :authenticate assert response.headers["Set-Cookie"] =~ /user_name=david/ end + + def test_permanent_cookie + get :set_permanent_cookie + assert_match /Jamie/, @response.headers["Set-Cookie"] + assert_match %r(#{20.years.from_now.year}), @response.headers["Set-Cookie"] + end + + def test_signed_cookie + get :set_signed_cookie + assert_equal 45, @controller.send(:cookies).signed[:user_id] + end + + def test_permanent_signed_cookie + get :set_permanent_signed_cookie + assert_match %r(#{20.years.from_now.year}), @response.headers["Set-Cookie"] + assert_equal 100, @controller.send(:cookies).signed[:remember_me] + end + private def assert_cookie_header(expected) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 66e0d5e9c5..9ef2922133 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,23 @@ *Edge* +* Added cookies.permanent, cookies.signed, and cookies.permanent.signed accessor for common cookie actions [DHH]. Examples: + + cookies.permanent[:prefers_open_id] = true + # => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT + + cookies.signed[:discount] = 45 + # => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/ + + cookies.signed[:discount] + # => 45 (if the cookie was changed, you'll get a InvalidSignature exception) + + cookies.permanent.signed[:remember_me] = current_user.id + # => Set-Cookie: discount=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT + + ...to use the signed cookies, you need to set a secret to ActionController::Base.cookie_verifier_secret (automatically done in config/initializers/cookie_verification_secret.rb for new Rails applications). + +* Added config/initializers/cookie_verification_secret.rb with an auto-generated secret for using ActionController::Base#cookies.signed [DHH] + * Fixed that the debugger wouldn't go into IRB mode because of left-over ARGVs [DHH] * I18n support for plugins. #2325 [Antonio Tapiador, Sven Fuchs] diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt new file mode 100644 index 0000000000..9808ea6a2d --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies 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. +ActionController::Base.cookie_verification_secret = '<%= app_secret %>'; -- cgit v1.2.3 From bf03ddc6365c8ea83647b5f315a34aa5c6b65df1 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 21 Dec 2009 13:18:11 -0800 Subject: Missed changelog entry for :inverse_of --- activerecord/CHANGELOG | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 1f7a95a53a..be4d197f99 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,12 @@ *Edge* +* Association inverses for belongs_to, has_one, and has_many. Optimization to reduce database queries. #3533 [Murray Steele] + + # post.comments sets each comment's post without needing to :include + class Post < ActiveRecord::Base + has_many :comments, :inverse_of => :post + end + * MySQL: add_ and change_column support positioning. #3286 [Ben Marini] * Reset your Active Record counter caches with the reset_counter_cache class method. #1211 [Mike Breen, Gabe da Silveira] -- cgit v1.2.3 From 3e33985e19a51a4ae824bc5765cbab84f5090021 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 21 Dec 2009 17:27:30 -0600 Subject: Update CI bundler --- ci/geminstaller.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/geminstaller.yml b/ci/geminstaller.yml index 776ae8d98d..3dc2000bf9 100644 --- a/ci/geminstaller.yml +++ b/ci/geminstaller.yml @@ -5,4 +5,4 @@ gems: - name: rubygems-update version: >= 1.3.5 - name: bundler - version: >= 0.6.0 + version: >= 0.7.1 -- cgit v1.2.3 From f82e1046f893c80c6234495f1bca28b4fb6520c9 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 21 Dec 2009 17:29:59 -0600 Subject: reset_session needs to be a real method so flash can override it --- .../lib/action_controller/metal/rack_delegation.rb | 6 +++++- actionpack/test/controller/flash_test.rb | 24 +++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/actionpack/lib/action_controller/metal/rack_delegation.rb b/actionpack/lib/action_controller/metal/rack_delegation.rb index 5141918499..833475cff7 100644 --- a/actionpack/lib/action_controller/metal/rack_delegation.rb +++ b/actionpack/lib/action_controller/metal/rack_delegation.rb @@ -3,7 +3,7 @@ module ActionController extend ActiveSupport::Concern included do - delegate :session, :reset_session, :to => "@_request" + delegate :session, :to => "@_request" delegate :headers, :status=, :location=, :content_type=, :status, :location, :content_type, :to => "@_response" attr_internal :request @@ -24,5 +24,9 @@ module ActionController response.body = body if response super end + + def reset_session + @_request.reset_session + end end end diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index 1f5be431ac..a9b60386f1 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -34,7 +34,7 @@ class FlashTest < ActionController::TestCase flash.keep render :inline => "hello" end - + def use_flash_and_update_it flash.update("this" => "hello again") @flash_copy = {}.update flash @@ -76,11 +76,11 @@ class FlashTest < ActionController::TestCase def redirect_with_alert redirect_to '/nowhere', :alert => "Beware the nowheres!" end - + def redirect_with_notice redirect_to '/somewhere', :notice => "Good luck in the somewheres!" end - + def redirect_with_other_flashes redirect_to '/wonderland', :flash => { :joyride => "Horses!" } end @@ -101,7 +101,7 @@ class FlashTest < ActionController::TestCase def test_keep_flash get :set_flash - + get :use_flash_and_keep_it assert_equal "hello", assigns["flash_copy"]["that"] assert_equal "hello", assigns["flashy"] @@ -112,7 +112,7 @@ class FlashTest < ActionController::TestCase get :use_flash assert_nil assigns["flash_copy"]["that"], "On third flash" end - + def test_flash_now get :set_flash_now assert_equal "hello", assigns["flash_copy"]["that"] @@ -123,8 +123,8 @@ class FlashTest < ActionController::TestCase assert_nil assigns["flash_copy"]["that"] assert_nil assigns["flash_copy"]["foo"] assert_nil assigns["flashy"] - end - + end + def test_update_flash get :set_flash get :use_flash_and_update_it @@ -140,7 +140,7 @@ class FlashTest < ActionController::TestCase assert_equal "hello", assigns["flashy_that"] assert_equal "good-bye", assigns["flashy_this"] assert_nil assigns["flashy_that_reset"] - end + end def test_does_not_set_the_session_if_the_flash_is_empty get :std_action @@ -165,24 +165,24 @@ class FlashTest < ActionController::TestCase assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed assert_nil flash.discard(:unknown) # non existant key passed assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard()) # nothing passed - assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed + assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed assert_nil flash.keep(:unknown) # non existant key passed assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep()) # nothing passed - assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed + assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed end def test_redirect_to_with_alert get :redirect_with_alert assert_equal "Beware the nowheres!", @controller.send(:flash)[:alert] end - + def test_redirect_to_with_notice get :redirect_with_notice assert_equal "Good luck in the somewheres!", @controller.send(:flash)[:notice] end - + def test_redirect_to_with_other_flashes get :redirect_with_other_flashes assert_equal "Horses!", @controller.send(:flash)[:joyride] -- cgit v1.2.3 From 715dd10961cefd1d8039cb81f47788d0a5b97d0d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 21 Dec 2009 17:34:53 -0600 Subject: Less annoying RoutingError message --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index bf2443c1be..a4dc5e0956 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -5,7 +5,7 @@ module ActionDispatch module Routing class RouteSet #:nodoc: NotFound = lambda { |env| - raise ActionController::RoutingError, "No route matches #{env['PATH_INFO'].inspect} with #{env.inspect}" + raise ActionController::RoutingError, "No route matches #{env['PATH_INFO'].inspect}" } PARAMETERS_KEY = 'action_dispatch.request.path_parameters' @@ -426,7 +426,7 @@ module ActionDispatch end end - raise ActionController::RoutingError, "No route matches #{path.inspect} with #{environment.inspect}" + raise ActionController::RoutingError, "No route matches #{path.inspect}" end end end -- cgit v1.2.3 From 880688a499b4066d6654a4cd11b76d14cc62865e Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 21 Dec 2009 17:47:03 -0600 Subject: Default route was removed from default route config, patch up failing tests. --- railties/test/application/routing_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index decde056fd..1add941ee0 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -28,6 +28,12 @@ module ApplicationTests end RUBY + app_file 'config/routes.rb', <<-RUBY + ActionController::Routing::Routes.draw do |map| + match ':controller(/:action)' + end + RUBY + get '/foo' assert_equal 'foo', last_response.body end @@ -49,6 +55,12 @@ module ApplicationTests end RUBY + app_file 'config/routes.rb', <<-RUBY + ActionController::Routing::Routes.draw do |map| + match ':controller(/:action)' + end + RUBY + get '/foo' assert_equal 'foo', last_response.body @@ -75,6 +87,12 @@ module ApplicationTests end RUBY + app_file 'config/routes.rb', <<-RUBY + ActionController::Routing::Routes.draw do |map| + match ':controller(/:action)' + end + RUBY + get '/foo' assert_equal 'foo', last_response.body -- cgit v1.2.3 From cf66d16bdfcb46c217ea06c05b8e0c5dfff73889 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 21 Dec 2009 15:49:52 -0800 Subject: Its cookie_verifier_secret --- .../app/templates/config/initializers/cookie_verification_secret.rb.tt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt index 9808ea6a2d..9f05cd5a31 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt @@ -4,4 +4,4 @@ # If you change this key, all old signed cookies 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. -ActionController::Base.cookie_verification_secret = '<%= app_secret %>'; +ActionController::Base.cookie_verifier_secret = '<%= app_secret %>'; -- cgit v1.2.3 From 36624b2c709925bcabb49d12082b9dd9d28c4c5c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 21 Dec 2009 15:55:59 -0800 Subject: Give the builtin controllers their own routes.rb now that the legacy catch-all is gone --- railties/builtin/routes.rb | 3 +++ railties/lib/rails/application.rb | 1 + railties/lib/rails/configuration.rb | 4 ++++ 3 files changed, 8 insertions(+) create mode 100644 railties/builtin/routes.rb diff --git a/railties/builtin/routes.rb b/railties/builtin/routes.rb new file mode 100644 index 0000000000..26a0daaa8c --- /dev/null +++ b/railties/builtin/routes.rb @@ -0,0 +1,3 @@ +ActionController::Routing::Routes.draw do |map| + match '/rails/info/properties' => "rails::info#properties" +end \ No newline at end of file diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index e65c20de2c..498fd6a723 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -418,6 +418,7 @@ module Rails initializer :initialize_routing do next unless configuration.frameworks.include?(:action_controller) route_configuration_files << configuration.routes_configuration_file + route_configuration_files << configuration.builtin_routes_configuration_file reload_routes! end # diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 0fa42091dd..bf5b9478cc 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -158,6 +158,10 @@ module Rails @routes_configuration_file ||= File.join(root, 'config', 'routes.rb') end + def builtin_routes_configuration_file + @builtin_routes_configuration_file ||= File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + end + def controller_paths @controller_paths ||= begin paths = [File.join(root, 'app', 'controllers')] -- cgit v1.2.3 From a110ff0fca45c6cc74c0da5d8dcdabeed43e78b1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 21 Dec 2009 16:03:04 -0800 Subject: Dont introspect inline templates for the logger and cleanup a few styling issues --- actionpack/lib/abstract_controller/rendering.rb | 9 ++++----- actionpack/lib/action_view/render/rendering.rb | 22 +++++++++------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index f4e1580977..64a8a5f241 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -125,8 +125,8 @@ module AbstractController if options.key?(:text) options[:_template] = ActionView::Template::Text.new(options[:text], format_for_text) elsif options.key?(:inline) - handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb") - template = ActionView::Template.new(options[:inline], "inline #{options[:inline].inspect}", handler, {}) + handler = ActionView::Template.handler_class_for_extension(options[:type] || "erb") + template = ActionView::Template.new(options[:inline], "inline template", handler, {}) options[:_template] = template elsif options.key?(:template) options[:_template_name] = options[:template] @@ -194,9 +194,8 @@ module AbstractController # otherwise, process the parameter into a ViewPathSet. def view_paths=(paths) clear_template_caches! - self._view_paths = paths.is_a?(ActionView::PathSet) ? - paths : ActionView::Base.process_view_paths(paths) + self._view_paths = paths.is_a?(ActionView::PathSet) ? paths : ActionView::Base.process_view_paths(paths) end end end -end +end \ No newline at end of file diff --git a/actionpack/lib/action_view/render/rendering.rb b/actionpack/lib/action_view/render/rendering.rb index 7006a5b968..d4d16b4d98 100644 --- a/actionpack/lib/action_view/render/rendering.rb +++ b/actionpack/lib/action_view/render/rendering.rb @@ -78,12 +78,12 @@ module ActionView end def _render_inline(inline, layout, options) - handler = Template.handler_class_for_extension(options[:type] || "erb") - template = Template.new(options[:inline], - "inline #{options[:inline].inspect}", handler, {}) + handler = Template.handler_class_for_extension(options[:type] || "erb") + template = Template.new(options[:inline], "inline template", handler, {}) - locals = options[:locals] + locals = options[:locals] content = template.render(self, locals) + _render_text(content, layout, locals) end @@ -91,6 +91,7 @@ module ActionView content = layout.render(self, locals) do |*name| _layout_for(*name) { content } end if layout + content end @@ -113,21 +114,16 @@ module ActionView msg end - locals = options[:locals] || {} - - content = if partial - _render_partial_object(template, options) - else - template.render(self, locals) - end - + locals = options[:locals] || {} + content = partial ? _render_partial_object(template, options) : template.render(self, locals) @_content_for[:layout] = content if layout @_layout = layout.identifier logger.info("Rendering template within #{layout.inspect}") if logger - content = layout.render(self, locals) {|*name| _layout_for(*name) } + content = layout.render(self, locals) { |*name| _layout_for(*name) } end + content end end -- cgit v1.2.3 From b0b4ae970c1cb586235bcfbc669d43475c7fe684 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Mon, 21 Dec 2009 16:03:20 -0800 Subject: test.rb, dev.rb, and production.rb just reopen the Application class; no more hax required --- railties/lib/rails/application.rb | 25 +++++----------- .../templates/config/environments/development.rb | 17 ----------- .../config/environments/development.rb.tt | 19 +++++++++++++ .../templates/config/environments/production.rb | 31 -------------------- .../templates/config/environments/production.rb.tt | 33 ++++++++++++++++++++++ .../app/templates/config/environments/test.rb | 27 ------------------ .../app/templates/config/environments/test.rb.tt | 29 +++++++++++++++++++ railties/test/initializer/initialize_i18n_test.rb | 11 ++++---- railties/test/initializer/path_test.rb | 8 +++--- railties/test/isolation/abstract_unit.rb | 7 +++++ 10 files changed, 104 insertions(+), 103 deletions(-) delete mode 100644 railties/lib/rails/generators/rails/app/templates/config/environments/development.rb create mode 100644 railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt delete mode 100644 railties/lib/rails/generators/rails/app/templates/config/environments/production.rb create mode 100644 railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt delete mode 100644 railties/lib/rails/generators/rails/app/templates/config/environments/test.rb create mode 100644 railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 498fd6a723..d83c65da8d 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -9,7 +9,13 @@ module Rails end def new - @instance ||= super + @instance ||= begin + begin + require config.environment_path + rescue LoadError + end + super + end end def config @@ -119,23 +125,6 @@ module Rails @app.call(env) end - - # Loads the environment specified by Configuration#environment_path, which - # is typically one of development, test, or production. - initializer :load_environment do - next unless File.file?(config.environment_path) - - config = self.config - - Kernel.class_eval do - meth = instance_method(:config) if Object.respond_to?(:config) - define_method(:config) { config } - require config.environment_path - remove_method :config - define_method(:config, &meth) if meth - end - end - # Set the $LOAD_PATH based on the value of # Configuration#load_paths. Duplicates are removed. initializer :set_load_path do diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb deleted file mode 100644 index 85c9a6080e..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb +++ /dev/null @@ -1,17 +0,0 @@ -# Settings specified here will take precedence over those in config/environment.rb - -# In the development environment your application's code is reloaded on -# every request. This slows down response time but is perfect for development -# since you don't have to restart the webserver when you make code changes. -config.cache_classes = false - -# Log error messages when you accidentally call methods on nil. -config.whiny_nils = 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 - -# Don't care if the mailer can't send -config.action_mailer.raise_delivery_errors = false \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt new file mode 100644 index 0000000000..2b3940d47f --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt @@ -0,0 +1,19 @@ +class <%= app_const %> + # Settings specified here will take precedence over those in config/environment.rb + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the webserver when you make code changes. + config.cache_classes = false + + # Log error messages when you accidentally call methods on nil. + config.whiny_nils = 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 + + # Don't care if the mailer can't send + config.action_mailer.raise_delivery_errors = false +end \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb deleted file mode 100644 index 377b9207c7..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb +++ /dev/null @@ -1,31 +0,0 @@ -# Settings specified here will take precedence over those in config/environment.rb - -# The production environment is meant for finished, "live" apps. -# Code is not reloaded between requests -config.cache_classes = true - -# Full error reports are disabled and caching is turned on -config.action_controller.consider_all_requests_local = false -config.action_controller.perform_caching = true - -# See everything in the log (default is :info) -# config.log_level = :debug - -# Use a different logger for distributed setups -# config.logger = SyslogLogger.new - -# Use a different cache store in production -# config.cache_store = :mem_cache_store - -# Disable Rails's static asset server -# In production, Apache or nginx will already do this -config.serve_static_assets = false - -# Enable serving of images, stylesheets, and javascripts from an asset server -# config.action_controller.asset_host = "http://assets.example.com" - -# Disable delivery errors, bad email addresses will be ignored -# config.action_mailer.raise_delivery_errors = false - -# Enable threaded mode -# config.threadsafe! diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt new file mode 100644 index 0000000000..eff5801526 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -0,0 +1,33 @@ +class <%= app_const %> + # Settings specified here will take precedence over those in config/environment.rb + + # The production environment is meant for finished, "live" apps. + # Code is not reloaded between requests + config.cache_classes = true + + # Full error reports are disabled and caching is turned on + config.action_controller.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # See everything in the log (default is :info) + # config.log_level = :debug + + # Use a different logger for distributed setups + # config.logger = SyslogLogger.new + + # Use a different cache store in production + # config.cache_store = :mem_cache_store + + # Disable Rails's static asset server + # In production, Apache or nginx will already do this + config.serve_static_assets = false + + # Enable serving of images, stylesheets, and javascripts from an asset server + # config.action_controller.asset_host = "http://assets.example.com" + + # Disable delivery errors, bad email addresses will be ignored + # config.action_mailer.raise_delivery_errors = false + + # Enable threaded mode + # config.threadsafe! +end \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb deleted file mode 100644 index 496eb9572b..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb +++ /dev/null @@ -1,27 +0,0 @@ -# Settings specified here will take precedence over those in config/environment.rb - -# The test environment is used exclusively to run your application's -# test suite. You never need to work with it otherwise. Remember that -# your test database is "scratch space" for the test suite and is wiped -# and recreated between test runs. Don't rely on the data there! -config.cache_classes = true - -# Log error messages when you accidentally call methods on nil. -config.whiny_nils = true - -# Show full error reports and disable caching -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 Action Mailer 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 - -# 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, -# like if you have constraints or database-specific column types -# config.active_record.schema_format = :sql \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt new file mode 100644 index 0000000000..3246c7b5f5 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt @@ -0,0 +1,29 @@ +class <%= app_const %> + # Settings specified here will take precedence over those in config/environment.rb + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Log error messages when you accidentally call methods on nil. + config.whiny_nils = true + + # Show full error reports and disable caching + 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 Action Mailer 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 + + # 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, + # like if you have constraints or database-specific column types + # config.active_record.schema_format = :sql +end \ No newline at end of file diff --git a/railties/test/initializer/initialize_i18n_test.rb b/railties/test/initializer/initialize_i18n_test.rb index 076816d73b..472566378d 100644 --- a/railties/test/initializer/initialize_i18n_test.rb +++ b/railties/test/initializer/initialize_i18n_test.rb @@ -7,16 +7,15 @@ module InitializerTests def setup build_app boot_rails - require "rails" end # test_config_defaults_and_settings_should_be_added_to_i18n_defaults test "i18n config defaults and settings should be added to i18n defaults" do - Rails::Initializer.run do |c| - c.root = app_path - c.i18n.load_path << "my/other/locale.yml" - end - Rails.initialize! + add_to_config <<-RUBY + config.root = "#{app_path}" + config.i18n.load_path << "my/other/locale.yml" + RUBY + require "#{app_path}/config/environment" #{RAILS_FRAMEWORK_ROOT}/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml assert_equal %W( diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb index 1b58a58555..fa66ebcd83 100644 --- a/railties/test/initializer/path_test.rb +++ b/railties/test/initializer/path_test.rb @@ -7,14 +7,14 @@ class PathsTest < Test::Unit::TestCase build_app boot_rails require "rails" - Rails::Initializer.run do |config| - config.root = app_path + add_to_config <<-RUBY + config.root = "#{app_path}" config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record] config.after_initialize do ActionController::Base.session_store = nil end - end - Rails.initialize! + RUBY + require "#{app_path}/config/environment" @paths = Rails.application.config.paths end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index ba8b35d5cc..6562a31d9e 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -89,6 +89,13 @@ module TestHelpers end end + routes = File.read("#{app_path}/config/routes.rb") + if routes =~ /(\n\s*end\s*)\Z/ + File.open("#{app_path}/config/routes.rb", 'w') do |f| + f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)'\n" + $1 + end + end + add_to_config 'config.action_controller.session = { :key => "_myapp_session", :secret => "bac838a849c1d5c4de2e6a50af826079" }' end -- cgit v1.2.3 From d982fe2b2fcc4e197087901774e2f21467c3cec1 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Mon, 21 Dec 2009 16:35:54 -0800 Subject: Replace reopening the class with App.configure as an alias to class_eval --- railties/lib/rails/application.rb | 4 ++++ .../rails/app/templates/config/environments/development.rb.tt | 2 +- .../rails/app/templates/config/environments/production.rb.tt | 2 +- .../generators/rails/app/templates/config/environments/test.rb.tt | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index d83c65da8d..695a1d7c87 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -66,6 +66,10 @@ module Rails self.class.config end + class << self + alias configure class_eval + end + def root config.root end diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt index 2b3940d47f..b10103b436 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt @@ -1,4 +1,4 @@ -class <%= app_const %> +<%= app_const %>.configure do # Settings specified here will take precedence over those in config/environment.rb # In the development environment your application's code is reloaded on diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index eff5801526..543a40108c 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -1,4 +1,4 @@ -class <%= app_const %> +<%= app_const %>.configure do # Settings specified here will take precedence over those in config/environment.rb # The production environment is meant for finished, "live" apps. diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt index 3246c7b5f5..428fa35633 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt @@ -1,4 +1,4 @@ -class <%= app_const %> +<%= app_const %>.configure do # Settings specified here will take precedence over those in config/environment.rb # The test environment is used exclusively to run your application's -- cgit v1.2.3 From a43a9c81cf3d85e8dca6afdd92307ce153643ebe Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 21 Dec 2009 16:41:02 -0800 Subject: Dont need to specify password_confirmation, that happens automatically --- .../rails/app/templates/app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb index e7991fff92..9889b52893 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb @@ -4,5 +4,5 @@ class ApplicationController < ActionController::Base helper :all protect_from_forgery - filter_parameter_logging :password, :password_confirmation + filter_parameter_logging :password end -- cgit v1.2.3 From be225adafbec267353fa7260179b0ce5a72e4283 Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Mon, 21 Dec 2009 16:48:44 -0800 Subject: Fix ActionMailer. The fact that ActionMailer::Base does not inherit from AbstractController::Base is either a bug or we need to re-evaluate the requirements of the mixins. --- actionmailer/lib/action_mailer/base.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index a69838fe43..40aff7f0d8 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -271,6 +271,8 @@ module ActionMailer #:nodoc: class_inheritable_accessor :view_paths self.view_paths = [] + attr_internal :formats + cattr_accessor :logger @@raise_delivery_errors = true @@ -452,6 +454,7 @@ module ActionMailer #:nodoc: # remain uninitialized (useful when you only need to invoke the "receive" # method, for instance). def initialize(method_name=nil, *parameters) #:nodoc: + @_formats = [] @_response_body = nil super() create!(method_name, *parameters) if method_name -- cgit v1.2.3 From 76e732a7be690f09d1e5d6c97138a6eb7d263423 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 21 Dec 2009 16:53:52 -0800 Subject: Fix the documentation for root :to. It should use a fully qualified controller#action syntax (Closes #3606) --- railties/lib/rails/generators/rails/app/templates/config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/rails/app/templates/config/routes.rb b/railties/lib/rails/generators/rails/app/templates/config/routes.rb index 0d1b6bab4f..e28ff42bdd 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/routes.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/routes.rb @@ -48,7 +48,7 @@ ActionController::Routing::Routes.draw do |map| # You can have the root of your site routed with "root" # just remember to delete public/index.html. - # root :to => "welcome" + # root :to => "welcome#index" # See how all your routes lay out with "rake routes" -- cgit v1.2.3 From 2e571e8f99e5e2712c0bc2558df8d62996204b03 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 21 Dec 2009 16:57:23 -0800 Subject: Blog -> Blog::Application. Leave the toplevel module up for grabs. --- .../rails/generators/rails/app/app_generator.rb | 2 +- .../rails/app/templates/config/application.rb | 62 +++++++++++----------- railties/test/application/configuration_test.rb | 2 +- railties/test/isolation/abstract_unit.rb | 2 +- railties/test/plugins/configuration_test.rb | 4 +- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index ae18fa843b..6f71f7005f 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -182,7 +182,7 @@ module Rails::Generators end def app_const - @app_const ||= app_name.classify + @app_const ||= "#{app_name.classify}::Application" end def app_secret diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb index 2c17de2a23..15dc553e53 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -1,41 +1,43 @@ require File.expand_path('../boot', __FILE__) -class <%= app_const %> < Rails::Application - # 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. +module <%= app_name.classify %> + class Application < Rails::Application + # 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. - # Add additional load paths for your own custom dirs - # config.load_paths += %W( #{root}/extras ) + # Add additional load paths for your own custom dirs + # config.load_paths += %W( #{root}/extras ) - # Only load the plugins named here, in the order given (default is alphabetical). - # :all can be used as a placeholder for all plugins not explicitly named - # config.plugins = [ :exception_notification, :ssl_requirement, :all ] + # Only load the plugins named here, in the order given (default is alphabetical). + # :all can be used as a placeholder for all plugins not explicitly named + # config.plugins = [ :exception_notification, :ssl_requirement, :all ] - # Skip frameworks you're not going to use. To use Rails without a database, - # you must remove the Active Record framework. + # Skip frameworks you're not going to use. To use Rails without a database, + # you must remove the Active Record framework. <% if options[:skip_activerecord] -%> - config.frameworks -= [ :active_record ] + config.frameworks -= [ :active_record ] <% else -%> - # config.frameworks -= [ :active_record, :active_resource, :action_mailer ] + # config.frameworks -= [ :active_record, :active_resource, :action_mailer ] - # Activate observers that should always be running - # config.active_record.observers = :cacher, :garbage_collector, :forum_observer + # Activate observers that should always be running + # config.active_record.observers = :cacher, :garbage_collector, :forum_observer <% end -%> - # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. - # Run "rake -D time" for a list of tasks for finding time zone names. - config.time_zone = 'UTC' - - # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. - # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] - # config.i18n.default_locale = :de - - # Configure generators values. Many other options are available, be sure to - # check the documentation. - # config.generators do |g| - # g.orm :active_record - # g.template_engine :erb - # g.test_framework :test_unit, :fixture => true - # end + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. + config.time_zone = 'UTC' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')] + # config.i18n.default_locale = :de + + # Configure generators values. Many other options are available, be sure to + # check the documentation. + # config.generators do |g| + # g.orm :active_record + # g.template_engine :erb + # g.test_framework :test_unit, :fixture => true + # end + end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 89337b7f66..83e1401993 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -60,7 +60,7 @@ module ApplicationTests RUBY require "#{app_path}/config/application" - assert AppTemplate.configuration.action_controller.allow_concurrency + assert AppTemplate::Application.configuration.action_controller.allow_concurrency end test "the application can be marked as threadsafe when there are no frameworks" do diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 6562a31d9e..c169c80bea 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -135,7 +135,7 @@ module TestHelpers def add_to_config(str) environment = File.read("#{app_path}/config/application.rb") - if environment =~ /(\n\s*end\s*)\Z/ + if environment =~ /(\n\s*end\s*end\s*)\Z/ File.open("#{app_path}/config/application.rb", 'w') do |f| f.puts $` + "\n#{str}\n" + $1 end diff --git a/railties/test/plugins/configuration_test.rb b/railties/test/plugins/configuration_test.rb index edf8bb37f5..5786316d1d 100644 --- a/railties/test/plugins/configuration_test.rb +++ b/railties/test/plugins/configuration_test.rb @@ -21,7 +21,7 @@ module PluginsTest test "plugin configurations are available in the application" do class Foo < Rails::Plugin ; config.foo.greetings = "hello" ; end require "#{app_path}/config/application" - assert_equal "hello", AppTemplate.config.foo.greetings + assert_equal "hello", AppTemplate::Application.config.foo.greetings end test "plugin config merges are deep" do @@ -33,4 +33,4 @@ module PluginsTest assert_equal "bar", MyApp.config.foo.bar end end -end \ No newline at end of file +end -- cgit v1.2.3 From 426348b48403f664cc10e8ec545b640e56c1c090 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 21 Dec 2009 20:15:27 -0600 Subject: Update routes.rb template to use App name --- railties/lib/rails/application.rb | 4 ++++ railties/lib/rails/generators/actions.rb | 2 +- railties/lib/rails/generators/rails/app/app_generator.rb | 6 +++--- .../rails/generators/rails/app/templates/config/routes.rb | 2 +- railties/test/application/initializer_test.rb | 10 ++++++++++ railties/test/application/routing_test.rb | 14 +++++++------- railties/test/initializer/initialize_i18n_test.rb | 1 + railties/test/initializer/path_test.rb | 1 + railties/test/paths_test.rb | 3 +-- 9 files changed, 29 insertions(+), 14 deletions(-) diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 498fd6a723..4e21287496 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -41,6 +41,10 @@ module Rails end end + def routes + ActionController::Routing::Routes + end + def call(env) new.call(env) end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 2efdf29127..f95b15acce 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -273,7 +273,7 @@ module Rails # def route(routing_code) log :route, routing_code - sentinel = "ActionController::Routing::Routes.draw do |map|" + sentinel = "routes.draw do |map|" in_root do inject_into_file 'config/routes.rb', "\n #{routing_code}\n", { :after => sentinel, :verbose => false } diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index ae18fa843b..b8f2911021 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -62,9 +62,9 @@ module Rails::Generators empty_directory "config" inside "config" do - copy_file "routes.rb" - template "application.rb" - template "environment.rb" + template "routes.rb" + template "application.rb" + template "environment.rb" directory "environments" directory "initializers" diff --git a/railties/lib/rails/generators/rails/app/templates/config/routes.rb b/railties/lib/rails/generators/rails/app/templates/config/routes.rb index 0d1b6bab4f..1959d3387f 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/routes.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/routes.rb @@ -1,4 +1,4 @@ -ActionController::Routing::Routes.draw do |map| +<%= app_const %>.routes.draw do |map| # The priority is based upon order of creation: # first created -> highest priority. diff --git a/railties/test/application/initializer_test.rb b/railties/test/application/initializer_test.rb index fa00d287ca..031fdc2e9f 100644 --- a/railties/test/application/initializer_test.rb +++ b/railties/test/application/initializer_test.rb @@ -15,6 +15,7 @@ module ApplicationTests Rails::Initializer.run do |config| config.root = app_path end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert $:.include?("#{app_path}/app/models") @@ -45,6 +46,7 @@ module ApplicationTests config.root = app_path config.eager_load_paths = "#{app_path}/lib" end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! @@ -55,6 +57,7 @@ module ApplicationTests app_file "config/environments/development.rb", "$initialize_test_set_from_env = 'success'" assert_nil $initialize_test_set_from_env Rails::Initializer.run { |config| config.root = app_path } + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert_equal "success", $initialize_test_set_from_env end @@ -75,6 +78,7 @@ module ApplicationTests config.after_initialize { $test_after_initialize_block1 = "success" } config.after_initialize { $test_after_initialize_block2 = "congratulations" } end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert_equal "success", $test_after_initialize_block1 @@ -88,6 +92,7 @@ module ApplicationTests config.after_initialize # don't pass a block, this is what we're testing! config.after_initialize { $test_after_initialize_block2 = "congratulations" } end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert_equal "success", $test_after_initialize_block1 @@ -100,6 +105,7 @@ module ApplicationTests config.root = app_path config.i18n.default_locale = :de end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert_equal :de, I18n.default_locale @@ -137,6 +143,7 @@ module ApplicationTests config.root = app_path config.action_controller.session_store = :cookie_store end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore) @@ -155,6 +162,7 @@ module ApplicationTests c.root = app_path c.action_controller.session_store = :active_record_store end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore] @@ -179,6 +187,7 @@ module ApplicationTests c.root = app_path c.frameworks -= [:action_view] end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert_equal nil, ActionMailer::Base.template_root @@ -189,6 +198,7 @@ module ApplicationTests Rails::Initializer.run do |c| c.root = app_path end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! assert_instance_of Pathname, Rails.root end diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 1add941ee0..7803794307 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -29,7 +29,7 @@ module ApplicationTests RUBY app_file 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match ':controller(/:action)' end RUBY @@ -56,7 +56,7 @@ module ApplicationTests RUBY app_file 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match ':controller(/:action)' end RUBY @@ -88,7 +88,7 @@ module ApplicationTests RUBY app_file 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match ':controller(/:action)' end RUBY @@ -110,7 +110,7 @@ module ApplicationTests RUBY app_file 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match 'foo', :to => 'foo#index' end RUBY @@ -125,7 +125,7 @@ module ApplicationTests RUBY plugin.write 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match 'bar', :to => 'bar#index' end RUBY @@ -152,7 +152,7 @@ module ApplicationTests RUBY app_file 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match 'foo', :to => 'foo#bar' end RUBY @@ -161,7 +161,7 @@ module ApplicationTests assert_equal 'bar', last_response.body app_file 'config/routes.rb', <<-RUBY - ActionController::Routing::Routes.draw do |map| + AppTemplate.routes.draw do |map| match 'foo', :to => 'foo#baz' end RUBY diff --git a/railties/test/initializer/initialize_i18n_test.rb b/railties/test/initializer/initialize_i18n_test.rb index 076816d73b..4642e53cfc 100644 --- a/railties/test/initializer/initialize_i18n_test.rb +++ b/railties/test/initializer/initialize_i18n_test.rb @@ -16,6 +16,7 @@ module InitializerTests c.root = app_path c.i18n.load_path << "my/other/locale.yml" end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! #{RAILS_FRAMEWORK_ROOT}/railties/test/fixtures/plugins/engines/engine/config/locales/en.yml diff --git a/railties/test/initializer/path_test.rb b/railties/test/initializer/path_test.rb index 1b58a58555..3724655c0f 100644 --- a/railties/test/initializer/path_test.rb +++ b/railties/test/initializer/path_test.rb @@ -14,6 +14,7 @@ class PathsTest < Test::Unit::TestCase ActionController::Base.session_store = nil end end + Object.const_set(:AppTemplate, Rails.application) Rails.initialize! @paths = Rails.application.config.paths end diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index c724799d64..d60d6177f6 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -2,7 +2,6 @@ require 'abstract_unit' require 'rails/paths' class PathsTest < ActiveSupport::TestCase - def setup @root = Rails::Application::Root.new("/foo/bar") end @@ -228,4 +227,4 @@ class PathsTest < ActiveSupport::TestCase @root.app.eager_load! assert_equal ["/foo/bar/app"], @root.load_paths end -end \ No newline at end of file +end -- cgit v1.2.3 From 8e48a5ef0ca488b2264acd2b38bdae14970c011f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 21 Dec 2009 21:49:36 -0800 Subject: Add test for root --- actionpack/test/dispatch/routing_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 7058bc2ea0..1c7822358d 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -109,6 +109,8 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest scope ':access_token', :constraints => { :access_token => /\w{5,5}/ } do resources :rooms end + + root :to => 'projects#index' end end @@ -458,6 +460,13 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_root + with_test_routes do + get '/' + assert_equal 'projects#index', @response.body + end + end + private def with_test_routes real_routes, temp_routes = ActionController::Routing::Routes, Routes -- cgit v1.2.3