From 46fe546c5f8bf505eb2503e0a784991c69a1d223 Mon Sep 17 00:00:00 2001 From: Stephen Blackstone Date: Fri, 11 Dec 2015 18:19:26 -0500 Subject: Allow users to pass flags from database.yml Fix white-space Add test case demonstrating flags are received by the adapter --- .../lib/active_record/connection_adapters/mysql2_adapter.rb | 4 ++-- activerecord/test/cases/adapters/mysql2/connection_test.rb | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 7ca597859d..6590e0140d 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -10,9 +10,9 @@ module ActiveRecord config = config.symbolize_keys config[:username] = 'root' if config[:username].nil? - + config[:flags] ||= 0 if Mysql2::Client.const_defined? :FOUND_ROWS - config[:flags] = Mysql2::Client::FOUND_ROWS + config[:flags] |= Mysql2::Client::FOUND_ROWS end client = Mysql2::Client.new(config) diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index 507d024bb6..8fabcfb5c0 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -83,6 +83,13 @@ class Mysql2ConnectionTest < ActiveRecord::Mysql2TestCase assert_equal [['']], result.rows end end + + def test_passing_arbitary_flags_to_adapter + run_without_connection do |orig_connection| + ActiveRecord::Base.establish_connection(orig_connection.merge({flags: Mysql2::Client::COMPRESS})) + assert_equal (Mysql2::Client::COMPRESS | Mysql2::Client::FOUND_ROWS), ActiveRecord::Base.connection.raw_connection.query_options[:flags] + end + end def test_mysql_strict_mode_specified_default run_without_connection do |orig_connection| -- cgit v1.2.3 From f7014c5bb6d73024057d114dbed24c5164b5870c Mon Sep 17 00:00:00 2001 From: Tony Ta Date: Tue, 15 Dec 2015 22:34:04 -0800 Subject: deletes commented code introduced in db045db (initial commit) --- activerecord/test/cases/fixtures_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index f30ed4fcc8..c73958900b 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -184,7 +184,6 @@ class FixturesTest < ActiveRecord::TestCase end def test_fixtures_from_root_yml_with_instantiation - # assert_equal 2, @accounts.size assert_equal 50, @unknown.credit_limit end -- cgit v1.2.3 From 2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Sat, 12 Dec 2015 14:25:00 +0100 Subject: Introduce ApplicationRecord, an Active Record layer supertype It's pretty common for folks to monkey patch `ActiveRecord::Base` to work around an issue or introduce extra functionality. Instead of shoving even more stuff in `ActiveRecord::Base`, `ApplicationRecord` can hold all those custom work the apps may need. Now, we don't wanna encourage all of the application models to inherit from `ActiveRecord::Base`, but we can encourage all the models that do, to inherit from `ApplicationRecord`. Newly generated applications have `app/models/application_record.rb` present by default. The model generators are smart enough to recognize that newly generated models have to inherit from `ApplicationRecord`, but only if it's present. --- activerecord/CHANGELOG.md | 15 +++++++++++++++ .../generators/active_record/model/model_generator.rb | 9 ++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 87420a0746..f1921dded8 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,18 @@ +* Introduce ApplicationRecord, an Active Record layer super type. + + An `ApplicationRecord` let's engines have models, isolated from the main + application. Plugin authors can use it to distribute extensions as modules + to be included into `ApplicationRecord`, instead of monkey patches. It can + also serve as a place for applications to customize the default + `ActiveRecord::Base` model behaviour. + + Newly generated applications have `app/models/application_record.rb` + present by default. Generators are smart enough to recognize that + newly generated models have to inherit from `ApplicationRecord` only if + it's present. + + *Genadi Samokovarov* + * Version the API presented to migration classes, so we can change parameter defaults without breaking existing migrations, or forcing them to be rewritten through a deprecation cycle. diff --git a/activerecord/lib/rails/generators/active_record/model/model_generator.rb b/activerecord/lib/rails/generators/active_record/model/model_generator.rb index 395951ac9d..e4c9539362 100644 --- a/activerecord/lib/rails/generators/active_record/model/model_generator.rb +++ b/activerecord/lib/rails/generators/active_record/model/model_generator.rb @@ -43,9 +43,16 @@ module ActiveRecord # Used by the migration template to determine the parent name of the model def parent_class_name - options[:parent] || "ActiveRecord::Base" + options[:parent] || determine_default_parent_class end + def determine_default_parent_class + if File.exist?('app/models/application_record.rb') + "ApplicationRecord" + else + "ActiveRecord::Base" + end + end end end end -- cgit v1.2.3 From 21f4017fd904532ebe4964481567d69636a711cf Mon Sep 17 00:00:00 2001 From: yui-knk Date: Tue, 15 Dec 2015 21:12:16 +0900 Subject: Deprecate passing string to define callback. --- activerecord/lib/active_record/callbacks.rb | 15 --------------- activerecord/test/cases/callbacks_test.rb | 2 +- 2 files changed, 1 insertion(+), 16 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 4058affec3..854f9776a3 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -175,21 +175,6 @@ module ActiveRecord # end # end # - # The callback macros usually accept a symbol for the method they're supposed to run, but you can also - # pass a "method string", which will then be evaluated within the binding of the callback. Example: - # - # class Topic < ActiveRecord::Base - # before_destroy 'self.class.delete_all "parent_id = #{id}"' - # end - # - # Notice that single quotes (') are used so the #{id} part isn't evaluated until the callback - # is triggered. Also note that these inline callbacks can be stacked just like the regular ones: - # - # class Topic < ActiveRecord::Base - # before_destroy 'self.class.delete_all "parent_id = #{id}"', - # 'puts "Evaluated after parents are destroyed"' - # end - # # == before_validation* returning statements # # If the +before_validation+ callback throws +:abort+, the process will be diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb index 73ac30e547..4f70ae3a1d 100644 --- a/activerecord/test/cases/callbacks_test.rb +++ b/activerecord/test/cases/callbacks_test.rb @@ -33,7 +33,7 @@ class CallbackDeveloper < ActiveRecord::Base ActiveRecord::Callbacks::CALLBACKS.each do |callback_method| next if callback_method.to_s =~ /^around_/ define_callback_method(callback_method) - send(callback_method, callback_string(callback_method)) + ActiveSupport::Deprecation.silence { send(callback_method, callback_string(callback_method)) } send(callback_method, callback_proc(callback_method)) send(callback_method, callback_object(callback_method)) send(callback_method) { |model| model.history << [callback_method, :block] } -- cgit v1.2.3