You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Evaluate relationship blocks in Resource context #51
The above code will not work, because the has_onedata block will eval in the context of a Relationship instance (ie self is_a JSONAPI::Serializable::Relationship). This means I cannot access other methods within the serializable (ie primary_office).
It's confusing, as we would have access to @model and other blocks (like the attribute block above), do eval in the context of the serializable.
I'd prefer this logic live in the serializable, not the model, as it's serialization-specific (For instance, a primary_office method on the resource may fire a query instead of sorting already-loaded objects).
You are right. The way things work currently is that relationship blocks are evald in a specific context in which exposures are forwarded.
A quick workaround for your situation would be to move the method definition inside the has_one block.
I am not sure what the best tradeoff is here.
@beauby the method needs to be shared with the rest of the serializable, though, so I'm not sure that's a DRY solution. Is there a downside to evaling these blocks in the serializable context?
I'm experiencing this same problem with a serializer I'm trying to define. I'm not too familiar with the jsonapi-rb internals, but I have a couple of ideas for tackling this.
If method_missing is too much of a concern, one idea could be to provide a way of accessing the resource object from within the relationship context.
attribute:titledoprimary_office.titleendhas_one:primary_officedo# `resource` could instead be `@resource`, but that would conflict with exposuresdata{resource.primary_office}endprivatedefprimary_office@object.offices.sort_by(&:rank).firstend
Another idea might be to fully scope helper methods within a defined abstraction. That would force attribute and relationships to use the same interface:
Maybe an arbitrary def foo outside of helpers (at the same level as attribute do ... end) is unavailable within blocks in this case?
Depending on how complicated this gets with all the overlapping contexts and generic instance variables, methods defined in helpers could be required to take parameters to enforce structure:
Activity
beauby commentedon Jan 4, 2017
You are right. The way things work currently is that relationship blocks are
evald in a specific context in which exposures are forwarded.A quick workaround for your situation would be to move the method definition inside the
has_oneblock.I am not sure what the best tradeoff is here.
richmolj commentedon Jan 4, 2017
@beauby the method needs to be shared with the rest of the serializable, though, so I'm not sure that's a DRY solution. Is there a downside to
evaling these blocks in the serializable context?andreaseger commentedon Jan 19, 2017
IMHO if it's eval'd in the resource context there needs to be another variable to access the relationship object
beauby commentedon Jul 12, 2017
So one possible solution to your problem @richmolj would be to forward to base object upon missing method. What do you think?
richmolj commentedon Jul 12, 2017
Sounds like a performance issue?
beauby commentedon Jul 12, 2017
I doubt it would be a bottleneck but I'll try to hack something together and we'll benchmark it.
aprescott commentedon Aug 30, 2018
I'm experiencing this same problem with a serializer I'm trying to define. I'm not too familiar with the jsonapi-rb internals, but I have a couple of ideas for tackling this.
If
method_missingis too much of a concern, one idea could be to provide a way of accessing the resource object from within the relationship context.Another idea might be to fully scope helper methods within a defined abstraction. That would force
attributeand relationships to use the same interface:Maybe an arbitrary
def foooutside ofhelpers(at the same level asattribute do ... end) is unavailable within blocks in this case?Depending on how complicated this gets with all the overlapping contexts and generic instance variables, methods defined in
helperscould be required to take parameters to enforce structure:aprescott commentedon Aug 30, 2018
Related to this problem:
linkhas the same issue:I'm not sure there's a work-around either.
def foowon't quite work the same way because there's no nested DSL form forlink.