{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1356","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1356/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1356/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1356/events","html_url":"https://github.com/rails-api/active_model_serializers/pull/1356","id":119609120,"number":1356,"title":"Add inline syntax for attributes and associations","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},"labels":[{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Changelog%20Missing","name":"Changelog Missing","color":"fbca04"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Feature","name":"Feature","color":"009800"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Needs%20Code%20Review","name":"Needs Code Review","color":"fbca04"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Needs%20Team%20Discussion","name":"Needs Team Discussion","color":"5319e7"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Ready%20for%20PR","name":"Ready for PR","color":"009800"},{"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,"milestone":null,"comments":15,"created_at":"2015-11-30T23:52:53Z","updated_at":"2015-12-04T20:35:06Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1356","html_url":"https://github.com/rails-api/active_model_serializers/pull/1356","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1356.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1356.patch"},"body":"Goals:\r\n\r\n1. Allow defining attributes so that they don't conflict with existing methods\r\n1. Allow customizing associations e.g. comments.last(5)\r\n1. Remove dynamically defined methods on the serializer\r\n\r\nBased on work in #1262, #1354\r\n\r\nRef: #1263, #1261, #1311\r\n\r\nThis PR is intended as a starting point for further work improving how\r\nserializers map their resources attributes and associations\r\n\r\nIntroduced, in support of this:\r\n\r\n- `Serializer#_attribute_mappings`\r\n- Extracted Serializer attribute methods to an Attributes module. I'd consider a class but this is a simpler first step and doesn't block changing it to an Attributes class than has many Attribute objects in the future\r\n\r\n[Possible regression](https://github.com/rails-api/active_model_serializers/pull/1356#discussion_r46381686): \r\n\r\nremoved referring to object attributes in the serializer without calling them directly on object. (I didn't even know this was possible. We could add a respond_to_missing / method_missing for this)\r\n\r\n```diff\r\n   def slug\r\n-    \"#{name}-#{id}\"\r\n+    \"#{object.name}-#{object.id}\"\r\n```"},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/162075019","html_url":"https://github.com/rails-api/active_model_serializers/pull/1356#issuecomment-162075019","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1356","id":162075019,"user":{"login":"groyoh","id":7434196,"avatar_url":"https://avatars.githubusercontent.com/u/7434196?v=3","gravatar_id":"","url":"https://api.github.com/users/groyoh","html_url":"https://github.com/groyoh","followers_url":"https://api.github.com/users/groyoh/followers","following_url":"https://api.github.com/users/groyoh/following{/other_user}","gists_url":"https://api.github.com/users/groyoh/gists{/gist_id}","starred_url":"https://api.github.com/users/groyoh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/groyoh/subscriptions","organizations_url":"https://api.github.com/users/groyoh/orgs","repos_url":"https://api.github.com/users/groyoh/repos","events_url":"https://api.github.com/users/groyoh/events{/privacy}","received_events_url":"https://api.github.com/users/groyoh/received_events","type":"User","site_admin":false},"created_at":"2015-12-04T20:35:05Z","updated_at":"2015-12-04T20:35:05Z","body":"@noahsilas I'm pretty sure we can work around this issue. For example, making sure that `meta`, and `link` methods return a certain object (e.g. a symbol as in @beauby's example) and serialize the relationship depending on the block return value:\r\n* Use `object.comments`\r\n```ruby\r\nhas_many :comments\r\n```\r\n* Use the block return value\r\n```ruby\r\nhas_many :comments do\r\n  object.special_comments\r\nend\r\n```\r\n* With link but use `object.comments`\r\n```ruby\r\nhas_many :comments do\r\n  link :self do\r\n    #something\r\n  end # => :SpecialSymbolToPreventInlineAssociation\r\nend\r\n```\r\n* With link and use block return value\r\n```ruby\r\nhas_many :comments do\r\n  link :related do\r\n    #something\r\n  end\r\n  object.association # or data object.association\r\nend\r\n```\r\n\r\nThe first two would work pretty well with any adapter while the last one would only fit JSONAPI and everybody is happy :grin: .\r\n\r\nOf course, this probably require more thoughts and maybe have some flaws too."}}