Module: ReactiveRecord::ScopedCollection

Defined in:
lib/reactive_record/active_record/reactive_record/scoped_collection.rb

Overview

The base collection class works with relationships method overrides for scoped collections

Instance Method Summary collapse

Instance Method Details



9
10
11
12
13
14
15
16
17
# File 'lib/reactive_record/active_record/reactive_record/scoped_collection.rb', line 9

def set_pre_sync_related_records(related_records, _record = nil)
  @pre_sync_related_records = nil
  ReactiveRecord::Base.catch_db_requests do
    @pre_sync_related_records = filter_records(related_records)
    live_scopes.each do |scope|
      scope.set_pre_sync_related_records(@pre_sync_related_records)
    end
  end if filter?
end

#sync_scopes(related_records, record, filtering = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/reactive_record/active_record/reactive_record/scoped_collection.rb', line 19

def sync_scopes(related_records, record, filtering = true)
  filtering =
    @pre_sync_related_records && filtering &&
    ReactiveRecord::Base.catch_db_requests do
      related_records = update_collection(related_records)
    end
  reload_from_db if !filtering && joins_with?(record)
  live_scopes.each { |scope| scope.sync_scopes(related_records, record, filtering) }
ensure
  @pre_sync_related_records = nil
end

#update_collection(related_records) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/reactive_record/active_record/reactive_record/scoped_collection.rb', line 31

def update_collection(related_records)
  if collector?
    update_collector_scope(related_records)
  else
    related_records = filter_records(related_records)
    update_filter_scope(@pre_sync_related_records, related_records)
  end
end

#update_collector_scope(related_records) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/reactive_record/active_record/reactive_record/scoped_collection.rb', line 40

def update_collector_scope(related_records)
  current = Set.new([*@collection])
  (related_records - @pre_sync_related_records).each { |r| current << r }
  (@pre_sync_related_records - related_records).each { |r| current.delete(r) }
  replace(filter_records(current))
  Set.new([*@collection])
end

#update_filter_scope(before, after) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reactive_record/active_record/reactive_record/scoped_collection.rb', line 48

def update_filter_scope(before, after)
  if (collection || !@count.nil?) && before != after
    if collection
      (after - before).each { |r| push r }
      (before - after).each { |r| delete r }
    else
      @count += (after - before).count
      @count -= (before - after).count
      notify_of_change self  # TODO remove self .... and retest
    end
  end
  after
end