diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 26 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/json_test.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/json_shared_test_cases.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/scoping/named_scoping_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 19 |
9 files changed, 49 insertions, 52 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 1a424896fe..38e7ba39d3 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -74,23 +74,19 @@ module ActiveRecord end def find(*args) - if block_given? - load_target.find(*args) { |*block_args| yield(*block_args) } - else - if options[:inverse_of] && loaded? - args_flatten = args.flatten - raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args_flatten.blank? - result = find_by_scan(*args) - - result_size = Array(result).size - if !result || result_size != args_flatten.size - scope.raise_record_not_found_exception!(args_flatten, result_size, args_flatten.size) - else - result - end + if options[:inverse_of] && loaded? + args_flatten = args.flatten + raise RecordNotFound, "Couldn't find #{scope.klass.name} without an ID" if args_flatten.blank? + result = find_by_scan(*args) + + result_size = Array(result).size + if !result || result_size != args_flatten.size + scope.raise_record_not_found_exception!(args_flatten, result_size, args_flatten.size) else - scope.find(*args) + result end + else + scope.find(*args) end end diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 0678b07699..412e89255d 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -135,8 +135,9 @@ module ActiveRecord # # #<Pet id: 2, name: "Spook", person_id: 1>, # # #<Pet id: 3, name: "Choo-Choo", person_id: 1> # # ] - def find(*args, &block) - @association.find(*args, &block) + def find(*args) + return super if block_given? + @association.find(*args) end ## diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index fbb4c671a5..a84e62ab11 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -143,7 +143,7 @@ module ActiveRecord self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new end - module ClassMethods + module ClassMethods # :nodoc: def allocate define_attribute_methods super @@ -251,7 +251,7 @@ module ActiveRecord end end - # Overwrite the default class equality method to provide support for association proxies. + # Overwrite the default class equality method to provide support for decorated models. def ===(object) object.is_a?(self) end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index d044f9dfe8..d2b85e168b 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -1073,10 +1073,6 @@ module ActiveRecord @reflection.scope end - def table_name - @reflection.table_name - end - def plural_name @reflection.plural_name end @@ -1112,10 +1108,6 @@ module ActiveRecord @association.klass end - def table_name - klass.table_name - end - def constraints @reflection.constraints end diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb index 79dcfe110c..ee08841eb3 100644 --- a/activerecord/test/cases/adapters/postgresql/json_test.rb +++ b/activerecord/test/cases/adapters/postgresql/json_test.rb @@ -21,8 +21,8 @@ module PostgresqlJSONSharedTestCases @connection.add_column "json_data_type", "permissions", column_type, default: { "users": "read", "posts": ["read", "write"] } klass.reset_column_information - assert_equal({ "users" => "read", "posts" => ["read", "write"] }, JsonDataType.column_defaults["permissions"]) - assert_equal({ "users" => "read", "posts" => ["read", "write"] }, JsonDataType.new.permissions) + assert_equal({ "users" => "read", "posts" => ["read", "write"] }, klass.column_defaults["permissions"]) + assert_equal({ "users" => "read", "posts" => ["read", "write"] }, klass.new.permissions) end def test_deserialize_with_array @@ -33,15 +33,6 @@ module PostgresqlJSONSharedTestCases x.reload assert_equal ["foo" => "bar"], x.objects end - - def test_not_compatible_with_serialize_macro - new_klass = Class.new(klass) do - serialize :payload, JSON - end - assert_raises(ActiveRecord::AttributeMethods::Serialization::ColumnNotSerializableError) do - new_klass.new - end - end end class PostgresqlJSONTest < ActiveRecord::PostgreSQLTestCase diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 1d33564989..1a1d4ce039 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -885,10 +885,17 @@ class BasicsTest < ActiveRecord::TestCase def test_bignum company = Company.find(1) - company.rating = 2147483647 + company.rating = 2147483648 company.save company = Company.find(1) - assert_equal 2147483647, company.rating + assert_equal 2147483648, company.rating + end + + unless current_adapter?(:SQLite3Adapter) + def test_bignum_pk + company = Company.create!(id: 2147483648, name: "foo") + assert_equal company, Company.find(company.id) + end end # TODO: extend defaults tests to other databases! diff --git a/activerecord/test/cases/json_shared_test_cases.rb b/activerecord/test/cases/json_shared_test_cases.rb index f708acf0aa..952194c6dc 100644 --- a/activerecord/test/cases/json_shared_test_cases.rb +++ b/activerecord/test/cases/json_shared_test_cases.rb @@ -216,6 +216,15 @@ module JSONSharedTestCases assert_equal true, json.payload end + def test_not_compatible_with_serialize_macro + new_klass = Class.new(klass) do + serialize :payload, JSON + end + assert_raises(ActiveRecord::AttributeMethods::Serialization::ColumnNotSerializableError) do + new_klass.new + end + end + private def klass JsonDataType diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index 1db7432e34..b0431a4e34 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -117,7 +117,8 @@ class NamedScopingTest < ActiveRecord::TestCase assert_not_equal Post.containing_the_letter_a, authors(:david).posts assert !Post.containing_the_letter_a.empty? - assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a + expected = authors(:david).posts & Post.containing_the_letter_a + assert_equal expected.sort_by(&:id), authors(:david).posts.containing_the_letter_a.sort_by(&:id) end def test_scope_with_STI @@ -129,7 +130,8 @@ class NamedScopingTest < ActiveRecord::TestCase assert_not_equal Comment.containing_the_letter_e, authors(:david).comments assert !Comment.containing_the_letter_e.empty? - assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e + expected = authors(:david).comments & Comment.containing_the_letter_e + assert_equal expected.sort_by(&:id), authors(:david).comments.containing_the_letter_e.sort_by(&:id) end def test_scopes_honor_current_scopes_from_when_defined diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 47749c07d2..8f872c38ba 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -9,7 +9,7 @@ ActiveRecord::Schema.define do # ------------------------------------------------------------------- # create_table :accounts, force: true do |t| - t.integer :firm_id + t.references :firm, index: false t.string :firm_name t.integer :credit_limit end @@ -197,11 +197,11 @@ ActiveRecord::Schema.define do create_table :companies, force: true do |t| t.string :type - t.integer :firm_id + t.references :firm, index: false t.string :firm_name t.string :name - t.integer :client_of - t.integer :rating, default: 1 + t.bigint :client_of + t.bigint :rating, default: 1 t.integer :account_id t.string :description, default: "" t.index [:firm_id, :type, :rating], name: "company_index", length: { type: 10 }, order: { rating: :desc } @@ -236,8 +236,8 @@ ActiveRecord::Schema.define do end create_table :contracts, force: true do |t| - t.integer :developer_id - t.integer :company_id + t.references :developer, index: false + t.references :company, index: false end create_table :customers, force: true do |t| @@ -263,7 +263,7 @@ ActiveRecord::Schema.define do t.string :name t.string :first_name t.integer :salary, default: 70000 - t.integer :firm_id + t.references :firm, index: false t.integer :mentor_id if subsecond_precision_supported? t.datetime :created_at, precision: 6 @@ -720,7 +720,7 @@ ActiveRecord::Schema.define do create_table :projects, force: true do |t| t.string :name t.string :type - t.integer :firm_id + t.references :firm, index: false t.integer :mentor_id end @@ -809,8 +809,7 @@ ActiveRecord::Schema.define do create_table :sponsors, force: true do |t| t.integer :club_id - t.integer :sponsorable_id - t.string :sponsorable_type + t.references :sponsorable, polymorphic: true, index: false end create_table :string_key_objects, id: false, force: true do |t| |