|
| 1 | +```ruby |
| 2 | +class Picture < ApplicationRecord |
| 3 | + belongs_to :imageable, polymorphic: true |
| 4 | +end |
| 5 | + |
| 6 | +class Employee < ApplicationRecord |
| 7 | + has_many :pictures, as: :imageable |
| 8 | +end |
| 9 | + |
| 10 | +class Product < ApplicationRecord |
| 11 | + has_many :pictures, as: :imageable |
| 12 | +end |
| 13 | +``` |
| 14 | + |
| 15 | +product/employee.pictures -> works almost as normal has_many as far as Hyperstack client is concerned |
| 16 | +imageable is the "alias" of product/employee. Its as if there is a class Imageable that is the superclass |
| 17 | +of Product and Employee. |
| 18 | + |
| 19 | +so has_many :pictures means the usual thing (i.e. there is a belongs_to relationship on Picture) its just that |
| 20 | +the belongs_to will be belonging to :imageable instead of :employee or :product. |
| 21 | + |
| 22 | +okay fine |
| 23 | + |
| 24 | +the other way: |
| 25 | + |
| 26 | +the problem is that picture.imageable while loading is pointing to a dummy class (sure call it Imageable) |
| 27 | +so if we say picture.imageable.foo.bar.blat what we get is a dummy value that responds to all methods, and returns itself: |
| 28 | + |
| 29 | +picture.imageable -> imageable123 .foo -> imageable123 .bar -> ... etc. but it is a dummy value that will cause a fetch of the actual imageable record (or nil). |
| 30 | + |
| 31 | +.imageable should be able to leverage off of server_method. |
| 32 | + |
| 33 | +server_method(:imageable, PolymorphicDummy.new(:imageable)) |
| 34 | + |
| 35 | +hmmmm.... |
| 36 | + |
| 37 | +really its like doing a picture.imageable.itself (?) (that may work Juuuust fine) |
| 38 | + |
| 39 | +so picture.imageable returns this funky dummy value but does an across the wire request for picture.imageable (which should get imageable_id per a normal relationship) and also get picture.imageable_type. |
| 40 | + |
| 41 | + |
| 42 | +start again.... |
| 43 | + |
| 44 | +what happens if we ignore (on the client) the polymorphic: and as: keys? |
| 45 | + |
| 46 | +belongs_to :imageable |
| 47 | + |
| 48 | +means there is a class Imageable, okay so we make one, and add has_many :pictures to it. |
| 49 | + |
| 50 | + |
| 51 | +and again.... |
| 52 | + |
| 53 | +```ruby |
| 54 | +def imageable |
| 55 | + if imageable_type.loaded? && imageable_id.loaded? |
| 56 | + const_get(imageable_type).find(imageable_id) |
| 57 | + else |
| 58 | + DummyImageable.new(self) |
| 59 | + end |
| 60 | +end |
| 61 | +``` |
| 62 | + |
| 63 | +very close but will not work for cases like this: |
| 64 | + |
| 65 | +```ruby |
| 66 | + pic = Picture.new |
| 67 | + employee.pictures << pic |
| 68 | + pic.imageable # FAIL... (until its been saved) |
| 69 | + ... |
| 70 | +``` |
| 71 | + |
| 72 | +but still it may be as simple as overriding `<<` so that it sets type on imageable. But we still to have a proper belongs to relationship. |
| 73 | + |
| 74 | +```ruby |
| 75 | +def imageable |
| 76 | + if we already have the attribute set |
| 77 | + return the attribute |
| 78 | + else |
| 79 | + set attribute to DummyPolyClass.new(self, 'imageable') |
| 80 | + # DummyPolyClass init will set up a fetch of the actual imageable value |
| 81 | + end |
| 82 | +end |
| 83 | + |
| 84 | +def imageable=(x) |
| 85 | + # will it just work ? |
| 86 | +end |
| 87 | +``` |
| 88 | + |
| 89 | +its all about the collection inverse. The inverse class of the has_many is the class containing the polymorphic belongs to. But the inverse of a polymorphic belongs to depends on the value. If the value is nil or a DummyPolyClass object then there is no inverse. |
| 90 | + |
| 91 | +I think if inverse takes this into account then `<<` and `=` should just "work" (well almost) and probably everything else will to. |
0 commit comments