From 209fb5190b1415633fcd42f7617ff5001669dd27 Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 4 Apr 2010 19:49:12 -0700 Subject: render_to_string should have the identical signature as render --- actionpack/lib/abstract_controller/rendering.rb | 17 ++++++++--------- actionpack/test/abstract/abstract_controller_test.rb | 10 ++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index b251bd6405..98c8c5fa67 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -89,9 +89,16 @@ module AbstractController # Normalize arguments, options and then delegates render_to_body and # sticks the result in self.response_body. def render(*args, &block) + self.response_body = render_to_string(*args, &block) + end + + # Raw rendering of a template to a string. Just convert the results of + # render_to_body into a String. + # :api: plugin + def render_to_string(*args, &block) options = _normalize_args(*args, &block) _normalize_options(options) - self.response_body = render_to_body(options) + render_to_body(options) end # Raw rendering of a template to a Rack-compatible body. @@ -101,14 +108,6 @@ module AbstractController _render_template(options) end - # Raw rendering of a template to a string. Just convert the results of - # render_to_body into a String. - # :api: plugin - def render_to_string(options={}) - _normalize_options(options) - render_to_body(options) - end - # Find and renders a template based on the options given. # :api: private def _render_template(options) #:nodoc: diff --git a/actionpack/test/abstract/abstract_controller_test.rb b/actionpack/test/abstract/abstract_controller_test.rb index f70d497481..5a0a2aef10 100644 --- a/actionpack/test/abstract/abstract_controller_test.rb +++ b/actionpack/test/abstract/abstract_controller_test.rb @@ -49,6 +49,11 @@ module AbstractController render "index.erb" end + + def index_to_string + self.response_body = render_to_string "index.erb" + end + def action_with_ivars @my_ivar = "Hello" render "action_with_ivars.erb" @@ -77,6 +82,11 @@ module AbstractController assert_equal "Hello from index.erb", @controller.response_body end + test "render_to_string works with a String as an argument" do + @controller.process(:index_to_string) + assert_equal "Hello from index.erb", @controller.response_body + end + test "rendering passes ivars to the view" do @controller.process(:action_with_ivars) assert_equal "Hello from index_with_ivars.erb", @controller.response_body -- cgit v1.2.3 From 485512c50f889adda2597fa1d3f3d5b47870617d Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 4 Apr 2010 19:57:55 -0700 Subject: Whitespace --- .../test/abstract/abstract_controller_test.rb | 67 +++++++++++----------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/actionpack/test/abstract/abstract_controller_test.rb b/actionpack/test/abstract/abstract_controller_test.rb index 5a0a2aef10..3b5013a47a 100644 --- a/actionpack/test/abstract/abstract_controller_test.rb +++ b/actionpack/test/abstract/abstract_controller_test.rb @@ -2,21 +2,21 @@ require 'abstract_unit' module AbstractController module Testing - + # Test basic dispatching. # ==== # * Call process # * Test that the response_body is set correctly class SimpleController < AbstractController::Base end - + class Me < SimpleController def index self.response_body = "Hello world" "Something else" - end + end end - + class TestBasic < ActiveSupport::TestCase test "dispatching works" do controller = Me.new @@ -24,7 +24,7 @@ module AbstractController assert_equal "Hello world", controller.response_body end end - + # Test Render mixin # ==== class RenderingController < AbstractController::Base @@ -36,19 +36,18 @@ module AbstractController if options.is_a?(String) options = {:_template_name => options} end - + options[:_prefix] = _prefix super end append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) end - + class Me2 < RenderingController def index render "index.erb" end - def index_to_string self.response_body = render_to_string "index.erb" @@ -58,7 +57,7 @@ module AbstractController @my_ivar = "Hello" render "action_with_ivars.erb" end - + def naked_render render end @@ -81,7 +80,7 @@ module AbstractController @controller.process(:index) assert_equal "Hello from index.erb", @controller.response_body end - + test "render_to_string works with a String as an argument" do @controller.process(:index_to_string) assert_equal "Hello from index.erb", @controller.response_body @@ -91,7 +90,7 @@ module AbstractController @controller.process(:action_with_ivars) assert_equal "Hello from index_with_ivars.erb", @controller.response_body end - + test "rendering with no template name" do @controller.process(:naked_render) assert_equal "Hello from naked_render.erb", @controller.response_body @@ -107,7 +106,7 @@ module AbstractController assert_equal "Hello from naked_render.erb", @controller.response_body end end - + # Test rendering with prefixes # ==== # * self._prefix is used when defined @@ -116,23 +115,23 @@ module AbstractController def self.prefix name.underscore end - + def _prefix self.class.prefix end end - + class Me3 < PrefixedViews def index render end - + def formatted self.formats = [:html] render end end - + class TestPrefixedViews < ActiveSupport::TestCase def setup @controller = Me3.new @@ -148,13 +147,13 @@ module AbstractController assert_equal "Hello from me3/formatted.html.erb", @controller.response_body end end - + # Test rendering with layouts # ==== # self._layout is used when defined class WithLayouts < PrefixedViews include Layouts - + private def self.layout(formats) begin @@ -170,21 +169,21 @@ module AbstractController def render_to_body(options = {}) options[:_layout] = options[:layout] || _default_layout({}) super - end + end end - + class Me4 < WithLayouts def index render end end - + class Me5 < WithLayouts def index render end end - + class TestLayouts < ActiveSupport::TestCase test "layouts are included" do controller = Me4.new @@ -192,7 +191,7 @@ module AbstractController assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", controller.response_body end end - + # respond_to_action?(action_name) # ==== # * A method can be used as an action only if this method @@ -201,7 +200,7 @@ module AbstractController class DefaultRespondToActionController < AbstractController::Base def index() self.response_body = "success" end end - + class ActionMissingRespondToActionController < AbstractController::Base # No actions private @@ -209,19 +208,19 @@ module AbstractController self.response_body = "success" end end - + class RespondToActionController < AbstractController::Base; def index() self.response_body = "success" end - + def fail() self.response_body = "fail" end - + private def method_for_action(action_name) action_name.to_s != "fail" && action_name end end - + class TestRespondToAction < ActiveSupport::TestCase def assert_dispatch(klass, body = "success", action = :index) @@ -229,27 +228,27 @@ module AbstractController controller.process(action) assert_equal body, controller.response_body end - + test "an arbitrary method is available as an action by default" do assert_dispatch DefaultRespondToActionController, "success", :index end - + test "raises ActionNotFound when method does not exist and action_missing is not defined" do assert_raise(ActionNotFound) { DefaultRespondToActionController.new.process(:fail) } end - + test "dispatches to action_missing when method does not exist and action_missing is defined" do assert_dispatch ActionMissingRespondToActionController, "success", :ohai end - + test "a method is available as an action if respond_to_action? returns true" do assert_dispatch RespondToActionController, "success", :index end - + test "raises ActionNotFound if method is defined but respond_to_action? returns false" do assert_raise(ActionNotFound) { RespondToActionController.new.process(:fail) } end end - + end end -- cgit v1.2.3 From b96486d457836318d125c167ffc99d3d1676a8c9 Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 4 Apr 2010 20:09:03 -0700 Subject: Whitespace --- actionpack/lib/action_controller/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 46a175d2fa..5bd59d4a87 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -6,7 +6,7 @@ module ActionController include AbstractController::Translation include ActionController::Helpers - + include ActionController::HideActions include ActionController::UrlFor include ActionController::Redirecting -- cgit v1.2.3 From d61c76840ae333d668ba61b54611653aea1e4d66 Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 4 Apr 2010 21:05:54 -0700 Subject: Bump the version --- RAILS_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RAILS_VERSION b/RAILS_VERSION index 90a578314f..d989233c8c 100644 --- a/RAILS_VERSION +++ b/RAILS_VERSION @@ -1 +1 @@ -3.0.0.beta2 +3.0.0.beta3 -- cgit v1.2.3 From 79d194e920a1b6e23cad9b6f3397da66b3938116 Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 4 Apr 2010 21:06:26 -0700 Subject: Make it easier to subclass AC::Metal with most, but not all, of the modules in AC::Base --- actionpack/lib/action_controller/base.rb | 88 ++++++++++++++++---------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 5bd59d4a87..d2118ec483 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -2,48 +2,59 @@ module ActionController class Base < Metal abstract! - include AbstractController::Layouts - include AbstractController::Translation + def self.modules_without(*modules) + modules = modules.map do |m| + m.is_a?(Symbol) ? ActionController.const_get(m) : m + end - include ActionController::Helpers + MODULES - modules + end - include ActionController::HideActions - include ActionController::UrlFor - include ActionController::Redirecting - include ActionController::Rendering - include ActionController::Renderers::All - include ActionController::ConditionalGet - include ActionController::RackDelegation + MODULES = [ + AbstractController::Layouts, + AbstractController::Translation, - # Legacy modules - include SessionManagement - include ActionController::Caching - include ActionController::MimeResponds - include ActionController::PolymorphicRoutes + Helpers, + HideActions, + UrlFor, + Redirecting, + Rendering, + Renderers::All, + ConditionalGet, + RackDelegation, + SessionManagement, + Caching, + MimeResponds, + PolymorphicRoutes, + ImplicitRender, - # Rails 2.x compatibility - include ActionController::Compatibility - include ActionController::ImplicitRender + Cookies, + Flash, + Verification, + RequestForgeryProtection, + Streaming, + RecordIdentifier, + HttpAuthentication::Basic::ControllerMethods, + HttpAuthentication::Digest::ControllerMethods, - include ActionController::Cookies - include ActionController::Flash - include ActionController::Verification - include ActionController::RequestForgeryProtection - include ActionController::Streaming - include ActionController::RecordIdentifier - include ActionController::HttpAuthentication::Basic::ControllerMethods - include ActionController::HttpAuthentication::Digest::ControllerMethods + # Add instrumentations hooks at the bottom, to ensure they instrument + # all the methods properly. + Instrumentation, - # Add instrumentations hooks at the bottom, to ensure they instrument - # all the methods properly. - include ActionController::Instrumentation + # Before callbacks should also be executed the earliest as possible, so + # also include them at the bottom. + AbstractController::Callbacks, - # Before callbacks should also be executed the earliest as possible, so - # also include them at the bottom. - include AbstractController::Callbacks + # The same with rescue, append it at the end to wrap as much as possible. + Rescue + ] - # The same with rescue, append it at the end to wrap as much as possible. - include ActionController::Rescue + MODULES.each do |mod| + include mod + end + + # Rails 2.x compatibility + include ActionController::Compatibility def self.inherited(klass) ::ActionController::Base.subclasses << klass.to_s @@ -55,15 +66,6 @@ module ActionController @subclasses ||= [] end - # This method has been moved to ActionDispatch::Request.filter_parameters - def self.filter_parameter_logging(*args, &block) - ActiveSupport::Deprecation.warn("Setting filter_parameter_logging in ActionController is deprecated and has no longer effect, please set 'config.filter_parameters' in config/application.rb instead", caller) - filter = Rails.application.config.filter_parameters - filter.concat(args) - filter << block if block - filter - end - ActiveSupport.run_load_hooks(:action_controller, self) end end -- cgit v1.2.3 From 5c8b4c6e231257bc08d32722e098927885e5e74d Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 4 Apr 2010 21:06:39 -0700 Subject: Move filter_parameter_logger to deprecated.rb --- actionpack/lib/action_controller/deprecated/base.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/actionpack/lib/action_controller/deprecated/base.rb b/actionpack/lib/action_controller/deprecated/base.rb index 2fd60aacc7..51d1e23753 100644 --- a/actionpack/lib/action_controller/deprecated/base.rb +++ b/actionpack/lib/action_controller/deprecated/base.rb @@ -6,6 +6,15 @@ module ActionController deprecated_config_writer(option, message) end + # This method has been moved to ActionDispatch::Request.filter_parameters + def filter_parameter_logging(*args, &block) + ActiveSupport::Deprecation.warn("Setting filter_parameter_logging in ActionController is deprecated and has no longer effect, please set 'config.filter_parameters' in config/application.rb instead", caller) + filter = Rails.application.config.filter_parameters + filter.concat(args) + filter << block if block + filter + end + def deprecated_config_reader(option, message = nil) message ||= "Reading #{option} directly from ActionController::Base is deprecated. " \ "Please read it from config.#{option}" -- cgit v1.2.3 From 6690d662920f0db854f7303cd2a5a36c72299199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 5 Apr 2010 10:52:47 +0200 Subject: Rename config.cookie_secret to config.secret_token and pass it as configuration in request.env. This is another step forward removing global configuration. --- Gemfile | 3 +- actionpack/lib/action_controller/base.rb | 2 +- .../lib/action_controller/deprecated/base.rb | 7 ++--- actionpack/lib/action_controller/metal/cookies.rb | 3 +- .../action_controller/metal/http_authentication.rb | 17 +++++++--- actionpack/lib/action_controller/railtie.rb | 1 - .../lib/action_dispatch/middleware/cookies.rb | 36 +++++++++++----------- .../middleware/session/cookie_store.rb | 2 +- .../lib/action_dispatch/testing/test_request.rb | 2 ++ actionpack/test/abstract_unit.rb | 4 --- actionpack/test/controller/cookie_test.rb | 3 +- .../controller/http_digest_authentication_test.rb | 5 ++- railties/CHANGELOG | 5 +-- railties/lib/rails/application.rb | 11 +++++-- railties/lib/rails/application/configuration.rb | 5 +-- railties/lib/rails/application/finisher.rb | 4 +++ railties/lib/rails/configuration.rb | 12 ++++++++ railties/lib/rails/engine.rb | 8 ++--- railties/lib/rails/engine/configuration.rb | 1 - .../initializers/cookie_verification_secret.rb.tt | 7 ----- .../config/initializers/secret_token.rb.tt | 7 +++++ .../config/initializers/session_store.rb.tt | 4 +-- railties/lib/rails/plugin.rb | 2 +- railties/test/application/configuration_test.rb | 16 ++++++++++ .../application/middleware_stack_defaults_test.rb | 2 +- railties/test/application/url_generation_test.rb | 2 +- railties/test/isolation/abstract_unit.rb | 2 +- 27 files changed, 102 insertions(+), 71 deletions(-) delete mode 100644 railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt create mode 100644 railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt diff --git a/Gemfile b/Gemfile index d516abe111..dd26a9d1c2 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,7 @@ -path File.dirname(__FILE__) source 'http://rubygems.org' gem "arel", :git => "git://github.com/rails/arel.git" -gem "rails", "3.0.0.beta2" +gem "rails", :path => File.dirname(__FILE__) gem "rake", ">= 0.8.7" gem "mocha", ">= 0.9.8" diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index d2118ec483..1dfc240029 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -2,7 +2,7 @@ module ActionController class Base < Metal abstract! - def self.modules_without(*modules) + def self.without_modules(*modules) modules = modules.map do |m| m.is_a?(Symbol) ? ActionController.const_get(m) : m end diff --git a/actionpack/lib/action_controller/deprecated/base.rb b/actionpack/lib/action_controller/deprecated/base.rb index 51d1e23753..05551ffee4 100644 --- a/actionpack/lib/action_controller/deprecated/base.rb +++ b/actionpack/lib/action_controller/deprecated/base.rb @@ -77,14 +77,11 @@ module ActionController def cookie_verifier_secret=(value) ActiveSupport::Deprecation.warn "ActionController::Base.cookie_verifier_secret= is deprecated. " << - "Please configure it on your application with config.cookie_secret=", caller - ActionController::Base.config.secret = value + "Please configure it on your application with config.secret_token=", caller end def cookie_verifier_secret - ActiveSupport::Deprecation.warn "ActionController::Base.cookie_verifier_secret is deprecated. " << - "Please use ActionController::Base.config.secret instead.", caller - ActionController::Base.config.secret + ActiveSupport::Deprecation.warn "ActionController::Base.cookie_verifier_secret is deprecated.", caller end def trusted_proxies=(value) diff --git a/actionpack/lib/action_controller/metal/cookies.rb b/actionpack/lib/action_controller/metal/cookies.rb index 4aaa705203..d787f014cd 100644 --- a/actionpack/lib/action_controller/metal/cookies.rb +++ b/actionpack/lib/action_controller/metal/cookies.rb @@ -10,8 +10,7 @@ module ActionController #:nodoc: private def cookies - raise "You must set config.cookie_secret in your app's config" if config.secret.blank? - request.cookie_jar(:signing_secret => config.secret) + request.cookie_jar end end end diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 424828f7e8..6bd6c15990 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -159,7 +159,7 @@ module ActionController # Authenticate with HTTP Digest, returns true or false def authenticate_with_http_digest(realm = "Application", &password_procedure) - HttpAuthentication::Digest.authenticate(config.secret, request, realm, &password_procedure) + HttpAuthentication::Digest.authenticate(request, realm, &password_procedure) end # Render output including the HTTP Digest authentication header @@ -169,14 +169,15 @@ module ActionController end # Returns false on a valid response, true otherwise - def authenticate(secret_key, request, realm, &password_procedure) - request.authorization && validate_digest_response(secret_key, request, realm, &password_procedure) + def authenticate(request, realm, &password_procedure) + request.authorization && validate_digest_response(request, realm, &password_procedure) end # Returns false unless the request credentials response value matches the expected value. # First try the password as a ha1 digest password. If this fails, then try it as a plain # text password. - def validate_digest_response(secret_key, request, realm, &password_procedure) + def validate_digest_response(request, realm, &password_procedure) + secret_key = secret_token(request) credentials = decode_credentials_header(request) valid_nonce = validate_nonce(secret_key, request, credentials[:nonce]) @@ -225,7 +226,7 @@ module ActionController end def authentication_header(controller, realm) - secret_key = controller.config.secret + secret_key = secret_token(controller.request) nonce = self.nonce(secret_key) opaque = opaque(secret_key) controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}") @@ -238,6 +239,12 @@ module ActionController controller.status = 401 end + def secret_token(request) + secret = request.env["action_dispatch.secret_token"] + raise "You must set config.secret_token in your app's config" if secret.blank? + secret + end + # Uses an MD5 digest based on time to generate a value to be used only once. # # A server-specified data string which should be uniquely generated each time a 401 response is made. diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 29d8523ee1..030ba4ec48 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -51,7 +51,6 @@ module ActionController ac.assets_dir = paths.public.to_a.first ac.javascripts_dir = paths.public.javascripts.to_a.first ac.stylesheets_dir = paths.public.stylesheets.to_a.first - ac.secret = app.config.cookie_secret ActiveSupport.on_load(:action_controller) do self.config.merge!(ac) diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 71dcac9e94..42ab1d1ebb 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -1,7 +1,9 @@ +require "active_support/core_ext/object/blank" + module ActionDispatch class Request - def cookie_jar(config = {}) - env['action_dispatch.cookies'] ||= Cookies::CookieJar.build(self, config) + def cookie_jar + env['action_dispatch.cookies'] ||= Cookies::CookieJar.build(self) end end @@ -51,17 +53,17 @@ module ActionDispatch # only HTTP. Defaults to +false+. class Cookies class CookieJar < Hash #:nodoc: - def self.build(request, config = {}) - new(config).tap do |hash| + def self.build(request) + secret = request.env["action_dispatch.secret_token"] + new(secret).tap do |hash| hash.update(request.cookies) end end - def initialize(config = {}) - @config = config + def initialize(secret=nil) + @secret = secret @set_cookies = {} @delete_cookies = {} - super() end @@ -112,7 +114,7 @@ module ActionDispatch # 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, @config) + @permanent ||= PermanentCookieJar.new(self, @secret) end # Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from @@ -120,7 +122,7 @@ module ActionDispatch # 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 your app's config.cookie_secret. + # This jar requires that you set a suitable secret for the verification on your app's config.secret_token. # # Example: # @@ -129,7 +131,7 @@ module ActionDispatch # # cookies.signed[:discount] # => 45 def signed - @signed ||= SignedCookieJar.new(self, @config) + @signed ||= SignedCookieJar.new(self, @secret) end def write(response) @@ -139,9 +141,8 @@ module ActionDispatch end class PermanentCookieJar < CookieJar #:nodoc: - def initialize(parent_jar, config = {}) - @parent_jar = parent_jar - @config = config + def initialize(parent_jar, secret) + @parent_jar, @secret = parent_jar, secret end def []=(key, options) @@ -156,7 +157,7 @@ module ActionDispatch end def signed - @signed ||= SignedCookieJar.new(self, @config) + @signed ||= SignedCookieJar.new(self, @secret) end def method_missing(method, *arguments, &block) @@ -165,11 +166,10 @@ module ActionDispatch end class SignedCookieJar < CookieJar #:nodoc: - def initialize(parent_jar, config = {}) - raise 'Missing cookie signing secret' if config[:signing_secret].blank? + def initialize(parent_jar, secret) + raise "You must set config.secret_token in your app's config" if secret.blank? @parent_jar = parent_jar - @config = config - @verifier = ActiveSupport::MessageVerifier.new(config[:signing_secret]) + @verifier = ActiveSupport::MessageVerifier.new(secret) end def [](name) diff --git a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb index 3331b7c25e..88ba941676 100644 --- a/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/cookie_store.rb @@ -192,7 +192,7 @@ module ActionDispatch if secret.blank? raise ArgumentError, "A secret is required to generate an " + "integrity hash for cookie session data. Use " + - "config.cookie_secret = \"some secret phrase of at " + + "config.secret_token = \"some secret phrase of at " + "least #{SECRET_MIN_LENGTH} characters\"" + "in config/application.rb" end diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb index 090e03cf44..b3e67f6e36 100644 --- a/actionpack/lib/action_dispatch/testing/test_request.rb +++ b/actionpack/lib/action_dispatch/testing/test_request.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/hash/reverse_merge' module ActionDispatch class TestRequest < Request @@ -9,6 +10,7 @@ module ActionDispatch end def initialize(env = {}) + env = Rails.application.env_defaults.merge(env) if defined?(Rails.application) super(DEFAULT_ENV.merge(env)) self.host = 'test.host' diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index fe78b8ec1f..acf887ee4a 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -192,10 +192,6 @@ end # Temporary base class class Rack::TestCase < ActionController::IntegrationTest - setup do - ActionController::Base.config.secret = "abc" * 30 - end - def self.testing(klass = nil) if klass @testing = "/#{klass.name.underscore}".sub!(/_controller$/, '') diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb index 278cae1415..4971866e7c 100644 --- a/actionpack/test/controller/cookie_test.rb +++ b/actionpack/test/controller/cookie_test.rb @@ -1,7 +1,5 @@ require 'abstract_unit' -ActionController::Base.config.secret = "thisISverySECRET123" - class CookieTest < ActionController::TestCase class TestController < ActionController::Base def authenticate @@ -76,6 +74,7 @@ class CookieTest < ActionController::TestCase def setup super + @request.env["action_dispatch.secret_token"] = "thisISverySECRET123" @request.host = "www.nextangle.com" end diff --git a/actionpack/test/controller/http_digest_authentication_test.rb b/actionpack/test/controller/http_digest_authentication_test.rb index eb2af523a2..b011536717 100644 --- a/actionpack/test/controller/http_digest_authentication_test.rb +++ b/actionpack/test/controller/http_digest_authentication_test.rb @@ -41,8 +41,7 @@ class HttpDigestAuthenticationTest < ActionController::TestCase setup do # Used as secret in generating nonce to prevent tampering of timestamp @secret = "session_options_secret" - @controller.config.secret = @secret - # @old_secret, ActionController::Base.config.secret[:secret] = ActionController::Base.session_options[:secret], @secret + @request.env["action_dispatch.secret_token"] = @secret end teardown do @@ -206,7 +205,7 @@ class HttpDigestAuthenticationTest < ActionController::TestCase test "validate_digest_response should fail with nil returning password_procedure" do @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => nil, :password => nil) - assert !ActionController::HttpAuthentication::Digest.validate_digest_response(@secret, @request, "SuperSecret"){nil} + assert !ActionController::HttpAuthentication::Digest.validate_digest_response(@request, "SuperSecret"){nil} end private diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 24ba378efe..82684e4614 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,3 +1,5 @@ +* Renamed config.cookie_secret to config.secret_token and pass it as env key. [JV] + *Rails 3.0.0 [beta 2] (April 1st, 2010)* * Session store configuration has changed [YK & CL] @@ -6,12 +8,11 @@ config.cookie_secret = "fdsfhisdghfidugnfdlg" * railtie_name and engine_name are deprecated. You can now add any object to - the configuration object: config.your_plugin = {} [JK] + the configuration object: config.your_plugin = {} [JV] * Added config.generators.templates to provide alternative paths for the generators to look for templates [JV] - *Rails 3.0.0 [beta 1] (February 4, 2010)* * Added "rake about" as a replacement for script/about [DHH] diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 0084309ea4..38a5aa8ca3 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,3 +1,4 @@ +require 'active_support/core_ext/hash/reverse_merge' require 'fileutils' require 'rails/plugin' require 'rails/engine' @@ -128,8 +129,14 @@ module Rails end def call(env) - env["action_dispatch.parameter_filter"] = config.filter_parameters - app.call(env) + app.call(env.reverse_merge!(env_defaults)) + end + + def env_defaults + @env_defaults ||= { + "action_dispatch.parameter_filter" => config.filter_parameters, + "action_dispatch.secret_token" => config.secret_token + } end def initializers diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 5c7de616be..d3e4742e8a 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -6,7 +6,7 @@ module Rails include ::Rails::Configuration::Deprecated attr_accessor :allow_concurrency, :cache_classes, :cache_store, - :cookie_secret, :consider_all_requests_local, :dependency_loading, + :secret_token, :consider_all_requests_local, :dependency_loading, :filter_parameters, :log_level, :logger, :metals, :plugins, :preload_frameworks, :reload_engines, :reload_plugins, :serve_static_assets, :time_zone, :whiny_nils @@ -37,6 +37,7 @@ module Rails paths.app.controllers << builtin_controller if builtin_controller paths.config.database "config/database.yml" paths.config.environment "config/environments", :glob => "#{Rails.env}.rb" + paths.lib.templates "lib/templates" paths.log "log/#{Rails.env}.log" paths.tmp "tmp" paths.tmp.cache "tmp/cache" @@ -123,7 +124,7 @@ module Rails def session_options return @session_options unless @session_store == :cookie_store - @session_options.merge(:secret => @cookie_secret) + @session_options.merge(:secret => @secret_token) end def default_middleware_stack diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 978490f25f..94507bb387 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -3,6 +3,10 @@ module Rails module Finisher include Initializable + initializer :add_generator_templates do + config.generators.templates.unshift(*paths.lib.templates.to_a) + end + initializer :ensure_load_once_paths_as_subset do extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb index 73ae9bcb16..dfd849b4bb 100644 --- a/railties/lib/rails/configuration.rb +++ b/railties/lib/rails/configuration.rb @@ -104,6 +104,18 @@ module Rails "please do paths.app.controllers instead", caller paths.app.controllers.to_a.uniq end + + def cookie_secret=(value) + ActiveSupport::Deprecation.warn "config.cookie_secret= is deprecated, " << + "please use config.secret_token= instead", caller + self.secret_token = value + end + + def cookie_secret + ActiveSupport::Deprecation.warn "config.cookie_secret is deprecated, " << + "please use config.secret_token instead", caller + self.secret_token + end end end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index e9013348b5..54c97258ce 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -193,17 +193,13 @@ module Rails app.metal_loader.paths.unshift(*paths.app.metals.to_a) end - initializer :add_generator_templates do |app| - config.generators.templates.unshift(*paths.lib.templates.to_a) - end - - initializer :load_application_initializers do + initializer :load_config_initializers do paths.config.initializers.to_a.sort.each do |initializer| load(initializer) end end - initializer :load_application_classes do |app| + initializer :load_app_classes do |app| next if $rails_rake_task if app.config.cache_classes diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index b8f1f1009c..2129e10af8 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -23,7 +23,6 @@ module Rails paths.app.views "app/views", :eager_load => true paths.lib "lib", :load_path => true paths.lib.tasks "lib/tasks", :glob => "**/*.rake" - paths.lib.templates "lib/templates" paths.config "config" paths.config.initializers "config/initializers", :glob => "**/*.rb" paths.config.locales "config/locales", :glob => "*.{rb,yml}" 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 deleted file mode 100644 index be627fbbcc..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/cookie_verification_secret.rb.tt +++ /dev/null @@ -1,7 +0,0 @@ -# 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. -Rails.application.config.cookie_secret = '<%= app_secret %>' diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt new file mode 100644 index 0000000000..c2fa31aadb --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.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. +Rails.application.config.secret_token = '<%= app_secret %>' diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt index 9e32fb930e..a869a21e2c 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt @@ -1,8 +1,6 @@ # Be sure to restart your server when you modify this file. -Rails.application.config.session_store :cookie_store, { - :key => '_<%= app_name %>_session', -} +Rails.application.config.session_store :cookie_store, :key => '_<%= app_name %>_session' # Use the database for sessions instead of the cookie-based default, # which shouldn't be used to store highly confidential information diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 0997be1b6f..fcdd099135 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -61,7 +61,7 @@ module Rails @config ||= Engine::Configuration.new end - initializer :load_init_rb, :before => :load_application_initializers do |app| + initializer :load_init_rb, :before => :load_config_initializers do |app| files = %w(rails/init.rb init.rb).map { |path| File.expand_path path, root } if initrb = files.find { |path| File.file? path } if initrb == files.first diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 68ca2acaad..90f2e2b370 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -234,6 +234,22 @@ module ApplicationTests assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"] end + test "config.secret_token is sent in env" do + make_basic_app do |app| + app.config.secret_token = 'ThisIsASECRET123' + end + + class ::OmgController < ActionController::Base + def index + cookies.signed[:some_key] = "some_value" + render :text => env["action_dispatch.secret_token"] + end + end + + get "/" + assert_equal 'ThisIsASECRET123', last_response.body + end + test "protect from forgery is the default in a new app" do make_basic_app diff --git a/railties/test/application/middleware_stack_defaults_test.rb b/railties/test/application/middleware_stack_defaults_test.rb index 284f7e2e5b..f31ca01fbf 100644 --- a/railties/test/application/middleware_stack_defaults_test.rb +++ b/railties/test/application/middleware_stack_defaults_test.rb @@ -10,7 +10,7 @@ class MiddlewareStackDefaultsTest < Test::Unit::TestCase Object.const_set(:MyApplication, Class.new(Rails::Application)) MyApplication.class_eval do - config.cookie_secret = "3b7cd727ee24e8444053437c36cc66c4" + config.secret_token = "3b7cd727ee24e8444053437c36cc66c4" config.session_store :cookie_store, :key => "_myapp_session" end end diff --git a/railties/test/application/url_generation_test.rb b/railties/test/application/url_generation_test.rb index 04f5454e09..72cae23985 100644 --- a/railties/test/application/url_generation_test.rb +++ b/railties/test/application/url_generation_test.rb @@ -14,7 +14,7 @@ module ApplicationTests require "action_controller/railtie" class MyApp < Rails::Application - config.cookie_secret = "3b7cd727ee24e8444053437c36cc66c4" + config.secret_token = "3b7cd727ee24e8444053437c36cc66c4" config.session_store :cookie_store, :key => "_myapp_session" end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 8f2f15b49e..e6896a1629 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -100,7 +100,7 @@ module TestHelpers end end - add_to_config 'config.cookie_secret = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"' + add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"' end class Bukkit -- cgit v1.2.3 From c140aca361506a3a759fbe03fad8adc748c807bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 5 Apr 2010 12:30:59 +0200 Subject: Remove app/views from the load paths [#4226 state:resolved] --- railties/lib/rails/engine/configuration.rb | 2 +- railties/lib/rails/paths.rb | 22 +++++++++++++++++----- railties/test/application/paths_test.rb | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 2129e10af8..28e7ef947d 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -20,7 +20,7 @@ module Rails paths.app.models "app/models", :eager_load => true paths.app.mailers "app/mailers", :eager_load => true paths.app.metals "app/metal", :eager_load => true - paths.app.views "app/views", :eager_load => true + paths.app.views "app/views" paths.lib "lib", :load_path => true paths.lib.tasks "lib/tasks", :glob => "**/*.rake" paths.config "config" diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index 55874813da..1c9e308631 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -3,6 +3,8 @@ require 'set' module Rails module Paths module PathParent + attr_reader :children + def method_missing(id, *args) name = id.to_s @@ -37,15 +39,15 @@ module Rails end def load_once - filter { |path| path.paths if path.load_once? } + filter_by(:load_once?) end def eager_load - filter { |path| path.paths if path.eager_load? } + filter_by(:eager_load?) end def load_paths - filter { |path| path.paths if path.load_path? } + filter_by(:load_path?) end def push(*) @@ -58,8 +60,16 @@ module Rails protected - def filter(&block) - all_paths.map(&block).compact.flatten.uniq.select { |p| File.exists?(p) } + def filter_by(constraint) + all_paths.map do |path| + if path.send(constraint) + paths = path.paths + paths -= path.children.values.map { |p| p.send(constraint) ? [] : p.paths }.flatten + paths + else + [] + end + end.flatten.uniq.select { |p| File.exists?(p) } end end @@ -129,10 +139,12 @@ module Rails def paths raise "You need to set a path root" unless @root.path + result = @paths.map do |p| path = File.expand_path(p, @root.path) @glob ? Dir[File.join(path, @glob)] : path end + result.flatten! result.uniq! result diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index 589e515d05..5ab558c026 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -71,6 +71,7 @@ module ApplicationTests assert_in_load_path "lib" assert_in_load_path "vendor" + assert_not_in_load_path "app", "views" assert_not_in_load_path "app", "metal" assert_not_in_load_path "config" assert_not_in_load_path "config", "locales" -- cgit v1.2.3 From 89978f10afbad3f856e2959a811bed1982715408 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 5 Apr 2010 12:15:08 -0700 Subject: moves Object#singleton_class to Kernel#singleton_class to match Ruby also there, same for #class_eval to simplify, and adds coverage for class_eval --- actionpack/lib/action_dispatch/testing/integration.rb | 2 +- activerecord/lib/active_record/base.rb | 2 +- activerecord/lib/active_record/migration.rb | 2 +- activerecord/lib/active_record/named_scope.rb | 2 +- activesupport/lib/active_support/callbacks.rb | 2 +- .../lib/active_support/core_ext/class/attribute.rb | 2 +- .../active_support/core_ext/class/delegating_attributes.rb | 2 +- activesupport/lib/active_support/core_ext/kernel.rb | 1 + .../lib/active_support/core_ext/kernel/singleton_class.rb | 13 +++++++++++++ activesupport/lib/active_support/core_ext/object.rb | 1 - .../lib/active_support/core_ext/object/singleton_class.rb | 13 ------------- .../lib/active_support/core_ext/string/output_safety.rb | 2 +- activesupport/lib/active_support/memoizable.rb | 2 +- activesupport/test/core_ext/kernel_test.rb | 11 +++++++++++ activesupport/test/core_ext/object_and_class_ext_test.rb | 5 ----- railties/lib/rails/generators.rb | 2 +- 16 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/kernel/singleton_class.rb delete mode 100644 activesupport/lib/active_support/core_ext/object/singleton_class.rb diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 8d107d9aa5..286797ba20 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -1,6 +1,6 @@ require 'stringio' require 'uri' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'rack/test' require 'test/unit/assertions' diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5f741c6ae7..f0b107255c 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -11,7 +11,7 @@ require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/hash/indifferent_access' require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/string/behavior' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/blank' diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index d4cda927ad..c163fb982a 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' module ActiveRecord # Exception that can be raised to stop migrations from going backwards. diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 632322b517..aeed52e72a 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -1,6 +1,6 @@ require 'active_support/core_ext/array' require 'active_support/core_ext/hash/except' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/object/blank' module ActiveRecord diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index b230bb8a40..c669630e47 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -1,7 +1,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/class/inheritable_attributes' require 'active_support/core_ext/kernel/reporting' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' module ActiveSupport # Callbacks are hooks into the lifecycle of an object that allow you to trigger logic diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index 577d5cbf45..725acad43a 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/module/remove_method' diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index 12caa76c98..29bf7c0f3d 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -1,6 +1,6 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/array/extract_options' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/remove_method' class Class diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index c3bed14f63..01cfe7fc10 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -2,3 +2,4 @@ require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/kernel/agnostics' require 'active_support/core_ext/kernel/requires' require 'active_support/core_ext/kernel/debugger' +require 'active_support/core_ext/kernel/singleton_class' diff --git a/activesupport/lib/active_support/core_ext/kernel/singleton_class.rb b/activesupport/lib/active_support/core_ext/kernel/singleton_class.rb new file mode 100644 index 0000000000..33612155fb --- /dev/null +++ b/activesupport/lib/active_support/core_ext/kernel/singleton_class.rb @@ -0,0 +1,13 @@ +module Kernel + # Returns the object's singleton class. + def singleton_class + class << self + self + end + end unless respond_to?(:singleton_class) # exists in 1.9.2 + + # class_eval on an object acts like singleton_class.class_eval. + def class_eval(*args, &block) + singleton_class.class_eval(*args, &block) + end +end diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 4f86d9d605..3a6100f776 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -5,7 +5,6 @@ require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/instance_variables' -require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/object/misc' require 'active_support/core_ext/object/extending' diff --git a/activesupport/lib/active_support/core_ext/object/singleton_class.rb b/activesupport/lib/active_support/core_ext/object/singleton_class.rb deleted file mode 100644 index 8dee54e71b..0000000000 --- a/activesupport/lib/active_support/core_ext/object/singleton_class.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Object - # Returns the object's singleton class. - def singleton_class - class << self - self - end - end unless respond_to?(:singleton_class) - - # class_eval on an object acts like singleton_class_eval. - def class_eval(*args, &block) - singleton_class.class_eval(*args, &block) - end -end diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index 3ee5bcaab4..b53929c2a3 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -1,5 +1,5 @@ require 'erb' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' class ERB module Util diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index ca1cfedae3..9fb506fea1 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/aliasing' module ActiveSupport diff --git a/activesupport/test/core_ext/kernel_test.rb b/activesupport/test/core_ext/kernel_test.rb index 1dfc283268..c22af89918 100644 --- a/activesupport/test/core_ext/kernel_test.rb +++ b/activesupport/test/core_ext/kernel_test.rb @@ -41,6 +41,17 @@ class KernelTest < Test::Unit::TestCase def test_silence_stderr_with_return_value assert_equal 1, silence_stderr { 1 } end + + def test_singleton_class + o = Object.new + assert_equal class << o; self end, o.singleton_class + end + + def test_class_eval + o = Object.new + class << o; @x = 1; end + assert_equal 1, o.class_eval { @x } + end end class KernelSupressTest < Test::Unit::TestCase diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 437bb78a4e..5e03389dc2 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -118,11 +118,6 @@ class ObjectTests < ActiveSupport::TestCase assert duck.acts_like?(:time) assert !duck.acts_like?(:date) end - - def test_singleton_class - o = Object.new - assert_equal class << o; self end, o.singleton_class - end end class ObjectInstanceVariableTest < Test::Unit::TestCase diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 57462707f4..146974ae20 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -3,7 +3,7 @@ $:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.inc require 'active_support' require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/object/singleton_class' +require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/module/attribute_accessors' -- cgit v1.2.3 From c6746ffaf4a2aeb5e4cec10c2a413f5614a6d137 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 5 Apr 2010 15:30:49 -0300 Subject: deprecate form_for(symbol_or_string, ...) in favor of :object_name option --- actionpack/lib/action_view/helpers/form_helper.rb | 13 +- actionpack/test/template/form_helper_test.rb | 539 +++++++++++++--------- 2 files changed, 336 insertions(+), 216 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 9467a0912a..e44b43300d 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -300,15 +300,16 @@ module ActionView case record_or_name_or_array when String, Symbol + ActiveSupport::Deprecation.warn("Use the option :object_name => ... instead of a Symbol or String as a the first argument", caller) object_name = record_or_name_or_array when Array object = record_or_name_or_array.last - object_name = ActionController::RecordIdentifier.singular_class_name(object) + object_name = options[:object_name] || ActionController::RecordIdentifier.singular_class_name(object) apply_form_for_options!(record_or_name_or_array, options) args.unshift object else object = record_or_name_or_array - object_name = ActionController::RecordIdentifier.singular_class_name(object) + object_name = options[:object_name] || ActionController::RecordIdentifier.singular_class_name(object) apply_form_for_options!([object], options) args.unshift object end @@ -326,9 +327,13 @@ module ActionView html_options = if object.respond_to?(:persisted?) && object.persisted? - { :class => dom_class(object, :edit), :id => dom_id(object, :edit), :method => :put } + { :class => options[:object_name] ? "#{options[:object_name]}_edit" : dom_class(object, :edit), + :id => options[:object_name] ? "#{options[:object_name]}_edit" : dom_id(object, :edit), + :method => :put } else - { :class => dom_class(object, :new), :id => dom_id(object), :method => :post } + { :class => options[:object_name] ? "#{options[:object_name]}_new" : dom_class(object, :new), + :id => options[:object_name] ? "#{options[:object_name]}_new" : dom_id(object), + :method => :post } end options[:html] ||= {} diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 4af38e52dd..f6401985cc 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -416,12 +416,14 @@ class FormHelperTest < ActionView::TestCase end def test_form_for - form_for(:post, @post, :html => { :id => 'create-post' }) do |f| - concat f.label(:title) - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) - concat f.submit('Create post') + assert_deprecated do + form_for(:post, @post, :html => { :id => 'create-post' }) do |f| + concat f.label(:title) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + concat f.submit('Create post') + end end expected = @@ -437,11 +439,35 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end - def test_form_for_with_method - form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| + def test_form_for_with_symbol_object_name + form_for(@post, :object_name => "other_name", :html => { :id => 'create-post' }) do |f| + concat f.label(:title) concat f.text_field(:title) concat f.text_area(:body) concat f.check_box(:secret) + concat f.submit('Create post') + end + + expected = + "
" + + "
" + + "" + + "" + + "" + + "" + + "" + + "
" + + assert_dom_equal expected, output_buffer + end + + def test_form_for_with_method + assert_deprecated do + form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = @@ -457,10 +483,12 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_remote - form_for(:post, @post, :remote => true, :html => { :id => 'create-post', :method => :put }) do |f| - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) + assert_deprecated do + form_for(:post, @post, :remote => true, :html => { :id => 'create-post', :method => :put }) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = @@ -476,10 +504,12 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_without_object - form_for(:post, :html => { :id => 'create-post' }) do |f| - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) + assert_deprecated do + form_for(:post, :html => { :id => 'create-post' }) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = @@ -494,16 +524,18 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_index - form_for("post[]", @post) do |f| - concat f.label(:title) - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) + assert_deprecated do + form_for("post[]", @post) do |f| + concat f.label(:title) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = "
" + - "" + + "" + "" + "" + "" + @@ -514,10 +546,12 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_nil_index_option_override - form_for("post[]", @post, :index => nil) do |f| - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) + assert_deprecated do + form_for("post[]", @post, :index => nil) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = @@ -535,8 +569,10 @@ class FormHelperTest < ActionView::TestCase old_locale, I18n.locale = I18n.locale, :submit @post.persisted = false - form_for(:post, @post) do |f| - concat f.submit + assert_deprecated do + form_for(:post, @post) do |f| + concat f.submit + end end expected = "" + @@ -550,8 +586,10 @@ class FormHelperTest < ActionView::TestCase def test_submit_with_object_as_existing_record_and_locale_strings old_locale, I18n.locale = I18n.locale, :submit - form_for(:post, @post) do |f| - concat f.submit + assert_deprecated do + form_for(:post, @post) do |f| + concat f.submit + end end expected = "" + @@ -565,8 +603,10 @@ class FormHelperTest < ActionView::TestCase def test_submit_without_object_and_locale_strings old_locale, I18n.locale = I18n.locale, :submit - form_for(:post) do |f| - concat f.submit :class => "extra" + assert_deprecated do + form_for(:post) do |f| + concat f.submit :class => "extra" + end end expected = "" + @@ -580,8 +620,10 @@ class FormHelperTest < ActionView::TestCase def test_submit_with_object_and_nested_lookup old_locale, I18n.locale = I18n.locale, :submit - form_for(:another_post, @post) do |f| - concat f.submit + assert_deprecated do + form_for(:another_post, @post) do |f| + concat f.submit + end end expected = "" + @@ -593,10 +635,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for - form_for(:post, @post) do |f| - concat f.fields_for(:comment, @post) { |c| - concat c.text_field(:title) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.fields_for(:comment, @post) { |c| + concat c.text_field(:title) + } + end end expected = "" + @@ -607,11 +651,13 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_nested_collections - form_for('post[]', @post) do |f| - concat f.text_field(:title) - concat f.fields_for('comment[]', @comment) { |c| - concat c.text_field(:name) - } + assert_deprecated do + form_for('post[]', @post) do |f| + concat f.text_field(:title) + concat f.fields_for('comment[]', @comment) { |c| + concat c.text_field(:name) + } + end end expected = "" + @@ -623,11 +669,13 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_index_and_parent_fields - form_for('post', @post, :index => 1) do |c| - concat c.text_field(:title) - concat c.fields_for('comment', @comment, :index => 1) { |r| - concat r.text_field(:name) - } + assert_deprecated do + form_for('post', @post, :index => 1) do |c| + concat c.text_field(:title) + concat c.fields_for('comment', @comment, :index => 1) { |r| + concat r.text_field(:name) + } + end end expected = "" + @@ -639,10 +687,12 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_index_and_nested_fields_for - output_buffer = form_for(:post, @post, :index => 1) do |f| - concat f.fields_for(:comment, @post) { |c| - concat c.text_field(:title) - } + assert_deprecated do + output_buffer = form_for(:post, @post, :index => 1) do |f| + concat f.fields_for(:comment, @post) { |c| + concat c.text_field(:title) + } + end end expected = "" + @@ -653,10 +703,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_index_on_both - form_for(:post, @post, :index => 1) do |f| - concat f.fields_for(:comment, @post, :index => 5) { |c| - concat c.text_field(:title) - } + assert_deprecated do + form_for(:post, @post, :index => 1) do |f| + concat f.fields_for(:comment, @post, :index => 5) { |c| + concat c.text_field(:title) + } + end end expected = "" + @@ -667,10 +719,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_auto_index - form_for("post[]", @post) do |f| - concat f.fields_for(:comment, @post) { |c| - concat c.text_field(:title) - } + assert_deprecated do + form_for("post[]", @post) do |f| + concat f.fields_for(:comment, @post) { |c| + concat c.text_field(:title) + } + end end expected = "" + @@ -681,10 +735,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_index_radio_button - form_for(:post, @post) do |f| - concat f.fields_for(:comment, @post, :index => 5) { |c| - concat c.radio_button(:title, "hello") - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.fields_for(:comment, @post, :index => 5) { |c| + concat c.radio_button(:title, "hello") + } + end end expected = "" + @@ -695,10 +751,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_auto_index_on_both - form_for("post[]", @post) do |f| - concat f.fields_for("comment[]", @post) { |c| - concat c.text_field(:title) - } + assert_deprecated do + form_for("post[]", @post) do |f| + concat f.fields_for("comment[]", @post) { |c| + concat c.text_field(:title) + } + end end expected = "" + @@ -709,36 +767,40 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_index_and_auto_index - output_buffer = form_for("post[]", @post) do |f| - concat f.fields_for(:comment, @post, :index => 5) { |c| - concat c.text_field(:title) - } - end + assert_deprecated do + output_buffer = form_for("post[]", @post) do |f| + concat f.fields_for(:comment, @post, :index => 5) { |c| + concat c.text_field(:title) + } + end - output_buffer << form_for(:post, @post, :index => 1) do |f| - concat f.fields_for("comment[]", @post) { |c| - concat c.text_field(:title) - } - end + output_buffer << form_for(:post, @post, :index => 1) do |f| + concat f.fields_for("comment[]", @post) { |c| + concat c.text_field(:title) + } + end - expected = "" + - "" + - "
" + - "
" + - "" + - "
" + expected = "
" + + "" + + "
" + + "
" + + "" + + "
" - assert_dom_equal expected, output_buffer + assert_dom_equal expected, output_buffer + end end def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association @post.author = Author.new - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.fields_for(:author) { |af| - concat af.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.fields_for(:author) { |af| + concat af.text_field(:name) + } + end end expected = '
' + @@ -750,10 +812,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association - form_for(:post, @post) do |f| - f.fields_for(:author, Author.new(123)) do |af| - assert_not_nil af.object - assert_equal 123, af.object.id + assert_deprecated do + form_for(:post, @post) do |f| + f.fields_for(:author, Author.new(123)) do |af| + assert_not_nil af.object + assert_equal 123, af.object.id + end end end end @@ -761,11 +825,13 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association @post.author = Author.new(321) - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.fields_for(:author) { |af| - concat af.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.fields_for(:author) { |af| + concat af.text_field(:name) + } + end end expected = '' + @@ -780,12 +846,14 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement @post.author = Author.new(321) - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.fields_for(:author) { |af| - concat af.hidden_field(:id) - concat af.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.fields_for(:author) { |af| + concat af.hidden_field(:id) + concat af.text_field(:name) + } + end end expected = '' + @@ -800,12 +868,14 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association @post.comments = Array.new(2) { |id| Comment.new(id + 1) } - form_for(:post, @post) do |f| - concat f.text_field(:title) - @post.comments.each do |comment| - concat f.fields_for(:comments, comment) { |cf| - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + concat f.fields_for(:comments, comment) { |cf| + concat cf.text_field(:name) + } + end end end @@ -823,13 +893,15 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association_with_explicit_hidden_field_placement @post.comments = Array.new(2) { |id| Comment.new(id + 1) } - form_for(:post, @post) do |f| - concat f.text_field(:title) - @post.comments.each do |comment| - concat f.fields_for(:comments, comment) { |cf| - concat cf.hidden_field(:id) - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + concat f.fields_for(:comments, comment) { |cf| + concat cf.hidden_field(:id) + concat cf.text_field(:name) + } + end end end @@ -847,12 +919,14 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association @post.comments = [Comment.new, Comment.new] - form_for(:post, @post) do |f| - concat f.text_field(:title) - @post.comments.each do |comment| - concat f.fields_for(:comments, comment) { |cf| - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + concat f.fields_for(:comments, comment) { |cf| + concat cf.text_field(:name) + } + end end end @@ -868,12 +942,14 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_collection_association @post.comments = [Comment.new(321), Comment.new] - form_for(:post, @post) do |f| - concat f.text_field(:title) - @post.comments.each do |comment| - concat f.fields_for(:comments, comment) { |cf| - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + @post.comments.each do |comment| + concat f.fields_for(:comments, comment) { |cf| + concat cf.text_field(:name) + } + end end end @@ -888,10 +964,12 @@ class FormHelperTest < ActionView::TestCase end def test_nested_fields_for_with_an_empty_supplied_attributes_collection - form_for(:post, @post) do |f| - concat f.text_field(:title) - f.fields_for(:comments, []) do |cf| - concat cf.text_field(:name) + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + f.fields_for(:comments, []) do |cf| + concat cf.text_field(:name) + end end end @@ -905,11 +983,13 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_existing_records_on_a_supplied_nested_attributes_collection @post.comments = Array.new(2) { |id| Comment.new(id + 1) } - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.fields_for(:comments, @post.comments) { |cf| - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.fields_for(:comments, @post.comments) { |cf| + concat cf.text_field(:name) + } + end end expected = '' + @@ -927,11 +1007,13 @@ class FormHelperTest < ActionView::TestCase comments = Array.new(2) { |id| Comment.new(id + 1) } @post.comments = [] - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.fields_for(:comments, comments) { |cf| - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.fields_for(:comments, comments) { |cf| + concat cf.text_field(:name) + } + end end expected = '' + @@ -949,12 +1031,14 @@ class FormHelperTest < ActionView::TestCase @post.comments = [Comment.new(321), Comment.new] yielded_comments = [] - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.fields_for(:comments) { |cf| - concat cf.text_field(:name) - yielded_comments << cf.object - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.fields_for(:comments) { |cf| + concat cf.text_field(:name) + yielded_comments << cf.object + } + end end expected = '' + @@ -971,10 +1055,12 @@ class FormHelperTest < ActionView::TestCase def test_nested_fields_for_with_child_index_option_override_on_a_nested_attributes_collection_association @post.comments = [] - form_for(:post, @post) do |f| - concat f.fields_for(:comments, Comment.new(321), :child_index => 'abc') { |cf| - concat cf.text_field(:name) - } + assert_deprecated do + form_for(:post, @post) do |f| + concat f.fields_for(:comments, Comment.new(321), :child_index => 'abc') { |cf| + concat cf.text_field(:name) + } + end end expected = '' + @@ -991,25 +1077,28 @@ class FormHelperTest < ActionView::TestCase @post.comments[0].relevances = [] @post.tags[0].relevances = [] @post.tags[1].relevances = [] - form_for(:post, @post) do |f| - concat f.fields_for(:comments, @post.comments[0]) { |cf| - concat cf.text_field(:name) - concat cf.fields_for(:relevances, CommentRelevance.new(314)) { |crf| - concat crf.text_field(:value) + + assert_deprecated do + form_for(:post, @post) do |f| + concat f.fields_for(:comments, @post.comments[0]) { |cf| + concat cf.text_field(:name) + concat cf.fields_for(:relevances, CommentRelevance.new(314)) { |crf| + concat crf.text_field(:value) + } } - } - concat f.fields_for(:tags, @post.tags[0]) { |tf| - concat tf.text_field(:value) - concat tf.fields_for(:relevances, TagRelevance.new(3141)) { |trf| - concat trf.text_field(:value) + concat f.fields_for(:tags, @post.tags[0]) { |tf| + concat tf.text_field(:value) + concat tf.fields_for(:relevances, TagRelevance.new(3141)) { |trf| + concat trf.text_field(:value) + } } - } - concat f.fields_for('tags', @post.tags[1]) { |tf| - concat tf.text_field(:value) - concat tf.fields_for(:relevances, TagRelevance.new(31415)) { |trf| - concat trf.text_field(:value) + concat f.fields_for('tags', @post.tags[1]) { |tf| + concat tf.text_field(:value) + concat tf.fields_for(:relevances, TagRelevance.new(31415)) { |trf| + concat trf.text_field(:value) + } } - } + end end expected = '' + @@ -1153,13 +1242,15 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_and_fields_for - form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| - concat post_form.text_field(:title) - concat post_form.text_area(:body) + assert_deprecated do + form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| + concat post_form.text_field(:title) + concat post_form.text_area(:body) - concat fields_for(:parent_post, @post) { |parent_fields| - concat parent_fields.check_box(:secret) - } + concat fields_for(:parent_post, @post) { |parent_fields| + concat parent_fields.check_box(:secret) + } + end end expected = @@ -1174,13 +1265,15 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_and_fields_for_with_object - form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| - concat post_form.text_field(:title) - concat post_form.text_area(:body) + assert_deprecated do + form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| + concat post_form.text_field(:title) + concat post_form.text_area(:body) - concat post_form.fields_for(@comment) { |comment_fields| - concat comment_fields.text_field(:name) - } + concat post_form.fields_for(@comment) { |comment_fields| + concat comment_fields.text_field(:name) + } + end end expected = @@ -1205,10 +1298,12 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_labelled_builder - form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) + assert_deprecated do + form_for(:post, @post, :builder => LabelledFormBuilder) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = @@ -1225,10 +1320,12 @@ class FormHelperTest < ActionView::TestCase old_default_form_builder, ActionView::Base.default_form_builder = ActionView::Base.default_form_builder, LabelledFormBuilder - form_for(:post, @post) do |f| - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) + assert_deprecated do + form_for(:post, @post) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end end expected = @@ -1244,9 +1341,11 @@ class FormHelperTest < ActionView::TestCase end def test_default_form_builder_with_active_record_helpers - form_for(:post, @post) do |f| - concat f.error_message_on('author_name') - concat f.error_messages + assert_deprecated do + form_for(:post, @post) do |f| + concat f.error_message_on('author_name') + concat f.error_messages + end end expected = %() + @@ -1262,9 +1361,11 @@ class FormHelperTest < ActionView::TestCase post = @post @post = nil - form_for(:post, post) do |f| - concat f.error_message_on('author_name') - concat f.error_messages + assert_deprecated do + form_for(:post, post) do |f| + concat f.error_message_on('author_name') + concat f.error_messages + end end expected = %() + @@ -1294,10 +1395,12 @@ class FormHelperTest < ActionView::TestCase def test_form_for_with_labelled_builder_with_nested_fields_for_without_options_hash klass = nil - form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - f.fields_for(:comments, Comment.new) do |nested_fields| - klass = nested_fields.class - '' + assert_deprecated do + form_for(:post, @post, :builder => LabelledFormBuilder) do |f| + f.fields_for(:comments, Comment.new) do |nested_fields| + klass = nested_fields.class + '' + end end end @@ -1307,10 +1410,12 @@ class FormHelperTest < ActionView::TestCase def test_form_for_with_labelled_builder_with_nested_fields_for_with_options_hash klass = nil - form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - f.fields_for(:comments, Comment.new, :index => 'foo') do |nested_fields| - klass = nested_fields.class - '' + assert_deprecated do + form_for(:post, @post, :builder => LabelledFormBuilder) do |f| + f.fields_for(:comments, Comment.new, :index => 'foo') do |nested_fields| + klass = nested_fields.class + '' + end end end @@ -1322,10 +1427,12 @@ class FormHelperTest < ActionView::TestCase def test_form_for_with_labelled_builder_with_nested_fields_for_with_custom_builder klass = nil - form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - f.fields_for(:comments, Comment.new, :builder => LabelledFormBuilderSubclass) do |nested_fields| - klass = nested_fields.class - '' + assert_deprecated do + form_for(:post, @post, :builder => LabelledFormBuilder) do |f| + f.fields_for(:comments, Comment.new, :builder => LabelledFormBuilderSubclass) do |nested_fields| + klass = nested_fields.class + '' + end end end @@ -1333,27 +1440,35 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_html_options_adds_options_to_form_tag - form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end + assert_deprecated do + form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end + end expected = "
" assert_dom_equal expected, output_buffer end def test_form_for_with_string_url_option - form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end + assert_deprecated do + form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end + end assert_equal '
', output_buffer end def test_form_for_with_hash_url_option - form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end + assert_deprecated do + form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end + end assert_equal 'controller', @url_for_options[:controller] assert_equal 'action', @url_for_options[:action] end def test_form_for_with_record_url_option - form_for(:post, @post, :url => @post) do |f| end + assert_deprecated do + form_for(:post, @post, :url => @post) do |f| end + end expected = "
" assert_equal expected, output_buffer -- cgit v1.2.3 From 72f89b5d971b48a133c4c0af56fbeda35d738dae Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 5 Apr 2010 17:11:59 -0300 Subject: create option to include_root_in_json for ActiveResource [#2584 state:committed] --- activeresource/lib/active_resource/base.rb | 9 +++++++++ activeresource/test/cases/base_test.rb | 25 +++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 5cfe4f7c7d..1e81fc099c 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -251,6 +251,9 @@ module ActiveResource # The logger for diagnosing and tracing Active Resource calls. cattr_accessor :logger + # Controls the top-level behavior of JSON serialization + cattr_accessor :include_root_in_json, :instance_writer => false + class << self # Creates a schema for this resource - setting the attributes that are # known prior to fetching an instance from the remote system. @@ -1240,6 +1243,12 @@ module ActiveResource case self.class.format when ActiveResource::Formats::XmlFormat self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) + when ActiveResource::Formats::JsonFormat + if ActiveResource::Base.include_root_in_json + self.class.format.encode({self.class.element_name => attributes}, options) + else + self.class.format.encode(attributes, options) + end else self.class.format.encode(attributes, options) end diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 0f10a0e4d7..31e0dc0abc 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -4,20 +4,22 @@ require "fixtures/customer" require "fixtures/street_address" require "fixtures/beast" require "fixtures/proxy" +require 'active_support/json' require 'active_support/core_ext/hash/conversions' require 'mocha' class BaseTest < Test::Unit::TestCase def setup - @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') - @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') - @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') - @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } - @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') + @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') + @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') + @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") + @joe = { 'person' => { :id => 6, :name => 'Joe' }}.to_json @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') - @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') + @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') # - deep nested resource - # - Luis (Customer) @@ -66,6 +68,7 @@ class BaseTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/2.xml", {}, @david + mock.get "/people/6.json", {}, @joe mock.get "/people/5.xml", {}, @marty mock.get "/people/Greg.xml", {}, @greg mock.get "/people/4.xml", {'key' => 'value'}, nil, 404 @@ -1012,6 +1015,16 @@ class BaseTest < Test::Unit::TestCase assert xml.include?('1') end + + def test_to_json_including_root + Person.include_root_in_json = true + Person.format = :json + joe = Person.find(6) + json = joe.encode + Person.format = :xml + assert_equal json, '{"person":{"person":{"name":"Joe","id":6}}}' + end + def test_to_param_quacks_like_active_record new_person = Person.new assert_nil new_person.to_param -- cgit v1.2.3 From 1f7b4447a9030ccec542ecaa1e5e11e0d4622a84 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 5 Apr 2010 21:54:39 +0100 Subject: Memoize association.named_scope calls --- .../lib/active_record/associations/association_collection.rb | 3 +++ activerecord/test/cases/named_scope_test.rb | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 6eda70d0ce..5ecdf1ac8d 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -405,6 +405,9 @@ module ActiveRecord else super end + elsif @reflection.klass.scopes[method] + @_scopes ||= {} + @_scopes[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) } else with_scope(construct_scope) do if block_given? diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 2396ca10b0..3d5ebb6cb8 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -413,6 +413,15 @@ class NamedScopeTest < ActiveRecord::TestCase Topic.approved.by_lifo.replied.written_before(Time.now).all end end + + def test_named_scopes_are_cached_on_associations + post = posts(:welcome) + + assert_equal post.comments.containing_the_letter_e.object_id, post.comments.containing_the_letter_e.object_id + + post.comments.containing_the_letter_e.all # force load + assert_no_queries { post.comments.containing_the_letter_e.all } + end end class DynamicScopeMatchTest < ActiveRecord::TestCase -- cgit v1.2.3 From efa7e96821a3d335fab9cd9ed1dfa6c6cd6744a2 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Tue, 6 Apr 2010 07:03:36 +1000 Subject: It seems the test directory backtrace line is output with a / before it, thereby previously making it not match the regex. Support APP_DIRS that have backtrace lines maybe beginning with /. [#4277 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- railties/lib/rails/backtrace_cleaner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/backtrace_cleaner.rb b/railties/lib/rails/backtrace_cleaner.rb index 14eccd5a6a..ee2635d4cd 100644 --- a/railties/lib/rails/backtrace_cleaner.rb +++ b/railties/lib/rails/backtrace_cleaner.rb @@ -13,7 +13,7 @@ module Rails add_gem_filters - add_silencer { |line| !APP_DIRS.any? { |dir| line =~ /^#{dir}/ } } + add_silencer { |line| !APP_DIRS.any? { |dir| line =~ /^\/?#{dir}/ } } end private -- cgit v1.2.3 From 4eab983b955fe17cf02c6fe96cab1c8a309cd606 Mon Sep 17 00:00:00 2001 From: Rolf Bjaanes Date: Tue, 15 Dec 2009 18:36:28 +0100 Subject: Changed the way inflections for uncountables work for 'funky jeans' [#3576 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activesupport/lib/active_support/inflections.rb | 2 +- activesupport/lib/active_support/inflector/inflections.rb | 2 +- activesupport/test/inflector_test_cases.rb | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/inflections.rb b/activesupport/lib/active_support/inflections.rb index 8fb3fa9aa2..e7b5387ed7 100644 --- a/activesupport/lib/active_support/inflections.rb +++ b/activesupport/lib/active_support/inflections.rb @@ -51,6 +51,6 @@ module ActiveSupport inflect.irregular('move', 'moves') inflect.irregular('cow', 'kine') - inflect.uncountable(%w(equipment information rice money species series fish sheep)) + inflect.uncountable(%w(equipment information rice money species series fish sheep jeans)) end end diff --git a/activesupport/lib/active_support/inflector/inflections.rb b/activesupport/lib/active_support/inflector/inflections.rb index 785e245ea4..3caf78bc7d 100644 --- a/activesupport/lib/active_support/inflector/inflections.rb +++ b/activesupport/lib/active_support/inflector/inflections.rb @@ -148,7 +148,7 @@ module ActiveSupport def singularize(word) result = word.to_s.dup - if inflections.uncountables.include?(result.downcase) + if inflections.uncountables.any? { |inflection| result =~ /#{inflection}\Z/i } result else inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } diff --git a/activesupport/test/inflector_test_cases.rb b/activesupport/test/inflector_test_cases.rb index 8dad9d8155..29f87ac302 100644 --- a/activesupport/test/inflector_test_cases.rb +++ b/activesupport/test/inflector_test_cases.rb @@ -12,6 +12,8 @@ module InflectorTestCases "stack" => "stacks", "wish" => "wishes", "fish" => "fish", + "jeans" => "jeans", + "funky jeans" => "funky jeans", "category" => "categories", "query" => "queries", -- cgit v1.2.3 From d270da569efeabd7cd563028816452236713aa9f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 5 Apr 2010 18:41:35 -0300 Subject: changed from :object_name to :as on form_for api --- actionpack/lib/action_view/helpers/form_helper.rb | 14 +++++++------- actionpack/test/template/erb/form_for_test.rb | 2 +- actionpack/test/template/form_helper_test.rb | 18 +++++++----------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index e44b43300d..45f41edcac 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -300,16 +300,16 @@ module ActionView case record_or_name_or_array when String, Symbol - ActiveSupport::Deprecation.warn("Use the option :object_name => ... instead of a Symbol or String as a the first argument", caller) + ActiveSupport::Deprecation.warn("Using form_for(:name, @resource) is deprecated. Please use form_for(@resource, :as => :name) instead.", caller) unless args.empty? object_name = record_or_name_or_array when Array object = record_or_name_or_array.last - object_name = options[:object_name] || ActionController::RecordIdentifier.singular_class_name(object) + object_name = options[:as] || ActionController::RecordIdentifier.singular_class_name(object) apply_form_for_options!(record_or_name_or_array, options) args.unshift object else object = record_or_name_or_array - object_name = options[:object_name] || ActionController::RecordIdentifier.singular_class_name(object) + object_name = options[:as] || ActionController::RecordIdentifier.singular_class_name(object) apply_form_for_options!([object], options) args.unshift object end @@ -327,12 +327,12 @@ module ActionView html_options = if object.respond_to?(:persisted?) && object.persisted? - { :class => options[:object_name] ? "#{options[:object_name]}_edit" : dom_class(object, :edit), - :id => options[:object_name] ? "#{options[:object_name]}_edit" : dom_id(object, :edit), + { :class => options[:as] ? "#{options[:as]}_edit" : dom_class(object, :edit), + :id => options[:as] ? "#{options[:as]}_edit" : dom_id(object, :edit), :method => :put } else - { :class => options[:object_name] ? "#{options[:object_name]}_new" : dom_class(object, :new), - :id => options[:object_name] ? "#{options[:object_name]}_new" : dom_id(object), + { :class => options[:as] ? "#{options[:as]}_new" : dom_class(object, :new), + :id => options[:as] ? "#{options[:as]}_new" : dom_id(object), :method => :post } end diff --git a/actionpack/test/template/erb/form_for_test.rb b/actionpack/test/template/erb/form_for_test.rb index 482dbb0287..ec6e872735 100644 --- a/actionpack/test/template/erb/form_for_test.rb +++ b/actionpack/test/template/erb/form_for_test.rb @@ -8,4 +8,4 @@ module ERBTest assert_equal "
", output end end -end \ No newline at end of file +end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index f6401985cc..88014a6564 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -440,7 +440,7 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_with_symbol_object_name - form_for(@post, :object_name => "other_name", :html => { :id => 'create-post' }) do |f| + form_for(@post, :as => "other_name", :html => { :id => 'create-post' }) do |f| concat f.label(:title) concat f.text_field(:title) concat f.text_area(:body) @@ -504,12 +504,10 @@ class FormHelperTest < ActionView::TestCase end def test_form_for_without_object - assert_deprecated do - form_for(:post, :html => { :id => 'create-post' }) do |f| - concat f.text_field(:title) - concat f.text_area(:body) - concat f.check_box(:secret) - end + form_for(:post, :html => { :id => 'create-post' }) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -603,10 +601,8 @@ class FormHelperTest < ActionView::TestCase def test_submit_without_object_and_locale_strings old_locale, I18n.locale = I18n.locale, :submit - assert_deprecated do - form_for(:post) do |f| - concat f.submit :class => "extra" - end + form_for(:post) do |f| + concat f.submit :class => "extra" end expected = "
" + -- cgit v1.2.3 From ea2c5fa8046e2f953b366adc94f0610ccfea0828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Apr 2010 00:12:28 +0200 Subject: A new application now comes with a layout and a stylesheet. --- railties/CHANGELOG | 2 + railties/lib/rails/generators.rb | 2 - .../generators/erb/scaffold/scaffold_generator.rb | 6 -- .../erb/scaffold/templates/layout.html.erb | 17 ------ .../rails/generators/rails/app/app_generator.rb | 2 +- .../app/controllers/application_controller.rb | 1 + .../templates/app/views/layouts/.empty_directory | 0 .../app/views/layouts/application.html.erb | 17 ++++++ .../templates/public/stylesheets/.empty_directory | 0 .../templates/public/stylesheets/application.css | 65 ++++++++++++++++++++++ .../rails/scaffold/scaffold_generator.rb | 1 - .../lib/rails/generators/rails/stylesheets/USAGE | 5 -- .../rails/stylesheets/stylesheets_generator.rb | 9 --- .../rails/stylesheets/templates/scaffold.css | 65 ---------------------- railties/test/generators/app_generator_test.rb | 7 +++ .../scaffold_controller_generator_test.rb | 1 - .../test/generators/scaffold_generator_test.rb | 8 --- .../test/generators/stylesheets_generator_test.rb | 17 ------ 18 files changed, 93 insertions(+), 132 deletions(-) delete mode 100644 railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb delete mode 100644 railties/lib/rails/generators/rails/app/templates/app/views/layouts/.empty_directory create mode 100644 railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb delete mode 100644 railties/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory create mode 100644 railties/lib/rails/generators/rails/app/templates/public/stylesheets/application.css delete mode 100644 railties/lib/rails/generators/rails/stylesheets/USAGE delete mode 100644 railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb delete mode 100644 railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css delete mode 100644 railties/test/generators/stylesheets_generator_test.rb diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 82684e4614..261333628d 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,3 +1,5 @@ +* A new application now comes with a layout and a stylesheet. [JV] + * Renamed config.cookie_secret to config.secret_token and pass it as env key. [JV] *Rails 3.0.0 [beta 2] (April 1st, 2010)* diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 146974ae20..ebb64d5025 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -26,7 +26,6 @@ module Rails :orm => '-o', :resource_controller => '-c', :scaffold_controller => '-c', - :stylesheets => '-y', :template_engine => '-e', :test_framework => '-t' }, @@ -56,7 +55,6 @@ module Rails :resource_controller => :controller, :scaffold_controller => :scaffold_controller, :singleton => false, - :stylesheets => true, :test_framework => nil, :template_engine => :erb }, diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb index f607e580a5..ee591d85b3 100644 --- a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -25,12 +25,6 @@ module Erb end end - def copy_layout_file - return unless options[:layout] - template filename_with_extensions(:layout), - File.join("app/views/layouts", controller_class_path, filename_with_extensions(controller_file_name)) - end - protected def available_views diff --git a/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb deleted file mode 100644 index 3f64be0c45..0000000000 --- a/railties/lib/rails/generators/erb/scaffold/templates/layout.html.erb +++ /dev/null @@ -1,17 +0,0 @@ - - - - <%= controller_class_name %>: <%%= controller.action_name %> - <%%= stylesheet_link_tag 'scaffold' %> - <%%= javascript_include_tag :defaults %> - <%%= csrf_meta_tag %> - - - -

