-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Evaluate relationship blocks in Resource context #51
Comments
You are right. The way things work currently is that relationship blocks are |
@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 |
IMHO if it's eval'd in the resource context there needs to be another variable to access the relationship object |
So one possible solution to your problem @richmolj would be to forward to base object upon missing method. What do you think? |
Sounds like a performance issue? |
I doubt it would be a bottleneck but I'll try to hack something together and we'll benchmark it. |
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 attribute :title do
primary_office.title
end
has_one :primary_office do
# `resource` could instead be `@resource`, but that would conflict with exposures
data { resource.primary_office }
end
private
def primary_office
@object.offices.sort_by(&:rank).first
end Another idea might be to fully scope helper methods within a defined abstraction. That would force attribute :title do
helpers.primary_office.title
end
has_one :primary_office do
data { helpers.primary_office }
end
helpers do
def primary_office
@object.offices.sort_by(&:rank).first
end
end Maybe an arbitrary Depending on how complicated this gets with all the overlapping contexts and generic instance variables, methods defined in attribute :title do
helpers.primary_office(@object)
end
helpers do
def primary_office(employee)
employee.offices.sort_by(&:rank).first
end
end |
Related to this problem: link :related do
# unable to reference methods defined at the serializer level
end I'm not sure there's a work-around either. |
Consider a serializer:
The above code will not work, because the
has_one
data
block will eval in the context of aRelationship
instance (ieself
is_aJSONAPI::Serializable::Relationship
). This means I cannot access other methods within the serializable (ieprimary_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).The text was updated successfully, but these errors were encountered: