From eec2d301d4ce9df9c71c1a5aa63053eb970b6818 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 15 Feb 2010 10:20:11 -0600 Subject: Fix test load paths for those not using bundler --- activerecord/lib/active_record.rb | 1 - activerecord/test/cases/helper.rb | 5 ++++- activerecord/test/cases/subscriber_test.rb | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index b79da4565d..99ff0a19a5 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -21,7 +21,6 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ - activesupport_path = File.expand_path('../../../activesupport/lib', __FILE__) $:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.include?(activesupport_path) diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 9e8bfbbee8..3254e7d800 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -1,4 +1,7 @@ -require File.expand_path('../../../../load_paths', __FILE__) +require File.expand_path('../../../../bundler', __FILE__) + +lib = File.expand_path("#{File.dirname(__FILE__)}/../../lib") +$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'config' diff --git a/activerecord/test/cases/subscriber_test.rb b/activerecord/test/cases/subscriber_test.rb index 5328d4468b..3d7a8f5bab 100644 --- a/activerecord/test/cases/subscriber_test.rb +++ b/activerecord/test/cases/subscriber_test.rb @@ -1,3 +1,6 @@ +railties_path = File.expand_path('../../../../railties/lib', __FILE__) +$:.unshift(railties_path) if File.directory?(railties_path) && !$:.include?(railties_path) + require "cases/helper" require "models/developer" require "rails/subscriber/test_helper" @@ -39,4 +42,4 @@ class SubscriberTest < ActiveSupport::TestCase assert_match /CACHE/, @logger.logged(:debug).last assert_match /SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last end -end \ No newline at end of file +end -- cgit v1.2.3 From f0523f72b46db14e2f50c8347a8708734c650f84 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 15 Feb 2010 21:44:30 +0700 Subject: Rename Rails::Subscriber to Rails::LogSubscriber --- activerecord/lib/active_record/base.rb | 2 +- activerecord/lib/active_record/railtie.rb | 4 +-- .../lib/active_record/railties/log_subscriber.rb | 27 ++++++++++++++ .../lib/active_record/railties/subscriber.rb | 27 -------------- activerecord/test/cases/log_subscriber_test.rb | 42 ++++++++++++++++++++++ activerecord/test/cases/subscriber_test.rb | 42 ---------------------- 6 files changed, 72 insertions(+), 72 deletions(-) create mode 100644 activerecord/lib/active_record/railties/log_subscriber.rb delete mode 100644 activerecord/lib/active_record/railties/subscriber.rb create mode 100644 activerecord/test/cases/log_subscriber_test.rb delete mode 100644 activerecord/test/cases/subscriber_test.rb (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f30eba4f06..c6dde078ca 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -552,7 +552,7 @@ module ActiveRecord #:nodoc: def colorize_logging(*args) ActiveSupport::Deprecation.warn "ActiveRecord::Base.colorize_logging and " << "config.active_record.colorize_logging are deprecated. Please use " << - "Rails::Subscriber.colorize_logging or config.colorize_logging instead", caller + "Rails::LogSubscriber.colorize_logging or config.colorize_logging instead", caller end alias :colorize_logging= :colorize_logging diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index e70b0d1bfb..b38bd9a644 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -20,8 +20,8 @@ module ActiveRecord end # TODO If we require the wrong file, the error never comes up. - require "active_record/railties/subscriber" - subscriber ActiveRecord::Railties::Subscriber.new + require "active_record/railties/log_subscriber" + log_subscriber ActiveRecord::Railties::LogSubscriber.new initializer "active_record.initialize_timezone" do ActiveRecord::Base.time_zone_aware_attributes = true diff --git a/activerecord/lib/active_record/railties/log_subscriber.rb b/activerecord/lib/active_record/railties/log_subscriber.rb new file mode 100644 index 0000000000..48b25032ce --- /dev/null +++ b/activerecord/lib/active_record/railties/log_subscriber.rb @@ -0,0 +1,27 @@ +module ActiveRecord + module Railties + class LogSubscriber < Rails::LogSubscriber + def sql(event) + name = '%s (%.1fms)' % [event.payload[:name], event.duration] + sql = event.payload[:sql].squeeze(' ') + + if odd? + name = color(name, :cyan, true) + sql = color(sql, nil, true) + else + name = color(name, :magenta, true) + end + + debug " #{name} #{sql}" + end + + def odd? + @odd_or_even = !@odd_or_even + end + + def logger + ActiveRecord::Base.logger + end + end + end +end \ No newline at end of file diff --git a/activerecord/lib/active_record/railties/subscriber.rb b/activerecord/lib/active_record/railties/subscriber.rb deleted file mode 100644 index fd873dbff8..0000000000 --- a/activerecord/lib/active_record/railties/subscriber.rb +++ /dev/null @@ -1,27 +0,0 @@ -module ActiveRecord - module Railties - class Subscriber < Rails::Subscriber - def sql(event) - name = '%s (%.1fms)' % [event.payload[:name], event.duration] - sql = event.payload[:sql].squeeze(' ') - - if odd? - name = color(name, :cyan, true) - sql = color(sql, nil, true) - else - name = color(name, :magenta, true) - end - - debug " #{name} #{sql}" - end - - def odd? - @odd_or_even = !@odd_or_even - end - - def logger - ActiveRecord::Base.logger - end - end - end -end \ No newline at end of file diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb new file mode 100644 index 0000000000..f0197ddf77 --- /dev/null +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -0,0 +1,42 @@ +require "cases/helper" +require "models/developer" +require "rails/log_subscriber/test_helper" +require "active_record/railties/log_subscriber" + +class LogSubscriberTest < ActiveSupport::TestCase + include Rails::LogSubscriber::TestHelper + Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) + + def setup + @old_logger = ActiveRecord::Base.logger + super + end + + def teardown + super + ActiveRecord::Base.logger = @old_logger + end + + def set_logger(logger) + ActiveRecord::Base.logger = logger + end + + def test_basic_query_logging + Developer.all + wait + assert_equal 1, @logger.logged(:debug).size + assert_match /Developer Load/, @logger.logged(:debug).last + assert_match /SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last + end + + def test_cached_queries + ActiveRecord::Base.cache do + Developer.all + Developer.all + end + wait + assert_equal 2, @logger.logged(:debug).size + assert_match /CACHE/, @logger.logged(:debug).last + assert_match /SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last + end +end \ No newline at end of file diff --git a/activerecord/test/cases/subscriber_test.rb b/activerecord/test/cases/subscriber_test.rb deleted file mode 100644 index 5328d4468b..0000000000 --- a/activerecord/test/cases/subscriber_test.rb +++ /dev/null @@ -1,42 +0,0 @@ -require "cases/helper" -require "models/developer" -require "rails/subscriber/test_helper" -require "active_record/railties/subscriber" - -class SubscriberTest < ActiveSupport::TestCase - include Rails::Subscriber::TestHelper - Rails::Subscriber.add(:active_record, ActiveRecord::Railties::Subscriber.new) - - def setup - @old_logger = ActiveRecord::Base.logger - super - end - - def teardown - super - ActiveRecord::Base.logger = @old_logger - end - - def set_logger(logger) - ActiveRecord::Base.logger = logger - end - - def test_basic_query_logging - Developer.all - wait - assert_equal 1, @logger.logged(:debug).size - assert_match /Developer Load/, @logger.logged(:debug).last - assert_match /SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last - end - - def test_cached_queries - ActiveRecord::Base.cache do - Developer.all - Developer.all - end - wait - assert_equal 2, @logger.logged(:debug).size - assert_match /CACHE/, @logger.logged(:debug).last - assert_match /SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last - end -end \ No newline at end of file -- cgit v1.2.3 From 9acd686753c43612984aaa4002e80113fda2b255 Mon Sep 17 00:00:00 2001 From: snusnu Date: Sat, 20 Feb 2010 08:24:10 +0100 Subject: Adds #key and #to_param to the AMo interface This commit introduces two new methods that every AMo compliant object must implement. Below are the default implementations along with the implied interface contract. # Returns an Enumerable of all (primary) key # attributes or nil if new_record? is true def key new_record? ? nil : [1] end # Returns a string representing the object's key # suitable for use in URLs, or nil if new_record? # is true def to_param key ? key.first.to_s : nil end 1) The #key method Previously rails' record_identifier code, which is used in the #dom_id helper, relied on calling #id on the record to provide a reasonable DOM id. Now with rails3 being all ORM agnostic, it's not safe anymore to assume that every record ever will have an #id as its primary key attribute. Having a #key method available on every AMo object means that #dom_id can be implemented using record.to_model.key # instead of record.id Using this we're able to take composite primary keys into account (e.g. available in datamapper) by implementing #dom_id using a newly added record_key_for_dom_id(record) method. The user can overwrite this method to provide customized versions of the object's key used in #dom_id. Also, dealing with more complex keys that can contain arbitrary strings, means that we need to make sure that we only provide DOM ids that are valid according to the spec. For this reason, this patch sends the key provided through a newly added sanitize_dom_id(candidate_id) method, that makes sure we only produce valid HTML The reason to not just add #dom_id to the AMo interface was that it feels like providing a DOM id should not be a model concern. Adding #dom_id to the AMo interface would force these concern on the model, while it's better left to be implemented in a helper. Now one could say the same is true for #to_param, and actually I think that it doesn't really fit into the model either, but it's used in AR and it's a main part of integrating into the rails router. This is different from #dom_id which is only used in view helpers and can be implemented on top of a semantically more meaningful method like #key. 2) The #to_param method Since the rails router relies on #to_param to be present, AR::Base implements it and returns the id by default, allowing the user to overwrite the method if desired. Now with different ORMs integrating into rails, every ORM railtie needs to implement it's own #to_param implementation while already providing code to be AMo compliant. Since the whole point of AMo compliance seems to be to integrate any ORM seamlessly into rails, it seems fair that all we really need to do as another ORM, is to be AMo compliant. By including #to_param into the official interface, we can make sure that this code can be centralized in the various AMo compliance layers, and not be added separately by every ORM railtie. 3) All specs pass --- .../lib/active_record/attribute_methods/primary_key.rb | 14 ++++++++++++++ activerecord/test/cases/pk_test.rb | 9 +++++++++ 2 files changed, 23 insertions(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 365fdeb55a..ba64b8ee2f 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -39,6 +39,20 @@ module ActiveRecord end alias :primary_key= :set_primary_key end + + module InstanceMethods + + # Returns this record's primary key value wrapped in an Array + # or nil if the record is a new_record? + # This is done to comply with the AMo interface that expects + # every AMo compliant object to respond_to?(:key) and return + # an Enumerable object from that call, or nil if new_record? + def key + new_record? ? nil : [ self.id ] + end + + end + end end end diff --git a/activerecord/test/cases/pk_test.rb b/activerecord/test/cases/pk_test.rb index c121e0aa0f..2502c136cc 100644 --- a/activerecord/test/cases/pk_test.rb +++ b/activerecord/test/cases/pk_test.rb @@ -9,6 +9,15 @@ require 'models/mixed_case_monkey' class PrimaryKeysTest < ActiveRecord::TestCase fixtures :topics, :subscribers, :movies, :mixed_case_monkeys + def test_key + # test new record + topic = Topic.new + assert topic.key.nil? + # test existing record + topic = Topic.find(1) + assert_equal topic.key, [1] + end + def test_integer_key topic = Topic.find(1) assert_equal(topics(:first).author_name, topic.author_name) -- cgit v1.2.3 From f81c6bc0404ba2a03eed0ec6c08bbac45661305f Mon Sep 17 00:00:00 2001 From: snusnu Date: Sun, 21 Feb 2010 03:05:28 +0100 Subject: AMo #key is now #to_key and CI is probably happy Obviously #key is a too common name to be included in the AMo interface, #to_key fits better and also relates nicely to #to_param. Thx wycats, koz and josevalim for the suggestion. AR's #to_key implementation now takes customized primary keys into account and there's a testcase for that too. The #to_param AMo lint makes no assumptions on how the method behaves in the presence of composite primary keys. It leaves the decision wether to provide a default, or to raise and thus signal to the user that implementing this method will need his special attention, up to the implementers. All AMo cares about is that #to_param is implemented and returns nil in case of a new_record?. The default CompliantObject used in lint_test provides a naive default implementation that just joins all key attributes with '-'. The #to_key default implementation in lint_test's CompliantObject now returns [id] instead of [1]. This was previously causing the (wrong) tests I added for AR's #to_key implementation to pass. The #to_key tests added with this patch should be better. The CI failure was caused by my lack of knowledge about the test:isolated task. The tests for the record_identifier code in action_controller are using fake non AR models and I forgot to stub the #to_key method over there. This issue didn't come up when running the test task, only test:isolated revealed it. This patch fixes that. All tests pass isolated or not, well, apart from one previously unpended test in action_controller that is unrelated to my patch. --- .../lib/active_record/attribute_methods/primary_key.rb | 8 +++++--- activerecord/test/cases/pk_test.rb | 15 ++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index ba64b8ee2f..8549414a12 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -45,10 +45,12 @@ module ActiveRecord # Returns this record's primary key value wrapped in an Array # or nil if the record is a new_record? # This is done to comply with the AMo interface that expects - # every AMo compliant object to respond_to?(:key) and return + # every AMo compliant object to respond_to?(:to_key) and return # an Enumerable object from that call, or nil if new_record? - def key - new_record? ? nil : [ self.id ] + # This method also takes custom primary keys specified via + # the +set_primary_key+ into account. + def to_key + new_record? ? nil : [ self.send(self.class.primary_key) ] end end diff --git a/activerecord/test/cases/pk_test.rb b/activerecord/test/cases/pk_test.rb index 2502c136cc..5bba1d5625 100644 --- a/activerecord/test/cases/pk_test.rb +++ b/activerecord/test/cases/pk_test.rb @@ -9,13 +9,18 @@ require 'models/mixed_case_monkey' class PrimaryKeysTest < ActiveRecord::TestCase fixtures :topics, :subscribers, :movies, :mixed_case_monkeys - def test_key - # test new record + def test_to_key_with_default_primary_key topic = Topic.new - assert topic.key.nil? - # test existing record + assert topic.to_key.nil? topic = Topic.find(1) - assert_equal topic.key, [1] + assert_equal topic.to_key, [1] + end + + def test_to_key_with_customized_primary_key + keyboard = Keyboard.new + assert keyboard.to_key.nil? + keyboard.save + assert_equal keyboard.to_key, [keyboard.id] end def test_integer_key -- cgit v1.2.3 From 9dd67fce25d3993a0ee494506ba246a45d395e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 21 Feb 2010 08:47:37 +0100 Subject: Add to_key and to_param methods to ActiveModel::Conversion. --- activerecord/lib/active_record/attribute_methods/primary_key.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 8549414a12..095814b635 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -50,7 +50,7 @@ module ActiveRecord # This method also takes custom primary keys specified via # the +set_primary_key+ into account. def to_key - new_record? ? nil : [ self.send(self.class.primary_key) ] + new_record? ? nil : [ self.primary_key ] end end -- cgit v1.2.3 From 250c8092461f5e6bf62751b313f6605a37fd1b2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 21 Feb 2010 11:09:21 +0100 Subject: Require persisted? in ActiveModel::Lint and remove new_record? and destroyed? methods. ActionPack does not care if the resource is new or if it was destroyed, it cares only if it's persisted somewhere or not. --- activerecord/lib/active_record/base.rb | 13 +++++++++---- activerecord/test/cases/base_test.rb | 22 +++++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c6dde078ca..ef5a7d5787 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1767,6 +1767,11 @@ module ActiveRecord #:nodoc: @destroyed || false end + # Returns if the record is persisted, i.e. it's not a new record and it was not destroyed. + def persisted? + !(new_record? || destroyed?) + end + # :call-seq: # save(options) # @@ -1816,7 +1821,7 @@ module ActiveRecord #:nodoc: # callbacks, Observer methods, or any :dependent association # options, use #destroy. def delete - self.class.delete(id) unless new_record? + self.class.delete(id) if persisted? @destroyed = true freeze end @@ -1824,7 +1829,7 @@ module ActiveRecord #:nodoc: # Deletes the record in the database and freezes this instance to reflect that no changes should # be made (since they can't be persisted). def destroy - unless new_record? + if persisted? self.class.unscoped.where(self.class.arel_table[self.class.primary_key].eq(id)).delete_all end @@ -1844,6 +1849,7 @@ module ActiveRecord #:nodoc: became.instance_variable_set("@attributes", @attributes) became.instance_variable_set("@attributes_cache", @attributes_cache) became.instance_variable_set("@new_record", new_record?) + became.instance_variable_set("@destroyed", destroyed?) became end @@ -2042,8 +2048,7 @@ module ActiveRecord #:nodoc: def ==(comparison_object) comparison_object.equal?(self) || (comparison_object.instance_of?(self.class) && - comparison_object.id == id && - !comparison_object.new_record?) + comparison_object.id == id && !comparison_object.new_record?) end # Delegates to == diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 1441b4278d..2c4e1a3c0f 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1330,11 +1330,6 @@ class BasicsTest < ActiveRecord::TestCase end def test_destroyed_returns_boolean - developer = Developer.new - assert_equal developer.destroyed?, false - developer.destroy - assert_equal developer.destroyed?, true - developer = Developer.first assert_equal developer.destroyed?, false developer.destroy @@ -1346,6 +1341,23 @@ class BasicsTest < ActiveRecord::TestCase assert_equal developer.destroyed?, true end + def test_persisted_returns_boolean + developer = Developer.new(:name => "Jose") + assert_equal developer.persisted?, false + developer.save! + assert_equal developer.persisted?, true + + developer = Developer.first + assert_equal developer.persisted?, true + developer.destroy + assert_equal developer.persisted?, false + + developer = Developer.last + assert_equal developer.persisted?, true + developer.delete + assert_equal developer.persisted?, false + end + def test_clone topic = Topic.find(1) cloned_topic = nil -- cgit v1.2.3 From ca92e92ba18132dcfd57cf30a8492c6fea1c113a Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 20 Feb 2010 19:51:47 -0200 Subject: avoid active not initialized warning --- activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 7e80347f75..6ffffc8654 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -37,6 +37,7 @@ module ActiveRecord @@row_even = true def initialize(connection, logger = nil) #:nodoc: + @active = nil @connection, @logger = connection, logger @runtime = 0 @query_cache_enabled = false -- cgit v1.2.3 From 2b43021499f6e00a19eba3e7e48db403a61161bb Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 22 Feb 2010 20:00:41 -0200 Subject: avoid @transaction_joinable not initialized warning --- .../active_record/connection_adapters/abstract/database_statements.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index 027d736484..abb695264e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -113,7 +113,7 @@ module ActiveRecord def transaction(options = {}) options.assert_valid_keys :requires_new, :joinable - last_transaction_joinable = @transaction_joinable + last_transaction_joinable = defined?(@transaction_joinable) ? @transaction_joinable : nil if options.has_key?(:joinable) @transaction_joinable = options[:joinable] else -- cgit v1.2.3 From ee541049fdbe5eeebcca2f3b83144a5d803345a9 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 22 Feb 2010 20:03:05 -0200 Subject: avoid @lock_value not initialized warning --- activerecord/lib/active_record/relation/query_methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 0266700f66..25ffa7860e 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -174,7 +174,7 @@ module ActiveRecord arel = arel.lock when String arel = arel.lock(@lock_value) - end + end if defined?(@lock_value) arel end -- cgit v1.2.3 From 24ab5665b2f12a589e96a4b742cc49c08bf0e9df Mon Sep 17 00:00:00 2001 From: Carlhuda Date: Tue, 23 Feb 2010 17:31:17 -0800 Subject: Revert "Fix test load paths for those not using bundler" This reverts commit eec2d301d4ce9df9c71c1a5aa63053eb970b6818. This commit broke tests. You cannot have a file called "bundler" on the load path. --- activerecord/lib/active_record.rb | 1 + activerecord/test/cases/helper.rb | 5 +---- activerecord/test/cases/log_subscriber_test.rb | 5 +---- 3 files changed, 3 insertions(+), 8 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 99ff0a19a5..b79da4565d 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -21,6 +21,7 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ + activesupport_path = File.expand_path('../../../activesupport/lib', __FILE__) $:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.include?(activesupport_path) diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 3254e7d800..9e8bfbbee8 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -1,7 +1,4 @@ -require File.expand_path('../../../../bundler', __FILE__) - -lib = File.expand_path("#{File.dirname(__FILE__)}/../../lib") -$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) +require File.expand_path('../../../../load_paths', __FILE__) require 'config' diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index f854499435..f0197ddf77 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -1,6 +1,3 @@ -railties_path = File.expand_path('../../../../railties/lib', __FILE__) -$:.unshift(railties_path) if File.directory?(railties_path) && !$:.include?(railties_path) - require "cases/helper" require "models/developer" require "rails/log_subscriber/test_helper" @@ -42,4 +39,4 @@ class LogSubscriberTest < ActiveSupport::TestCase assert_match /CACHE/, @logger.logged(:debug).last assert_match /SELECT .*?FROM .?developers.?/, @logger.logged(:debug).last end -end +end \ No newline at end of file -- cgit v1.2.3 From f7b0a857e97304a5daeb47313759b9bf0d7e2fc9 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 25 Feb 2010 09:32:29 -0800 Subject: Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. --- activerecord/lib/active_record/base.rb | 2 +- activerecord/lib/active_record/migration.rb | 4 ++-- activerecord/lib/active_record/named_scope.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index ef5a7d5787..83f0b58e8a 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/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/module/delegation' module ActiveRecord #:nodoc: diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 068d2a25b2..fd5ffc6d77 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/metaclass' +require 'active_support/core_ext/object/singleton_class' module ActiveRecord # Exception that can be raised to stop migrations from going backwards. @@ -303,7 +303,7 @@ module ActiveRecord case sym when :up, :down - metaclass.send(:alias_method_chain, sym, "benchmarks") + singleton_class.send(:alias_method_chain, sym, "benchmarks") end ensure @ignore_new_methods = false diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index ff6c041ef4..f1f56850ae 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/metaclass' +require 'active_support/core_ext/object/singleton_class' module ActiveRecord module NamedScope @@ -112,7 +112,7 @@ module ActiveRecord options.call(*args) end, &block) end - metaclass.instance_eval do + singleton_class.instance_eval do define_method name do |*args| scopes[name].call(self, *args) end -- cgit v1.2.3 From bf9a0ae12b701cba7a8aac2955ce243866ac7bf6 Mon Sep 17 00:00:00 2001 From: Henry Hsu Date: Fri, 26 Feb 2010 11:06:55 +0100 Subject: Fix a bug where default_scope was overriding attributes given on model initialization [#3218 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/base.rb | 2 +- activerecord/test/cases/method_scoping_test.rb | 10 ++++++++++ activerecord/test/models/developer.rb | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 83f0b58e8a..c1c49c3d84 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1669,12 +1669,12 @@ module ActiveRecord #:nodoc: @attributes_cache = {} @new_record = true ensure_proper_type - self.attributes = attributes unless attributes.nil? if scope = self.class.send(:current_scoped_methods) create_with = scope.scope_for_create create_with.each { |att,value| self.send("#{att}=", value) } if create_with end + self.attributes = attributes unless attributes.nil? result = yield self if block_given? _run_initialize_callbacks diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 1081aa40a9..3151457440 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -663,6 +663,16 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 2, posts.count assert_equal posts(:thinking), posts.first end + + def test_create_attribute_overwrites_default_scoping + assert_equal 'David', PoorDeveloperCalledJamis.create!(:name => 'David').name + assert_equal 200000, PoorDeveloperCalledJamis.create!(:name => 'David', :salary => 200000).salary + end + + def test_create_attribute_overwrites_default_values + assert_equal nil, PoorDeveloperCalledJamis.create!(:salary => nil).salary + assert_equal 50000, PoorDeveloperCalledJamis.create!(:name => 'David').salary + end end =begin diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index e7a1e110d7..e35de3b9b0 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -99,3 +99,8 @@ class DeveloperCalledJamis < ActiveRecord::Base self.table_name = 'developers' default_scope :conditions => { :name => 'Jamis' } end + +class PoorDeveloperCalledJamis < ActiveRecord::Base + self.table_name = 'developers' + default_scope :conditions => { :name => 'Jamis', :salary => 50000 } +end \ No newline at end of file -- cgit v1.2.3 From 4db72b702f7faca7da30a64e45daeee13733762a Mon Sep 17 00:00:00 2001 From: Tobias Bielohlawek Date: Tue, 8 Sep 2009 15:51:56 +0200 Subject: fixed a 'RecordNotFound' bug when calling 'reload' on a object which doesn't met the default_scope conditions, added test [#3166 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reload method didn't made use of 'with_exclusive_scope' when reloading the object. This lead to a RecordNotFound exception, in case the object doesn't met the default_scope condition (anymore) - which is obviously a bug. This quick fix makes use of with_exclusive_scope in the reload method as well. See test for full example. Signed-off-by: José Valim --- activerecord/lib/active_record/base.rb | 2 +- activerecord/test/cases/base_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c1c49c3d84..cd67490573 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1932,7 +1932,7 @@ module ActiveRecord #:nodoc: def reload(options = nil) clear_aggregation_cache clear_association_cache - @attributes.update(self.class.find(self.id, options).instance_variable_get('@attributes')) + @attributes.update(self.class.send(:with_exclusive_scope) { self.class.find(self.id, options) }.instance_variable_get('@attributes')) @attributes_cache = {} self end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 2c4e1a3c0f..e3047fe873 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1723,6 +1723,12 @@ class BasicsTest < ActiveRecord::TestCase assert_equal t1.title, t2.title end + def test_reload_with_exclusive_scope + dev = DeveloperCalledDavid.first + dev.update_attributes!( :name => "NotDavid" ) + assert_equal dev, dev.reload + end + def test_define_attr_method_with_value k = Class.new( ActiveRecord::Base ) k.send(:define_attr_method, :table_name, "foo") -- cgit v1.2.3 From b25c41e6d884372f7d70801f832e24dbff3e9077 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 26 Feb 2010 20:37:26 -0200 Subject: no more warning interpreted as argument prefix on association_preload.rb --- activerecord/lib/active_record/association_preload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index a5b06460fe..6725d4e88b 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -126,7 +126,7 @@ module ActiveRecord parent_records.each do |parent_record| association_proxy = parent_record.send(reflection_name) association_proxy.loaded - association_proxy.target.push *Array.wrap(associated_record) + association_proxy.target.push(*Array.wrap(associated_record)) association_proxy.__send__(:set_inverse_instance, associated_record, parent_record) end -- cgit v1.2.3 From 09cb0977d47c7f5fe0797645a011f43b01121bd3 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Mon, 15 Feb 2010 12:18:44 +0100 Subject: Print database.rake errors to stderr. Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/railties/databases.rake | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index ed7d2a045e..fa6caa4910 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -109,7 +109,7 @@ namespace :db do # Only connect to local databases local_database?(config) { drop_database(config) } rescue Exception => e - puts "Couldn't drop #{config['database']} : #{e.inspect}" + $stderr.puts "Couldn't drop #{config['database']} : #{e.inspect}" end end end @@ -121,7 +121,7 @@ namespace :db do begin drop_database(config) rescue Exception => e - puts "Couldn't drop #{config['database']} : #{e.inspect}" + $stderr.puts "Couldn't drop #{config['database']} : #{e.inspect}" end end @@ -129,7 +129,7 @@ namespace :db do if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank? yield else - puts "This task only modifies local databases. #{config['database']} is on a remote host." + $stderr.puts "This task only modifies local databases. #{config['database']} is on a remote host." end end @@ -204,7 +204,7 @@ namespace :db do ActiveRecord::Base.establish_connection(config) puts ActiveRecord::Base.connection.encoding else - puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' + $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' end end @@ -216,7 +216,7 @@ namespace :db do ActiveRecord::Base.establish_connection(config) puts ActiveRecord::Base.connection.collation else - puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' + $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' end end -- cgit v1.2.3 From f3839b2b9996f84be6599e940a38e43237f35367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 28 Feb 2010 11:53:48 +0100 Subject: ActiveRecord should raise an error on invalid migration types. --- .../connection_adapters/abstract/schema_definitions.rb | 11 +++++++---- activerecord/test/cases/ar_schema_test.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 520f3c8c0c..64faaef4a0 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -319,16 +319,19 @@ module ActiveRecord def method_missing(symbol, *args) if symbol.to_s == 'xml' xml_column_fallback(args) + else + super end end def xml_column_fallback(*args) case @base.adapter_name.downcase - when 'sqlite', 'mysql' - options = args.extract_options! - column(args[0], :text, options) - end + when 'sqlite', 'mysql' + options = args.extract_options! + column(args[0], :text, options) end + end + # Appends a primary key definition to the table definition. # Can be called multiple times, but this is probably not a good idea. def primary_key(name) diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index 4c1589d965..665c387d5d 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -27,6 +27,16 @@ if ActiveRecord::Base.connection.supports_migrations? assert_nothing_raised { @connection.select_all "SELECT * FROM schema_migrations" } assert_equal 7, ActiveRecord::Migrator::current_version end + + def test_schema_raises_an_error_for_invalid_column_ntype + assert_raise NoMethodError do + ActiveRecord::Schema.define(:version => 8) do + create_table :vegetables do |t| + t.unknown :color + end + end + end + end end end -- cgit v1.2.3 From 5cc17da8946abbaa86a4d9c91dcd4b05a289493f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 28 Feb 2010 18:28:25 -0600 Subject: activerecord test load paths --- activerecord/test/cases/helper.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 9e8bfbbee8..f77dc801dc 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -1,5 +1,8 @@ require File.expand_path('../../../../load_paths', __FILE__) +lib = File.expand_path("#{File.dirname(__FILE__)}/../../lib") +$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) + require 'config' require 'test/unit' -- cgit v1.2.3 From 11db694e0b825e3077e7545b08c1bf975997021f Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 1 Mar 2010 21:55:43 -0500 Subject: Remove cruft in the gem packaging and release code --- activerecord/Rakefile | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'activerecord') diff --git a/activerecord/Rakefile b/activerecord/Rakefile index 8a414a751b..c53215e75e 100644 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -8,22 +8,8 @@ require 'rake/gempackagetask' require File.join(File.dirname(__FILE__), 'lib', 'active_record', 'version') require File.expand_path(File.dirname(__FILE__)) + "/test/config" -PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : '' -PKG_NAME = 'activerecord' -PKG_VERSION = ActiveRecord::VERSION::STRING + PKG_BUILD -PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}" - -RELEASE_NAME = "REL #{PKG_VERSION}" - -RUBY_FORGE_PROJECT = "activerecord" -RUBY_FORGE_USER = "webster132" - MYSQL_DB_USER = 'rails' -PKG_FILES = FileList[ - "lib/**/*", "test/**/*", "examples/**/*", "doc/**/*", "[A-Z]*", "install.rb", "Rakefile" -].exclude(/\bCVS\b|~$/) - def run_without_aborting(*tasks) errors = [] -- cgit v1.2.3 From a4111bbca0884e4a748ab32ba7d7b550ec8d9186 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 1 Mar 2010 23:03:07 -0500 Subject: Update versions of all components to normalize them to new format --- activerecord/lib/active_record/version.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/version.rb b/activerecord/lib/active_record/version.rb index 286ecd0289..eaf5dc6545 100644 --- a/activerecord/lib/active_record/version.rb +++ b/activerecord/lib/active_record/version.rb @@ -2,8 +2,9 @@ module ActiveRecord module VERSION #:nodoc: MAJOR = 3 MINOR = 0 - TINY = "0.beta1" + TINY = 0 + BUILD = "beta1" - STRING = [MAJOR, MINOR, TINY].join('.') + STRING = [MAJOR, MINOR, TINY, BUILD].join('.') end end -- cgit v1.2.3 From f221a6f19f4cee31e2d103ea9a1930f59223fc25 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 1 Mar 2010 23:36:54 -0500 Subject: Leverage VERSION constants from gemspecs to avoid tedious updates when releasing --- activerecord/Rakefile | 1 - activerecord/activerecord.gemspec | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/Rakefile b/activerecord/Rakefile index c53215e75e..e638da0f3e 100644 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -5,7 +5,6 @@ require 'rake/rdoctask' require 'rake/packagetask' require 'rake/gempackagetask' -require File.join(File.dirname(__FILE__), 'lib', 'active_record', 'version') require File.expand_path(File.dirname(__FILE__)) + "/test/config" MYSQL_DB_USER = 'rails' diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index aeaafbb694..b269126a86 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -1,7 +1,10 @@ +$:.unshift "lib" +require "active_record/version" + Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = 'activerecord' - s.version = '3.0.0.beta1' + s.version = ActiveRecord::VERSION::STRING s.summary = 'Object-relational mapper framework (part of Rails).' s.description = 'Object-relational mapper framework (part of Rails).' s.required_ruby_version = '>= 1.8.7' @@ -18,7 +21,7 @@ Gem::Specification.new do |s| s.extra_rdoc_files = %w( README ) s.rdoc_options.concat ['--main', 'README'] - s.add_dependency('activesupport', '= 3.0.0.beta1') - s.add_dependency('activemodel', '= 3.0.0.beta1') + s.add_dependency('activesupport', "= #{ActiveRecord::VERSION::STRING}") + s.add_dependency('activemodel', "= #{ActiveRecord::VERSION::STRING}") s.add_dependency('arel', '~> 0.2.1') end -- cgit v1.2.3 From 56ea20605a54386158e90e290caa6d15e7623ade Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 2 Mar 2010 10:32:09 -0800 Subject: Wordsmith gem descriptions --- activerecord/activerecord.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index b269126a86..5d76333950 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -6,7 +6,7 @@ Gem::Specification.new do |s| s.name = 'activerecord' s.version = ActiveRecord::VERSION::STRING s.summary = 'Object-relational mapper framework (part of Rails).' - s.description = 'Object-relational mapper framework (part of Rails).' + s.description = 'Databases on Rails. Build a persistent domain model by mapping database tables to Ruby classes. Strong conventions for associations, validations, aggregations, migrations, and testing come baked-in.' s.required_ruby_version = '>= 1.8.7' s.author = 'David Heinemeier Hansson' -- cgit v1.2.3 From 07b5146442afbfd8c3b5db9ba8c66382889910ef Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 2 Mar 2010 23:01:31 -0800 Subject: Fix active_records' log tests --- activerecord/test/cases/log_subscriber_test.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index f0197ddf77..9e46de50d5 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -5,11 +5,12 @@ require "active_record/railties/log_subscriber" class LogSubscriberTest < ActiveSupport::TestCase include Rails::LogSubscriber::TestHelper - Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) def setup - @old_logger = ActiveRecord::Base.logger super + + @old_logger = ActiveRecord::Base.logger + Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) end def teardown -- cgit v1.2.3 From 0de3b9ec93e7221cfe24bd13d57616d8d8d03a57 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 4 Mar 2010 02:20:01 -0800 Subject: Somehow this fixes the AR tests when ran with rake. I should probably figure out why. --- activerecord/test/cases/log_subscriber_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index 9e46de50d5..f3b94eb829 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -7,9 +7,9 @@ class LogSubscriberTest < ActiveSupport::TestCase include Rails::LogSubscriber::TestHelper def setup + @old_logger = ActiveRecord::Base.logger super - @old_logger = ActiveRecord::Base.logger Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) end -- cgit v1.2.3 From deb00bcb8cf88eb6420282c9af0af64fb69aeed4 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 4 Mar 2010 22:41:39 -0500 Subject: Read Rails version from a file instead of modifying the load path and doing requires Signed-off-by: Jeremy Kemper --- activerecord/activerecord.gemspec | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'activerecord') diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index 5d76333950..08666f08e9 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -1,10 +1,9 @@ -$:.unshift "lib" -require "active_record/version" +version = File.read("../RAILS_VERSION").strip Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = 'activerecord' - s.version = ActiveRecord::VERSION::STRING + s.version = version s.summary = 'Object-relational mapper framework (part of Rails).' s.description = 'Databases on Rails. Build a persistent domain model by mapping database tables to Ruby classes. Strong conventions for associations, validations, aggregations, migrations, and testing come baked-in.' s.required_ruby_version = '>= 1.8.7' @@ -21,7 +20,7 @@ Gem::Specification.new do |s| s.extra_rdoc_files = %w( README ) s.rdoc_options.concat ['--main', 'README'] - s.add_dependency('activesupport', "= #{ActiveRecord::VERSION::STRING}") - s.add_dependency('activemodel', "= #{ActiveRecord::VERSION::STRING}") + s.add_dependency('activesupport', version) + s.add_dependency('activemodel', version) s.add_dependency('arel', '~> 0.2.1') end -- cgit v1.2.3 From 45c36ea0961d015bf040043c7ab2ac2069d3b92f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Mar 2010 13:18:14 -0800 Subject: refactoring build_where Signed-off-by: wycats --- activerecord/lib/active_record/relation/query_methods.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 25ffa7860e..6c4e84a958 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -184,16 +184,16 @@ module ActiveRecord builder = PredicateBuilder.new(table.engine) - conditions = if [String, Array].include?(args.first.class) - @klass.send(:sanitize_sql, args.size > 1 ? args : args.first) - elsif args.first.is_a?(Hash) - attributes = @klass.send(:expand_hash_conditions_for_aggregates, args.first) + opts = args.first + case opts + when String, Array + @klass.send(:sanitize_sql, args.size > 1 ? args : opts) + when Hash + attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) builder.build_from_hash(attributes, table) else - args.first + opts end - - conditions end private -- cgit v1.2.3 From 3789539916bcfbf82ee01f9aa35fca9f32049ea8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Mar 2010 15:30:43 -0800 Subject: converting inject to each and map [#4119 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/base.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index cd67490573..042a67b490 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2001,10 +2001,9 @@ module ActiveRecord #:nodoc: # Returns a hash of all the attributes with their names as keys and the values of the attributes as values. def attributes - self.attribute_names.inject({}) do |attrs, name| - attrs[name] = read_attribute(name) - attrs - end + attrs = {} + attribute_names.each { |name| attrs[name] = read_attribute(name) } + attrs end # Returns an #inspect-like string for the value of the @@ -2348,7 +2347,7 @@ module ActiveRecord #:nodoc: # Returns a comma-separated pair list, like "key1 = val1, key2 = val2". def comma_pair_list(hash) - hash.inject([]) { |list, pair| list << "#{pair.first} = #{pair.last}" }.join(", ") + hash.map { |k,v| "#{k} = #{v}" }.join(", ") end def quote_columns(quoter, hash) -- cgit v1.2.3 From 4bc2cbc3cfa231b3dea0dce38a6fa2723c7ed94d Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sat, 6 Mar 2010 13:30:25 -0600 Subject: Load RAILS_VERSION relative to the gemspec file. --- activerecord/activerecord.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index 08666f08e9..8c7f3f5f2f 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -1,4 +1,4 @@ -version = File.read("../RAILS_VERSION").strip +version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY -- cgit v1.2.3 From 939291e8f2b68e11cde12f5f08886f8fca3ef2b2 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Mar 2010 21:30:44 -0800 Subject: deleting repeated code Signed-off-by: wycats --- activerecord/lib/active_record/associations.rb | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 57785b4c93..86edfc386d 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1537,35 +1537,25 @@ module ActiveRecord # has_one associated objects, according to the defined :dependent rule. def configure_dependency_for_has_one(reflection) if reflection.options.include?(:dependent) - case reflection.options[:dependent] - when :destroy - method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym + name = reflection.options[:dependent] + method_name = :"has_one_dependent_#{name}_for_#{reflection.name}" + + case name + when :destroy, :delete define_method(method_name) do association = send(reflection.name) association.destroy unless association.nil? end - before_destroy method_name - when :delete - method_name = "has_one_dependent_delete_for_#{reflection.name}".to_sym - define_method(method_name) do - # Retrieve the associated object and delete it. The retrieval - # is necessary because there may be multiple associated objects - # with foreign keys pointing to this object, and we only want - # to delete the correct one, not all of them. - association = send(reflection.name) - association.delete unless association.nil? - end - before_destroy method_name when :nullify - method_name = "has_one_dependent_nullify_for_#{reflection.name}".to_sym define_method(method_name) do association = send(reflection.name) association.update_attribute(reflection.primary_key_name, nil) unless association.nil? end - before_destroy method_name else raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify (#{reflection.options[:dependent].inspect})" end + + before_destroy method_name end end -- cgit v1.2.3 From a424f199a9143e7ea451ba6f5e7dc54eb6103988 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Mar 2010 21:39:05 -0800 Subject: drying up more code in associations.rb Signed-off-by: wycats --- activerecord/lib/active_record/associations.rb | 28 ++++++++++---------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 86edfc386d..e1a9c54e5d 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1561,24 +1561,18 @@ module ActiveRecord def configure_dependency_for_belongs_to(reflection) if reflection.options.include?(:dependent) - case reflection.options[:dependent] - when :destroy - method_name = "belongs_to_dependent_destroy_for_#{reflection.name}".to_sym - define_method(method_name) do - association = send(reflection.name) - association.destroy unless association.nil? - end - after_destroy method_name - when :delete - method_name = "belongs_to_dependent_delete_for_#{reflection.name}".to_sym - define_method(method_name) do - association = send(reflection.name) - association.delete unless association.nil? - end - after_destroy method_name - else - raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})" + name = reflection.options[:dependent] + + unless [:destroy, :delete].include?(name) + raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})" + end + + method_name = :"belongs_to_dependent_#{name}_for_#{reflection.name}" + define_method(method_name) do + association = send(reflection.name) + association.destroy unless association.nil? end + after_destroy method_name end end -- cgit v1.2.3 From 39d6f9e112f2320d8c2006ee3bcc160cfa761d0a Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 7 Mar 2010 06:24:30 -0800 Subject: Make many parts of Rails lazy. In order to facilitate this, add lazy_load_hooks.rb, which allows us to declare code that should be run at some later time. For instance, this allows us to defer requiring ActiveRecord::Base at boot time purely to apply configuration. Instead, we register a hook that should apply configuration once ActiveRecord::Base is loaded. With these changes, brings down total boot time of a new app to 300ms in production and 400ms in dev. TODO: rename base_hook --- activerecord/lib/active_record.rb | 13 ++- activerecord/lib/active_record/base.rb | 170 +----------------------------- activerecord/lib/active_record/errors.rb | 165 +++++++++++++++++++++++++++++ activerecord/lib/active_record/railtie.rb | 42 +++++--- activerecord/test/cases/helper.rb | 1 + 5 files changed, 206 insertions(+), 185 deletions(-) create mode 100644 activerecord/lib/active_record/errors.rb (limited to 'activerecord') diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index b79da4565d..5942640c85 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -30,7 +30,6 @@ $:.unshift(activemodel_path) if File.directory?(activemodel_path) && !$:.include require 'active_support' require 'active_model' -require 'arel' module ActiveRecord extend ActiveSupport::Autoload @@ -38,8 +37,8 @@ module ActiveRecord eager_autoload do autoload :VERSION - autoload :ActiveRecordError, 'active_record/base' - autoload :ConnectionNotEstablished, 'active_record/base' + autoload :ActiveRecordError, 'active_record/errors' + autoload :ConnectionNotEstablished, 'active_record/errors' autoload :Aggregations autoload :AssociationPreload @@ -106,12 +105,16 @@ module ActiveRecord eager_autoload do autoload :AbstractAdapter + autoload :ConnectionManagement, "active_record/connection_adapters/abstract/connection_pool" end end autoload :TestCase autoload :TestFixtures, 'active_record/fixtures' + + base_hook do + Arel::Table.engine = Arel::Sql::Engine.new(self) + end end -Arel::Table.engine = Arel::Sql::Engine.new(ActiveRecord::Base) -I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' +I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml' \ No newline at end of file diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 042a67b490..52587ef251 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -13,172 +13,10 @@ 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/module/delegation' +require 'arel' +require 'active_record/errors' module ActiveRecord #:nodoc: - # Generic Active Record exception class. - class ActiveRecordError < StandardError - end - - # Raised when the single-table inheritance mechanism fails to locate the subclass - # (for example due to improper usage of column that +inheritance_column+ points to). - class SubclassNotFound < ActiveRecordError #:nodoc: - end - - # Raised when an object assigned to an association has an incorrect type. - # - # class Ticket < ActiveRecord::Base - # has_many :patches - # end - # - # class Patch < ActiveRecord::Base - # belongs_to :ticket - # end - # - # # Comments are not patches, this assignment raises AssociationTypeMismatch. - # @ticket.patches << Comment.new(:content => "Please attach tests to your patch.") - class AssociationTypeMismatch < ActiveRecordError - end - - # Raised when unserialized object's type mismatches one specified for serializable field. - class SerializationTypeMismatch < ActiveRecordError - end - - # Raised when adapter not specified on connection (or configuration file config/database.yml misses adapter field). - class AdapterNotSpecified < ActiveRecordError - end - - # Raised when Active Record cannot find database adapter specified in config/database.yml or programmatically. - class AdapterNotFound < ActiveRecordError - end - - # Raised when connection to the database could not been established (for example when connection= is given a nil object). - class ConnectionNotEstablished < ActiveRecordError - end - - # Raised when Active Record cannot find record by given id or set of ids. - class RecordNotFound < ActiveRecordError - end - - # Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be - # saved because record is invalid. - class RecordNotSaved < ActiveRecordError - end - - # Raised when SQL statement cannot be executed by the database (for example, it's often the case for MySQL when Ruby driver used is too old). - class StatementInvalid < ActiveRecordError - end - - # Raised when SQL statement is invalid and the application gets a blank result. - class ThrowResult < ActiveRecordError - end - - # Parent class for all specific exceptions which wrap database driver exceptions - # provides access to the original exception also. - class WrappedDatabaseException < StatementInvalid - attr_reader :original_exception - - def initialize(message, original_exception) - super(message) - @original_exception = original_exception - end - end - - # Raised when a record cannot be inserted because it would violate a uniqueness constraint. - class RecordNotUnique < WrappedDatabaseException - end - - # Raised when a record cannot be inserted or updated because it references a non-existent record. - class InvalidForeignKey < WrappedDatabaseException - end - - # Raised when number of bind variables in statement given to :condition key (for example, when using +find+ method) - # does not match number of expected variables. - # - # For example, in - # - # Location.find :all, :conditions => ["lat = ? AND lng = ?", 53.7362] - # - # two placeholders are given but only one variable to fill them. - class PreparedStatementInvalid < ActiveRecordError - end - - # Raised on attempt to save stale record. Record is stale when it's being saved in another query after - # instantiation, for example, when two users edit the same wiki page and one starts editing and saves - # the page before the other. - # - # Read more about optimistic locking in ActiveRecord::Locking module RDoc. - class StaleObjectError < ActiveRecordError - end - - # Raised when association is being configured improperly or - # user tries to use offset and limit together with has_many or has_and_belongs_to_many associations. - class ConfigurationError < ActiveRecordError - end - - # Raised on attempt to update record that is instantiated as read only. - class ReadOnlyRecord < ActiveRecordError - end - - # ActiveRecord::Transactions::ClassMethods.transaction uses this exception - # to distinguish a deliberate rollback from other exceptional situations. - # Normally, raising an exception will cause the +transaction+ method to rollback - # the database transaction *and* pass on the exception. But if you raise an - # ActiveRecord::Rollback exception, then the database transaction will be rolled back, - # without passing on the exception. - # - # For example, you could do this in your controller to rollback a transaction: - # - # class BooksController < ActionController::Base - # def create - # Book.transaction do - # book = Book.new(params[:book]) - # book.save! - # if today_is_friday? - # # The system must fail on Friday so that our support department - # # won't be out of job. We silently rollback this transaction - # # without telling the user. - # raise ActiveRecord::Rollback, "Call tech support!" - # end - # end - # # ActiveRecord::Rollback is the only exception that won't be passed on - # # by ActiveRecord::Base.transaction, so this line will still be reached - # # even on Friday. - # redirect_to root_url - # end - # end - class Rollback < ActiveRecordError - end - - # Raised when attribute has a name reserved by Active Record (when attribute has name of one of Active Record instance methods). - class DangerousAttributeError < ActiveRecordError - end - - # Raised when unknown attributes are supplied via mass assignment. - class UnknownAttributeError < NoMethodError - end - - # Raised when an error occurred while doing a mass assignment to an attribute through the - # attributes= method. The exception has an +attribute+ property that is the name of the - # offending attribute. - class AttributeAssignmentError < ActiveRecordError - attr_reader :exception, :attribute - def initialize(message, exception, attribute) - @exception = exception - @attribute = attribute - @message = message - end - end - - # Raised when there are multiple errors while doing a mass assignment through the +attributes+ - # method. The exception has an +errors+ property that contains an array of AttributeAssignmentError - # objects, each corresponding to the error while assigning to an attribute. - class MultiparameterAssignmentErrors < ActiveRecordError - attr_reader :errors - def initialize(errors) - @errors = errors - end - end - # Active Record objects don't specify their attributes directly, but rather infer them from the table definition with # which they're linked. Adding, removing, and changing attributes and their type is done directly in the database. Any change # is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain @@ -551,7 +389,7 @@ module ActiveRecord #:nodoc: class << self # Class methods def colorize_logging(*args) ActiveSupport::Deprecation.warn "ActiveRecord::Base.colorize_logging and " << - "config.active_record.colorize_logging are deprecated. Please use " << + "config.active_record.colorize_logging are deprecated. Please use " << "Rails::LogSubscriber.colorize_logging or config.colorize_logging instead", caller end alias :colorize_logging= :colorize_logging @@ -2401,8 +2239,10 @@ module ActiveRecord #:nodoc: include Aggregations, Transactions, Reflection, Serialization + NilClass.add_whiner(self) if NilClass.respond_to?(:add_whiner) end end # TODO: Remove this and make it work with LAZY flag require 'active_record/connection_adapters/abstract_adapter' +ActiveRecord.run_base_hooks(ActiveRecord::Base) \ No newline at end of file diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb new file mode 100644 index 0000000000..cf5ddca2ba --- /dev/null +++ b/activerecord/lib/active_record/errors.rb @@ -0,0 +1,165 @@ +module ActiveRecord + # Generic Active Record exception class. + class ActiveRecordError < StandardError + end + + # Raised when the single-table inheritance mechanism fails to locate the subclass + # (for example due to improper usage of column that +inheritance_column+ points to). + class SubclassNotFound < ActiveRecordError #:nodoc: + end + + # Raised when an object assigned to an association has an incorrect type. + # + # class Ticket < ActiveRecord::Base + # has_many :patches + # end + # + # class Patch < ActiveRecord::Base + # belongs_to :ticket + # end + # + # # Comments are not patches, this assignment raises AssociationTypeMismatch. + # @ticket.patches << Comment.new(:content => "Please attach tests to your patch.") + class AssociationTypeMismatch < ActiveRecordError + end + + # Raised when unserialized object's type mismatches one specified for serializable field. + class SerializationTypeMismatch < ActiveRecordError + end + + # Raised when adapter not specified on connection (or configuration file config/database.yml misses adapter field). + class AdapterNotSpecified < ActiveRecordError + end + + # Raised when Active Record cannot find database adapter specified in config/database.yml or programmatically. + class AdapterNotFound < ActiveRecordError + end + + # Raised when connection to the database could not been established (for example when connection= is given a nil object). + class ConnectionNotEstablished < ActiveRecordError + end + + # Raised when Active Record cannot find record by given id or set of ids. + class RecordNotFound < ActiveRecordError + end + + # Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be + # saved because record is invalid. + class RecordNotSaved < ActiveRecordError + end + + # Raised when SQL statement cannot be executed by the database (for example, it's often the case for MySQL when Ruby driver used is too old). + class StatementInvalid < ActiveRecordError + end + + # Raised when SQL statement is invalid and the application gets a blank result. + class ThrowResult < ActiveRecordError + end + + # Parent class for all specific exceptions which wrap database driver exceptions + # provides access to the original exception also. + class WrappedDatabaseException < StatementInvalid + attr_reader :original_exception + + def initialize(message, original_exception) + super(message) + @original_exception = original_exception + end + end + + # Raised when a record cannot be inserted because it would violate a uniqueness constraint. + class RecordNotUnique < WrappedDatabaseException + end + + # Raised when a record cannot be inserted or updated because it references a non-existent record. + class InvalidForeignKey < WrappedDatabaseException + end + + # Raised when number of bind variables in statement given to :condition key (for example, when using +find+ method) + # does not match number of expected variables. + # + # For example, in + # + # Location.find :all, :conditions => ["lat = ? AND lng = ?", 53.7362] + # + # two placeholders are given but only one variable to fill them. + class PreparedStatementInvalid < ActiveRecordError + end + + # Raised on attempt to save stale record. Record is stale when it's being saved in another query after + # instantiation, for example, when two users edit the same wiki page and one starts editing and saves + # the page before the other. + # + # Read more about optimistic locking in ActiveRecord::Locking module RDoc. + class StaleObjectError < ActiveRecordError + end + + # Raised when association is being configured improperly or + # user tries to use offset and limit together with has_many or has_and_belongs_to_many associations. + class ConfigurationError < ActiveRecordError + end + + # Raised on attempt to update record that is instantiated as read only. + class ReadOnlyRecord < ActiveRecordError + end + + # ActiveRecord::Transactions::ClassMethods.transaction uses this exception + # to distinguish a deliberate rollback from other exceptional situations. + # Normally, raising an exception will cause the +transaction+ method to rollback + # the database transaction *and* pass on the exception. But if you raise an + # ActiveRecord::Rollback exception, then the database transaction will be rolled back, + # without passing on the exception. + # + # For example, you could do this in your controller to rollback a transaction: + # + # class BooksController < ActionController::Base + # def create + # Book.transaction do + # book = Book.new(params[:book]) + # book.save! + # if today_is_friday? + # # The system must fail on Friday so that our support department + # # won't be out of job. We silently rollback this transaction + # # without telling the user. + # raise ActiveRecord::Rollback, "Call tech support!" + # end + # end + # # ActiveRecord::Rollback is the only exception that won't be passed on + # # by ActiveRecord::Base.transaction, so this line will still be reached + # # even on Friday. + # redirect_to root_url + # end + # end + class Rollback < ActiveRecordError + end + + # Raised when attribute has a name reserved by Active Record (when attribute has name of one of Active Record instance methods). + class DangerousAttributeError < ActiveRecordError + end + + # Raised when unknown attributes are supplied via mass assignment. + class UnknownAttributeError < NoMethodError + end + + # Raised when an error occurred while doing a mass assignment to an attribute through the + # attributes= method. The exception has an +attribute+ property that is the name of the + # offending attribute. + class AttributeAssignmentError < ActiveRecordError + attr_reader :exception, :attribute + def initialize(message, exception, attribute) + @exception = exception + @attribute = attribute + @message = message + end + end + + # Raised when there are multiple errors while doing a mass assignment through the +attributes+ + # method. The exception has an +errors+ property that contains an array of AttributeAssignmentError + # objects, each corresponding to the error while assigning to an attribute. + class MultiparameterAssignmentErrors < ActiveRecordError + attr_reader :errors + def initialize(errors) + @errors = errors + end + end +end \ No newline at end of file diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index b38bd9a644..60addd46c6 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -24,31 +24,39 @@ module ActiveRecord log_subscriber ActiveRecord::Railties::LogSubscriber.new initializer "active_record.initialize_timezone" do - ActiveRecord::Base.time_zone_aware_attributes = true - ActiveRecord::Base.default_timezone = :utc + ActiveRecord.base_hook do + self.time_zone_aware_attributes = true + self.default_timezone = :utc + end end initializer "active_record.logger" do - ActiveRecord::Base.logger ||= ::Rails.logger + ActiveRecord.base_hook { self.logger ||= ::Rails.logger } end initializer "active_record.set_configs" do |app| - app.config.active_record.each do |k,v| - ActiveRecord::Base.send "#{k}=", v + ActiveRecord.base_hook do + app.config.active_record.each do |k,v| + send "#{k}=", v + end end end # This sets the database configuration from Configuration#database_configuration # and then establishes the connection. initializer "active_record.initialize_database" do |app| - ActiveRecord::Base.configurations = app.config.database_configuration - ActiveRecord::Base.establish_connection + ActiveRecord.base_hook do + self.configurations = app.config.database_configuration + establish_connection + end end # Expose database runtime to controller for logging. initializer "active_record.log_runtime" do |app| require "active_record/railties/controller_runtime" - ActionController::Base.send :include, ActiveRecord::Railties::ControllerRuntime + ActionController.base_hook do + include ActiveRecord::Railties::ControllerRuntime + end end # Setup database middleware after initializers have run @@ -64,18 +72,22 @@ module ActiveRecord end initializer "active_record.load_observers" do - ActiveRecord::Base.instantiate_observers + ActiveRecord.base_hook { instantiate_observers } - ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do - ActiveRecord::Base.instantiate_observers + ActiveRecord.base_hook do + ActionDispatch::Callbacks.to_prepare(:activerecord_instantiate_observers) do + ActiveRecord::Base.instantiate_observers + end end end initializer "active_record.set_dispatch_hooks", :before => :set_clear_dependencies_hook do |app| - unless app.config.cache_classes - ActionDispatch::Callbacks.after do - ActiveRecord::Base.reset_subclasses - ActiveRecord::Base.clear_reloadable_connections! + ActiveRecord.base_hook do + unless app.config.cache_classes + ActionDispatch::Callbacks.after do + ActiveRecord::Base.reset_subclasses + ActiveRecord::Base.clear_reloadable_connections! + end end end end diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index f77dc801dc..e831ebf36c 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -9,6 +9,7 @@ require 'test/unit' require 'stringio' require 'active_record' +require 'active_support/dependencies' require 'connection' begin -- cgit v1.2.3 From 47d252f9928568620844edce2161acd457c352c0 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sun, 7 Mar 2010 21:53:21 -0300 Subject: Fix associations to call :destroy or :delete based on the right :dependent option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/lib/active_record/associations.rb | 6 ++-- .../associations/belongs_to_associations_test.rb | 32 ++++++++++++++++++---- .../associations/has_many_associations_test.rb | 20 +------------- .../associations/has_one_associations_test.rb | 4 +-- activerecord/test/cases/calculations_test.rb | 4 +-- activerecord/test/models/author.rb | 9 ++---- activerecord/test/models/company.rb | 4 +-- 7 files changed, 39 insertions(+), 40 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index e1a9c54e5d..b69577f8dd 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1544,12 +1544,12 @@ module ActiveRecord when :destroy, :delete define_method(method_name) do association = send(reflection.name) - association.destroy unless association.nil? + association.send(name) if association end when :nullify define_method(method_name) do association = send(reflection.name) - association.update_attribute(reflection.primary_key_name, nil) unless association.nil? + association.update_attribute(reflection.primary_key_name, nil) if association end else raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify (#{reflection.options[:dependent].inspect})" @@ -1570,7 +1570,7 @@ module ActiveRecord method_name = :"belongs_to_dependent_#{name}_for_#{reflection.name}" define_method(method_name) do association = send(reflection.name) - association.destroy unless association.nil? + association.send(name) if association end after_destroy method_name end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 2a77eed1b5..41a23d7f61 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -18,7 +18,8 @@ require 'models/essay' class BelongsToAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :topics, - :developers_projects, :computers, :authors, :posts, :tags, :taggings, :comments + :developers_projects, :computers, :authors, :author_addresses, + :posts, :tags, :taggings, :comments def test_belongs_to Client.find(3).firm.name @@ -346,14 +347,14 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! } assert companies(:first_client).readonly_firm.readonly? end - + def test_polymorphic_assignment_foreign_type_field_updating # should update when assigning a saved record sponsor = Sponsor.new member = Member.create sponsor.sponsorable = member assert_equal "Member", sponsor.sponsorable_type - + # should update when assigning a new record sponsor = Sponsor.new member = Member.new @@ -374,15 +375,15 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase essay.writer = writer assert_equal "Author", essay.writer_type end - + def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records sponsor = Sponsor.new saved_member = Member.create new_member = Member.new - + sponsor.sponsorable = saved_member assert_equal saved_member.id, sponsor.sponsorable_id - + sponsor.sponsorable = new_member assert_equal nil, sponsor.sponsorable_id end @@ -424,4 +425,23 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase Account.find(@account.id, :include => :firm).save! end end + + def test_dependent_delete_and_destroy_with_belongs_to + author_address = author_addresses(:david_address) + author_address_extra = author_addresses(:david_address_extra) + assert_equal [], AuthorAddress.destroyed_author_address_ids + + assert_difference "AuthorAddress.count", -2 do + authors(:david).destroy + end + + assert_equal [], AuthorAddress.find_all_by_id([author_address.id, author_address_extra.id]) + assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids + end + + def test_invalid_belongs_to_dependent_option_raises_exception + assert_raise ArgumentError do + Author.belongs_to :special_author_address, :dependent => :nullify + end + end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index ce7eedbb54..54624e79ce 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -14,7 +14,7 @@ require 'models/tagging' class HasManyAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :categories, :companies, :developers, :projects, - :developers_projects, :topics, :authors, :comments, :author_addresses, + :developers_projects, :topics, :authors, :comments, :people, :posts, :readers, :taggings def setup @@ -684,24 +684,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 'Microsoft', another_ms_client.name end - def test_dependent_delete_and_destroy_with_belongs_to - author_address = author_addresses(:david_address) - assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id] - - assert_difference "AuthorAddress.count", -2 do - authors(:david).destroy - end - - assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_id) - assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_extra_id) - end - - def test_invalid_belongs_to_dependent_option_raises_exception - assert_raise ArgumentError do - Author.belongs_to :special_author_address, :dependent => :nullify - end - end - def test_clearing_without_initial_access firm = companies(:first_firm) diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index d359ad48c5..d5dbb88886 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -14,7 +14,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal companies(:first_firm).account, Account.find(1) assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit end - + def test_has_one_cache_nils firm = companies(:another_firm) assert_queries(1) { assert_nil firm.account } @@ -96,7 +96,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_nil Account.find(old_account_id).firm_id end - def test_association_changecalls_delete + def test_association_change_calls_delete companies(:first_firm).deletable_account = Account.new assert_equal [], Account.destroyed_account_ids[companies(:first_firm).id] end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index e6d56a7193..28a1ae5feb 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -38,12 +38,12 @@ class CalculationsTest < ActiveRecord::TestCase end def test_should_get_maximum_of_field_with_include - assert_equal 50, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'") + assert_equal 55, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'") end def test_should_get_maximum_of_field_with_scoped_include Account.send :with_scope, :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do - assert_equal 50, Account.maximum(:credit_limit) + assert_equal 55, Account.maximum(:credit_limit) end end diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 7cbc6e803f..025f6207f8 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -35,7 +35,7 @@ class Author < ActiveRecord::Base has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id' has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC' has_many :readonly_comments, :through => :posts, :source => :comments, :readonly => true - + has_many :special_posts has_many :special_post_comments, :through => :special_posts, :source => :comments @@ -130,14 +130,11 @@ class AuthorAddress < ActiveRecord::Base has_one :author def self.destroyed_author_address_ids - @destroyed_author_address_ids ||= Hash.new { |h,k| h[k] = [] } + @destroyed_author_address_ids ||= [] end before_destroy do |author_address| - if author_address.author - AuthorAddress.destroyed_author_address_ids[author_address.author.id] << author_address.id - end - true + AuthorAddress.destroyed_author_address_ids << author_address.id end end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index df5fd10b6b..f31d5f87e5 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -82,7 +82,7 @@ class Firm < Company has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account", :order => "id" has_one :account_using_foreign_and_primary_keys, :foreign_key => "firm_name", :primary_key => "name", :class_name => "Account" has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete - + has_one :account_limit_500_with_hash_conditions, :foreign_key => "firm_id", :class_name => "Account", :conditions => { :credit_limit => 500 } has_one :unautosaved_account, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false @@ -155,7 +155,7 @@ class VerySpecialClient < SpecialClient end class Account < ActiveRecord::Base - belongs_to :firm + belongs_to :firm, :class_name => 'Company' belongs_to :unautosaved_firm, :foreign_key => "firm_id", :class_name => "Firm", :autosave => false def self.destroyed_account_ids -- cgit v1.2.3 From 181c414baa877d748671d03fb09499c10f81ec02 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 10 Mar 2010 11:28:04 +0000 Subject: Fix scope loading issue when the table doesn't exist --- activerecord/lib/active_record/relation/predicate_builder.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 65e5c0495c..49bf2fefe0 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -20,7 +20,9 @@ module ActiveRecord table = Arel::Table.new(table_name, :engine => @engine) end - attribute = table[column] || Arel::Attribute.new(table, column.to_sym) + # TODO : Arel::Table#[] should fallback to using Arel::Attribute if the table/column doesn't exist + # attribute = table[column] + attribute = Arel::Attribute.new(table, column.to_sym) case value when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope -- cgit v1.2.3 From aad432a9e91d991218276b5945d43e9dcb247b63 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 10 Mar 2010 08:51:11 -0800 Subject: removing spawn from SpawnMethods Signed-off-by: wycats --- activerecord/lib/active_record/named_scope.rb | 2 +- activerecord/lib/active_record/relation.rb | 4 ++++ activerecord/lib/active_record/relation/query_methods.rb | 12 ++++++------ activerecord/lib/active_record/relation/spawn_methods.rb | 8 ++------ 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index f1f56850ae..394e1587e1 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -26,7 +26,7 @@ module ActiveRecord if options.present? Scope.init(self, options, &block) else - current_scoped_methods ? unscoped.merge(current_scoped_methods) : unscoped.spawn + current_scoped_methods ? unscoped.merge(current_scoped_methods) : unscoped.clone end end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 7bc3d3bf33..aca4629dd8 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -21,6 +21,10 @@ module ActiveRecord with_create_scope { @klass.new(*args, &block) } end + def initialize_copy(other) + reset + end + alias build new def create(*args, &block) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 6c4e84a958..e00d9cdf27 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -8,7 +8,7 @@ module ActiveRecord class_eval <<-CEVAL def #{query_method}(*args) - new_relation = spawn + new_relation = clone value = Array.wrap(args.flatten).reject {|x| x.blank? } new_relation.#{query_method}_values += value if value.present? new_relation @@ -19,7 +19,7 @@ module ActiveRecord [:where, :having].each do |query_method| class_eval <<-CEVAL def #{query_method}(*args) - new_relation = spawn + new_relation = clone value = build_where(*args) new_relation.#{query_method}_values += [*value] if value.present? new_relation @@ -32,7 +32,7 @@ module ActiveRecord class_eval <<-CEVAL def #{query_method}(value = true) - new_relation = spawn + new_relation = clone new_relation.#{query_method}_value = value new_relation end @@ -41,12 +41,12 @@ module ActiveRecord end def lock(locks = true) - relation = spawn + relation = clone case locks when String, TrueClass, NilClass - spawn.tap {|new_relation| new_relation.lock_value = locks || true } + clone.tap {|new_relation| new_relation.lock_value = locks || true } else - spawn.tap {|new_relation| new_relation.lock_value = false } + clone.tap {|new_relation| new_relation.lock_value = false } end end diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index cccf413e67..a18380f01c 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -1,11 +1,7 @@ module ActiveRecord module SpawnMethods - def spawn - clone.reset - end - def merge(r) - merged_relation = spawn + merged_relation = clone return merged_relation unless r merged_relation = merged_relation.eager_load(r.eager_load_values).preload(r.preload_values).includes(r.includes_values) @@ -83,7 +79,7 @@ module ActiveRecord :order, :select, :readonly, :group, :having, :from, :lock ] def apply_finder_options(options) - relation = spawn + relation = clone return relation unless options options.assert_valid_keys(VALID_FIND_OPTIONS) -- cgit v1.2.3 From e08c153a08a0af79b36853c0ad8bb6e916ca3e66 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 10 Mar 2010 18:30:09 -0300 Subject: Arel now fallback to using Arel::Attribute if the table/column doesn't exists. [#4142 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/relation/predicate_builder.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 49bf2fefe0..7e83eccbb5 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -20,9 +20,7 @@ module ActiveRecord table = Arel::Table.new(table_name, :engine => @engine) end - # TODO : Arel::Table#[] should fallback to using Arel::Attribute if the table/column doesn't exist - # attribute = table[column] - attribute = Arel::Attribute.new(table, column.to_sym) + attribute = table[column] case value when Array, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope -- cgit v1.2.3 From 275df5510699f7426775e8425fdcea0a30b4e615 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Mar 2010 13:44:07 -0800 Subject: Bump arel dep to 0.3.0+ --- activerecord/activerecord.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index 8c7f3f5f2f..c07e495950 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -22,5 +22,5 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', version) s.add_dependency('activemodel', version) - s.add_dependency('arel', '~> 0.2.1') + s.add_dependency('arel', '~> 0.3.0') end -- cgit v1.2.3 From c5ff181480c2add95401799a85b8f5bb1356aef1 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 10 Mar 2010 14:04:10 -0800 Subject: Use arel 0.3.1, for srs --- activerecord/activerecord.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec index c07e495950..8b1ea8596a 100644 --- a/activerecord/activerecord.gemspec +++ b/activerecord/activerecord.gemspec @@ -22,5 +22,5 @@ Gem::Specification.new do |s| s.add_dependency('activesupport', version) s.add_dependency('activemodel', version) - s.add_dependency('arel', '~> 0.3.0') + s.add_dependency('arel', '~> 0.3.1') end -- cgit v1.2.3