Skip to content

Commit e550488

Browse files
committed
wip
1 parent 8ac1c7b commit e550488

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def source_associations(model)
9999
# through relationship
100100
the_klass = klass(model)
101101
@source_associations[the_klass] ||= owner_class.reflect_on_all_associations.collect do |sibling|
102-
sibling.klass(model).reflect_on_all_associations.select do |assoc|
102+
sibling.klass.reflect_on_all_associations.select do |assoc|
103103
assoc.source == attribute && assoc.source_type == the_klass.name
104-
end
104+
end unless sibling.polymorphic?
105105
end.flatten
106106
end
107107

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ def push_onto_collection(model, association, ar_instance)
167167
def update_has_many_through_associations(assoc, value)
168168
# note that through and source_associations returns an empty set of
169169
# the provided ar_instance does not belong to a has_many_through association
170-
assoc.through_associations(@ar_instance)
170+
assoc.through_associations(value)
171171
.each { |ta| update_through_association(assoc, ta, value) }
172-
assoc.source_associations(@ar_instance)
172+
assoc.source_associations(value)
173173
.each { |sa| update_source_association(assoc, sa, value) }
174174
end
175175

ruby/hyper-model/polymorph-notes.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,37 @@ So only places that are applying inverse to an association that is NOT a collect
107107
All inverse_of method calls have been checked and updated
108108

109109
that leaves inverse which is only used in SETTERS hurray!
110+
111+
112+
### Latest thinking
113+
114+
going from `has_many / has_one as: ...` is easy its essentially setting the association foreign_key using the name supplied to the as:
115+
116+
The problem is going from the polymorphic belongs_to side.
117+
118+
We don't know the actual type we are loading which presents two problems.
119+
120+
First we just don't know the type. So if I say `Picture.find(1).imageable.foo.bar` I really can't do anything with foo and bar. This is solved by having a DummyPolymorph class, which responds to all missing methods with itself, and on creation sets up a vector to pull it the id, and type of the record being fetched. This will cause a second fetch to actually get `foo.bar` because we don't know what they are yet. (Its cool beacuse this is like Type inference actually, and I think we could eventually use a type inference system to get rid of the second fetch!!!)
121+
122+
Second we don't know the inverse of the relationship (since we don't know the type)
123+
124+
We can solve this by aliasing the inverse relationship (the one with the `as: SOMENAME` option) to be `has_many #{__hyperstack_polymorphic_inverse_of_#{SOMENAME}` and then defining method(s) against the relationship name. This way regardless of what the polymorphic relationship points to we know the inverse is `__hyperstack_polymorphic_inverse_of_#{SOMENAME}`.
125+
126+
If the inverse relationship is a has_many then we define
127+
```ruby
128+
def #{RELATIONSHIP_NAME}
129+
__hyperstack_polymorphic_inverse_of_#{SOMENAME}
130+
end
131+
```
132+
133+
If the inverse relationship is a has_one we have to work a bit harder:
134+
```ruby
135+
def #{RELATIONSHIP_NAME}
136+
__hyperstack_polymorphic_inverse_of_#{SOMENAME}[0]
137+
end
138+
def #{RELATIONSHIP_NAME}=(x)
139+
__hyperstack_polymorphic_inverse_of_#{SOMENAME}[0] = x # or perhaps we have to replace the array using the internal method in collection for that purpose.
140+
end
141+
```
142+
143+
The remaining problem is that the server side will have no such relationships defined so we need to add the `has_many __hyperstack_polymorphic_inverse_of_#{SOMENAME} as: SOMENAME` server side.

0 commit comments

Comments
 (0)