Skip to content

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

Merged
merged 1 commit into from
May 26, 2016

Conversation

Empact
Copy link
Contributor

@Empact Empact commented Jan 12, 2016

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.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s//ActiveModelSerializers.config

@bf4
Copy link
Member

bf4 commented Jan 12, 2016

rubocop failures

@beauby
Copy link
Contributor

beauby commented Jan 13, 2016

I 👍 the idea.

@@ -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.
Copy link
Member

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?

Copy link
Contributor

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. 😞

Copy link
Member

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.

@noahsilas
Copy link
Contributor

Fixed conflicts, added changelog entry.

@bf4
Copy link
Member

bf4 commented Mar 2, 2016

@noahsilas looks good. I don't see the changelog...

@noahsilas
Copy link
Contributor

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)
Copy link
Member

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@noahsilas noahsilas force-pushed the default-include branch 2 times, most recently from 8eb1ef4 to cfe798b Compare March 15, 2016 02:06
@remear
Copy link
Member

remear commented Mar 15, 2016

Any progress on this?

@noahsilas
Copy link
Contributor

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?

@noahsilas
Copy link
Contributor

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.

@beauby
Copy link
Contributor

beauby commented May 10, 2016

You have the full list of rubocop offences here.

@remear
Copy link
Member

remear commented May 11, 2016

Would you please rebase this again to pull in the latest master that pins rubocop to 0.39.0.

@hut8
Copy link
Contributor

hut8 commented May 11, 2016

This is awesome - any chance of it being merged shorrtly?

@default_include_tree ||= ActiveModel::Serializer::IncludeTree.from_include_args(
ActiveModelSerializers.config.default_includes
)
end
Copy link
Member

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

@bf4
Copy link
Member

bf4 commented May 11, 2016

Looks good to me, though I have some followup questions

@noahsilas
Copy link
Contributor

I've updated the memoized default include tree to be used in more places. I made it a public API on ActiveModelSerializers, which has a couple of benefits:

  • Only one instance, instead of one instance per serializer class
  • Easy to clear this cache, for instance when testing different configurations (see test/action_controller/json/include_test.rb).

@cached_attributes = options[:cache_attributes] || {}
@include_tree = \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for \ here.

Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@NullVoxPopuli
Copy link
Contributor

needs rebase :-(

@noahsilas
Copy link
Contributor

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.
@NullVoxPopuli
Copy link
Contributor

LGTM

@NullVoxPopuli NullVoxPopuli merged commit 7d7329b into rails-api:master May 26, 2016
@hut8
Copy link
Contributor

hut8 commented May 26, 2016

Thanks so much for this, @noahsilas and others! Awesome job 👍

@Empact
Copy link
Contributor Author

Empact commented May 26, 2016

Same, thanks @noahsilas for finishing it off. 👍

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

Successfully merging this pull request may close these issues.

8 participants