Skip to content

Commit

Permalink
All array properties will return a default collection proxy if nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
apersaud committed Aug 10, 2017
1 parent ed99c10 commit 67544cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/parse/model/associations/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lib/parse/model/core/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/lib/parse/models/collection_proxy_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 67544cd

Please sign in to comment.