Skip to content

Commit d7af764

Browse files
committed
wip - all non has-many-through specs passing
1 parent 96b2a5a commit d7af764

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class AssociationReflection
4040
attr_reader :source
4141
attr_reader :source_type
4242
attr_reader :options
43+
attr_reader :polymorphic_type_attribute
4344

4445
def initialize(owner_class, macro, name, options = {})
4546
owner_class.reflect_on_all_associations << self
@@ -59,6 +60,7 @@ def initialize(owner_class, macro, name, options = {})
5960
@source = options[:source] || @klass_name.underscore
6061
@source_type = options[:source_type] || @klass_name
6162
end
63+
@polymorphic_type_attribute = "#{name}_type" if options[:polymorphic]
6264
@attribute = name
6365
@through_associations = Hash.new { |_h, k| [] unless k }
6466
end
@@ -136,9 +138,7 @@ def source_associations(model)
136138
source_belongs_to_association.through_associations(model)
137139
end
138140

139-
def polymorphic?
140-
!@klass_name
141-
end
141+
alias :polymorphic? polymorphic_type_attribute
142142

143143
def inverse(model = nil)
144144
return @inverse if @inverse

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,32 +377,53 @@ def _react_param_conversion(param, opt = nil)
377377

378378
associations = reflect_on_all_associations
379379

380+
already_processed_keys = Set.new
381+
old_param = param.dup
382+
380383
param = param.collect do |key, value|
381-
assoc = associations.detect do |association|
382-
association.association_foreign_key == key
384+
next if already_processed_keys.include? key
385+
386+
model_name = model_id = nil
387+
388+
assoc = associations.detect do |poly_assoc|
389+
if key == poly_assoc.polymorphic_type_attribute
390+
model_name = value
391+
already_processed_keys << poly_assoc.association_foreign_key
392+
elsif key == poly_assoc.association_foreign_key
393+
model_id = value
394+
already_processed_keys << poly_assoc.polymorphic_type_attribute
395+
end
383396
end
384397

385398
if assoc
386399
if !value
387400
[assoc.attribute, [nil]]
388401
elsif assoc.polymorphic?
389-
model_name = param.detect { |k, *| k == "#{assoc.attribute}_type" }&.last
390-
model_name ||= target.send("#{assoc.attribute}_type")
402+
model_id ||= param.detect { |k, *| k == assoc.association_foreign_key }&.last
403+
model_id ||= target.send(assoc.attribute)&.id
404+
if model_id.nil?
405+
raise "Error in #{self.name}._react_param_conversion. \n"\
406+
"Could not determine the id of #{assoc.attribute} of #{target.inspect}.\n"\
407+
"It was not provided in the conversion data, "\
408+
"and it is unknown on the client"
409+
end
410+
model_name ||= param.detect { |k, *| k == assoc.polymorphic_type_attribute }&.last
411+
model_name ||= target.send(assoc.polymorphic_type_attribute)
391412
unless Object.const_defined?(model_name)
392413
raise "Error in #{self.name}._react_param_conversion. \n"\
393414
"Could not determine the type of #{assoc.attribute} of #{target.inspect}.\n"\
394415
"It was not provided in the conversion data, "\
395416
"and it is unknown on the client"
396417
end
397-
[assoc.attribute, { id: [value], model_name: [model_name] }]
418+
419+
[assoc.attribute, { id: [model_id], model_name: [model_name] }]
398420
else
399421
[assoc.attribute, { id: [value]}]
400422
end
401423
else
402424
[key, [value]]
403425
end
404-
end
405-
# TODO: verify wrapping with load_data was added so broadcasting works in 1.0.0.lap28
426+
end.compact
406427
ReactiveRecord::Base.load_data do
407428
ReactiveRecord::ServerDataCache.load_from_json(Hash[param], target)
408429
end

ruby/hyper-model/spec/batch7/poly_assoc_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'test_components'
33
require 'rspec-steps'
44

5-
describe "polymorphic relationships", js: true, skip: true do
5+
describe "polymorphic relationships", js: true do
66

77
before(:all) do
88
require 'pusher'
@@ -173,12 +173,13 @@ def self.build_tables
173173
def compare_to_server(model, expression, expected_result, load=true)
174174
server_side = eval("#{model.class}.find(#{model.id}).#{expression}")
175175
expect(server_side).to eq(expected_result)
176+
be_expected_result = expected_result.is_a?(Array) ? contain_exactly(*expected_result) : eq(expected_result)
176177
if load
177178
expect_promise("Hyperstack::Model.load { #{model.class}.find(#{model.id}).#{expression} }")
178-
.to contain_exactly(*expected_result)
179+
.to be_expected_result
179180
else
180181
wait_for_ajax
181-
expect_evaluate_ruby("#{model.class}.find(#{model.id}).#{expression}").to contain_exactly(*expected_result)
182+
expect_evaluate_ruby("#{model.class}.find(#{model.id}).#{expression}").to be_expected_result
182183
end
183184
end
184185

@@ -216,6 +217,7 @@ def compare_to_server(model, expression, expected_result, load=true)
216217
it 'create server side with broadcast update' do
217218
compare_to_server @imageable1, 'pictures.collect(&:name)', ['picture11', 'picture12']
218219
Picture.create(name: 'picture15', imageable: @imageable1)
220+
wait_for_ajax
219221
compare_to_server @imageable1, 'pictures.collect(&:name)', ['picture11', 'picture12', 'picture15'], false
220222
end
221223

@@ -234,27 +236,25 @@ def compare_to_server(model, expression, expected_result, load=true)
234236

235237
it 'changing belongs to relationship on client' do
236238
# not working yet
237-
# compare_to_server @picture11, 'imageable.name', 'imageable1'
238-
# compare_to_server @picture21, 'imageable.name', 'imageable2'
239239
compare_to_server @imageable1, 'pictures.collect(&:name)', ['picture11', 'picture12']
240240
compare_to_server @imageable2, 'pictures.collect(&:name)', ['picture21', 'picture22']
241-
242-
evaluate_ruby do
241+
evaluate_promise do
243242
p = Picture.find(1)
244243
p.imageable = Product.find(1)
245244
p.save
246245
end
247-
binding.pry
246+
wait_for_ajax
248247
compare_to_server @imageable1, 'pictures.collect(&:name)', ['picture12'], false
249248
compare_to_server @imageable2, 'pictures.collect(&:name)', ['picture11', 'picture21', 'picture22'], false
250249
end
251250

252251
it 'changing belongs to relationship on server' do
252+
# compare_to_server @picture11, 'imageable.name', 'imageable1' # here for debug assist
253+
# compare_to_server @picture11, 'imageable.ss', '123' # here for debug assist
254+
253255
# just debugging here... when id doesn't change we don't realize that data is changing
254256
compare_to_server @imageable1, 'pictures.collect(&:name)', ['picture11', 'picture12']
255257
compare_to_server @imageable2, 'pictures.collect(&:name)', ['picture21', 'picture22']
256-
257-
wait_for_ajax
258258
p = Picture.find_by_name('picture11')
259259
p.imageable = @imageable2
260260
p.save

0 commit comments

Comments
 (0)