-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a default_include_tree config variable to ActiveModel::Serializer #1426
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
Conversation
@@ -4,7 +4,9 @@ module Adapter | |||
class Attributes < Base | |||
def initialize(serializer, options = {}) | |||
super | |||
@include_tree = IncludeTree.from_include_args(options[:include] || '*') | |||
@include_tree = IncludeTree.from_include_args( | |||
options[:include] || ActiveModel::Serializer.config.default_include_tree |
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.
s//ActiveModelSerializers.config
rubocop failures |
I 👍 the idea. |
9a0fbba
to
0ea5328
Compare
@@ -9,6 +9,8 @@ preferably inside an initializer. | |||
|
|||
- `adapter`: The [adapter](adapters.md) to use. Possible values: `:attributes, :json, :json_api`. Default: `:attributes`. | |||
- `serializer_lookup_enabled`: When `false`, serializers must be explicitly specified. Default: `true` | |||
- `default_includes`: What relationships to serialize by default. Default: `'*'`, which includes one level of related | |||
objects. See [includes](adapters.md#included) for more info. |
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.
Shouldn't it be See [included]
since the anchor is called included
?
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.
Hmm. I wrote this to match the config key, although I can see it being a little disconcerting to click through and find a different header. Of course, the thing actually being described is the include
option, which doesn't match either of these 100%. And, of course, it's a little weird to be linking to docs for the JSON API adapter to show behavior that is also applicable to adapters like Attributes
. 😞
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.
You're right. What we're actually missing here is a general documentation of the include
option for all the adapters.
0ea5328
to
2029ba5
Compare
Fixed conflicts, added changelog entry. |
@noahsilas looks good. I don't see the changelog... |
2029ba5
to
0b9d674
Compare
D'oh. Updated again with the changelog entry. |
@@ -14,6 +14,7 @@ Features: | |||
- [#1454](https://github.com/rails-api/active_model_serializers/pull/1454) Add support for | |||
relationship-level links and meta attributes. (@beauby) | |||
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby) | |||
- [#1426](https://github.com/rails-api/active_model_serializers/pull/1426) Add ActiveModelSerializers.config.default_includes (@empact) |
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.
@Empact @noahsilas put at top of features plz (sorry)
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.
Done.
8eb1ef4
to
cfe798b
Compare
Any progress on this? |
I've rebased to fix merge conflicts, and updated tests to match feedback that we weren't covering the most common cases to use this config option. How do y'all feel now? |
I'm surprised to see this build failing. It seems like this failure is related to the RuboCop 0.40 release. (https://github.com/bbatsov/rubocop/releases/tag/v0.40.0) Running with 0.39 locally, there are no errors. |
You have the full list of rubocop offences here. |
Would you please rebase this again to pull in the latest master that pins rubocop to 0.39.0. |
628fc58
to
2a5fac0
Compare
This is awesome - any chance of it being merged shorrtly? |
@default_include_tree ||= ActiveModel::Serializer::IncludeTree.from_include_args( | ||
ActiveModelSerializers.config.default_includes | ||
) | ||
end |
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.
nice 'cause easy to override. should this be a private api? I'd consider making it public, but no need to delay this PR
Looks good to me, though I have some followup questions |
2a5fac0
to
e1def82
Compare
I've updated the memoized default include tree to be used in more places. I made it a public API on
|
@cached_attributes = options[:cache_attributes] || {} | ||
@include_tree = \ |
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.
No need for \
here.
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.
@Empact, can you address this?
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.
Done.
needs rebase :-( |
e1def82
to
ba6b211
Compare
Rebased :-) |
This is useful to set application-wide default behavior - e.g. in previous versions of AMS the default behavior was to serialize the full object graph by default - equivalent to the '**' include tree. Currently just the global setting, but I think this could also work on a per-serializer basis, with more attention.
ba6b211
to
8c18d18
Compare
LGTM |
Thanks so much for this, @noahsilas and others! Awesome job 👍 |
Same, thanks @noahsilas for finishing it off. 👍 |
This is useful to set application-wide default behavior - e.g. in
previous versions of AMS the default behavior was to serialize the
full object graph by default - equivalent to the '**' include tree.
Currently just the global setting, but I think this could also work
on a per-serializer basis, with more attention.