diff options
Diffstat (limited to 'activerecord/test')
33 files changed, 448 insertions, 141 deletions
| diff --git a/activerecord/test/cases/adapters/mysql2/active_schema_test.rb b/activerecord/test/cases/adapters/mysql2/active_schema_test.rb index 4e73c557ed..6931b085a8 100644 --- a/activerecord/test/cases/adapters/mysql2/active_schema_test.rb +++ b/activerecord/test/cases/adapters/mysql2/active_schema_test.rb @@ -68,14 +68,14 @@ class Mysql2ActiveSchemaTest < ActiveRecord::Mysql2TestCase      def (ActiveRecord::Base.connection).data_source_exists?(*); false; end      %w(SPATIAL FULLTEXT UNIQUE).each do |type| -      expected = "CREATE TABLE `people` (#{type} INDEX `index_people_on_last_name`  (`last_name`)) ENGINE=InnoDB" +      expected = "CREATE TABLE `people` (#{type} INDEX `index_people_on_last_name`  (`last_name`))"        actual = ActiveRecord::Base.connection.create_table(:people, id: false) do |t|          t.index :last_name, type: type        end        assert_equal expected, actual      end -    expected = "CREATE TABLE `people` ( INDEX `index_people_on_last_name` USING btree (`last_name`(10))) ENGINE=InnoDB" +    expected = "CREATE TABLE `people` ( INDEX `index_people_on_last_name` USING btree (`last_name`(10)))"      actual = ActiveRecord::Base.connection.create_table(:people, id: false) do |t|        t.index :last_name, length: 10, using: :btree      end @@ -160,7 +160,7 @@ class Mysql2ActiveSchemaTest < ActiveRecord::Mysql2TestCase      ActiveRecord::Base.connection.stubs(:data_source_exists?).with(:temp).returns(false)      ActiveRecord::Base.connection.stubs(:index_name_exists?).with(:index_temp_on_zip).returns(false) -    expected = "CREATE TEMPORARY TABLE `temp` ( INDEX `index_temp_on_zip`  (`zip`)) ENGINE=InnoDB AS SELECT id, name, zip FROM a_really_complicated_query" +    expected = "CREATE TEMPORARY TABLE `temp` ( INDEX `index_temp_on_zip`  (`zip`)) AS SELECT id, name, zip FROM a_really_complicated_query"      actual = ActiveRecord::Base.connection.create_table(:temp, temporary: true, as: "SELECT id, name, zip FROM a_really_complicated_query") do |t|        t.index :zip      end diff --git a/activerecord/test/cases/adapters/mysql2/table_options_test.rb b/activerecord/test/cases/adapters/mysql2/table_options_test.rb index 9f6bd38a8c..1c92df940f 100644 --- a/activerecord/test/cases/adapters/mysql2/table_options_test.rb +++ b/activerecord/test/cases/adapters/mysql2/table_options_test.rb @@ -42,3 +42,78 @@ class Mysql2TableOptionsTest < ActiveRecord::Mysql2TestCase      assert_match %r{COLLATE=utf8mb4_bin}, options    end  end + +class Mysql2DefaultEngineOptionSchemaDumpTest < ActiveRecord::Mysql2TestCase +  include SchemaDumpingHelper +  self.use_transactional_tests = false + +  def setup +    @verbose_was = ActiveRecord::Migration.verbose +    ActiveRecord::Migration.verbose = false +  end + +  def teardown +    ActiveRecord::Base.connection.drop_table "mysql_table_options", if_exists: true +    ActiveRecord::Migration.verbose = @verbose_was +    ActiveRecord::SchemaMigration.delete_all rescue nil +  end + +  test "schema dump includes ENGINE=InnoDB if not provided" do +    ActiveRecord::Base.connection.create_table "mysql_table_options", force: true + +    output  = dump_table_schema("mysql_table_options") +    options = %r{create_table "mysql_table_options", options: "(?<options>.*)"}.match(output)[:options] +    assert_match %r{ENGINE=InnoDB}, options +  end + +  test "schema dump includes ENGINE=InnoDB in legacy migrations" do +    migration = Class.new(ActiveRecord::Migration[5.1]) do +      def migrate(x) +        create_table "mysql_table_options", force: true +      end +    end.new + +    ActiveRecord::Migrator.new(:up, [migration]).migrate + +    output  = dump_table_schema("mysql_table_options") +    options = %r{create_table "mysql_table_options", options: "(?<options>.*)"}.match(output)[:options] +    assert_match %r{ENGINE=InnoDB}, options +  end +end + +class Mysql2DefaultEngineOptionSqlOutputTest < ActiveRecord::Mysql2TestCase +  self.use_transactional_tests = false + +  def setup +    @logger_was  = ActiveRecord::Base.logger +    @log         = StringIO.new +    @verbose_was = ActiveRecord::Migration.verbose +    ActiveRecord::Base.logger = ActiveSupport::Logger.new(@log) +    ActiveRecord::Migration.verbose = false +  end + +  def teardown +    ActiveRecord::Base.logger       = @logger_was +    ActiveRecord::Migration.verbose = @verbose_was +    ActiveRecord::Base.connection.drop_table "mysql_table_options", if_exists: true +    ActiveRecord::SchemaMigration.delete_all rescue nil +  end + +  test "new migrations do not contain default ENGINE=InnoDB option" do +    ActiveRecord::Base.connection.create_table "mysql_table_options", force: true + +    assert_no_match %r{ENGINE=InnoDB}, @log.string +  end + +  test "legacy migrations contain default ENGINE=InnoDB option" do +    migration = Class.new(ActiveRecord::Migration[5.1]) do +      def migrate(x) +        create_table "mysql_table_options", force: true +      end +    end.new + +    ActiveRecord::Migrator.new(:up, [migration]).migrate + +    assert_match %r{ENGINE=InnoDB}, @log.string +  end +end diff --git a/activerecord/test/cases/adapters/postgresql/range_test.rb b/activerecord/test/cases/adapters/postgresql/range_test.rb index 813a8721a2..261c24634e 100644 --- a/activerecord/test/cases/adapters/postgresql/range_test.rb +++ b/activerecord/test/cases/adapters/postgresql/range_test.rb @@ -358,6 +358,18 @@ _SQL        end      end +    def test_infinity_values +      PostgresqlRange.create!(int4_range: 1..Float::INFINITY, +                              int8_range: -Float::INFINITY..0, +                              float_range: -Float::INFINITY..Float::INFINITY) + +      record = PostgresqlRange.first + +      assert_equal(1...Float::INFINITY, record.int4_range) +      assert_equal(-Float::INFINITY...1, record.int8_range) +      assert_equal(-Float::INFINITY...Float::INFINITY, record.float_range) +    end +      private        def assert_equal_round_trip(range, attribute, value)          round_trip(range, attribute, value) diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index 1357719422..20579c6476 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -372,9 +372,7 @@ module ActiveRecord          code = "214fe0c2-dd47-46df-b53b-66090b3c1d40"          Barcode.create!(code: code, other_attr: "xxx") -        connection.change_table "barcodes" do |t| -          connection.remove_column("barcodes", "other_attr") -        end +        connection.remove_column("barcodes", "other_attr")          assert_equal code, Barcode.first.id        ensure @@ -392,9 +390,7 @@ module ActiveRecord          code = "214fe0c2-dd47-46df-b53b-66090b3c1d40"          Barcode.create!(region: region, code: code, other_attr: "xxx") -        connection.change_table "barcodes" do |t| -          connection.remove_column("barcodes", "other_attr") -        end +        connection.remove_column("barcodes", "other_attr")          assert_equal ["region", "code"], connection.primary_keys("barcodes") @@ -405,6 +401,75 @@ module ActiveRecord          Barcode.reset_column_information        end +      def test_custom_primary_key_in_create_table +        connection = Barcode.connection +        connection.create_table :barcodes, id: false, force: true do |t| +          t.primary_key :id, :string +        end + +        assert_equal "id", connection.primary_key("barcodes") + +        custom_pk = Barcode.columns_hash["id"] + +        assert_equal :string, custom_pk.type +        assert_not custom_pk.null +      ensure +        Barcode.reset_column_information +      end + +      def test_custom_primary_key_in_change_table +        connection = Barcode.connection +        connection.create_table :barcodes, id: false, force: true do |t| +          t.integer :dummy +        end +        connection.change_table :barcodes do |t| +          t.primary_key :id, :string +        end + +        assert_equal "id", connection.primary_key("barcodes") + +        custom_pk = Barcode.columns_hash["id"] + +        assert_equal :string, custom_pk.type +        assert_not custom_pk.null +      ensure +        Barcode.reset_column_information +      end + +      def test_add_column_with_custom_primary_key +        connection = Barcode.connection +        connection.create_table :barcodes, id: false, force: true do |t| +          t.integer :dummy +        end +        connection.add_column :barcodes, :id, :string, primary_key: true + +        assert_equal "id", connection.primary_key("barcodes") + +        custom_pk = Barcode.columns_hash["id"] + +        assert_equal :string, custom_pk.type +        assert_not custom_pk.null +      ensure +        Barcode.reset_column_information +      end + +      def test_remove_column_preserves_partial_indexes +        connection = Barcode.connection +        connection.create_table :barcodes, force: true do |t| +          t.string :code +          t.string :region +          t.boolean :bool_attr + +          t.index :code, unique: true, where: :bool_attr, name: "partial" +        end +        connection.remove_column :barcodes, :region + +        index = connection.indexes("barcodes").find { |idx| idx.name == "partial" } +        assert_equal "bool_attr", index.where +      ensure +        Barcode.reset_column_information +      end +        def test_supports_extensions          assert_not @conn.supports_extensions?, "does not support extensions"        end @@ -435,6 +500,10 @@ module ActiveRecord          end        end +      def test_deprecate_valid_alter_table_type +        assert_deprecated { @conn.valid_alter_table_type?(:string) } +      end +        private          def assert_logged(logs) diff --git a/activerecord/test/cases/aggregations_test.rb b/activerecord/test/cases/aggregations_test.rb index 7f654ec6f6..fbdf2ada4b 100644 --- a/activerecord/test/cases/aggregations_test.rb +++ b/activerecord/test/cases/aggregations_test.rb @@ -27,7 +27,7 @@ class AggregationsTest < ActiveRecord::TestCase    def test_immutable_value_objects      customers(:david).balance = Money.new(100) -    assert_raise(RuntimeError) { customers(:david).balance.instance_eval { @amount = 20 } } +    assert_raise(frozen_error_class) { customers(:david).balance.instance_eval { @amount = 20 } }    end    def test_inferred_mapping diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 2649dc010f..9830917bc3 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -787,7 +787,7 @@ class EagerAssociationTest < ActiveRecord::TestCase      Tagging.create!(taggable_type: "Post", taggable_id: post2.id, tag: tag)      tag_with_includes = OrderedTag.includes(:tagged_posts).find(tag.id) -    assert_equal(tag_with_includes.taggings.map(&:taggable).map(&:title), tag_with_includes.tagged_posts.map(&:title)) +    assert_equal tag_with_includes.ordered_taggings.map(&:taggable).map(&:title), tag_with_includes.tagged_posts.map(&:title)    end    def test_eager_has_many_through_multiple_with_order diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 5ed8d0ee81..18548f8516 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -2509,6 +2509,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase      assert_same car, new_bulb.car    end +  test "reattach to new objects replaces inverse association and foreign key" do +    bulb = Bulb.create!(car: Car.create!) +    assert bulb.car_id +    car = Car.new +    car.bulbs << bulb +    assert_equal car, bulb.car +    assert_nil bulb.car_id +  end +    test "in memory replacement maintains order" do      first_bulb = Bulb.create!      second_bulb = Bulb.create! @@ -2520,6 +2529,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase      assert_equal [first_bulb, second_bulb], car.bulbs    end +  test "association size calculation works with default scoped selects when not previously fetched" do +    firm = Firm.create!(name: "Firm") +    5.times { firm.developers_with_select << Developer.create!(name: "Developer") } + +    same_firm = Firm.find(firm.id) +    assert_equal 5, same_firm.developers_with_select.size +  end +    test "prevent double insertion of new object when the parent association loaded in the after save callback" do      reset_callbacks(:save, Bulb) do        Bulb.after_save { |record| record.car.bulbs.load } diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 046020e310..7c9c9e81ab 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -869,6 +869,14 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase      assert_equal [dev], company.developers    end +  def test_collection_singular_ids_setter_with_required_type_cast +    company = companies(:rails_core) +    dev = Developer.first + +    company.developer_ids = [dev.id.to_s] +    assert_equal [dev], company.developers +  end +    def test_collection_singular_ids_setter_with_string_primary_keys      assert_nothing_raised do        book = books(:awdr) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 7fc4a396e4..a49990008c 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -104,7 +104,7 @@ class BasicsTest < ActiveRecord::TestCase      pk = Author.columns_hash["id"]      ref = Post.columns_hash["author_id"] -    assert_equal pk.bigint?, ref.bigint? +    assert_equal pk.sql_type, ref.sql_type    end    def test_many_mutations @@ -1498,10 +1498,14 @@ class BasicsTest < ActiveRecord::TestCase    test "column names are quoted when using #from clause and model has ignored columns" do      refute_empty Developer.ignored_columns -    query = Developer.from("`developers`").to_sql -    quoted_id = Developer.connection.quote_table_name("id") +    query = Developer.from("developers").to_sql +    quoted_id = "#{Developer.quoted_table_name}.#{Developer.quoted_primary_key}" -    assert_match(/SELECT #{quoted_id}.* FROM `developers`/, query) +    assert_match(/SELECT #{quoted_id}.* FROM developers/, query) +  end + +  test "using table name qualified column names unless having SELECT list explicitly" do +    assert_equal developers(:david), Developer.from("developers").joins(:shared_computers).take    end    test "protected environments by default is an array with production" do diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 55b50e4f84..e682da6fed 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -236,6 +236,12 @@ class CalculationsTest < ActiveRecord::TestCase      end    end +  def test_distinct_count_all_with_custom_select_and_order +    accounts = Account.distinct.select("credit_limit % 10").order(Arel.sql("credit_limit % 10")) +    assert_queries(1) { assert_equal 3, accounts.count(:all) } +    assert_queries(1) { assert_equal 3, accounts.load.size } +  end +    def test_distinct_count_with_order_and_limit      assert_equal 4, Account.distinct.order(:firm_id).limit(4).count    end @@ -682,6 +688,11 @@ class CalculationsTest < ActiveRecord::TestCase      assert_equal [], Topic.includes(:replies).limit(1).where("0 = 1").pluck(:id)    end +  def test_pluck_with_includes_offset +    assert_equal [5], Topic.includes(:replies).order(:id).offset(4).pluck(:id) +    assert_equal [], Topic.includes(:replies).order(:id).offset(5).pluck(:id) +  end +    def test_pluck_not_auto_table_name_prefix_if_column_included      Company.create!(name: "test", contracts: [Contract.new(developer_id: 7)])      ids = Company.includes(:contracts).pluck(:developer_id) diff --git a/activerecord/test/cases/collection_cache_key_test.rb b/activerecord/test/cases/collection_cache_key_test.rb index 479c9e03a5..cfe95b2360 100644 --- a/activerecord/test/cases/collection_cache_key_test.rb +++ b/activerecord/test/cases/collection_cache_key_test.rb @@ -141,5 +141,17 @@ module ActiveRecord        assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key)      end + +    test "cache_key with a relation having distinct and order" do +      developers = Developer.distinct.order(:salary).limit(5) + +      assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key) +    end + +    test "cache_key with a relation having custom select and order" do +      developers = Developer.select("name AS dev_name").order("dev_name DESC").limit(5) + +      assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, developers.cache_key) +    end    end  end diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index cae74a2b9b..603ed63a8c 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -232,15 +232,15 @@ module ActiveRecord          def test_a_class_using_custom_pool_and_switching_back_to_primary            klass2 = Class.new(Base) { def self.name; "klass2"; end } -          assert_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id +          assert_same klass2.connection, ActiveRecord::Base.connection            pool = klass2.establish_connection(ActiveRecord::Base.connection_pool.spec.config) -          assert_equal klass2.connection.object_id, pool.connection.object_id -          refute_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id +          assert_same klass2.connection, pool.connection +          refute_same klass2.connection, ActiveRecord::Base.connection            klass2.remove_connection -          assert_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id +          assert_same klass2.connection, ActiveRecord::Base.connection          end          def test_connection_specification_name_should_fallback_to_parent @@ -255,8 +255,8 @@ module ActiveRecord          def test_remove_connection_should_not_remove_parent            klass2 = Class.new(Base) { def self.name; "klass2"; end }            klass2.remove_connection -          refute_nil ActiveRecord::Base.connection.object_id -          assert_equal klass2.connection.object_id, ActiveRecord::Base.connection.object_id +          refute_nil ActiveRecord::Base.connection +          assert_same klass2.connection, ActiveRecord::Base.connection          end        end      end diff --git a/activerecord/test/cases/connection_adapters/type_lookup_test.rb b/activerecord/test/cases/connection_adapters/type_lookup_test.rb index 917a04ebc3..1c79d776f0 100644 --- a/activerecord/test/cases/connection_adapters/type_lookup_test.rb +++ b/activerecord/test/cases/connection_adapters/type_lookup_test.rb @@ -82,11 +82,11 @@ unless current_adapter?(:PostgreSQLAdapter) # PostgreSQL does not use type strin          end          def test_bigint_limit -          cast_type = @connection.send(:type_map).lookup("bigint") +          limit = @connection.send(:type_map).lookup("bigint").send(:_limit)            if current_adapter?(:OracleAdapter) -            assert_equal 19, cast_type.limit +            assert_equal 19, limit            else -            assert_equal 8, cast_type.limit +            assert_equal 8, limit            end          end diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb index 70c0ffb3bf..cb29c578b7 100644 --- a/activerecord/test/cases/connection_pool_test.rb +++ b/activerecord/test/cases/connection_pool_test.rb @@ -91,7 +91,7 @@ module ActiveRecord        end        def test_full_pool_exception -        @pool.size.times { @pool.checkout } +        @pool.size.times { assert @pool.checkout }          assert_raises(ConnectionTimeoutError) do            @pool.checkout          end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index a602f83d8c..d4408776d3 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -771,6 +771,13 @@ class DirtyTest < ActiveRecord::TestCase      assert person.changed?    end +  test "attributes not selected are still missing after save" do +    person = Person.select(:id).first +    assert_raises(ActiveModel::MissingAttributeError) { person.first_name } +    assert person.save # calls forget_attribute_assignments +    assert_raises(ActiveModel::MissingAttributeError) { person.first_name } +  end +    test "saved_change_to_attribute? returns whether a change occurred in the last save" do      person = Person.create!(first_name: "Sean") diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 62d5d88fcc..c0485a7be7 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -287,10 +287,8 @@ class FinderTest < ActiveRecord::TestCase    end    def test_exists_should_reference_correct_aliases_while_joining_tables_of_has_many_through_association -    assert_nothing_raised do -      developer = developers(:david) -      developer.ratings.includes(comment: :post).where(posts: { id: 1 }).exists? -    end +    developer = developers(:david) +    assert_not_predicate developer.ratings.includes(comment: :post).where(posts: { id: 1 }), :exists?    end    def test_exists_with_empty_table_and_no_args_given @@ -679,6 +677,10 @@ class FinderTest < ActiveRecord::TestCase      assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)      assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3) +    assert_equal comments.offset(2).to_a.last, comments.offset(2).last +    assert_equal comments.offset(2).to_a.last(2), comments.offset(2).last(2) +    assert_equal comments.offset(2).to_a.last(3), comments.offset(2).last(3) +      comments = comments.offset(1)      assert_equal comments.limit(2).to_a.last, comments.limit(2).last      assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2) @@ -1051,14 +1053,6 @@ class FinderTest < ActiveRecord::TestCase      assert_raise(ArgumentError) { Topic.find_by_title_and_author_name("The First Topic") }    end -  def test_find_last_with_offset -    devs = Developer.order("id") - -    assert_equal devs[2], Developer.offset(2).first -    assert_equal devs[-3], Developer.offset(2).last -    assert_equal devs[-3], Developer.offset(2).order("id DESC").first -  end -    def test_find_by_nil_attribute      topic = Topic.find_by_last_read nil      assert_not_nil topic diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 437a5a38a3..3701be4b11 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -406,7 +406,7 @@ class OptimisticLockingTest < ActiveRecord::TestCase      assert_equal 0, car.lock_version      previously_car_updated_at = car.updated_at -    travel(1.second) do +    travel(2.second) do        Wheel.create!(wheelable: car)      end @@ -422,7 +422,7 @@ class OptimisticLockingTest < ActiveRecord::TestCase      assert_equal 2, car.lock_version      previously_car_updated_at = car.updated_at -    travel(1.second) do +    travel(2.second) do        car.wheels.first.destroy!      end diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb index cc2391f349..26d3b3e29d 100644 --- a/activerecord/test/cases/migration/compatibility_test.rb +++ b/activerecord/test/cases/migration/compatibility_test.rb @@ -179,9 +179,7 @@ module LegacyPrimaryKeyTestCases      @migration.migrate(:up) -    legacy_pk = LegacyPrimaryKey.columns_hash["id"] -    assert_not legacy_pk.bigint? -    assert_not legacy_pk.null +    assert_legacy_primary_key      legacy_ref = LegacyPrimaryKey.columns_hash["legacy_ref_id"]      assert_not legacy_ref.bigint? @@ -216,55 +214,50 @@ module LegacyPrimaryKeyTestCases      assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema    end -  if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter) -    def test_legacy_primary_key_in_create_table_should_be_integer -      @migration = Class.new(migration_class) { -        def change -          create_table :legacy_primary_keys, id: false do |t| -            t.primary_key :id -          end +  def test_legacy_primary_key_in_create_table_should_be_integer +    @migration = Class.new(migration_class) { +      def change +        create_table :legacy_primary_keys, id: false do |t| +          t.primary_key :id          end -      }.new +      end +    }.new -      @migration.migrate(:up) +    @migration.migrate(:up) -      schema = dump_table_schema "legacy_primary_keys" -      assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema -    end +    assert_legacy_primary_key +  end -    def test_legacy_primary_key_in_change_table_should_be_integer -      @migration = Class.new(migration_class) { -        def change -          create_table :legacy_primary_keys, id: false do |t| -            t.integer :dummy -          end -          change_table :legacy_primary_keys do |t| -            t.primary_key :id -          end +  def test_legacy_primary_key_in_change_table_should_be_integer +    @migration = Class.new(migration_class) { +      def change +        create_table :legacy_primary_keys, id: false do |t| +          t.integer :dummy          end -      }.new +        change_table :legacy_primary_keys do |t| +          t.primary_key :id +        end +      end +    }.new -      @migration.migrate(:up) +    @migration.migrate(:up) -      schema = dump_table_schema "legacy_primary_keys" -      assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema -    end +    assert_legacy_primary_key +  end -    def test_add_column_with_legacy_primary_key_should_be_integer -      @migration = Class.new(migration_class) { -        def change -          create_table :legacy_primary_keys, id: false do |t| -            t.integer :dummy -          end -          add_column :legacy_primary_keys, :id, :primary_key +  def test_add_column_with_legacy_primary_key_should_be_integer +    @migration = Class.new(migration_class) { +      def change +        create_table :legacy_primary_keys, id: false do |t| +          t.integer :dummy          end -      }.new +        add_column :legacy_primary_keys, :id, :primary_key +      end +    }.new -      @migration.migrate(:up) +    @migration.migrate(:up) -      schema = dump_table_schema "legacy_primary_keys" -      assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema -    end +    assert_legacy_primary_key    end    def test_legacy_join_table_foreign_keys_should_be_integer @@ -333,6 +326,22 @@ module LegacyPrimaryKeyTestCases        assert_match %r{create_table "legacy_primary_keys", id: :bigint, default: nil}, schema      end    end + +  private +    def assert_legacy_primary_key +      assert_equal "id", LegacyPrimaryKey.primary_key + +      legacy_pk = LegacyPrimaryKey.columns_hash["id"] + +      assert_equal :integer, legacy_pk.type +      assert_not legacy_pk.bigint? +      assert_not legacy_pk.null + +      if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter) +        schema = dump_table_schema "legacy_primary_keys" +        assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema +      end +    end  end  module LegacyPrimaryKeyTest diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index b18af2ab55..a3ebc8070a 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -801,8 +801,15 @@ if ActiveRecord::Base.connection.supports_bulk_alter?          t.integer :age        end -      # Adding an index fires a query every time to check if an index already exists or not -      assert_queries(3) do +      classname = ActiveRecord::Base.connection.class.name[/[^:]*$/] +      expected_query_count = { +        "Mysql2Adapter"     => 3, # Adding an index fires a query every time to check if an index already exists or not +        "PostgreSQLAdapter" => 2, +      }.fetch(classname) { +        raise "need an expected query count for #{classname}" +      } + +      assert_queries(expected_query_count) do          with_bulk_change_table do |t|            t.index :username, unique: true, name: :awesome_username_index            t.index [:name, :age] @@ -826,7 +833,15 @@ if ActiveRecord::Base.connection.supports_bulk_alter?        assert index(:index_delete_me_on_name) -      assert_queries(3) do +      classname = ActiveRecord::Base.connection.class.name[/[^:]*$/] +      expected_query_count = { +        "Mysql2Adapter"     => 3, # Adding an index fires a query every time to check if an index already exists or not +        "PostgreSQLAdapter" => 2, +      }.fetch(classname) { +        raise "need an expected query count for #{classname}" +      } + +      assert_queries(expected_query_count) do          with_bulk_change_table do |t|            t.remove_index :name            t.index :name, name: :new_name_index, unique: true @@ -848,10 +863,15 @@ if ActiveRecord::Base.connection.supports_bulk_alter?        assert ! column(:name).default        assert_equal :date, column(:birthdate).type -      # One query for columns (delete_me table) -      # One query for primary key (delete_me table) -      # One query to do the bulk change -      assert_queries(3, ignore_none: true) do +      classname = ActiveRecord::Base.connection.class.name[/[^:]*$/] +      expected_query_count = { +        "Mysql2Adapter"     => 3, # one query for columns, one query for primary key, one query to do the bulk change +        "PostgreSQLAdapter" => 2, # one query for columns, one for bulk change +      }.fetch(classname) { +        raise "need an expected query count for #{classname}" +      } + +      assert_queries(expected_query_count, ignore_none: true) do          with_bulk_change_table do |t|            t.change :name, :string, default: "NONAME"            t.change :birthdate, :datetime diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 4cc66a2e49..0fa8ea212f 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -59,13 +59,42 @@ class PersistenceTest < ActiveRecord::TestCase      def test_update_all_with_order_and_limit_updates_subset_only        author = authors(:david) -      assert_nothing_raised do -        assert_equal 1, author.posts_sorted_by_id_limited.size -        assert_equal 2, author.posts_sorted_by_id_limited.limit(2).to_a.size -        assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ]) -        assert_equal "bulk update!", posts(:welcome).body -        assert_not_equal "bulk update!", posts(:thinking).body -      end +      limited_posts = author.posts_sorted_by_id_limited +      assert_equal 1, limited_posts.size +      assert_equal 2, limited_posts.limit(2).size +      assert_equal 1, limited_posts.update_all([ "body = ?", "bulk update!" ]) +      assert_equal "bulk update!", posts(:welcome).body +      assert_not_equal "bulk update!", posts(:thinking).body +    end + +    def test_update_all_with_order_and_limit_and_offset_updates_subset_only +      author = authors(:david) +      limited_posts = author.posts_sorted_by_id_limited.offset(1) +      assert_equal 1, limited_posts.size +      assert_equal 2, limited_posts.limit(2).size +      assert_equal 1, limited_posts.update_all([ "body = ?", "bulk update!" ]) +      assert_equal "bulk update!", posts(:thinking).body +      assert_not_equal "bulk update!", posts(:welcome).body +    end + +    def test_delete_all_with_order_and_limit_deletes_subset_only +      author = authors(:david) +      limited_posts = Post.where(author: author).order(:id).limit(1) +      assert_equal 1, limited_posts.size +      assert_equal 2, limited_posts.limit(2).size +      assert_equal 1, limited_posts.delete_all +      assert_raise(ActiveRecord::RecordNotFound) { posts(:welcome) } +      assert posts(:thinking) +    end + +    def test_delete_all_with_order_and_limit_and_offset_deletes_subset_only +      author = authors(:david) +      limited_posts = Post.where(author: author).order(:id).limit(1).offset(1) +      assert_equal 1, limited_posts.size +      assert_equal 2, limited_posts.limit(2).size +      assert_equal 1, limited_posts.delete_all +      assert_raise(ActiveRecord::RecordNotFound) { posts(:thinking) } +      assert posts(:welcome)      end    end @@ -353,12 +382,13 @@ class PersistenceTest < ActiveRecord::TestCase    def test_create_columns_not_equal_attributes      topic = Topic.instantiate( -      "attributes" => { -        "title"          => "Another New Topic", -        "does_not_exist" => "test" -      } +      "title"          => "Another New Topic", +      "does_not_exist" => "test"      ) +    topic = topic.dup # reset @new_record      assert_nothing_raised { topic.save } +    assert topic.persisted? +    assert_equal "Another New Topic", topic.reload.title    end    def test_create_through_factory_with_block @@ -405,6 +435,8 @@ class PersistenceTest < ActiveRecord::TestCase      topic_reloaded = Topic.instantiate(topic.attributes.merge("does_not_exist" => "test"))      topic_reloaded.title = "A New Topic"      assert_nothing_raised { topic_reloaded.save } +    assert topic_reloaded.persisted? +    assert_equal "A New Topic", topic_reloaded.reload.title    end    def test_update_for_record_with_only_primary_key @@ -601,6 +633,9 @@ class PersistenceTest < ActiveRecord::TestCase      Topic.find(1).update_attribute(:approved, false)      assert !Topic.find(1).approved? + +    Topic.find(1).update_attribute(:change_approved_before_save, true) +    assert Topic.find(1).approved?    end    def test_update_attribute_for_readonly_attribute @@ -1103,13 +1138,18 @@ class PersistenceTest < ActiveRecord::TestCase    end    def test_reset_column_information_resets_children -    child = Class.new(Topic) -    child.new # force schema to load +    child_class = Class.new(Topic) +    child_class.new # force schema to load      ActiveRecord::Base.connection.add_column(:topics, :foo, :string)      Topic.reset_column_information -    assert_equal "bar", child.new(foo: :bar).foo +    # this should redefine attribute methods +    child_class.new + +    assert child_class.instance_methods.include?(:foo) +    assert child_class.instance_methods.include?(:foo_changed?) +    assert_equal "bar", child_class.new(foo: :bar).foo    ensure      ActiveRecord::Base.connection.remove_column(:topics, :foo)      Topic.reset_column_information diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index 46f90b0bca..ad05f70933 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -283,7 +283,7 @@ class QueryCacheTest < ActiveRecord::TestCase        payload[:sql].downcase!      end -    assert_raises RuntimeError do +    assert_raises frozen_error_class do        ActiveRecord::Base.cache do          assert_queries(1) { Task.find(1); Task.find(1) }        end diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 37c2235f1a..e78c3cee8a 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -254,23 +254,34 @@ class ReflectionTest < ActiveRecord::TestCase    end    def test_scope_chain_does_not_interfere_with_hmt_with_polymorphic_case -    @hotel = Hotel.create! -    @department = @hotel.departments.create! -    @department.chefs.create!(employable: CakeDesigner.create!) -    @department.chefs.create!(employable: DrinkDesigner.create!) +    hotel = Hotel.create! +    department = hotel.departments.create! +    department.chefs.create!(employable: CakeDesigner.create!) +    department.chefs.create!(employable: DrinkDesigner.create!) -    assert_equal 1, @hotel.cake_designers.size -    assert_equal 1, @hotel.drink_designers.size -    assert_equal 2, @hotel.chefs.size +    assert_equal 1, hotel.cake_designers.size +    assert_equal 1, hotel.cake_designers.count +    assert_equal 1, hotel.drink_designers.size +    assert_equal 1, hotel.drink_designers.count +    assert_equal 2, hotel.chefs.size +    assert_equal 2, hotel.chefs.count    end    def test_scope_chain_does_not_interfere_with_hmt_with_polymorphic_case_and_sti -    @hotel = Hotel.create! -    @hotel.mocktail_designers << MocktailDesigner.create! +    hotel = Hotel.create! +    hotel.mocktail_designers << MocktailDesigner.create! + +    assert_equal 1, hotel.mocktail_designers.size +    assert_equal 1, hotel.mocktail_designers.count +    assert_equal 1, hotel.chef_lists.size +    assert_equal 1, hotel.chef_lists.count -    assert_equal 1, @hotel.mocktail_designers.size -    assert_equal 1, @hotel.mocktail_designers.count -    assert_equal 1, @hotel.chef_lists.size +    hotel.mocktail_designers = [] + +    assert_equal 0, hotel.mocktail_designers.size +    assert_equal 0, hotel.mocktail_designers.count +    assert_equal 0, hotel.chef_lists.size +    assert_equal 0, hotel.chef_lists.count    end    def test_scope_chain_of_polymorphic_association_does_not_leak_into_other_hmt_associations @@ -310,15 +321,6 @@ class ReflectionTest < ActiveRecord::TestCase      assert_equal "custom_primary_key", Author.reflect_on_association(:tags_with_primary_key).association_primary_key.to_s # nested    end -  def test_association_primary_key_type -    # Normal Association -    assert_equal :integer, Author.reflect_on_association(:posts).association_primary_key_type.type -    assert_equal :string,  Author.reflect_on_association(:essay).association_primary_key_type.type - -    # Through Association -    assert_equal :string, Author.reflect_on_association(:essay_category).association_primary_key_type.type -  end -    def test_association_primary_key_raises_when_missing_primary_key      reflection = ActiveRecord::Reflection.create(:has_many, :edge, nil, {}, Author)      assert_raises(ActiveRecord::UnknownPrimaryKey) { reflection.association_primary_key } diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb index b424ca91de..dbf3389774 100644 --- a/activerecord/test/cases/relation_test.rb +++ b/activerecord/test/cases/relation_test.rb @@ -24,10 +24,9 @@ module ActiveRecord      def test_initialize_single_values        relation = Relation.new(FakeKlass, :b, nil) -      (Relation::SINGLE_VALUE_METHODS - [:create_with, :readonly]).each do |method| +      (Relation::SINGLE_VALUE_METHODS - [:create_with]).each do |method|          assert_nil relation.send("#{method}_value"), method.to_s        end -      assert_equal false, relation.readonly_value        value = relation.create_with_value        assert_equal({}, value)        assert_predicate value, :frozen? diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 675aafabda..7785f8c99b 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -780,8 +780,6 @@ class RelationTest < ActiveRecord::TestCase    def test_find_all_using_where_with_relation      david = authors(:david) -    # switching the lines below would succeed in current rails -    # assert_queries(2) {      assert_queries(1) {        relation = Author.where(id: Author.where(id: david.id))        assert_equal [david], relation.to_a @@ -820,8 +818,6 @@ class RelationTest < ActiveRecord::TestCase    def test_find_all_using_where_with_relation_and_alternate_primary_key      cool_first = minivans(:cool_first) -    # switching the lines below would succeed in current rails -    # assert_queries(2) {      assert_queries(1) {        relation = Minivan.where(minivan_id: Minivan.where(name: cool_first.name))        assert_equal [cool_first], relation.to_a @@ -896,11 +892,9 @@ class RelationTest < ActiveRecord::TestCase    end    def test_delete_all_with_unpermitted_relation_raises_error -    assert_raises(ActiveRecord::ActiveRecordError) { Author.limit(10).delete_all }      assert_raises(ActiveRecord::ActiveRecordError) { Author.distinct.delete_all }      assert_raises(ActiveRecord::ActiveRecordError) { Author.group(:name).delete_all }      assert_raises(ActiveRecord::ActiveRecordError) { Author.having("SUM(id) < 3").delete_all } -    assert_raises(ActiveRecord::ActiveRecordError) { Author.offset(10).delete_all }    end    def test_select_with_aggregates @@ -969,6 +963,12 @@ class RelationTest < ActiveRecord::TestCase      assert_equal 11, posts.distinct(false).select(:comments_count).count    end +  def test_size_with_distinct +    posts = Post.distinct.select(:author_id, :comments_count) +    assert_queries(1) { assert_equal 8, posts.size } +    assert_queries(1) { assert_equal 8, posts.load.size } +  end +    def test_update_all_with_scope      tag = Tag.first      Post.tagged_with(tag.id).update_all title: "rofl" diff --git a/activerecord/test/cases/reserved_word_test.rb b/activerecord/test/cases/reserved_word_test.rb index 0214dbec17..4f8ca392b9 100644 --- a/activerecord/test/cases/reserved_word_test.rb +++ b/activerecord/test/cases/reserved_word_test.rb @@ -39,7 +39,7 @@ class ReservedWordTest < ActiveRecord::TestCase        t.string :order        t.belongs_to :select      end -    @connection.create_table :values, force: true do |t| +    @connection.create_table :values, primary_key: :as, force: true do |t|        t.belongs_to :group      end    end @@ -88,6 +88,13 @@ class ReservedWordTest < ActiveRecord::TestCase      assert_equal x, Group.find(x.id)    end +  def test_delete_all_with_subselect +    create_test_fixtures :values +    assert_equal 1, Values.order(:as).limit(1).offset(1).delete_all +    assert_raise(ActiveRecord::RecordNotFound) { Values.find(2) } +    assert Values.find(1) +  end +    def test_has_one_associations      create_test_fixtures :group, :values      v = Group.find(1).values diff --git a/activerecord/test/cases/sanitize_test.rb b/activerecord/test/cases/sanitize_test.rb index 86e45b3cbd..1b0605e369 100644 --- a/activerecord/test/cases/sanitize_test.rb +++ b/activerecord/test/cases/sanitize_test.rb @@ -72,13 +72,21 @@ class SanitizeTest < ActiveRecord::TestCase    def test_sanitize_sql_like_example_use_case      searchable_post = Class.new(Post) do -      def self.search(term) +      def self.search_as_method(term)          where("title LIKE ?", sanitize_sql_like(term, "!"))        end + +      scope :search_as_scope, -> (term) { +        where("title LIKE ?", sanitize_sql_like(term, "!")) +      } +    end + +    assert_sql(/LIKE '20!% !_reduction!_!!'/) do +      searchable_post.search_as_method("20% _reduction_!").to_a      end      assert_sql(/LIKE '20!% !_reduction!_!!'/) do -      searchable_post.search("20% _reduction_!").to_a +      searchable_post.search_as_scope("20% _reduction_!").to_a      end    end diff --git a/activerecord/test/cases/test_case.rb b/activerecord/test/cases/test_case.rb index 06a8693a7d..024b5bd8a1 100644 --- a/activerecord/test/cases/test_case.rb +++ b/activerecord/test/cases/test_case.rb @@ -77,6 +77,10 @@ module ActiveRecord        model.reset_column_information        model.column_names.include?(column_name.to_s)      end + +    def frozen_error_class +      Object.const_defined?(:FrozenError) ? FrozenError : RuntimeError +    end    end    class PostgreSQLTestCase < TestCase @@ -112,7 +116,7 @@ module ActiveRecord      # instead examining the SQL content.      oracle_ignored     = [/^select .*nextval/i, /^SAVEPOINT/, /^ROLLBACK TO/, /^\s*select .* from all_triggers/im, /^\s*select .* from all_constraints/im, /^\s*select .* from all_tab_cols/im, /^\s*select .* from all_sequences/im]      mysql_ignored      = [/^SHOW FULL TABLES/i, /^SHOW FULL FIELDS/, /^SHOW CREATE TABLE /i, /^SHOW VARIABLES /, /^\s*SELECT (?:column_name|table_name)\b.*\bFROM information_schema\.(?:key_column_usage|tables)\b/im] -    postgresql_ignored = [/^\s*select\b.*\bfrom\b.*pg_namespace\b/im, /^\s*select tablename\b.*from pg_tables\b/im, /^\s*select\b.*\battname\b.*\bfrom\b.*\bpg_attribute\b/im, /^SHOW search_path/i] +    postgresql_ignored = [/^\s*select\b.*\bfrom\b.*pg_namespace\b/im, /^\s*select tablename\b.*from pg_tables\b/im, /^\s*select\b.*\battname\b.*\bfrom\b.*\bpg_attribute\b/im, /^SHOW search_path/i, /^\s*SELECT\b.*::regtype::oid\b/im]      sqlite3_ignored =    [/^\s*SELECT name\b.*\bFROM sqlite_master/im, /^\s*SELECT sql\b.*\bFROM sqlite_master/im]      [oracle_ignored, mysql_ignored, postgresql_ignored, sqlite3_ignored].each do |db_ignored_sql| diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 5c8ae4d3cb..c110fa2f7d 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -576,7 +576,7 @@ class TransactionTest < ActiveRecord::TestCase    def test_rollback_when_saving_a_frozen_record      topic = Topic.new(title: "test")      topic.freeze -    e = assert_raise(RuntimeError) { topic.save } +    e = assert_raise(frozen_error_class) { topic.save }      # Not good enough, but we can't do much      # about it since there is no specific error      # for frozen objects. diff --git a/activerecord/test/fixtures/reserved_words/values.yml b/activerecord/test/fixtures/reserved_words/values.yml index 7d109609ab..9ed9e5edc5 100644 --- a/activerecord/test/fixtures/reserved_words/values.yml +++ b/activerecord/test/fixtures/reserved_words/values.yml @@ -1,7 +1,7 @@  values1: -  id: 1 +  as: 1    group_id: 2  values2: -  id: 2 +  as: 2    group_id: 1 diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index bbc5fc2b2d..fc6488f729 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -87,6 +87,8 @@ class Firm < Company    has_many :association_with_references, -> { references(:foo) }, class_name: "Client" +  has_many :developers_with_select, -> { select("id, name, first_name") }, class_name: "Developer" +    has_one :lead_developer, class_name: "Developer"    has_many :projects diff --git a/activerecord/test/models/contract.rb b/activerecord/test/models/contract.rb index 9454217e8d..f273badd85 100644 --- a/activerecord/test/models/contract.rb +++ b/activerecord/test/models/contract.rb @@ -2,7 +2,7 @@  class Contract < ActiveRecord::Base    belongs_to :company -  belongs_to :developer +  belongs_to :developer, primary_key: :id    belongs_to :firm, foreign_key: "company_id"    before_save :hi diff --git a/activerecord/test/models/tag.rb b/activerecord/test/models/tag.rb index bc13c3a42d..c1a8890a8a 100644 --- a/activerecord/test/models/tag.rb +++ b/activerecord/test/models/tag.rb @@ -11,6 +11,6 @@ end  class OrderedTag < Tag    self.table_name = "tags" -  has_many :taggings, -> { order("taggings.id DESC") }, foreign_key: "tag_id" -  has_many :tagged_posts, through: :taggings, source: "taggable", source_type: "Post" +  has_many :ordered_taggings, -> { order("taggings.id DESC") }, foreign_key: "tag_id", class_name: "Tagging" +  has_many :tagged_posts, through: :ordered_taggings, source: "taggable", source_type: "Post"  end diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 2154b50ef7..8cd4dc352a 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -65,6 +65,9 @@ class Topic < ActiveRecord::Base    after_initialize :set_email_address +  attr_accessor :change_approved_before_save +  before_save :change_approved_callback +    class_attribute :after_initialize_called    after_initialize do      self.class.after_initialize_called = true @@ -96,6 +99,10 @@ class Topic < ActiveRecord::Base      def before_destroy_for_transaction; end      def after_save_for_transaction; end      def after_create_for_transaction; end + +    def change_approved_callback +      self.approved = change_approved_before_save unless change_approved_before_save.nil? +    end  end  class ImportantTopic < Topic | 
