Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 2c35682

Browse files
committed
Policies not used unless there is an initializer and patch to reactrb to prevent multiple renders during data load
1 parent 25255af commit 2c35682

File tree

11 files changed

+56
-26
lines changed

11 files changed

+56
-26
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
source 'https://rubygems.org'
22
# Specify your gem's dependencies in synchromesh.gemspec
33
gem 'hyper-trace'
4+
gem 'opal-browser'
45
gemspec

lib/react/state_patches.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ def has_observers?(object, name)
88
!observers_by_name[object][name].empty?
99
end
1010

11-
def set_state2(object, name, value, updates)
11+
def set_state2(object, name, value, updates, exclusions = nil)
1212
# set object's name state to value, tell all observers it has changed.
1313
# Observers must implement update_react_js_state
1414
object_needs_notification = object.respond_to? :update_react_js_state
1515
observers_by_name[object][name].dup.each do |observer|
16+
next if exclusions && exclusions.include?(observer)
1617
updates[observer] += [object, name, value]
1718
object_needs_notification = false if object == observer
1819
end
@@ -27,18 +28,32 @@ def bulk_update
2728
@bulk_update_flag = saved_bulk_update_flag
2829
end
2930

31+
def get_state(object, name, current_observer = @current_observer)
32+
# get current value of name for object, remember that the current object depends on this state,
33+
# current observer can be overriden with last param
34+
if current_observer && !new_observers[current_observer][object].include?(name)
35+
new_observers[current_observer][object] << name
36+
end
37+
if @delayed_updates && @delayed_updates[object][name]
38+
@delayed_updates[object][name][1] << current_observer
39+
end
40+
states[object][name]
41+
end
42+
3043
def set_state(object, name, value, delay=nil)
3144
states[object][name] = value
3245
if delay || @bulk_update_flag
33-
@delayed_updates ||= []
34-
@delayed_updates << [object, name, value]
46+
@delayed_updates ||= Hash.new { |h, k| h[k] = {} }
47+
@delayed_updates[object][name] = [value, Set.new]
3548
@delayed_updater ||= after(0.001) do
3649
delayed_updates = @delayed_updates
37-
@delayed_updates = []
50+
@delayed_updates = Hash.new { |h, k| h[k] = {} } # could this be nil???
3851
@delayed_updater = nil
3952
updates = Hash.new { |hash, key| hash[key] = Array.new }
40-
delayed_updates.each do |object, name, value|
41-
set_state2(object, name, value, updates)
53+
delayed_updates.each do |object, name_hash|
54+
name_hash.each do |name, value_and_set|
55+
set_state2(object, name, value_and_set[0], updates, value_and_set[1])
56+
end
4257
end
4358
updates.each { |observer, args| observer.update_react_js_state(*args) }
4459
end

lib/reactive_record/collection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def observed
227227
@observing = true
228228
link_to_parent
229229
reload_from_db(true) if @out_of_date
230-
React::State.get_state(self, :collection) unless ReactiveRecord::Base.data_loading?
230+
React::State.get_state(self, :collection)
231231
ensure
232232
@observing = false
233233
end
@@ -236,7 +236,7 @@ def observed
236236
alias pre_synchromesh_instance_variable_set instance_variable_set
237237

238238
def instance_variable_set(var, val)
239-
if var == :@count && !ReactiveRecord::WhileLoading.has_observers?
239+
if var == :@count && !ReactiveRecord::WhileLoading.has_observers?# && !ReactiveRecord::Base.data_loading? # !ReactiveRecord::WhileLoading.has_observers?
240240
React::State.set_state(self, :collection, collection, true)
241241
end
242242
pre_synchromesh_instance_variable_set var, val

lib/synchromesh.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
# end
1010
# end
1111

12-
require_relative 'active_record_base'
1312
require 'set'
1413
if RUBY_ENGINE == 'opal'
15-
#require 'reactive-record'
14+
require 'reactive-record'
15+
require_relative 'active_record_base'
1616
require_relative 'react/reset_prerender_history'
1717
require_relative 'synchromesh/version'
1818
require_relative 'json/parse_patch'
@@ -36,7 +36,8 @@
3636
end
3737
#require 'active_record/transactions'
3838
require 'reactive-record'
39-
require 'reactive_record/permission_patches'
39+
require_relative 'active_record_base'
40+
#require 'reactive_record/permission_patches'
4041
require 'reactive_record/synchromesh_controller'
4142
require 'reactive_record/base_patches'
4243
require 'synchromesh/version'

lib/synchromesh/synchromesh.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ module Synchromesh
66
extend Configuration
77

88
def self.config_reset
9-
if Rails.application.config.cache_store == :null_store
10-
raise 'Cannot Run Synchromesh with cache_store == :null_store'
11-
end
9+
require File.join(File.dirname(__FILE__), '..', 'reactive_record', 'permission_patches')
1210
Object.send(:remove_const, :Application) if @fake_application_defined
1311
policy = begin
1412
Object.const_get 'ApplicationPolicy'

