diff options
-rw-r--r-- | actionpack/lib/action_controller/railtie.rb | 2 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/http/rack_cache.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/sprockets/railtie.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 7 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/validates.rb | 8 | ||||
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 8 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/associated.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/migration/logger_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/validations/association_validation_test.rb | 17 | ||||
-rw-r--r-- | activesupport/lib/active_support/tagged_logging.rb | 5 | ||||
-rw-r--r-- | railties/guides/source/configuring.textile | 4 | ||||
-rw-r--r-- | railties/lib/rails.rb | 7 | ||||
-rw-r--r-- | railties/lib/rails/application/bootstrap.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/deprecation.rb | 39 | ||||
-rw-r--r-- | railties/test/application/middleware_test.rb | 4 |
17 files changed, 106 insertions, 25 deletions
diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index fb810c41b1..a288e69649 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -14,7 +14,7 @@ module ActionController end initializer "action_controller.initialize_framework_caches" do - ActiveSupport.on_load(:action_controller) { self.cache_store ||= RAILS_CACHE } + ActiveSupport.on_load(:action_controller) { self.cache_store ||= Rails.cache } end initializer "action_controller.assets_config", :group => :all do |app| diff --git a/actionpack/lib/action_dispatch/http/rack_cache.rb b/actionpack/lib/action_dispatch/http/rack_cache.rb index cc8edee300..003ae4029d 100644 --- a/actionpack/lib/action_dispatch/http/rack_cache.rb +++ b/actionpack/lib/action_dispatch/http/rack_cache.rb @@ -8,8 +8,7 @@ module ActionDispatch new end - # TODO: Finally deal with the RAILS_CACHE global - def initialize(store = RAILS_CACHE) + def initialize(store = Rails.cache) @store = store end @@ -33,7 +32,7 @@ module ActionDispatch new end - def initialize(store = RAILS_CACHE) + def initialize(store = Rails.cache) @store = store end diff --git a/actionpack/lib/sprockets/railtie.rb b/actionpack/lib/sprockets/railtie.rb index a7eb03acaf..9f4151e4b3 100644 --- a/actionpack/lib/sprockets/railtie.rb +++ b/actionpack/lib/sprockets/railtie.rb @@ -21,7 +21,7 @@ module Sprockets require 'sprockets' app.assets = Sprockets::Environment.new(app.root.to_s) do |env| - env.logger = ::Rails.logger + env.logger = config.assets.logger || ::Rails.logger env.version = ::Rails.env + "-#{config.assets.version}" if config.assets.cache_store != false diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index c6535082d3..5cd8f77f0d 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -78,6 +78,11 @@ module ActiveModel @messages = ActiveSupport::OrderedHash.new end + def initialize_dup(other) + @messages = other.messages.dup + super + end + # Clear the messages def clear messages.clear @@ -118,7 +123,7 @@ module ActiveModel # p.errors[:name] = "must be set" # p.errors[:name] # => ['must be set'] def []=(attribute, error) - self[attribute.to_sym] << error + self[attribute] << error end # Iterates through each error key, value pair in the error messages hash. diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index 8e09f6ac35..3713fc828e 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -59,7 +59,7 @@ module ActiveModel # # validates :name, :'film/title' => true # - # The validators hash can also handle regular expressions, ranges, + # The validators hash can also handle regular expressions, ranges, # arrays and strings in shortcut form, e.g. # # validates :email, :format => /@/ @@ -70,7 +70,7 @@ module ActiveModel # validator's initializer as +options[:in]+ while other types including # regular expressions and strings are passed as +options[:with]+ # - # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+ + # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+, +:allow_nil+ and +:strict+ # can be given to one specific validator, as a hash: # # validates :password, :presence => { :if => :password_required? }, :confirmation => true @@ -80,7 +80,7 @@ module ActiveModel # validates :password, :presence => true, :confirmation => true, :if => :password_required? # def validates(*attributes) - defaults = attributes.extract_options! + defaults = attributes.extract_options!.dup validations = defaults.slice!(*_validates_default_keys) raise ArgumentError, "You need to supply at least one attribute" if attributes.empty? @@ -102,7 +102,7 @@ module ActiveModel end # This method is used to define validation that can not be corrected by end user - # and is considered exceptional. + # and is considered exceptional. # So each validator defined with bang or <tt>:strict</tt> option set to <tt>true</tt> # will always raise <tt>ActiveModel::InternalValidationFailed</tt> instead of adding error # when validation fails diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 4edeece3e8..ab80f193b6 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -40,6 +40,14 @@ class ErrorsTest < ActiveModel::TestCase assert errors.include?(:foo), 'errors should include :foo' end + def test_dup + errors = ActiveModel::Errors.new(self) + errors[:foo] = 'bar' + errors_dup = errors.dup + errors_dup[:bar] = 'omg' + assert_not_same errors_dup.messages, errors.messages + end + def test_has_key? errors = ActiveModel::Errors.new(self) errors[:foo] = 'omg' diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 2f4376bd41..ed4d8fcdca 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -330,4 +330,10 @@ class ValidationsTest < ActiveModel::TestCase Topic.new.valid? end end + + def test_does_not_modify_options_argument + options = {:presence => true} + Topic.validates :title, options + assert_equal({:presence => true}, options) + end end diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 8031912669..d468663084 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -295,7 +295,7 @@ module ActiveRecord def association_valid?(reflection, record) return true if record.destroyed? || record.marked_for_destruction? - unless valid = record.valid? + unless valid = record.valid?(validation_context) if reflection.options[:autosave] record.errors.each do |attribute, message| attribute = "#{reflection.name}.#{attribute}" diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb index 9f072c4c39..afce149da9 100644 --- a/activerecord/lib/active_record/validations/associated.rb +++ b/activerecord/lib/active_record/validations/associated.rb @@ -2,7 +2,7 @@ module ActiveRecord module Validations class AssociatedValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any? + if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?(record.validation_context) }.any? record.errors.add(attribute, :invalid, options.merge(:value => value)) end end diff --git a/activerecord/test/cases/migration/logger_test.rb b/activerecord/test/cases/migration/logger_test.rb index 8466562daf..ee0c20747e 100644 --- a/activerecord/test/cases/migration/logger_test.rb +++ b/activerecord/test/cases/migration/logger_test.rb @@ -3,13 +3,16 @@ require "cases/helper" module ActiveRecord class Migration class LoggerTest < ActiveRecord::TestCase + # mysql can't roll back ddl changes + self.use_transactional_fixtures = false + Migration = Struct.new(:name, :version) do def migrate direction # do nothing end end - def initialize(*args) + def setup super ActiveRecord::SchemaMigration.create_table ActiveRecord::SchemaMigration.delete_all diff --git a/activerecord/test/cases/validations/association_validation_test.rb b/activerecord/test/cases/validations/association_validation_test.rb index f155b9bc40..c72b7b35cd 100644 --- a/activerecord/test/cases/validations/association_validation_test.rb +++ b/activerecord/test/cases/validations/association_validation_test.rb @@ -118,4 +118,21 @@ class AssociationValidationTest < ActiveRecord::TestCase end end + def test_validates_associated_models_in_the_same_context + Topic.validates_presence_of :title, :on => :custom_context + Topic.validates_associated :replies + Reply.validates_presence_of :title, :on => :custom_context + + t = Topic.new('title' => '') + r = t.replies.new('title' => '') + + assert t.valid? + assert !t.valid?(:custom_context) + + t.title = "Longer" + assert !t.valid?(:custom_context), "Should NOT be valid if the associated object is not valid in the same context." + + r.title = "Longer" + assert t.valid?(:custom_context), "Should be valid if the associated object is not valid in the same context." + end end diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index dc3ca25938..4e2e1a1ff5 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -14,7 +14,6 @@ module ActiveSupport class TaggedLogging def initialize(logger) @logger = logger - @tags = Hash.new { |h,k| h[k] = [] } end def tagged(*new_tags) @@ -39,7 +38,7 @@ module ActiveSupport end def flush - @tags.delete(Thread.current) + current_tags.clear @logger.flush if @logger.respond_to?(:flush) end @@ -57,7 +56,7 @@ module ActiveSupport end def current_tags - @tags[Thread.current] + Thread.current[:activesupport_tagged_logging_tags] ||= [] end end end diff --git a/railties/guides/source/configuring.textile b/railties/guides/source/configuring.textile index 3153cac2f9..fb8031b16d 100644 --- a/railties/guides/source/configuring.textile +++ b/railties/guides/source/configuring.textile @@ -533,7 +533,7 @@ Serves as a placeholder so that +:load_environment_config+ can be defined to run *+initialize_logger+* Initializes the logger (an +ActiveSupport::BufferedLogger+ object) for the application and makes it accessible at +Rails.logger+, provided that no initializer inserted before this point has defined +Rails.logger+. -*+initialize_cache+* If +RAILS_CACHE+ isn't set yet, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +RAILS_CACHE+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack. +*+initialize_cache+* If +Rails.cache+ isn't set yet, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +Rails.cache+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack. *+set_clear_dependencies_hook+* Provides a hook for +active_record.set_dispatch_hooks+ to use, which will run before this initializer. This initializer -- which runs only if +cache_classes+ is set to +false+ -- uses +ActionDispatch::Callbacks.after+ to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request. @@ -571,7 +571,7 @@ The error occurred while evaluating nil.each *+action_controller.logger+* Sets +ActionController::Base.logger+ -- if it's not already set -- to +Rails.logger+. -*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +RAILS_CACHE+. +*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +Rails.cache+. *+action_controller.set_configs+* Sets up Action Controller by using the settings in +config.action_controller+ by +send+'ing the method names as setters to +ActionController::Base+ and passing the values through. diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index 658756ad51..77a09507a8 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -8,6 +8,7 @@ require 'active_support/core_ext/array/extract_options' require 'rails/application' require 'rails/version' +require 'rails/deprecation' require 'active_support/railtie' require 'action_dispatch/railtie' @@ -77,7 +78,11 @@ module Rails end def cache - RAILS_CACHE + @@cache ||= nil + end + + def cache=(cache) + @@cache = cache end # Returns all rails groups for loading based on: diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index f96a7d1772..d55ec982ec 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -50,11 +50,11 @@ module Rails # Initialize cache early in the stack so railties can make use of it. initializer :initialize_cache, :group => :all do - unless defined?(RAILS_CACHE) - silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(config.cache_store) } + unless Rails.cache + Rails.cache = ActiveSupport::Cache.lookup_store(config.cache_store) - if RAILS_CACHE.respond_to?(:middleware) - config.middleware.insert_before("Rack::Runtime", RAILS_CACHE.middleware) + if Rails.cache.respond_to?(:middleware) + config.middleware.insert_before("Rack::Runtime", Rails.cache.middleware) end end end diff --git a/railties/lib/rails/deprecation.rb b/railties/lib/rails/deprecation.rb new file mode 100644 index 0000000000..71adcd61f4 --- /dev/null +++ b/railties/lib/rails/deprecation.rb @@ -0,0 +1,39 @@ +require "active_support/string_inquirer" +require "active_support/basic_object" + +module Rails + module Initializer + def self.run(&block) + klass = Class.new(Rails::Application) + klass.instance_exec(klass.config, &block) + klass.initialize! + end + end + + class DeprecatedConstant < ActiveSupport::BasicObject + def self.deprecate(old, new) + constant = self.new(old, new) + eval "::#{old} = constant" + end + + def initialize(old, new) + @old, @new = old, new + @target = ::Kernel.eval "proc { #{@new} }" + @warned = false + end + + def method_missing(meth, *args, &block) + ::ActiveSupport::Deprecation.warn("#{@old} is deprecated. Please use #{@new}") unless @warned + @warned = true + + target = @target.call + if target.respond_to?(meth) + target.send(meth, *args, &block) + else + super + end + end + end + + DeprecatedConstant.deprecate("RAILS_CACHE", "::Rails.cache") +end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 2f1df1fa59..d0a550d2f0 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -132,13 +132,13 @@ module ApplicationTests assert_equal "Rack::Config", middleware.second end - test "RAILS_CACHE does not respond to middleware" do + test "Rails.cache does not respond to middleware" do add_to_config "config.cache_store = :memory_store" boot! assert_equal "Rack::Runtime", middleware.third end - test "RAILS_CACHE does respond to middleware" do + test "Rails.cache does respond to middleware" do boot! assert_equal "Rack::Runtime", middleware.fourth end |