{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1442","repository_url":"https://api.github.com/repos/rails-api/active_model_serializers","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1442/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1442/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1442/events","html_url":"https://github.com/rails-api/active_model_serializers/issues/1442","id":127272014,"number":1442,"title":"[RFC] Automatic serializer lookup","user":{"login":"beauby","id":2215778,"avatar_url":"https://avatars.githubusercontent.com/u/2215778?v=3","gravatar_id":"","url":"https://api.github.com/users/beauby","html_url":"https://github.com/beauby","followers_url":"https://api.github.com/users/beauby/followers","following_url":"https://api.github.com/users/beauby/following{/other_user}","gists_url":"https://api.github.com/users/beauby/gists{/gist_id}","starred_url":"https://api.github.com/users/beauby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beauby/subscriptions","organizations_url":"https://api.github.com/users/beauby/orgs","repos_url":"https://api.github.com/users/beauby/repos","events_url":"https://api.github.com/users/beauby/events{/privacy}","received_events_url":"https://api.github.com/users/beauby/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/C:%20RFC","name":"C: RFC","color":"fbca04"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/V:%200.10.x","name":"V: 0.10.x","color":"bfd4f2"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":28,"created_at":"2016-01-18T17:12:54Z","updated_at":"2016-07-12T02:19:40Z","closed_at":null,"body":"# TL;DR\r\nWhat should be the lookup mechanism when inferring a serializer for a resource?\r\n\r\n# Proposal\r\nWhat seems to make most sense is the following:\r\n```ruby\r\n# resource.class == ::ResNamespace::Resource\r\n# controller.class == ::Api::V1::ResourcesController\r\nlookup_chain = prefixes(controller.class.to_s).map { |p| \"#{p}::#{resource.class}Serializer\" }\r\n# => [ \"::Api::V1::ResourcesController::ResNamespace::ResourceSerializer\", \r\n# \"::Api::V1::ResNamespace::ResourceSerializer\",\r\n# \"::Api::ResNamespace::ResourceSerializer\",\r\n# \"::ResNamespace::ResourceSerializer\" ]\r\n\r\n# When the resource is serialized as an association of an other serializer, say SerNamespace::OtherResourceSerializer,\r\n# add \"SerNamespace::OtherResourceSerializer::ResNamespace::ResourceSerializer\" at the beginning of the lookup chain \r\n```\r\n\r\nImplemented in #1436.\r\n\r\n# Current situation\r\nWork has been done to get us to a more flexible situation in this regard.\r\n\r\nThe whole automatic lookup can be disabled by setting `ActiveModelSerializers.config.automatic_lookup = false` from within an initializer.\r\n\r\nCurrently, for a resource `CoolNamespace::CoolResource`, AMS will look for a `CoolResourceSerializer` in the following namespaces:\r\n+ If it is being serialized as an association of some other resource using `OtherNamespace::OtherResourceSerializer`, first look for `OtherNamespace::OtherResourceSerializer::CoolResourceSerializer` (this is designed so that defining single-purpose serializers for associations is less cumbersome),\r\n+ otherwise, look for `CoolNamespace::CoolResourceSerializer` (this is kind of the \"normal\" lookup),\r\n+ if none is found, recursively do the same for the superclass of `CoolResource`.\r\n\r\n## Other possible places we might want to look at:\r\n+ the calling controller's namespace (#1436), i.e. when doing `render json: @posts` inside `Api::V1::PostsController#index`, look for `Api::V1::PostSerializer`,\r\n+ the calling controller's namespace, concatenated with the resource's namespace, i.e. when doing `render json: @cool_resources` inside `Api::V1::CoolResourcesController#index`, look for `Api::V1::CoolNamespace::CoolResourceSerializer` (@natematykiewicz),\r\n+ *all* of the calling controller's prefix namespaces (in the previous case it would be `::Api::V1`, `::Api`, and `::`,\r\n+ the parent serializer's namespace, i.e. when serializing `CoolResource` as an association through `OtherNamespace::OtherResourceSerializer`, look for `OtherNamespace::CoolResourceSerializer` (so that when you do `render @post, serializer: Experimental::CoolFeature::PostSerializer`, the post's author can be inferred to be `Experimental::CoolFeature::AuthorSerializer`),\r\n+ a user-specified list of namespaces,\r\n+ possibly many other things.\r\n\r\n## Possible API for user-provided paths\r\n+ add callbacks at the beginning and end of the lookup chain\r\n\r\nPlease chip in with your ideas, use-cases and strong opinions!"},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/231919988","html_url":"https://github.com/rails-api/active_model_serializers/issues/1442#issuecomment-231919988","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1442","id":231919988,"user":{"login":"bf4","id":142914,"avatar_url":"https://avatars.githubusercontent.com/u/142914?v=3","gravatar_id":"","url":"https://api.github.com/users/bf4","html_url":"https://github.com/bf4","followers_url":"https://api.github.com/users/bf4/followers","following_url":"https://api.github.com/users/bf4/following{/other_user}","gists_url":"https://api.github.com/users/bf4/gists{/gist_id}","starred_url":"https://api.github.com/users/bf4/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bf4/subscriptions","organizations_url":"https://api.github.com/users/bf4/orgs","repos_url":"https://api.github.com/users/bf4/repos","events_url":"https://api.github.com/users/bf4/events{/privacy}","received_events_url":"https://api.github.com/users/bf4/received_events","type":"User","site_admin":false},"created_at":"2016-07-12T02:19:40Z","updated_at":"2016-07-12T02:19:40Z","body":"My own solution is rather simple: I have a method in the controller that drrives the record class and passes that in to render, so it's not a pressure I'm feeling.  It's def on the todo list, just needs a simpl impl\n\nB mobile phone\n\n> On Jul 11, 2016, at 7:11 PM, L. Preston Sego III <notifications@github.com> wrote:\n> \n> if you have any opinions, please share! :-)\n> \n> —\n> You are receiving this because you were mentioned.\n> Reply to this email directly, view it on GitHub, or mute the thread.\n> \n"}}
{"actor":{"display_login":"bf4"}}