diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-08-03 11:51:52 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-08-03 11:51:52 +0100 |
commit | 55b24888bbe23af3361fa23891c150e28f27809e (patch) | |
tree | 1bb4d28de4838071f31afcf861fc623c06359966 | |
parent | 0a6833b6f701c8c8febadfe2f45e25df29493602 (diff) | |
download | rails-55b24888bbe23af3361fa23891c150e28f27809e.tar.gz rails-55b24888bbe23af3361fa23891c150e28f27809e.tar.bz2 rails-55b24888bbe23af3361fa23891c150e28f27809e.zip |
Remove ActiveRecord::Base.to_a
On reflection, it seems like a bit of a weird method to have on
ActiveRecord::Base, and it shouldn't be needed most of the time anyway.
18 files changed, 55 insertions, 64 deletions
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index ab8bcae402..acc96ddc9f 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -214,7 +214,7 @@ module ActiveModel # end # end def observed_classes - [observed_class].compact.flatten + Array(observed_class) end # The class observed by default is inferred from the observer's class name: diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index cb7dbfe454..8275577467 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -54,8 +54,7 @@ *Jon Leighton* * `Model.all` now returns an `ActiveRecord::Relation`, rather than an - array of records. Use `Model.to_a` or `Relation#to_a` if you really - want an array. + array of records. Use ``Relation#to_a` if you really want an array. In some specific cases, this may cause breakage when upgrading. However in most cases the `ActiveRecord::Relation` will just act as a diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 79713b6970..13e09eda53 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -1,7 +1,7 @@ module ActiveRecord module Querying - delegate :find, :take, :take!, :first, :first!, :last, :last!, :to_a, :exists?, :any?, :many?, :to => :all + delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :to => :all delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :all delegate :find_by, :find_by!, :to => :all delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :all diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 53bc60445c..f3520d43e0 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -359,7 +359,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase def test_deleting_array david = Developer.find(1) david.projects.reload - david.projects.delete(Project.to_a) + david.projects.delete(Project.all.to_a) assert_equal 0, david.projects.size assert_equal 0, david.projects(true).size end @@ -426,7 +426,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase def test_destroying_many david = Developer.find(1) david.projects.reload - projects = Project.to_a + projects = Project.all.to_a assert_no_difference "Project.count" do david.projects.destroy(*projects) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index bceec80b6b..43440c1146 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1130,7 +1130,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_adding_array_and_collection - assert_nothing_raised { Firm.first.clients + Firm.to_a.last.clients } + assert_nothing_raised { Firm.first.clients + Firm.all.last.clients } end def test_replace_with_less diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 195597ace6..062f196a12 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1635,9 +1635,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_all - developers = Developer.to_a - assert_kind_of Array, developers - assert_equal Developer.to_a, developers + developers = Developer.all + assert_kind_of ActiveRecord::Relation, developers + assert_equal Developer.all, developers end def test_all_with_conditions @@ -2032,12 +2032,4 @@ class BasicsTest < ActiveRecord::TestCase klass = Class.new(ActiveRecord::Base) assert_equal ['foo'], klass.all.merge!(select: 'foo').select_values end - - test "Model.to_a returns an array" do - assert_equal Post.all.to_a, Post.to_a - end - - test "Model.all returns a relation" do - assert Post.all.is_a?(ActiveRecord::Relation) - end end diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 7583539d04..cdd4b49042 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -116,7 +116,7 @@ class EachTest < ActiveRecord::TestCase end def test_find_in_batches_should_not_ignore_the_default_scope_if_it_is_other_then_order - special_posts_ids = SpecialPostWithDefaultScope.to_a.map(&:id).sort + special_posts_ids = SpecialPostWithDefaultScope.all.map(&:id).sort posts = [] SpecialPostWithDefaultScope.find_in_batches do |batch| posts.concat(batch) diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 55f6ec06c0..40e712072f 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -542,7 +542,7 @@ class CalculationsTest < ActiveRecord::TestCase end def test_plucks_with_ids - assert_equal Company.to_a.map(&:id).sort, Company.ids.sort + assert_equal Company.all.map(&:id).sort, Company.ids.sort end def test_pluck_not_auto_table_name_prefix_if_column_included diff --git a/activerecord/test/cases/explain_test.rb b/activerecord/test/cases/explain_test.rb index 52e747db35..6dce8ccdd1 100644 --- a/activerecord/test/cases/explain_test.rb +++ b/activerecord/test/cases/explain_test.rb @@ -112,7 +112,7 @@ if ActiveRecord::Base.connection.supports_explain? base.expects(:collecting_sqls_for_explain).never base.logger.expects(:warn).never base.silence_auto_explain do - with_threshold(0) { Car.to_a } + with_threshold(0) { Car.all } end end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 342f36f626..20c8e8894d 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -120,7 +120,7 @@ class FinderTest < ActiveRecord::TestCase # Also test an edge case: If you have 11 results, and you set a # limit of 3 and offset of 9, then you should find that there # will be only 2 results, regardless of the limit. - devs = Developer.to_a + devs = Developer.all last_devs = Developer.all.merge!(:limit => 3, :offset => 9).find devs.map(&:id) assert_equal 2, last_devs.size end diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index b14f580c77..e80259a7f1 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -27,7 +27,7 @@ class InheritanceTest < ActiveRecord::TestCase end }) company.save! - company = Company.to_a.find { |x| x.id == company.id } + company = Company.all.to_a.find { |x| x.id == company.id } assert_equal ' ', company.type end @@ -179,7 +179,7 @@ class InheritanceTest < ActiveRecord::TestCase def test_update_all_within_inheritance Client.update_all "name = 'I am a client'" - assert_equal "I am a client", Client.to_a.first.name + assert_equal "I am a client", Client.first.name # Order by added as otherwise Oracle tests were failing because of different order of results assert_equal "37signals", Firm.all.merge!(:order => "id").to_a.first.name end diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index 049a57d8b9..70d00aecf9 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -54,7 +54,7 @@ class LogSubscriberTest < ActiveRecord::TestCase end def test_basic_query_logging - Developer.to_a + Developer.all.load wait assert_equal 1, @logger.logged(:debug).size assert_match(/Developer Load/, @logger.logged(:debug).last) @@ -71,8 +71,8 @@ class LogSubscriberTest < ActiveRecord::TestCase def test_cached_queries ActiveRecord::Base.cache do - Developer.to_a - Developer.to_a + Developer.all.load + Developer.all.load end wait assert_equal 2, @logger.logged(:debug).size @@ -82,7 +82,7 @@ class LogSubscriberTest < ActiveRecord::TestCase def test_basic_query_doesnt_log_when_level_is_not_debug @logger.level = INFO - Developer.to_a + Developer.all.load wait assert_equal 0, @logger.logged(:debug).size end @@ -90,8 +90,8 @@ class LogSubscriberTest < ActiveRecord::TestCase def test_cached_queries_doesnt_log_when_level_is_not_debug @logger.level = INFO ActiveRecord::Base.cache do - Developer.to_a - Developer.to_a + Developer.all.load + Developer.all.load end wait assert_equal 0, @logger.logged(:debug).size diff --git a/activerecord/test/cases/migration/rename_column_test.rb b/activerecord/test/cases/migration/rename_column_test.rb index df45445ef2..d1a85ee5e4 100644 --- a/activerecord/test/cases/migration/rename_column_test.rb +++ b/activerecord/test/cases/migration/rename_column_test.rb @@ -33,7 +33,7 @@ module ActiveRecord rename_column :test_models, :first_name, :nick_name TestModel.reset_column_information assert TestModel.column_names.include?("nick_name") - assert_equal ['foo'], TestModel.to_a.map(&:nick_name) + assert_equal ['foo'], TestModel.all.map(&:nick_name) end # FIXME: another integration test. We should decouple this from the @@ -46,7 +46,7 @@ module ActiveRecord rename_column "test_models", "first_name", "nick_name" TestModel.reset_column_information assert TestModel.column_names.include?("nick_name") - assert_equal ['foo'], TestModel.to_a.map(&:nick_name) + assert_equal ['foo'], TestModel.all.map(&:nick_name) end def test_rename_column_preserves_default_value_not_null diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 9f0e2d5fc8..bd121126e7 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -10,12 +10,12 @@ class NamedScopeTest < ActiveRecord::TestCase fixtures :posts, :authors, :topics, :comments, :author_addresses def test_implements_enumerable - assert !Topic.to_a.empty? + assert !Topic.all.empty? - assert_equal Topic.to_a, Topic.base - assert_equal Topic.to_a, Topic.base.to_a - assert_equal Topic.first, Topic.base.first - assert_equal Topic.to_a, Topic.base.map { |i| i } + assert_equal Topic.all.to_a, Topic.base + assert_equal Topic.all.to_a, Topic.base.to_a + assert_equal Topic.first, Topic.base.first + assert_equal Topic.all.to_a, Topic.base.map { |i| i } end def test_found_items_are_cached @@ -37,11 +37,11 @@ class NamedScopeTest < ActiveRecord::TestCase end def test_delegates_finds_and_calculations_to_the_base_class - assert !Topic.to_a.empty? + assert !Topic.all.empty? - assert_equal Topic.to_a, Topic.base.to_a - assert_equal Topic.first, Topic.base.first - assert_equal Topic.count, Topic.base.count + assert_equal Topic.all.to_a, Topic.base.to_a + assert_equal Topic.first, Topic.base.first + assert_equal Topic.count, Topic.base.count assert_equal Topic.average(:replies_count), Topic.base.average(:replies_count) end @@ -97,7 +97,7 @@ class NamedScopeTest < ActiveRecord::TestCase end def test_procedural_scopes_returning_nil - all_topics = Topic.to_a + all_topics = Topic.all assert_equal all_topics, Topic.written_before(nil) end @@ -138,9 +138,9 @@ class NamedScopeTest < ActiveRecord::TestCase end def test_active_records_have_scope_named__all__ - assert !Topic.to_a.empty? + assert !Topic.all.empty? - assert_equal Topic.to_a, Topic.base + assert_equal Topic.all.to_a, Topic.base end def test_active_records_have_scope_named__scoped__ @@ -444,6 +444,6 @@ class NamedScopeTest < ActiveRecord::TestCase assert_deprecated do klass.send(:default_scope, klass.where(:id => posts(:welcome).id)) end - assert_equal [posts(:welcome).title], klass.to_a.map(&:title) + assert_equal [posts(:welcome).title], klass.all.map(&:title) end end diff --git a/activerecord/test/cases/readonly_test.rb b/activerecord/test/cases/readonly_test.rb index 9b9f11c7de..df076c97b4 100644 --- a/activerecord/test/cases/readonly_test.rb +++ b/activerecord/test/cases/readonly_test.rb @@ -28,7 +28,7 @@ class ReadOnlyTest < ActiveRecord::TestCase def test_find_with_readonly_option - Developer.to_a.each { |d| assert !d.readonly? } + Developer.all.each { |d| assert !d.readonly? } Developer.readonly(false).each { |d| assert !d.readonly? } Developer.readonly(true).each { |d| assert d.readonly? } Developer.readonly.each { |d| assert d.readonly? } diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index ef9905da2e..d318dab1e1 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -70,7 +70,7 @@ class RelationScopingTest < ActiveRecord::TestCase def test_scoped_find_all Developer.where("name = 'David'").scoping do - assert_equal [developers(:david)], Developer.to_a + assert_equal [developers(:david)], Developer.all end end @@ -179,7 +179,7 @@ class NestedRelationScopingTest < ActiveRecord::TestCase def test_merge_inner_scope_has_priority Developer.limit(5).scoping do Developer.limit(10).scoping do - assert_equal 10, Developer.to_a.size + assert_equal 10, Developer.all.size end end end @@ -313,32 +313,32 @@ class DefaultScopingTest < ActiveRecord::TestCase def test_default_scope expected = Developer.all.merge!(:order => 'salary DESC').to_a.collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.to_a.collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all.collect { |dev| dev.salary } assert_equal expected, received end def test_default_scope_as_class_method - assert_equal [developers(:david).becomes(ClassMethodDeveloperCalledDavid)], ClassMethodDeveloperCalledDavid.to_a + assert_equal [developers(:david).becomes(ClassMethodDeveloperCalledDavid)], ClassMethodDeveloperCalledDavid.all end def test_default_scope_as_class_method_referencing_scope - assert_equal [developers(:david).becomes(ClassMethodReferencingScopeDeveloperCalledDavid)], ClassMethodReferencingScopeDeveloperCalledDavid.to_a + assert_equal [developers(:david).becomes(ClassMethodReferencingScopeDeveloperCalledDavid)], ClassMethodReferencingScopeDeveloperCalledDavid.all end def test_default_scope_as_block_referencing_scope - assert_equal [developers(:david).becomes(LazyBlockReferencingScopeDeveloperCalledDavid)], LazyBlockReferencingScopeDeveloperCalledDavid.to_a + assert_equal [developers(:david).becomes(LazyBlockReferencingScopeDeveloperCalledDavid)], LazyBlockReferencingScopeDeveloperCalledDavid.all end def test_default_scope_with_lambda - assert_equal [developers(:david).becomes(LazyLambdaDeveloperCalledDavid)], LazyLambdaDeveloperCalledDavid.to_a + assert_equal [developers(:david).becomes(LazyLambdaDeveloperCalledDavid)], LazyLambdaDeveloperCalledDavid.all end def test_default_scope_with_block - assert_equal [developers(:david).becomes(LazyBlockDeveloperCalledDavid)], LazyBlockDeveloperCalledDavid.to_a + assert_equal [developers(:david).becomes(LazyBlockDeveloperCalledDavid)], LazyBlockDeveloperCalledDavid.all end def test_default_scope_with_callable - assert_equal [developers(:david).becomes(CallableDeveloperCalledDavid)], CallableDeveloperCalledDavid.to_a + assert_equal [developers(:david).becomes(CallableDeveloperCalledDavid)], CallableDeveloperCalledDavid.all end def test_default_scope_is_unscoped_on_find @@ -351,12 +351,12 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scope_with_conditions_string - assert_equal Developer.where(name: 'David').map(&:id).sort, DeveloperCalledDavid.to_a.map(&:id).sort + assert_equal Developer.where(name: 'David').map(&:id).sort, DeveloperCalledDavid.all.map(&:id).sort assert_equal nil, DeveloperCalledDavid.create!.name end def test_default_scope_with_conditions_hash - assert_equal Developer.where(name: 'Jamis').map(&:id).sort, DeveloperCalledJamis.to_a.map(&:id).sort + assert_equal Developer.where(name: 'Jamis').map(&:id).sort, DeveloperCalledJamis.all.map(&:id).sort assert_equal 'Jamis', DeveloperCalledJamis.create!.name end @@ -472,16 +472,16 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scope_select_ignored_by_aggregations - assert_equal DeveloperWithSelect.to_a.count, DeveloperWithSelect.count + assert_equal DeveloperWithSelect.all.to_a.count, DeveloperWithSelect.count end def test_default_scope_select_ignored_by_grouped_aggregations - assert_equal Hash[Developer.to_a.group_by(&:salary).map { |s, d| [s, d.count] }], + assert_equal Hash[Developer.all.group_by(&:salary).map { |s, d| [s, d.count] }], DeveloperWithSelect.group(:salary).count end def test_default_scope_order_ignored_by_aggregations - assert_equal DeveloperOrderedBySalary.to_a.count, DeveloperOrderedBySalary.count + assert_equal DeveloperOrderedBySalary.all.count, DeveloperOrderedBySalary.count end def test_default_scope_find_last @@ -508,10 +508,10 @@ class DefaultScopingTest < ActiveRecord::TestCase threads << Thread.new do Thread.current[:long_default_scope] = true - assert_equal 1, ThreadsafeDeveloper.to_a.count + assert_equal 1, ThreadsafeDeveloper.all.to_a.count end threads << Thread.new do - assert_equal 1, ThreadsafeDeveloper.to_a.count + assert_equal 1, ThreadsafeDeveloper.all.to_a.count end threads.each(&:join) end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 1fbd76bc03..0bd48913e1 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1032,7 +1032,7 @@ class RelationTest < ActiveRecord::TestCase assert_equal Post.where(:author_id => 1).to_a, author_posts.to_a all_posts = relation.except(:where, :order, :limit) - assert_equal Post.to_a, all_posts.to_a + assert_equal Post.all, all_posts end def test_only diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index ff4f1412d2..7249ae9e4d 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -286,7 +286,7 @@ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase # getting appended to. def test_modules - projects = MyApplication::Business::Project.to_a + projects = MyApplication::Business::Project.all xml = projects.to_xml root = projects.first.class.to_s.underscore.pluralize.tr('/','_').dasherize assert_match "<#{root} type=\"array\">", xml |