aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb1
-rw-r--r--activerecord/lib/active_record/autosave_association.rb14
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb57
-rw-r--r--activerecord/test/cases/associations/eager_load_nested_include_test.rb14
-rw-r--r--activerecord/test/cases/autosave_association_test.rb8
-rw-r--r--activerecord/test/cases/json_serialization_test.rb19
-rw-r--r--activerecord/test/cases/named_scope_test.rb2
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb4
9 files changed, 37 insertions, 84 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 7d5fd8e96c..95d76ae456 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -2,6 +2,7 @@ require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/string/conversions'
module ActiveRecord
class InverseOfAssociationNotFoundError < ActiveRecordError #:nodoc:
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index c553e95bad..0dcadfab5f 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/array/wrap'
+
module ActiveRecord
# AutosaveAssociation is a module that takes care of automatically saving
# your associations when the parent is saved. In addition to saving, it
@@ -238,16 +240,10 @@ module ActiveRecord
# go through nested autosave associations that are loaded in memory (without loading
# any new ones), and return true if is changed for autosave
def nested_records_changed_for_autosave?
- self.class.reflect_on_all_autosave_associations.each do |reflection|
- if association = association_instance_get(reflection.name)
- if [:belongs_to, :has_one].include?(reflection.macro)
- return true if association.target && association.target.changed_for_autosave?
- else
- association.target.each {|record| return true if record.changed_for_autosave? }
- end
- end
+ self.class.reflect_on_all_autosave_associations.any? do |reflection|
+ association = association_instance_get(reflection.name)
+ association && Array.wrap(association.target).any?(&:changed_for_autosave?)
end
- false
end
# Validate the association if <tt>:validate</tt> or <tt>:autosave</tt> is
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index bf8c546d2e..454d3e60e3 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -211,7 +211,7 @@ module ActiveRecord
# calling +checkout+ on this pool.
def checkin(conn)
@connection_mutex.synchronize do
- conn.run_callbacks :checkin do
+ conn.send(:_run_checkin_callbacks) do
@checked_out.delete conn
@queue.signal
end
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index ec25bbf18e..7c7bc5e292 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -3,48 +3,6 @@ require 'active_support/core_ext/kernel/requires'
require 'active_support/core_ext/object/blank'
require 'set'
-module MysqlCompat #:nodoc:
- # add all_hashes method to standard mysql-c bindings or pure ruby version
- def self.define_all_hashes_method!
- raise 'Mysql not loaded' unless defined?(::Mysql)
-
- target = defined?(Mysql::Result) ? Mysql::Result : MysqlRes
- return if target.instance_methods.include?('all_hashes') ||
- target.instance_methods.include?(:all_hashes)
-
- # Ruby driver has a version string and returns null values in each_hash
- # C driver >= 2.7 returns null values in each_hash
- if Mysql.const_defined?(:VERSION) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700)
- target.class_eval <<-'end_eval', __FILE__, __LINE__ + 1
- def all_hashes # def all_hashes
- rows = [] # rows = []
- each_hash { |row| rows << row } # each_hash { |row| rows << row }
- rows # rows
- end # end
- end_eval
-
- # adapters before 2.7 don't have a version constant
- # and don't return null values in each_hash
- else
- target.class_eval <<-'end_eval', __FILE__, __LINE__ + 1
- def all_hashes # def all_hashes
- rows = [] # rows = []
- all_fields = fetch_fields.inject({}) { |fields, f| # all_fields = fetch_fields.inject({}) { |fields, f|
- fields[f.name] = nil; fields # fields[f.name] = nil; fields
- } # }
- each_hash { |row| rows << all_fields.dup.update(row) } # each_hash { |row| rows << all_fields.dup.update(row) }
- rows # rows
- end # end
- end_eval
- end
-
- unless target.instance_methods.include?('all_hashes') ||
- target.instance_methods.include?(:all_hashes)
- raise "Failed to defined #{target.name}#all_hashes method. Mysql::VERSION = #{Mysql::VERSION.inspect}"
- end
- end
-end
-
module ActiveRecord
class Base
# Establishes a connection to the database that's used by all Active Record objects.
@@ -57,17 +15,17 @@ module ActiveRecord
password = config[:password].to_s
database = config[:database]
- # Require the MySQL driver and define Mysql::Result.all_hashes
unless defined? Mysql
begin
- require_library_or_gem('mysql')
+ require 'mysql'
rescue LoadError
- $stderr.puts '!!! Please install the mysql gem and try again: gem install mysql.'
- raise
+ raise "!!! Missing the mysql gem. Add it to your Gemfile: gem 'mysql', '2.8.1'"
end
- end
- MysqlCompat.define_all_hashes_method!
+ unless defined?(Mysql::Result) && Mysql::Result.method_defined?(:each_hash)
+ raise "!!! Outdated mysql gem. Upgrade to 2.8.1 or later. In your Gemfile: gem 'mysql', '2.8.1'"
+ end
+ end
mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey]
@@ -656,7 +614,8 @@ module ActiveRecord
def select(sql, name = nil)
@connection.query_with_result = true
result = execute(sql, name)
- rows = result.all_hashes
+ rows = []
+ result.each_hash { |row| rows << row }
result.free
rows
end
diff --git a/activerecord/test/cases/associations/eager_load_nested_include_test.rb b/activerecord/test/cases/associations/eager_load_nested_include_test.rb
index 2beb3f8365..c7671a8c22 100644
--- a/activerecord/test/cases/associations/eager_load_nested_include_test.rb
+++ b/activerecord/test/cases/associations/eager_load_nested_include_test.rb
@@ -17,7 +17,7 @@ module Remembered
module ClassMethods
def remembered; @@remembered ||= []; end
- def random_element; @@remembered.random_element; end
+ def sample; @@remembered.sample; end
end
end
@@ -79,14 +79,14 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
[Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
end
1.upto(NUM_SIMPLE_OBJS) do
- PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id)
- PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
+ PaintColor.create!(:non_poly_one_id => NonPolyOne.sample.id)
+ PaintTexture.create!(:non_poly_two_id => NonPolyTwo.sample.id)
end
1.upto(NUM_SHAPE_EXPRESSIONS) do
- shape_type = [Circle, Square, Triangle].random_element
- paint_type = [PaintColor, PaintTexture].random_element
- ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id,
- :paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id)
+ shape_type = [Circle, Square, Triangle].sample
+ paint_type = [PaintColor, PaintTexture].sample
+ ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.sample.id,
+ :paint_type => paint_type.to_s, :paint_id => paint_type.sample.id)
end
end
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index 063f0f0fb2..4e4f9c385c 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -1149,7 +1149,7 @@ class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::T
include AutosaveAssociationOnACollectionAssociationTests
end
-class TestAutosaveAssociationValidationsOnAHasManyAssocication < ActiveRecord::TestCase
+class TestAutosaveAssociationValidationsOnAHasManyAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def setup
@@ -1165,7 +1165,7 @@ class TestAutosaveAssociationValidationsOnAHasManyAssocication < ActiveRecord::T
end
end
-class TestAutosaveAssociationValidationsOnAHasOneAssocication < ActiveRecord::TestCase
+class TestAutosaveAssociationValidationsOnAHasOneAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def setup
@@ -1186,7 +1186,7 @@ class TestAutosaveAssociationValidationsOnAHasOneAssocication < ActiveRecord::Te
end
end
-class TestAutosaveAssociationValidationsOnABelongsToAssocication < ActiveRecord::TestCase
+class TestAutosaveAssociationValidationsOnABelongsToAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def setup
@@ -1206,7 +1206,7 @@ class TestAutosaveAssociationValidationsOnABelongsToAssocication < ActiveRecord:
end
end
-class TestAutosaveAssociationValidationsOnAHABTMAssocication < ActiveRecord::TestCase
+class TestAutosaveAssociationValidationsOnAHABTMAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def setup
diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb
index a3145d2c04..c275557da8 100644
--- a/activerecord/test/cases/json_serialization_test.rb
+++ b/activerecord/test/cases/json_serialization_test.rb
@@ -8,7 +8,7 @@ require 'models/comment'
class JsonSerializationTest < ActiveRecord::TestCase
class NamespacedContact < Contact
- column :name, :string
+ column :name, :string
end
def setup
@@ -23,16 +23,12 @@ class JsonSerializationTest < ActiveRecord::TestCase
end
def test_should_demodulize_root_in_json
- NamespacedContact.include_root_in_json = true
@contact = NamespacedContact.new :name => 'whatever'
json = @contact.to_json
assert_match %r{^\{"namespaced_contact":\{}, json
- ensure
- NamespacedContact.include_root_in_json = false
end
def test_should_include_root_in_json
- Contact.include_root_in_json = true
json = @contact.to_json
assert_match %r{^\{"contact":\{}, json
@@ -41,8 +37,6 @@ class JsonSerializationTest < ActiveRecord::TestCase
assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
assert_match %r{"awesome":true}, json
assert_match %r{"preferences":\{"shows":"anime"\}}, json
- ensure
- Contact.include_root_in_json = false
end
def test_should_encode_all_encodable_attributes
@@ -170,15 +164,19 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
end
def test_should_allow_only_option_for_list_of_authors
+ ActiveRecord::Base.include_root_in_json = false
authors = [@david, @mary]
-
assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
+ ensure
+ ActiveRecord::Base.include_root_in_json = true
end
def test_should_allow_except_option_for_list_of_authors
+ ActiveRecord::Base.include_root_in_json = false
authors = [@david, @mary]
-
assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
+ ensure
+ ActiveRecord::Base.include_root_in_json = true
end
def test_should_allow_includes_for_list_of_authors
@@ -201,7 +199,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
1 => @david,
2 => @mary
}
-
- assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
+ assert_equal %({"1":{"author":{"name":"David"}}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
end
end
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index 91d8d2828b..33ffb041c1 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -301,7 +301,7 @@ class NamedScopeTest < ActiveRecord::TestCase
end
def test_rand_should_select_a_random_object_from_proxy
- assert_kind_of Topic, Topic.approved.random_element
+ assert_kind_of Topic, Topic.approved.sample
end
def test_should_use_where_in_query_for_named_scope
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index 57b66fb312..685b11cb03 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -734,7 +734,7 @@ class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase
end
end
-class TestHasOneAutosaveAssoictaionWhichItselfHasAutosaveAssociations < ActiveRecord::TestCase
+class TestHasOneAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def setup
@@ -774,7 +774,7 @@ class TestHasOneAutosaveAssoictaionWhichItselfHasAutosaveAssociations < ActiveRe
end
end
-class TestHasManyAutosaveAssoictaionWhichItselfHasAutosaveAssociations < ActiveRecord::TestCase
+class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveRecord::TestCase
self.use_transactional_fixtures = false
def setup