Skip to content
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

Fixes virtual value not being used #975

Merged
merged 1 commit into from
Jul 2, 2015

Conversation

GriffinHeart
Copy link
Contributor

First PR here so let me know if I should change something,

Noticed that virtual_value was never being used and this PR fixes it

@joaomdmoura
Copy link
Member

hey @GriffinHeart I appreciate what you did here 😄, but Virtual value is used behind the scenes, on every association that doesn't have a serializer itself as you check here

I'm not sure if virtual_value was created to be used as an option. Because, as you can see on the the serializer.rb (link above), it wouldn't work on associations that doesn't have a serializer (or arrays where one elements doesn't have a serializer)

Also, I can't see a use case, because you can only work with static values. But if you use the a method to override it instead, you have access to object and scope.

I'll not close it right now because I may have not seen some use case here, did you had something in mind when you decided to work on it? 😄

Btw your virtual value representation doesn't match the JSON API format. Check the resource linakge and the resource identifier object

... A "resource identifier object" MUST contain type and id members.

@bf4
Copy link
Member

bf4 commented Jun 30, 2015

On the other hand @GriffinHeart has pointed out that val is passed in but not used

-          resource[:relationships][name] = { data: nil }
+          resource[:relationships][name] = { data: val }

@joaomdmoura
Copy link
Member

Indeed. But val should be parsed to follow JSON API conventions, even without a serializer.

@bf4
Copy link
Member

bf4 commented Jul 1, 2015

Fortunately, it appears the only place this code is used is here.

50          def add_relationship(resource, name, serializer, val=nil)
… 
137         def add_resource_relationships(attrs, serializer, options = {})
138           if association.respond_to?(:each)
139             add_relationships(attrs, name, association)
140           else
141             if opts[:virtual_value]
142               add_relationship(attrs, name, nil, opts[:virtual_value])
143             else
144               add_relationship(attrs, name, association)

Which means this code is equivalent to the below diff, unless I'm missing something

+50         def add_relationship(resource, name, serializer)
-50         def add_relationship(resource, name, serializer, val=nil)
… 
137         def add_resource_relationships(attrs, serializer, options = {})
138           if association.respond_to?(:each)
139             add_relationships(attrs, name, association)
140           else
141             if opts[:virtual_value]
+142              add_relationship(attrs, name, nil)
-142              add_relationship(attrs, name, nil, opts[:virtual_value])
143             else
144               add_relationship(attrs, name, association)

@GriffinHeart
Copy link
Contributor Author

@joaomdmoura Sorry for a late reply.

My biggest issue is that if you add relationships that don't have a serialiser using the jsonapi adapter they will be rendered as nil. When the relationship doesn't have a serialiser it will be using the virtual_value to carry the relationship.

In either case I just want the option to be able to render something instead of a nil, which I'm not sure is compatible with jsonapi 1.0

@joaomdmoura
Copy link
Member

@GriffinHeart you are right! Just tested it. Great catch!

So yeah, I'm willing to merge it. But I don't think it should be an option. But this might be done in another PR.

Another optmization would be formatting the object to fit with the JSON API conventions as I mentioned earlier, guaranteeing it would have the usual type and idkeys.

I'm merging this one because it's a great fix! But would be awesome to see both optimizations in further PRs 😄

joaomdmoura added a commit that referenced this pull request Jul 2, 2015
@joaomdmoura joaomdmoura merged commit a895de7 into rails-api:master Jul 2, 2015
@GriffinHeart
Copy link
Contributor Author

@joaomdmoura Agreed, virtual_value doesn't make as much sense being a public option since you can always overwrite attributes and relationships

I think the real use case is not virtual_value just the ability to define the relationship like

has_many :rewards

def rewards
  { ...somestub }
end

and have the adapter use that for serialisation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants