Fixed pushing of objects with associations (did not push associations).

This commit is contained in:
Martin Edenhofer 2014-12-01 11:03:26 +01:00
parent aed7e70179
commit 0f5b98221a
2 changed files with 38 additions and 11 deletions

View file

@ -84,34 +84,41 @@ class Sessions::Backend::Collections::Base
Sessions::CacheIn.set( self.client_key, true, { :expires_in => 10.seconds } ) Sessions::CacheIn.set( self.client_key, true, { :expires_in => 10.seconds } )
return if !self.changed? return if !self.changed?
data = self.load items = self.load
return if !data||data.empty? return if !items||items.empty?
# get relations of data
all = []
items.each {|item|
all.push item.attributes_with_associations
}
all = items
# collect assets # collect assets
assets = {} assets = {}
data.each {|item| items.each {|item|
assets = item.assets(assets) assets = item.assets(assets)
} }
if !@client if !@client
return { return {
:collection => { :collection => {
data.first.class.to_app_model => data, items.first.class.to_app_model => all,
}, },
:assets => assets, :assets => assets,
} }
end end
@client.log 'notify', "push assets for push_collection #{ data.first.class.to_s } for user #{ @user.id }" @client.log 'notify', "push assets for push_collection #{ items.first.class.to_s } for user #{ @user.id }"
@client.send({ @client.send({
:data => assets, :data => assets,
:event => [ 'loadAssets' ], :event => [ 'loadAssets' ],
}) })
@client.log 'notify', "push push_collection #{ data.first.class.to_s } for user #{ @user.id }" @client.log 'notify', "push push_collection #{ items.first.class.to_s } for user #{ @user.id }"
@client.send({ @client.send({
:event => 'resetCollection', :event => 'resetCollection',
:data => { :data => {
data.first.class.to_app_model => data, items.first.class.to_app_model => all,
}, },
}) })
end end

View file

@ -109,13 +109,13 @@ class SessionCollectionsTest < ActiveSupport::TestCase
assert( check_if_collection_exists(result3, :Group), "check collections - after touch" ) assert( check_if_collection_exists(result3, :Group), "check collections - after touch" )
# change collection # change collection
org = Organization.create( :name => 'SomeOrg::' + rand(999999).to_s, :active => true ) org = Organization.create( :name => 'SomeOrg::' + rand(999999).to_s, :active => true, :member_ids => [customer1.id] )
sleep 16 sleep 16
# get whole collections # get whole collections
result1 = collection_client1.push result1 = collection_client1.push
assert( result1, "check collections - after create" ) assert( result1, "check collections - after create" )
assert( check_if_collection_exists(result1, :Organization), "check collections - after create" ) assert( check_if_collection_exists(result1, :Organization, { :id => org.id, :member_ids => [customer1.id] } ), "check collections - after create with attributes" )
sleep 0.5 sleep 0.5
result2 = collection_client2.push result2 = collection_client2.push
assert( result2, "check collections - after create" ) assert( result2, "check collections - after create" )
@ -137,9 +137,29 @@ class SessionCollectionsTest < ActiveSupport::TestCase
assert( result3.empty?, "check collections - recall" ) assert( result3.empty?, "check collections - recall" )
end end
def check_if_collection_exists(results, collection) def check_if_collection_exists(results, collection, attributes = nil)
results.each {|result| results.each {|result|
return true if result && result[:collection] && result[:collection][collection] next if !result
if result[:collection] && result[:collection][collection]
# check just if collection exists
if !attributes
return true
end
# check if objetc with attributes in collection exists
result[:collection][collection].each {|item|
match_all = true
attributes.each {|key, value|
if item.attributes_with_associations[ key.to_s ] != value
match_all = false
end
}
if match_all
return true
end
}
end
} }
nil nil
end end