Skip to content

Commit 3ba519e

Browse files
committed
wip refactored setters has_many_through update methods
1 parent 2420ee1 commit 3ba519e

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,25 @@ def find_inverse(model) # private
177177
def klass(model = nil)
178178
@klass ||= Object.const_get(@klass_name) if @klass_name
179179
raise "model is not correct class" if @klass && model && model.class != @klass
180+
debugger unless @klass || model
180181
raise "no model supplied for polymorphic relationship" unless @klass || model
181182
@klass || model.class
182183
end
183184

184185
def collection?
185186
[:has_many].include? @macro
186187
end
188+
189+
def remove_member(member, owner)
190+
collection = owner.attributes[attribute]
191+
return if collection.nil?
192+
collection.delete(member)
193+
end
194+
195+
def add_member(member, owner)
196+
owner.attributes[attribute] ||= Collection.new(owner_class, owner, assoc)
197+
owner.attributes[attribute] << member
198+
end
187199
end
188200
end
189201
end

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ def set_has_many(assoc, raw_value)
4444

4545
def set_belongs_to(assoc, raw_value)
4646
set_common(assoc.attribute, raw_value) do |value, attr|
47-
update_has_many_through_associations assoc, value
48-
update_current_inverse_attribute assoc, @attributes[assoc.attribute]
49-
update_new_inverse_attribute assoc, value
47+
current_value = @attributes[assoc.attribute]
48+
update_has_many_through_associations assoc, current_value, :remove_member
49+
update_has_many_through_associations assoc, value, :add_member
50+
remove_current_inverse_attribute assoc, current_value
51+
add_new_inverse_attribute assoc, value
5052
update_belongs_to attr, value.itself
5153
end
5254
end
@@ -134,7 +136,7 @@ def change_status_and_notify_helper(attr, changed)
134136
# change from has_one to has_many. So we first deal with the current value, then
135137
# update the new value which uses the push_onto_collection helper
136138

137-
def update_current_inverse_attribute(association, model)
139+
def remove_current_inverse_attribute(association, model)
138140
return if model.nil?
139141
inverse_association = association.inverse(model)
140142
if inverse_association.collection?
@@ -146,7 +148,7 @@ def update_current_inverse_attribute(association, model)
146148
end
147149
end
148150

149-
def update_new_inverse_attribute(association, model)
151+
def add_new_inverse_attribute(association, model)
150152
return if model.nil?
151153
inverse_association = association.inverse(model)
152154
if inverse_association.collection?
@@ -191,36 +193,28 @@ def push_onto_collection(model, association, ar_instance)
191193
# and we have to then find any inverse has_many_through association (i.e. group or projects.uzers) and delete the
192194
# current value from those collections and push the new value on
193195

194-
def update_has_many_through_associations(assoc, value)
195-
# note that through and source_associations returns an empty set of
196-
# the provided ar_instance does not belong to a has_many_through association
196+
def update_has_many_through_associations(assoc, value, method)
197+
return if value.nil?
197198
assoc.through_associations(value).each do |ta|
198199
source_value = @attributes[ta.source]
199-
current_value = @attributes[assoc.attribute]
200200
# skip if source value is nil or if type of the association does not match type of source
201201
next unless source_value.class.to_s == ta.source_type
202-
update_through_association(ta, source_value, current_value, value)
202+
ta.send method, source_value, value
203203
ta.source_associations(source_value).each do |sa|
204-
update_source_association(sa, source_value, current_value, value)
204+
sa.send method, value, source_value
205205
end
206206
end
207207
end
208208

209-
def update_through_association(ta, source_value, current_value, new_value)
210-
unless current_value.nil? || current_value.attributes[ta.attribute].nil?
211-
current_value.attributes[ta.attribute].delete(source_value)
212-
end
213-
return unless new_value
214-
new_value.attributes[ta.attribute] ||= Collection.new(ta.owner_class, new_value, ta)
215-
new_value.attributes[ta.attribute] << source_value
216-
end
209+
# def remove_src_assoc(sa, source_value, current_value)
210+
# source_inverse_collection = source_value.attributes[sa.attribute]
211+
# source_inverse_collection.delete(current_value) if source_inverse_collection
212+
# end
213+
#
214+
# def add_src_assoc(sa, source_value, new_value)
215+
# source_value.attributes[sa.attribute] ||= Collection.new(sa.owner_class, source_value, sa)
216+
# source_value.attributes[sa.attribute] << new_value
217+
# end
217218

218-
def update_source_association(sa, source_value, current_value, new_value)
219-
source_inverse_collection = source_value.attributes[sa.attribute]
220-
source_inverse_collection.delete(current_value) if source_inverse_collection
221-
return unless new_value
222-
source_value.attributes[sa.attribute] ||= Collection.new(sa.owner_class, source_value, sa)
223-
source_value.attributes[sa.attribute] << new_value
224-
end
225219
end
226220
end

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

Lines changed: 12 additions & 13 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: 'not working yet' do
5+
describe "polymorphic relationships", js: true, skip: 'wip' do
66

77
before(:all) do
88
require 'pusher'
@@ -18,12 +18,6 @@
1818
config.opts = {app_id: Pusher.app_id, key: Pusher.key, secret: Pusher.secret}.merge(PusherFake.configuration.web_options)
1919
end
2020

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-
2721
class ActiveRecord::Base
2822
class << self
2923
def public_columns_hash
@@ -35,6 +29,17 @@ def public_columns_hash
3529

3630
before(:each) do
3731

32+
# spec_helper resets the policy system after each test so we have to setup
33+
# before each test
34+
stub_const 'TestApplicationPolicy', Class.new
35+
TestApplicationPolicy.class_eval do
36+
always_allow_connection
37+
regulate_all_broadcasts { |policy| policy.send_all }
38+
allow_change(to: :all, on: [:create, :update, :destroy]) { true }
39+
end
40+
41+
size_window(:small, :large)
42+
3843
isomorphic do
3944

4045
class Picture < ActiveRecord::Base
@@ -164,11 +169,6 @@ def self.build_tables
164169
@imageable3 = Product.create(name: 'imageable3', description: 'product2 description')
165170

166171
end
167-
#
168-
# it 'read has_many' do # baseline
169-
# expect(@imageable1.pictures.collect(&:name)).to eq ['picture11', 'picture12']
170-
# expect(@imageable2.pictures.collect(&:name)).to eq ['picture21', 'picture22']
171-
# end
172172

173173
def compare_to_server(model, expression, expected_result, load=true)
174174
server_side = eval("#{model.class}.find(#{model.id}).#{expression}")
@@ -182,7 +182,6 @@ def compare_to_server(model, expression, expected_result, load=true)
182182
end
183183
end
184184

185-
186185
it 'read belongs_to' do
187186
compare_to_server @picture11, 'imageable.name', 'imageable1'
188187
compare_to_server @picture11, 'imageable.ss', '123'

0 commit comments

Comments
 (0)