From 67544cdf13ed33264738dd74c8dc44e39be9d24f Mon Sep 17 00:00:00 2001 From: Anthony Persaud Date: Thu, 10 Aug 2017 15:12:30 -0700 Subject: [PATCH] All array properties will return a default collection proxy if nil. --- Changes.md | 1 + .../model/associations/collection_proxy.rb | 2 +- lib/parse/model/core/properties.rb | 6 +++++ .../lib/parse/models/collection_proxy_test.rb | 25 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/lib/parse/models/collection_proxy_test.rb diff --git a/Changes.md b/Changes.md index 64d2e70..c56c58a 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,7 @@ ### 1.7.2 - FIXED: first_or_create will now apply dirty tracking to newly created fields. +- FIXED: Properties of :array type will always return a Parse::CollectionProxy if their internal value is nil. The object will not be marked dirty until something is added to the array. - NEW: `Parse::Model.autosave_on_create` has been removed in favor of `first_or_create!`. - FIXED: Encoding a Parse::Object into JSON will remove any values that are `nil` which were not explicitly changed to that value. diff --git a/lib/parse/model/associations/collection_proxy.rb b/lib/parse/model/associations/collection_proxy.rb index 42efadb..e018916 100644 --- a/lib/parse/model/associations/collection_proxy.rb +++ b/lib/parse/model/associations/collection_proxy.rb @@ -75,7 +75,7 @@ def loaded? # @param params [Object] method parameters # @return [Object] the return value from the forwarded method. def forward(method, params = nil) - return unless @delegate.present? && @delegate.respond_to?(method) + return unless @delegate && @delegate.respond_to?(method) params.nil? ? @delegate.send(method) : @delegate.send(method, params) end diff --git a/lib/parse/model/core/properties.rb b/lib/parse/model/core/properties.rb index 41a9cf0..4bd2bbc 100644 --- a/lib/parse/model/core/properties.rb +++ b/lib/parse/model/core/properties.rb @@ -300,6 +300,12 @@ def property(key, data_type = :string, **opts) # lets set the variable with the updated value instance_variable_set ivar, value send will_change_method + elsif value.nil? && data_type == :array + value = Parse::CollectionProxy.new [], delegate: self, key: key + instance_variable_set ivar, value + # don't send the notification yet until they actually add something + # which will be handled by the collection proxy. + # send will_change_method end # if the value is a String (like an iso8601 date) and the data type of diff --git a/test/lib/parse/models/collection_proxy_test.rb b/test/lib/parse/models/collection_proxy_test.rb new file mode 100644 index 0000000..6d0e368 --- /dev/null +++ b/test/lib/parse/models/collection_proxy_test.rb @@ -0,0 +1,25 @@ +require_relative '../../../test_helper' + +class MyCollectionTest < Parse::Object + property :list, :array +end + +class TestCollectionProxy < Minitest::Test + + def test_default_access + o = MyCollectionTest.new + assert_equal o.list, [] + changes = o.changes + changes.delete("acl") + + assert_equal changes, {}, "Make sure default proxy collection doesn't affect dirty tracking" + refute o.list_changed?, "Make sure it didn't affect will_change methods." + refute o.changed.include?("list") + + o.list.add "something" + assert o.changed.include?("list"), "Verify it is now included in dirty tracking list." + assert o.list_changed?, "Make sure it dirty tracking was forwarded on change." + + end + +end