-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add supports for :links and :data options to has_many associations for jsonapi adapter #1282
Conversation
@@ -27,6 +27,7 @@ def get_serializer(resource, options = {}) | |||
end | |||
serializable_resource = ActiveModel::SerializableResource.new(resource, options) | |||
if serializable_resource.serializer? | |||
serializable_resource.url_helper = self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this
👎 We will not include the controller. That would be a huge bug and complexity attractor
I think there's an issue for this, but I think the :context
option should be renamed :request_context
and be an object named RequestContext
that has the methods original_url
, query_params
, and url_helper
, though them's just my thoughts.. but the main this is that we will not pass around a controller instance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Would you be ok with this pr only including hash with proc links option? In the mean time it will allow to manually expose links and we can se for automatic link generator in the feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, plus there's already a serialization_scope
options, so maybe serialization_context
would be good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref: #1269
@@ -1,6 +1,7 @@ | |||
verbose = $VERBOSE | |||
$VERBOSE = nil | |||
class Model | |||
extend ActiveModel::Naming |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove
Thinking aloud: This is a big (important) change. Maybe let's make this smaller and start with the simplest case? |
@bf4 completely agree about starting smaller. Supporting only has_many :posts, links: { related: url }, data: false and has_many :posts, links: { related: -> (object, name) { url } }, data: false sound's like small enough for you? |
or even just |
@bf4 the problem with |
Yup, @tchak is right. We have basically 3 options here:
|
@beauby my current plan is to simplify this PR to support only proc/lambda. Everything else can be implemented in terms of proc on top of it once we figured out |
@tchak uri templates is a very interesting option as well, I'm not sure which is best |
b7adbb8
to
92dd0ed
Compare
@beauby I completely agree. I am just thinking making it one step at a time. |
31cfd12
to
5f81788
Compare
Link template + link_attributes (like mustache) is easy to understand and switch out impl Using the template rfc makes building an info response object easier but isn't super high priority B mobile phone
|
We have to be careful when passing |
@beauby Right! I will look in to it. |
4ffde66
to
f26bde2
Compare
@beauby I added a bunch of tests to test different combinations of |
def relationship_links_for(name, owner, options = {}) | ||
options[:links].each_with_object({}) do |(key, link), hash| | ||
if link.is_a? Proc | ||
hash[key] = link.call(owner, name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be respond_to?(:call)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here and elsewhere
hash[association.key] = {} | ||
|
||
if association.options.fetch(:data, true) | ||
hash[association.key][:data] = relationship_data_for(association.serializer, association.options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, interesting, you're re-using the :data key
@tchak Looks really good, 💯! Two more things:
|
@bf4 I was planing on implementing uri template in termes of default proc once this was merged. I think having proc as a backup "power-user" option is good. If you prefer I can add uri template directly to this PR. I am just not sure about all the "variables" I should interpolate with uri template. Obvious one is |
f26bde2
to
2d0e417
Compare
I actually really like the procs since I can imagine scenarios the url should be dynamic. Something like: has_many :posts, proc { |object, name|
if v2_feature_toggled_on_for_current_user?
"http://test.host/apt/v2/posts"
else
"http://test.host/apt/v1/posts"
end
} |
@tchak are we still working on this? |
@bf4 I will have some time to rebase and cleanup this weekend |
Awesome! B mobile phone
|
Is this supposed to resolve: #834? |
While using this, I noticed that I didn't have access to
to reduce the repetitiveness of this task,
|
@djsegal The url handling is going to be in the serialization_context #1313, but just hasn't been finished. Would you like to give it a shot? |
ref #1454 |
@rails-api/ams Is this still relevant? |
This is a rebase from #1028
I hope @lsylvester don't mind :)
I also added more test including the one requested by @joaomdmoura with
links
+data
. I also added support for passing in a hash of urls or proc in order to be able to provide custom links.I hope we can move forward from here.