Skip to content

Commit aeee495

Browse files
committed
closes #169 #170
1 parent 72cc289 commit aeee495

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

ruby/hyper-model/lib/reactive_record/active_record/instance_methods.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ def dup
8181
end
8282

8383
def ==(ar_instance)
84-
@backing_record == ar_instance.instance_eval { @backing_record }
84+
return true if @backing_record == ar_instance.instance_eval { @backing_record }
85+
return false unless ar_instance.is_a?(ActiveRecord::Base)
86+
return false if ar_instance.new_record?
87+
return false unless self.class.base_class == ar_instance.class.base_class
88+
id == ar_instance.id
8589
end
8690

8791
def [](attr)

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/isomorphic_base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def self.get_type_hash(record)
198198
if RUBY_ENGINE == 'opal'
199199

200200
def self.gather_records(records_to_process, force, record_being_saved)
201+
puts "gather_records(#{records_to_process.count}, #{force}, #{record_being_saved.inspect})"
201202
# we want to pass not just the model data to save, but also enough information so that on return from the server
202203
# we can update the models on the client
203204

@@ -213,6 +214,7 @@ def self.gather_records(records_to_process, force, record_being_saved)
213214
backing_records = Hash[*records_to_process.collect { |record| [record.object_id, record] }.flatten(1)]
214215

215216
add_new_association = lambda do |record, attribute, assoc_record|
217+
puts "adding new association(#{record.inspect}, #{attribute}, #{assoc_record})"
216218
unless backing_records[assoc_record.object_id]
217219
records_to_process << assoc_record
218220
backing_records[assoc_record.object_id] = assoc_record

ruby/hyper-model/lib/reactive_record/server_data_cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def self.load_from_json(tree, target = nil)
510510
load_from_json(value, new_target) if new_target
511511
end
512512
rescue Exception => e
513-
raise e
513+
raise e
514514
end
515515
end
516516
end
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
require 'spec_helper'
2+
require 'test_components'
3+
require 'rspec-steps'
4+
5+
RSpec::Steps.steps "record equality", js: true do
6+
7+
before(:all) do
8+
require 'pusher'
9+
require 'pusher-fake'
10+
Pusher.app_id = "MY_TEST_ID"
11+
Pusher.key = "MY_TEST_KEY"
12+
Pusher.secret = "MY_TEST_SECRET"
13+
require "pusher-fake/support/base"
14+
15+
Hyperstack.configuration do |config|
16+
config.transport = :pusher
17+
config.channel_prefix = "synchromesh"
18+
config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options)
19+
end
20+
21+
class ApplicationPolicy
22+
always_allow_connection
23+
regulate_all_broadcasts { |policy| policy.send_all }
24+
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
25+
end
26+
27+
class ActiveRecord::Base
28+
class << self
29+
def public_columns_hash
30+
@public_columns_hash ||= {}
31+
end
32+
end
33+
end
34+
end
35+
36+
before(:each) do
37+
38+
isomorphic do
39+
module Sti
40+
class Base < ActiveRecord::Base
41+
def self.build_tables
42+
connection.create_table :bases, force: true do |t|
43+
t.string :type
44+
t.string :data
45+
t.timestamps
46+
end
47+
ActiveRecord::Base.public_columns_hash[name] = columns_hash
48+
end
49+
scope :a_scope, -> () {}, regulate: :always_allow
50+
scope :is_subclass1, -> () { where(type: 'Sti::SubClass1') }, regulate: :always_allow, client: -> { type == 'Base::SubClass1' }
51+
end
52+
53+
class SubClass1 < Base
54+
end
55+
end
56+
end
57+
58+
Sti::Base.build_tables
59+
60+
end
61+
62+
it "two records are the same if the point the same base record" do
63+
Sti::Base.create(data: 'record 1')
64+
expect_promise do
65+
Hyperstack::Model.load do
66+
Sti::Base.find(1) == Sti::Base.find_by_data('record 1')
67+
end
68+
end.to be_truthy
69+
end
70+
71+
it "two records are not the same if they point to different records" do
72+
Sti::Base.create(data: 'record 2')
73+
expect_promise do
74+
Hyperstack::Model.load do
75+
Sti::Base.find(1) == Sti::Base.find_by_data('record 2')
76+
end
77+
end.to be_falsy
78+
end
79+
80+
it "two new records are never the same" do
81+
expect_evaluate_ruby do
82+
Sti::Base.new(data: 'new record') == Sti::Base.new(data: 'new record')
83+
end.to be_falsy
84+
end
85+
86+
it "a record is never something else" do
87+
expect_evaluate_ruby do
88+
Sti::Base.new(data: 'new record') == Object.new
89+
end.to be_falsy
90+
end
91+
92+
it "an STI base record is the same record if the id's match" do
93+
expect_evaluate_ruby do
94+
Sti::Base.find(3) == Sti::SubClass1.find(3)
95+
end.to be_truthy
96+
end
97+
end

ruby/hyper-operation/lib/hyper-operation/transport/client_drivers.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,13 @@ def self.connect_to(channel_name, id = nil)
9494
channel = "#{ClientDrivers.opts[:channel]}-#{channel_string}"
9595
%x{
9696
var channel = #{ClientDrivers.opts[:pusher_api]}.subscribe(#{channel.gsub('::', '==')});
97-
channel.bind('dispatch', #{ClientDrivers.opts[:dispatch]})
97+
if (#{!@pusher_dispatcher_registered}) {
98+
console.log('registering dispatch for pusher')
99+
channel.bind('dispatch', #{ClientDrivers.opts[:dispatch]})
100+
}
98101
channel.bind('pusher:subscription_succeeded', #{lambda {ClientDrivers.get_queued_data("connect-to-transport", channel_string)}})
99102
}
103+
@pusher_dispatcher_registered = true
100104
elsif ClientDrivers.opts[:transport] == :action_cable
101105
channel = "#{ClientDrivers.opts[:channel]}-#{channel_string}"
102106
Hyperstack::HTTP.post(ClientDrivers.polling_path('action-cable-auth', channel), headers: { 'X-CSRF-Token' => ClientDrivers.opts[:form_authenticity_token] }).then do |response|

ruby/hyper-spec/lib/hyper-spec/component_test_helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ def open_in_chrome
281281
def pause(message = nil)
282282
if message
283283
puts message
284-
page.evaluate_ruby "puts #{message.inspect}.to_s + ' (type go() to continue)'"
284+
page.evaluate_script "console.log('#{message} (type go() to continue)')"
285285
end
286286

287287
page.evaluate_script('window.hyper_spec_waiting_for_go = true')
288-
288+
page.evaluate_script('go = function() {window.hyper_spec_waiting_for_go = false}')
289289
loop do
290290
sleep 0.25
291291
break unless page.evaluate_script('window.hyper_spec_waiting_for_go')

0 commit comments

Comments
 (0)