Method: ReactiveRecord::Collection.sync_scopes
- Defined in:
- lib/reactive_record/active_record/reactive_record/collection.rb
.sync_scopes(broadcast) ⇒ Object
sync_scopes takes a newly broadcasted record change and updates all relevant currently active scopes This is particularly hard when the client proc is specified. For example consider this scope:
class TestModel < ApplicationRecord
scope :quicker, -> { where(completed: true) }, client: -> { completed }
end
and this slice of reactive code:
DIV { "quicker.count = #{TestModel.quicker.count}" }
then on the server this code is executed:
TestModel.last.update(completed: false)
This will result in the changes being broadcast to the client, which may cauase the value of TestModel.quicker.count to increase or decrease. Of course we may not actually have the all the records, perhaps we just have the aggregate count.
To determine this sync_scopes first asks if the record being changed is in the scope given its value
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 130 def sync_scopes(broadcast) # record_with_current_values will return nil if data between # the broadcast record and the value on the client is out of sync # not running set_pre_sync_related_records will cause sync scopes # to refresh all related scopes React::State.bulk_update do record = broadcast.record_with_current_values apply_to_all_collections( :set_pre_sync_related_records, record, broadcast.new? ) if record record = broadcast.record_with_new_values apply_to_all_collections( :sync_scopes, record, record.destroyed? ) record.backing_record.sync_unscoped_collection! if record.destroyed? || broadcast.new? end end |