<%%= notice %>

-

<%%= alert %>

- -<%%= yield %> - - - diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index fbad3c9ef1..df6e98f38d 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -137,7 +137,7 @@ module Rails::Generators end def create_public_stylesheets_files - empty_directory_with_gitkeep "public/stylesheets" + directory "public/stylesheets" end def create_prototype_files 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 e8065d9505..f2569b3a77 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 @@ -1,3 +1,4 @@ class ApplicationController < ActionController::Base protect_from_forgery + layout 'application' end diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/.empty_directory b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/.empty_directory deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb new file mode 100644 index 0000000000..7a08d5b957 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb @@ -0,0 +1,17 @@ + + + + <%= controller_name.humanize %>: <%= action_name %> + <%= stylesheet_link_tag 'application' %> + <%= javascript_include_tag :defaults %> + <%= csrf_meta_tag %> + + + +

<%= notice %>

+

<%= alert %>

+ +<%= yield %> + + + diff --git a/railties/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory b/railties/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/railties/lib/rails/generators/rails/app/templates/public/stylesheets/application.css b/railties/lib/rails/generators/rails/app/templates/public/stylesheets/application.css new file mode 100644 index 0000000000..ea3dc9b8b5 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/public/stylesheets/application.css @@ -0,0 +1,65 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +div.field, div.actions { + margin-bottom: 10px; +} + +.notice { + color: green; +} + +.alert { + color: red; +} + +.fieldWithErrors { + padding: 2px; + background-color: red; + display: table; +} + +#errorExplanation { + width: 400px; + border: 2px solid red; + padding: 7px; + padding-bottom: 12px; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#errorExplanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + background-color: #c00; + color: #fff; +} + +#errorExplanation p { + color: #333; + margin-bottom: 0; + padding: 5px; +} + +#errorExplanation ul li { + font-size: 12px; + list-style: square; +} diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb index 779f933785..bd156f399c 100644 --- a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -7,7 +7,6 @@ module Rails remove_class_option :actions hook_for :scaffold_controller, :required => true - hook_for :stylesheets end end end diff --git a/railties/lib/rails/generators/rails/stylesheets/USAGE b/railties/lib/rails/generators/rails/stylesheets/USAGE deleted file mode 100644 index 59e5495d0b..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/USAGE +++ /dev/null @@ -1,5 +0,0 @@ -Description: - Copies scaffold stylesheets to public/stylesheets/. - -Examples: - `rails generate stylesheets` diff --git a/railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb b/railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb deleted file mode 100644 index ce68443c39..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Rails - module Generators - class StylesheetsGenerator < Base - def copy_stylesheets_file - template "scaffold.css", "public/stylesheets/scaffold.css" if behavior == :invoke - end - end - end -end diff --git a/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css b/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css deleted file mode 100644 index ea3dc9b8b5..0000000000 --- a/railties/lib/rails/generators/rails/stylesheets/templates/scaffold.css +++ /dev/null @@ -1,65 +0,0 @@ -body { background-color: #fff; color: #333; } - -body, p, ol, ul, td { - font-family: verdana, arial, helvetica, sans-serif; - font-size: 13px; - line-height: 18px; -} - -pre { - background-color: #eee; - padding: 10px; - font-size: 11px; -} - -a { color: #000; } -a:visited { color: #666; } -a:hover { color: #fff; background-color:#000; } - -div.field, div.actions { - margin-bottom: 10px; -} - -.notice { - color: green; -} - -.alert { - color: red; -} - -.fieldWithErrors { - padding: 2px; - background-color: red; - display: table; -} - -#errorExplanation { - width: 400px; - border: 2px solid red; - padding: 7px; - padding-bottom: 12px; - margin-bottom: 20px; - background-color: #f0f0f0; -} - -#errorExplanation h2 { - text-align: left; - font-weight: bold; - padding: 5px 5px 5px 15px; - font-size: 12px; - margin: -7px; - background-color: #c00; - color: #fff; -} - -#errorExplanation p { - color: #333; - margin-bottom: 0; - padding: 5px; -} - -#errorExplanation ul li { - font-size: 12px; - list-style: square; -} diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 95ca2acf2c..64579c1205 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -51,6 +51,13 @@ class AppGeneratorTest < Rails::Generators::TestCase ).each{ |path| assert_file path } end + def test_application_controller_and_layout_files + run_generator + assert_file "app/controllers/application_controller.rb", /layout 'application'/ + assert_file "app/views/layouts/application.html.erb", /stylesheet_link_tag 'application'/ + assert_file "public/stylesheets/application.css" + end + def test_name_collision_raises_an_error content = capture(:stderr){ run_generator [File.join(destination_root, "generate")] } assert_equal "Invalid application name generate. Please give a name which does not match one of the reserved rails words.\n", content diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 77ed63b1bb..f5af137ced 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -66,7 +66,6 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase new show ).each { |view| assert_file "app/views/users/#{view}.html.erb" } - assert_file "app/views/layouts/users.html.erb" end def test_functional_tests diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 6cf2efca45..3cad65f503 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -70,14 +70,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase show _form ).each { |view| assert_file "app/views/product_lines/#{view}.html.erb" } - assert_file "app/views/layouts/product_lines.html.erb" # Helpers assert_file "app/helpers/product_lines_helper.rb" assert_file "test/unit/helpers/product_lines_helper_test.rb" - - # Stylesheets - assert_file "public/stylesheets/scaffold.css" end def test_scaffold_on_revoke @@ -101,13 +97,9 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Views assert_no_file "app/views/product_lines" - assert_no_file "app/views/layouts/product_lines.html.erb" # Helpers assert_no_file "app/helpers/product_lines_helper.rb" assert_no_file "test/unit/helpers/product_lines_helper_test.rb" - - # Stylesheets (should not be removed) - assert_file "public/stylesheets/scaffold.css" end end diff --git a/railties/test/generators/stylesheets_generator_test.rb b/railties/test/generators/stylesheets_generator_test.rb deleted file mode 100644 index aaeb686daa..0000000000 --- a/railties/test/generators/stylesheets_generator_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'generators/generators_test_helper' -require 'rails/generators/rails/stylesheets/stylesheets_generator' - -class StylesheetsGeneratorTest < Rails::Generators::TestCase - include GeneratorsTestHelper - - def test_copy_stylesheets - run_generator - assert_file "public/stylesheets/scaffold.css" - end - - def test_stylesheets_are_not_deleted_on_revoke - run_generator - run_generator [], :behavior => :revoke - assert_file "public/stylesheets/scaffold.css" - end -end -- cgit v1.2.3 From 570c54c39acc702eefcee9589ebe02d85173a1c1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 5 Apr 2010 17:01:33 -0500 Subject: Fix cookie access in integration tests with other host names --- .../lib/action_dispatch/testing/integration.rb | 28 ++++++++++---- actionpack/test/controller/integration_test.rb | 45 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 286797ba20..64eb6d8de7 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -137,7 +137,10 @@ module ActionDispatch end # The hostname used in the last request. - attr_accessor :host + def host + @host || DEFAULT_HOST + end + attr_writer :host # The remote_addr used in the last request. attr_accessor :remote_addr @@ -148,7 +151,7 @@ module ActionDispatch # A map of the cookies returned by the last response, and which will be # sent with the next request. def cookies - @mock_session.cookie_jar + _mock_session.cookie_jar end # A reference to the controller instance used by the last request. @@ -189,8 +192,8 @@ module ActionDispatch # session.reset! def reset! @https = false - @mock_session = Rack::MockSession.new(@app, DEFAULT_HOST) @controller = @request = @response = nil + @_mock_session = nil @request_count = 0 self.host = DEFAULT_HOST @@ -234,6 +237,9 @@ module ActionDispatch end private + def _mock_session + @_mock_session ||= Rack::MockSession.new(@app, host) + end # Performs the actual request. def process(method, path, parameters = nil, rack_environment = nil) @@ -254,7 +260,7 @@ module ActionDispatch :method => method, :params => parameters, - "SERVER_NAME" => host, + "SERVER_NAME" => host.split(':')[0], "SERVER_PORT" => (https? ? "443" : "80"), "HTTPS" => https? ? "on" : "off", "rack.url_scheme" => https? ? "https" : "http", @@ -266,17 +272,25 @@ module ActionDispatch "HTTP_ACCEPT" => accept } - session = Rack::Test::Session.new(@mock_session) + session = Rack::Test::Session.new(_mock_session) (rack_environment || {}).each do |key, value| env[key] = value end - session.request(path, env) + # NOTE: rack-test v0.5 doesn't build a default uri correctly + # Make sure requested path is always a full uri + uri = URI.parse('/') + uri.scheme ||= env['rack.url_scheme'] + uri.host ||= env['SERVER_NAME'] + uri.port ||= env['SERVER_PORT'].try(:to_i) + uri += path + + session.request(uri.to_s, env) @request_count += 1 @request = ActionDispatch::Request.new(session.last_request.env) - response = @mock_session.last_response + response = _mock_session.last_response @response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body) @html_document = nil diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 14c0c3708b..20dc96d38d 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -245,6 +245,15 @@ class IntegrationProcessTest < ActionController::IntegrationTest render :text => "Gone", :status => 410 end + def set_cookie + cookies["foo"] = 'bar' + head :ok + end + + def get_cookie + render :text => cookies["foo"] + end + def redirect redirect_to :action => "get" end @@ -292,6 +301,42 @@ class IntegrationProcessTest < ActionController::IntegrationTest end end + test 'cookie persist to next request' do + with_test_route_set do + get '/set_cookie' + assert_response :success + + assert_equal "foo=bar; path=/", headers["Set-Cookie"] + assert_equal({"foo"=>"bar"}, cookies.to_hash) + + get '/get_cookie' + assert_response :success + assert_equal "bar", body + + assert_equal nil, headers["Set-Cookie"] + assert_equal({"foo"=>"bar"}, cookies.to_hash) + end + end + + test 'cookie persist to next request on another domain' do + with_test_route_set do + host! "37s.backpack.test" + + get '/set_cookie' + assert_response :success + + assert_equal "foo=bar; path=/", headers["Set-Cookie"] + assert_equal({"foo"=>"bar"}, cookies.to_hash) + + get '/get_cookie' + assert_response :success + assert_equal "bar", body + + assert_equal nil, headers["Set-Cookie"] + assert_equal({"foo"=>"bar"}, cookies.to_hash) + end + end + def test_redirect with_test_route_set do get '/redirect' -- cgit v1.2.3 From 542946a0c2e8a694eac58a9cf50c8b9c734b5ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Apr 2010 00:32:03 +0200 Subject: Update the documentation for Engine and Railtie. --- railties/lib/rails/engine.rb | 8 ++++---- railties/lib/rails/railtie.rb | 28 +++------------------------- 2 files changed, 7 insertions(+), 29 deletions(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 54c97258ce..c284840a38 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -20,7 +20,6 @@ module Rails # # lib/my_engine.rb # module MyEngine # class Engine < Rails::Engine - # engine_name :my_engine # end # end # @@ -38,11 +37,12 @@ module Rails # Example: # # class MyEngine < Rails::Engine - # # config.middleware is shared configururation - # config.middleware.use MyEngine::Middleware - # # # Add a load path for this specific Engine # config.load_paths << File.expand_path("../lib/some/path", __FILE__) + # + # initializer "my_engine.add_middleware" do |app| + # app.middlewares.use MyEngine::Middleware + # end # end # # == Paths diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 0d68abb323..6ac6be092e 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -39,7 +39,6 @@ module Rails # # lib/my_gem/railtie.rb # module MyGem # class Railtie < Rails::Railtie - # railtie_name :mygem # end # end # @@ -51,24 +50,8 @@ module Rails # # module MyGem # class Railtie < Rails::Railtie - # railtie_name :mygem # end # end - # - # * Make sure your Gem loads the railtie.rb file if Rails is loaded first, an easy - # way to check is by checking for the Rails constant which will exist if Rails - # has started: - # - # # lib/my_gem.rb - # module MyGem - # require 'lib/my_gem/railtie' if defined?(Rails) - # end - # - # * Or instead of doing the require automatically, you can ask your users to require - # it for you in their Gemfile: - # - # # #{USER_RAILS_ROOT}/Gemfile - # gem "my_gem", :require_as => ["my_gem", "my_gem/railtie"] # # == Initializers # @@ -82,13 +65,11 @@ module Rails # end # # If specified, the block can also receive the application object, in case you - # need to access some application specific configuration: + # need to access some application specific configuration, like middleware: # # class MyRailtie < Rails::Railtie # initializer "my_railtie.configure_rails_initialization" do |app| - # if app.config.cache_classes - # # some initialization behavior - # end + # app.middlewares.use MyRailtie::Middleware # end # end # @@ -104,9 +85,6 @@ module Rails # # Customize the ORM # config.generators.orm :my_railtie_orm # - # # Add a middleware - # config.middlewares.use MyRailtie::Middleware - # # # Add a to_prepare block which is executed once in production # # and before which request in development # config.to_prepare do @@ -160,7 +138,7 @@ module Rails # By registering it: # # class MyRailtie < Railtie - # subscriber MyRailtie::Subscriber.new + # subscriber :my_gem, MyRailtie::Subscriber.new # end # # Take a look in Rails::Subscriber docs for more information. -- cgit v1.2.3 From 4bacc2a66d26b2e257718bb44c89583f79c138e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Apr 2010 00:32:03 +0200 Subject: Update the documentation for Engine and Railtie. --- railties/lib/rails/engine.rb | 8 ++++---- railties/lib/rails/railtie.rb | 28 +++------------------------- 2 files changed, 7 insertions(+), 29 deletions(-) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 54c97258ce..c284840a38 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -20,7 +20,6 @@ module Rails # # lib/my_engine.rb # module MyEngine # class Engine < Rails::Engine - # engine_name :my_engine # end # end # @@ -38,11 +37,12 @@ module Rails # Example: # # class MyEngine < Rails::Engine - # # config.middleware is shared configururation - # config.middleware.use MyEngine::Middleware - # # # Add a load path for this specific Engine # config.load_paths << File.expand_path("../lib/some/path", __FILE__) + # + # initializer "my_engine.add_middleware" do |app| + # app.middlewares.use MyEngine::Middleware + # end # end # # == Paths diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 0d68abb323..6ac6be092e 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -39,7 +39,6 @@ module Rails # # lib/my_gem/railtie.rb # module MyGem # class Railtie < Rails::Railtie - # railtie_name :mygem # end # end # @@ -51,24 +50,8 @@ module Rails # # module MyGem # class Railtie < Rails::Railtie - # railtie_name :mygem # end # end - # - # * Make sure your Gem loads the railtie.rb file if Rails is loaded first, an easy - # way to check is by checking for the Rails constant which will exist if Rails - # has started: - # - # # lib/my_gem.rb - # module MyGem - # require 'lib/my_gem/railtie' if defined?(Rails) - # end - # - # * Or instead of doing the require automatically, you can ask your users to require - # it for you in their Gemfile: - # - # # #{USER_RAILS_ROOT}/Gemfile - # gem "my_gem", :require_as => ["my_gem", "my_gem/railtie"] # # == Initializers # @@ -82,13 +65,11 @@ module Rails # end # # If specified, the block can also receive the application object, in case you - # need to access some application specific configuration: + # need to access some application specific configuration, like middleware: # # class MyRailtie < Rails::Railtie # initializer "my_railtie.configure_rails_initialization" do |app| - # if app.config.cache_classes - # # some initialization behavior - # end + # app.middlewares.use MyRailtie::Middleware # end # end # @@ -104,9 +85,6 @@ module Rails # # Customize the ORM # config.generators.orm :my_railtie_orm # - # # Add a middleware - # config.middlewares.use MyRailtie::Middleware - # # # Add a to_prepare block which is executed once in production # # and before which request in development # config.to_prepare do @@ -160,7 +138,7 @@ module Rails # By registering it: # # class MyRailtie < Railtie - # subscriber MyRailtie::Subscriber.new + # subscriber :my_gem, MyRailtie::Subscriber.new # end # # Take a look in Rails::Subscriber docs for more information. -- cgit v1.2.3 From 0087d0748b9e47816245deb4ba3a570b77688282 Mon Sep 17 00:00:00 2001 From: wycats Date: Mon, 5 Apr 2010 16:07:35 -0700 Subject: Include author in changelog --- actionpack/CHANGELOG | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index c4ebedf1b0..5b5d01b124 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,17 +1,17 @@ *Rails 3.0.0 [Edge] (pending)* * Changed the object used in routing constraints to be an instance of - ActionDispatch::Request rather than Rack::Request + ActionDispatch::Request rather than Rack::Request [YK] * Changed ActionDispatch::Request#method to return a String, to be compatible with Rack::Request. Added ActionDispatch::Request#method_symbol to - return a symbol form of the request method. + return a symbol form of the request method. [YK] * Changed ActionDispatch::Request#method to return the original method and #request_method to return the overridden method in the case of methodoverride being used (this means that #method returns "HEAD" and #request_method returns "GET" in HEAD requests). This - is for compatibility with Rack::Request + is for compatibility with Rack::Request [YK] *Rails 3.0.0 [beta 2] (April 1st, 2010)* -- cgit v1.2.3 From f8730e5ce6bba4de7639ac09c6c193458038f748 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 5 Apr 2010 16:07:44 -0700 Subject: Added all the new HTML5 form types as individual form tag methods (search, url, number, etc) (Closes #3646) [Stephen Celis] --- actionpack/CHANGELOG | 3 ++ actionpack/lib/action_view/helpers/form_helper.rb | 58 ++++++++++++++++++++ .../lib/action_view/helpers/form_tag_helper.rb | 63 ++++++++++++++++++++++ actionpack/test/template/form_helper_test.rb | 30 +++++++++++ actionpack/test/template/form_tag_helper_test.rb | 30 +++++++++++ 5 files changed, 184 insertions(+) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index c4ebedf1b0..46210690cd 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0.0 [Edge] (pending)* +* Added all the new HTML5 form types as individual form tag methods (search, url, number, etc) #3646 [Stephen Celis] + * Changed the object used in routing constraints to be an instance of ActionDispatch::Request rather than Rack::Request @@ -13,6 +15,7 @@ "HEAD" and #request_method returns "GET" in HEAD requests). This is for compatibility with Rack::Request + *Rails 3.0.0 [beta 2] (April 1st, 2010)* * #concat is now deprecated in favor of using <%= %> helpers [YK] diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 45f41edcac..932711f9de 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -784,6 +784,56 @@ module ActionView def radio_button(object_name, method, tag_value, options = {}) InstanceTag.new(object_name, method, self, options.delete(:object)).to_radio_button_tag(tag_value, options) end + + # Returns a text_field of type "search". + def search_field(object_name, method, options = {}) + options = options.stringify_keys + + if options["autosave"] + if options["autosave"] == true + options["autosave"] = request.host.split(".").reverse.join(".") + end + options["results"] ||= 10 + end + + if options["onsearch"] + options["incremental"] = true unless options.has_key?("incremental") + end + + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("search", options) + end + + # Returns a text_field of type "tel". + def telephone_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("tel", options) + end + alias phone_field telephone_field + + # Returns a text_field of type "url". + def url_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("url", options) + end + + # Returns a text_field of type "email". + def email_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("email", options) + end + + # Returns an input tag of type "number". + # + # ==== Options + # * Accepts same options as number_field_tag + def number_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_number_field_tag("number", options) + end + + # Returns an input tag of type "range". + # + # ==== Options + # * Accepts same options as range_field_tag + def range_field(object_name, method, options = {}) + InstanceTag.new(object_name, method, self, options.delete(:object)).to_number_field_tag("range", options) + end end module InstanceTagMethods #:nodoc: @@ -847,6 +897,14 @@ module ActionView tag("input", options) end + def to_number_field_tag(field_type, options = {}) + options = options.stringify_keys + if range = options.delete("in") || options.delete("within") + options.update("min" => range.min, "max" => range.max) + end + to_input_field_tag(field_type, options) + end + def to_radio_button_tag(tag_value, options = {}) options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys) options["type"] = "radio" diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 0d47c6eecb..10660ecec1 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -457,6 +457,69 @@ module ActionView output.safe_concat("") end + # Creates a text field of type "search". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def search_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "search")) + end + + # Creates a text field of type "tel". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def telephone_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "tel")) + end + alias phone_field_tag telephone_field_tag + + # Creates a text field of type "url". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def url_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "url")) + end + + # Creates a text field of type "email". + # + # ==== Options + # * Accepts the same options as text_field_tag. + def email_field_tag(name, value = nil, options = {}) + text_field_tag(name, value, options.stringify_keys.update("type" => "email")) + end + + # Creates a number field. + # + # ==== Options + # * :min - The minimum acceptable value. + # * :max - The maximum acceptable value. + # * :in - A range specifying the :min and + # :max values. + # * :step - The acceptable value granularity. + # * Otherwise accepts the same options as text_field_tag. + # + # ==== Examples + # number_field_tag 'quantity', nil, :in => 1...10 + # => + def number_field_tag(name, value = nil, options = {}) + options = options.stringify_keys + options["type"] ||= "number" + if range = options.delete("in") || options.delete("within") + options.update("min" => range.min, "max" => range.max) + end + text_field_tag(name, value, options) + end + + # Creates a range form element. + # + # ==== Options + # * Accepts the same options as number_field_tag. + def range_field_tag(name, value = nil, options = {}) + number_field_tag(name, value, options.stringify_keys.update("type" => "range")) + end + private def html_options_for_form(url_for_options, options, *parameters_for_url) returning options.stringify_keys do |html_options| diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 88014a6564..d62406c2d3 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -349,6 +349,36 @@ class FormHelperTest < ActionView::TestCase ) end + def search_field + expected = %{} + assert_dom_equal(expected, search_field("contact", "notes_query")) + end + + def test_telephone_field + expected = %{} + assert_dom_equal(expected, telephone_field("user", "cell")) + end + + def test_url_field + expected = %{} + assert_dom_equal(expected, url_field("user", "homepage")) + end + + def test_email_field + expected = %{} + assert_dom_equal(expected, email_field("user", "address")) + end + + def test_number_field + expected = %{} + assert_dom_equal(expected, number_field("order", "quantity", :in => 1...10)) + end + + def test_range_input + expected = %{} + assert_dom_equal(expected, range_field("hifi", "volume", :in => 0..11, :step => 0.1)) + end + def test_explicit_name assert_dom_equal( '', text_field("post", "title", "name" => "dont guess") diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 3b8760351e..ef612b879b 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -335,6 +335,36 @@ class FormTagHelperTest < ActionView::TestCase ) end + def test_search_field_tag + expected = %{} + assert_dom_equal(expected, search_field_tag("query")) + end + + def telephone_field_tag + expected = %{} + assert_dom_equal(expected, telephone_field_tag("cell")) + end + + def test_url_field_tag + expected = %{} + assert_dom_equal(expected, url_field_tag("homepage")) + end + + def test_email_field_tag + expected = %{} + assert_dom_equal(expected, email_field_tag("address")) + end + + def test_number_field_tag + expected = %{} + assert_dom_equal(expected, number_field_tag("quantity", nil, :in => 1...10)) + end + + def test_range_input_tag + expected = %{} + assert_dom_equal(expected, range_field_tag("volume", nil, :in => 0..11, :step => 0.1)) + end + def test_pass assert_equal 1, 1 end -- cgit v1.2.3 From 4f1d0f6b7e5718d629795b94ac12616241791334 Mon Sep 17 00:00:00 2001 From: Simon Effenberg Date: Wed, 31 Mar 2010 01:24:44 +0200 Subject: remove_index now uses quote_table_name() [#4300 state:resolved] Signed-off-by: Michael Koziarski --- .../lib/active_record/connection_adapters/abstract/schema_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 86ba7d72c3..6d4ab501fa 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -291,7 +291,7 @@ module ActiveRecord # Remove the index named by_branch_party in the accounts table. # remove_index :accounts, :name => :by_branch_party def remove_index(table_name, options = {}) - execute "DROP INDEX #{quote_column_name(index_name(table_name, options))} ON #{table_name}" + execute "DROP INDEX #{quote_column_name(index_name(table_name, options))} ON #{quote_table_name(table_name)}" end def index_name(table_name, options) #:nodoc: -- cgit v1.2.3 From 77b238d169b26f8d3a517f89f66d740b7eb52a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Apr 2010 11:51:30 +0200 Subject: Remove :layout option from generators. --- railties/lib/rails/generators.rb | 5 ----- railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb | 1 - 2 files changed, 6 deletions(-) diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index ebb64d5025..5f93dbf18f 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -41,14 +41,9 @@ module Rails } DEFAULT_OPTIONS = { - :erb => { - :layout => true - }, - :rails => { :force_plural => false, :helper => true, - :layout => true, :orm => nil, :integration_tool => nil, :performance_tool => nil, diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb index ee591d85b3..2db7f7bbf3 100644 --- a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -8,7 +8,6 @@ module Erb argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" - class_option :layout, :type => :boolean class_option :singleton, :type => :boolean, :desc => "Supply to skip index view" def create_root_folder -- cgit v1.2.3 From 46e2a44ee14a19cc2392d8ca174529ad70ee36dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Apr 2010 11:53:29 +0200 Subject: Rename search_field to test_search_field (ht: Piotr Usewicz) --- actionpack/test/template/form_helper_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index d62406c2d3..64dfbde0b0 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -349,7 +349,7 @@ class FormHelperTest < ActionView::TestCase ) end - def search_field + def test_search_field expected = %{} assert_dom_equal(expected, search_field("contact", "notes_query")) end -- cgit v1.2.3 From af130575249571464ec984efa84fcea1267e8cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Apr 2010 23:08:21 +0200 Subject: Use raw in yield since templates engines should not be required to html_safe their contents. --- .../rails/app/templates/app/views/layouts/application.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb index 7a08d5b957..6b87d9d3ec 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb @@ -11,7 +11,7 @@

<%= notice %>

<%= alert %>

-<%= yield %> +<%=raw yield %> -- cgit v1.2.3