spec/synchromesh/connection_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,25 @@
4747
described_class.open('TestChannel', 1)
4848
described_class.open('AnotherChannel', 0)
4949
described_class.send_to_channel('TestChannel', 'data')
50-
expect(described_class.read(0)).to eq(['data'])
51-
expect(described_class.read(0)).to eq([])
52-
expect(described_class.read(1)).to eq(['data'])
53-
expect(described_class.read(1)).to eq([])
54-
expect(described_class.read(0)).to eq([])
50+
expect(described_class.read(0, 'path')).to eq(['data'])
51+
expect(described_class.read(0, 'path')).to eq([])
52+
expect(described_class.read(1, 'path')).to eq(['data'])
53+
expect(described_class.read(1, 'path')).to eq([])
54+
expect(described_class.read(0, 'path')).to eq([])
5555
end
5656

5757
it 'will update the expiration time after reading' do
5858
described_class.open('TestChannel', 0)
5959
described_class.send_to_channel('TestChannel', 'data')
60-
described_class.read(0)
60+
described_class.read(0, 'path')
6161
Timecop.travel(Time.now+described_class.transport.expire_new_connection_in)
6262
expect(described_class.active).to eq(['TestChannel'])
6363
end
6464

6565
it 'will expire a polled connection' do
6666
described_class.open('TestChannel', 0)
6767
described_class.send_to_channel('TestChannel', 'data')
68-
described_class.read(0)
68+
described_class.read(0, 'path')
6969
Timecop.travel(Time.now+described_class.transport.expire_polled_connection_in)
7070
expect(described_class.active).to eq([])
7171
end
@@ -94,7 +94,7 @@
9494

9595
it "will only effect the session being connected" do
9696
described_class.connect_to_transport('TestChannel', 0, nil)
97-
expect(described_class.read(1)).to eq(['data'])
97+
expect(described_class.read(1, 'path')).to eq(['data'])
9898
end
9999

100100
it "will begin refreshing the channel list" do
@@ -112,7 +112,7 @@
112112
described_class.open('AnotherChannel', 0)
113113
described_class.connect_to_transport('TestChannel', 0, nil)
114114
Timecop.travel(Time.now+described_class.transport.refresh_channels_every-1)
115-
described_class.read(1)
115+
described_class.read(1, 'path')
116116
described_class.connect_to_transport('AnotherChannel', 0, nil)
117117
expect(described_class.active).to eq(['TestChannel', 'AnotherChannel'])
118118
Timecop.travel(Time.now+1)
@@ -127,7 +127,7 @@
127127
end
128128
described_class.open('AnotherChannel', 0)
129129
Timecop.travel(Time.now+described_class.transport.refresh_channels_every)
130-
described_class.read(0)
130+
described_class.read(0, 'path')
131131
described_class.connect_to_transport('AnotherChannel', 0, nil)
132132
expect(described_class.active).to eq(['TestChannel', 'AnotherChannel'])
133133
described_class.open('TestChannel', 2)
@@ -138,7 +138,7 @@
138138
expect(Synchromesh).to receive(:send).with('TestChannel', 'data2')
139139
described_class.connect_to_transport('TestChannel', 0, nil)
140140
described_class.send_to_channel('TestChannel', 'data2')
141-
expect(described_class.read(1)).to eq(['data', 'data2'])
141+
expect(described_class.read(1, 'path')).to eq(['data', 'data2'])
142142
end
143143
end
144144
end

spec/synchromesh/crud_access_regulation/broadcast_controls_access_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
describe "regulate access allowed" do
55

6+
before(:all) do
7+
Synchromesh.configuration do |config|
8+
config.transport = :none
9+
end
10+
end
11+
612
context "basic tests" do
713
before(:each) do
814
# spec_helper resets the policy system after each test so we have to setup

spec/synchromesh/crud_access_regulation/model_policies_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
describe "regulate access allowed" do
55

6+
before(:all) do
7+
Synchromesh.configuration do |config|
8+
config.transport = :none
9+
end
10+
end
11+
612
before(:each) do
713
stub_const 'DummyModel', Class.new(ActiveRecord::Base)
814
DummyModel.class_eval do

spec/test_app/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ gem 'reactrb-router'
6060
gem 'reactive-record', '>= 0.8.0'
6161
gem 'synchromesh', path: '../..' #git: "https://github.com/reactrb/synchromesh.git", branch: 'authorization-policies'
6262
gem 'hyper-trace'
63+
gem 'opal-browser'

spec/test_app/Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ GEM
7070
globalid (0.3.7)
7171
activesupport (>= 4.1.0)
7272
hike (1.2.3)
73-
hyper-trace (0.1.0)
73+
hyper-trace (0.3.0)
7474
i18n (0.7.0)
7575
jbuilder (2.6.0)
7676
activesupport (>= 3.0.0, < 5.1)
@@ -233,6 +233,7 @@ DEPENDENCIES
233233
jquery-rails
234234
jquery-ui-rails
235235
mysql2
236+
opal-browser
236237
opal-rails (>= 0.8.1)
237238
pry
238239
puma

0 commit comments

Comments
 (0)