reactive-record 0.7.39 → 0.7.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38627e5aff47130857117db10b665d62df420ddd
4
- data.tar.gz: d5f8d93a888c7ac27a7032f020ca784553b1dceb
3
+ metadata.gz: 4bb0f789a2d04f7a3256763e9f6df888a030bd36
4
+ data.tar.gz: 4192db45d77b75734e1b1be6280144c06d94e510
5
5
  SHA512:
6
- metadata.gz: 11dcbafdbb5d8bc1d9c00586c38ea6bb15726d6045ab18a75ff622f087c18c76755899dce2fe6e5a2f5cf424d3b05e23b73b8beb3a4127a07b3782a1e1f4e9b3
7
- data.tar.gz: 47c7b2bf0db583da4940395dbe9c3fc476892bebd04697335a9a38a9a6b1e6c0b3eb9f93447e5020b18acf66db88707a928803bcc1b7faa1126973ae152b3923
6
+ metadata.gz: 2d1b9081a8b0133ce8dc3a2d1195b5ed9d4478b40520739b25bb6a77ef9c75f978f92c472a6a1722acbed85e86561ede0dc4bdd0241559c38804b89d383317f6
7
+ data.tar.gz: ad0483d874c65bd03407e8e0f7058033bbb18f318cd1228f8d183238bb41b31dbff4d113516afacec7b852d29520abbc68909f2de5b82abc46866327adeb9c67
@@ -9,6 +9,7 @@ if RUBY_ENGINE == 'opal'
9
9
  require "reactive_record/active_record/associations"
10
10
  require "reactive_record/active_record/reactive_record/base"
11
11
  require "reactive_record/active_record/reactive_record/collection"
12
+ require "reactive_record/reactive_scope"
12
13
  require "reactive_record/active_record/class_methods"
13
14
  require "reactive_record/active_record/instance_methods"
14
15
  require "reactive_record/active_record/base"
@@ -22,6 +23,7 @@ else
22
23
  require "reactive_record/engine"
23
24
  require "reactive_record/server_data_cache"
24
25
  require "reactive_record/active_record/reactive_record/isomorphic_base"
26
+ require "reactive_record/reactive_scope"
25
27
  require "reactive_record/serializers"
26
28
  require "reactive_record/pry"
27
29
 
@@ -65,6 +65,28 @@ module ReactiveRecord
65
65
  @class_scopes[model.base_class]
66
66
  end
67
67
 
68
+ def self.sync_blocks
69
+ # @sync_blocks[watch_model][sync_model][scope_name][...array of blocks...]
70
+ @sync_blocks ||= Hash.new { |hash, key| hash[key] = Hash.new { |hash, key| hash[key] = Hash.new { |hash, key| hash[key] = [] } } }
71
+ end
72
+
73
+ def sync_scopes
74
+ self.class.sync_blocks[self.model].each do |watching_class, scopes|
75
+ scopes.each do |scope_name, blocks|
76
+ blocks.each do |block|
77
+ if block.arity > 0
78
+ block.call watching_class.send(scope_name), @ar_instance
79
+ elsif @ar_instance.instance_eval &block
80
+ watching_class.send(scope_name) << @ar_instance
81
+ else
82
+ watching_class.send(scope_name).delete(@ar_instance)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ model.all << @ar_instance
88
+ end
89
+
68
90
  def self.find(model, attribute, value)
69
91
  # will return the unique record with this attribute-value pair
70
92
  # value cannot be an association or aggregation
@@ -27,6 +27,7 @@ module ReactiveRecord
27
27
  end
28
28
 
29
29
  def all
30
+ observed
30
31
  @dummy_collection.notify if @dummy_collection
31
32
  unless @collection
32
33
  @collection = []
@@ -43,7 +44,7 @@ module ReactiveRecord
43
44
  end
44
45
 
45
46
  def [](index)
46
-
47
+ observed
47
48
  if (@collection || all).length <= index and @dummy_collection
48
49
  (@collection.length..index).each do |i|
49
50
  new_dummy_record = ReactiveRecord::Base.new_from_vector(@target_klass, nil, *@vector, "*#{i}")
@@ -55,6 +56,9 @@ module ReactiveRecord
55
56
  end
56
57
 
57
58
  def ==(other_collection)
59
+ observed
60
+ return nil unless other_collection.is_a? Collection
61
+ other_collection.observed
58
62
  my_collection = (@collection || []).select { |target| target != @dummy_record }
59
63
  other_collection = (other_collection ? (other_collection.collection || []) : []).select { |target| target != other_collection.dummy_record }
60
64
  my_collection == other_collection
@@ -68,6 +72,7 @@ module ReactiveRecord
68
72
  end
69
73
 
70
74
  def count
75
+ observed
71
76
  if @collection
72
77
  @collection.count
