diff options
-rw-r--r-- | activerecord/CHANGELOG.md | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 3 | ||||
-rw-r--r-- | activerecord/lib/active_record/type/internal/abstract_json.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/mysql2/json_test.rb | 16 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/json_test.rb | 16 | ||||
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/load_error.rb | 1 | ||||
-rw-r--r-- | guides/source/testing.md | 8 |
9 files changed, 62 insertions, 13 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 6cc667b754..4d0c1a4178 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,14 @@ +* Serialize JSON attribute value `nil` as SQL `NULL`, not JSON `null` + + *Trung Duc Tran* + +* Return `true` from `update_attribute` when the value of the attribute + to be updated is unchanged. + + Fixes #26593. + + *Prathamesh Sonpatki* + * Always store errors details information with symbols. When the association is autosaved we were storing the details with diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 978fb27cab..65248f3a32 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -252,7 +252,8 @@ module ActiveRecord name = name.to_s verify_readonly_attribute(name) public_send("#{name}=", value) - save(validate: false) if changed? + + changed? ? save(validate: false) : true end # Updates the attributes of the model from the passed-in hash and saves the diff --git a/activerecord/lib/active_record/type/internal/abstract_json.rb b/activerecord/lib/active_record/type/internal/abstract_json.rb index 513c938088..e19c5a14da 100644 --- a/activerecord/lib/active_record/type/internal/abstract_json.rb +++ b/activerecord/lib/active_record/type/internal/abstract_json.rb @@ -17,7 +17,11 @@ module ActiveRecord end def serialize(value) - ::ActiveSupport::JSON.encode(value) + if value.nil? + nil + else + ::ActiveSupport::JSON.encode(value) + end end def accessor diff --git a/activerecord/test/cases/adapters/mysql2/json_test.rb b/activerecord/test/cases/adapters/mysql2/json_test.rb index 6b7d259023..630cdb36a4 100644 --- a/activerecord/test/cases/adapters/mysql2/json_test.rb +++ b/activerecord/test/cases/adapters/mysql2/json_test.rb @@ -102,6 +102,22 @@ if ActiveRecord::Base.connection.supports_json? assert_equal(["v0", { "k1" => "v1" }], x.payload) end + def test_select_nil_json_after_create + json = JsonDataType.create(payload: nil) + x = JsonDataType.where(payload:nil).first + assert_equal(json, x) + end + + def test_select_nil_json_after_update + json = JsonDataType.create(payload: "foo") + x = JsonDataType.where(payload:nil).first + assert_equal(nil, x) + + json.update_attributes payload: nil + x = JsonDataType.where(payload:nil).first + assert_equal(json.reload, x) + end + def test_rewrite_array_json_value @connection.execute %q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')| x = JsonDataType.first diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb index c74f70f251..273b2c1c7b 100644 --- a/activerecord/test/cases/adapters/postgresql/json_test.rb +++ b/activerecord/test/cases/adapters/postgresql/json_test.rb @@ -113,6 +113,22 @@ module PostgresqlJSONSharedTestCases assert_equal(nil, x.payload) end + def test_select_nil_json_after_create + json = JsonDataType.create(payload: nil) + x = JsonDataType.where(payload:nil).first + assert_equal(json, x) + end + + def test_select_nil_json_after_update + json = JsonDataType.create(payload: "foo") + x = JsonDataType.where(payload:nil).first + assert_equal(nil, x) + + json.update_attributes payload: nil + x = JsonDataType.where(payload:nil).first + assert_equal(json.reload, x) + end + def test_select_array_json_value @connection.execute %q|insert into json_data_type (payload) VALUES ('["v0",{"k1":"v1"}]')| x = JsonDataType.first diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index d83360e327..688c3ed2b1 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -391,14 +391,14 @@ class PersistenceTest < ActiveRecord::TestCase end topic = klass.create(title: "Another New Topic") assert_queries(0) do - topic.update_attribute(:title, "Another New Topic") + assert topic.update_attribute(:title, "Another New Topic") end end def test_update_does_not_run_sql_if_record_has_not_changed topic = Topic.create(title: "Another New Topic") - assert_queries(0) { topic.update(title: "Another New Topic") } - assert_queries(0) { topic.update_attributes(title: "Another New Topic") } + assert_queries(0) { assert topic.update(title: "Another New Topic") } + assert_queries(0) { assert topic.update_attributes(title: "Another New Topic") } end def test_delete diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 3a2c7b0e74..6d39be1c1f 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -738,11 +738,11 @@ module ActiveSupport # # ===== Notes # - # +names+ passed to `define_callbacks` must not end with - # `!`, `?` or `=`. + # +names+ passed to +define_callbacks+ must not end with + # <tt>!</tt>, <tt>?</tt> or <tt>=</tt>. # - # Calling `define_callbacks` multiple times with the same +names+ will - # overwrite previous callbacks registered with `set_callback`. + # Calling +define_callbacks+ multiple times with the same +names+ will + # overwrite previous callbacks registered with +set_callback+. def define_callbacks(*names) options = names.extract_options! diff --git a/activesupport/lib/active_support/core_ext/load_error.rb b/activesupport/lib/active_support/core_ext/load_error.rb index 4cb6ffea5e..cd00d1b662 100644 --- a/activesupport/lib/active_support/core_ext/load_error.rb +++ b/activesupport/lib/active_support/core_ext/load_error.rb @@ -1,3 +1,4 @@ +require "active_support/deprecation" require "active_support/deprecation/proxy_wrappers" class LoadError diff --git a/guides/source/testing.md b/guides/source/testing.md index 8f9246dea2..98847fde18 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -761,8 +761,8 @@ and also ensuring that the right response body has been generated. The `get` method kicks off the web request and populates the results into the `@response`. It can accept up to 6 arguments: -* The action of the controller you are requesting. - This can be in the form of a string or a route (i.e. `articles_url`). +* The URI of the controller action you are requesting. + This can be in the form of a string or a route helper (e.g. `articles_url`). * `params`: option with a hash of request parameters to pass into the action (e.g. query string parameters or article variables). * `headers`: for setting the headers that will be passed with the request. @@ -775,13 +775,13 @@ All of these keyword arguments are optional. Example: Calling the `:show` action, passing an `id` of 12 as the `params` and setting `HTTP_REFERER` header: ```ruby -get :show, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://example.com/home" } +get article_url, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://example.com/home" } ``` Another example: Calling the `:update` action, passing an `id` of 12 as the `params` as an Ajax request. ```ruby -patch update_url, params: { id: 12 }, xhr: true +patch article_url, params: { id: 12 }, xhr: true ``` NOTE: If you try running `test_should_create_article` test from `articles_controller_test.rb` it will fail on account of the newly added model level validation and rightly so. |