73
78
  elsif @count ||= ReactiveRecord::Base.fetch_from_db([*@vector, "*count"])
@@ -89,6 +94,7 @@ module ReactiveRecord
89
94
  end
90
95
 
91
96
  def <<(item)
97
+ return delete(item) if item.destroyed? # pushing a destroyed item is the same as removing it
92
98
  backing_record = item.backing_record
93
99
  all << item unless all.include? item # does this use == if so we are okay...
94
100
  if backing_record and @owner and @association and inverse_of = @association.inverse_of and item.attributes[inverse_of] != @owner
@@ -104,7 +110,7 @@ module ReactiveRecord
104
110
  @dummy_record = @collection.detect { |r| r.backing_record.vector.last =~ /^\*[0-9]+$/ }
105
111
  @dummy_collection = nil
106
112
  end
107
- self
113
+ notify_of_change
108
114
  end
109
115
 
110
116
  [:first, :last].each do |method|
@@ -141,11 +147,11 @@ module ReactiveRecord
141
147
  @dummy_collection = @dummy_record = nil
142
148
  new_array.each { |item| self << item }
143
149
  end
144
- new_array
150
+ notify_of_change new_array
145
151
  end
146
152
 
147
153
  def delete(item)
148
- if @owner and @association and inverse_of = @association.inverse_of
154
+ notify_of_change(if @owner and @association and inverse_of = @association.inverse_of
149
155
  if backing_record = item.backing_record and backing_record.attributes[inverse_of] == @owner
150
156
  # the if prevents double update if delete is being called from << (see << above)
151
157
  backing_record.update_attribute(inverse_of, nil)
@@ -153,7 +159,7 @@ module ReactiveRecord
153
159
  all.delete(item).tap { @owner.backing_record.update_attribute(@association.attribute) } # forces a check if association contents have changed from synced values
154
160
  else
155
161
  all.delete(item)
156
- end
162
+ end)
157
163
  end
158
164
 
159
165
  def loading?
@@ -185,6 +191,19 @@ module ReactiveRecord
185
191
  @dummy_collection
186
192
  end
187
193
 
194
+ def notify_of_change(value = nil)
195
+ begin
196
+ React::State.set_state(self, "collection", collection) unless ReactiveRecord::Base.data_loading?
197
+ rescue Exception => e
198
+ `debugger`
199
+ end
200
+ value
201
+ end
202
+
203
+ def observed
204
+ React::State.get_state(self, "collection") unless ReactiveRecord::Base.data_loading?
205
+ end
206
+
188
207
  end
189
208
 
190
209
  end
@@ -316,7 +316,10 @@ module ReactiveRecord
316
316
  end
317
317
  end
318
318
 
319
- response.json[:saved_models].each { | item | backing_records[item[0]].errors! item[3] }
319
+ response.json[:saved_models].each do | item |
320
+ backing_records[item[0]].sync_scopes
321
+ backing_records[item[0]].errors! item[3]
322
+ end
320
323
 
321
324
  yield response.json[:success], response.json[:message], response.json[:models] if block
322
325
  promise.resolve response.json
@@ -546,6 +549,7 @@ module ReactiveRecord
546
549
 
547
550
  if !data_loading? and (id or vector)
548
551
  HTTP.post(`window.ReactiveRecordEnginePath`+"/destroy", payload: {model: ar_instance.model_name, id: id, vector: vector}).then do |response|
552
+ sync_scopes
549
553
  yield response.json[:success], response.json[:message] if block
550
554
  promise.resolve response.json
551
555
  end
@@ -0,0 +1,18 @@
1
+ class ActiveRecord::Base
2
+
3
+ def self.to_sync(scope_name, opts={}, &block)
4
+ watch_list = if opts[:watch]
5
+ [*opts.delete[:watch]]
6
+ else
7
+ [self]
8
+ end
9
+ if RUBY_ENGINE=='opal'
10
+ watch_list.each do |klass_to_watch|
11
+ ReactiveRecord::Base.sync_blocks[klass_to_watch][self][scope_name] << block
12
+ end
13
+ else
14
+ # this is where we put server side watchers in place to sync all clients!
15
+ end
16
+ end
17
+
18
+ end
@@ -1,3 +1,3 @@
1
1
  module ReactiveRecord
2
- VERSION = "0.7.39"
2
+ VERSION = "0.7.40"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reactive-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.39
4
+ version: 0.7.40
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitch VanDuyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-29 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -165,6 +165,7 @@ files:
165
165
  - lib/reactive_record/interval.rb
166
166
  - lib/reactive_record/permissions.rb
167
167
  - lib/reactive_record/pry.rb
168
+ - lib/reactive_record/reactive_scope.rb
168
169
  - lib/reactive_record/serializers.rb
169
170
  - lib/reactive_record/server_data_cache.rb
170
171
  - lib/reactive_record/version.rb