{"action":"created","comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/38876807","id":38876807,"diff_hunk":"@@ -46,6 +22,111 @@ def serializable_hash(options = nil)\n         def fragment_cache(cached_hash, non_cached_hash)\n           Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)\n         end\n+\n+        private\n+\n+        # iterate through the associations on the serializer,\n+        # adding them to the parent as needed (as singular or plural)\n+        #\n+        # nested_associations is a list of symbols that governs what\n+        # associations on the passed in seralizer to include\n+        def add_resource_relationships(parent, serializer, nested_associations = [])\n+          # have the include array normalized\n+          nested_associations = ActiveModel::Serializer::Utils.include_array_to_hash(nested_associations)\n+\n+          included_associations = if nested_associations.present?\n+            serializer.associations.select{ |association|\n+              # nested_associations is a hash of:\n+              #   key => nested association to include\n+              nested_associations.has_key?(association.name)\n+            }\n+          else\n+            serializer.associations\n+          end\n+\n+          included_associations.each do |association|\n+            serializer = association.serializer\n+            opts = association.options\n+            key = association.key\n+\n+            # sanity check if the association has nesting data\n+            has_nesting = nested_associations[key].present?\n+            if has_nesting\n+              include_options_from_parent = { include: nested_associations[key] }\n+              opts = opts.merge(include_options_from_parent)\n+            end\n+\n+            if serializer.respond_to?(:each)\n+              parent[key] = add_relationships(serializer, opts)\n+            else\n+              parent[key] = add_relationship(serializer, opts)\n+            end\n+          end\n+\n+          parent\n+        end\n+\n+        # add a singular relationship\n+        # the options should always belong to the serializer\n+        def add_relationship(serializer, options)\n+          serialized_relationship = serialized_or_virtual_of(serializer, options)\n+\n+          nested_associations_to_include = options[:include]\n+          if nested_associations_to_include.present?\n+            serialized_relationship = add_resource_relationships(\n+              serialized_relationship,\n+              serializer,\n+              nested_associations_to_include)\n+          end\n+\n+          serialized_relationship\n+        end\n+\n+        # add a many relationship\n+        def add_relationships(serializer, options)\n+          serialize_array(serializer, options) do |serialized_item, item_serializer|\n+            nested_associations_to_include = options[:include]\n+\n+            if nested_associations_to_include.present?\n+              serialized_item = add_resource_relationships(\n+                serialized_item,\n+                item_serializer,\n+                nested_associations_to_include)\n+            end\n+\n+            serialized_item\n+          end\n+        end\n+\n+\n+        def serialize_array_without_root(serializer, options)\n+          serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }\n+        end\n+\n+        # a virtual value is something that doesn't need a serializer,\n+        # such as a ruby array, or any other raw value\n+        def serialized_or_virtual_of(serializer, options)\n+          if serializer && serializer.object\n+            resource_object_for(serializer)\n+          elsif options[:virtual_value]\n+            options[:virtual_value]\n+          end\n+        end\n+\n+        def serialize_array(serializer, options)","path":"lib/active_model/serializer/adapter/json.rb","position":137,"original_position":137,"commit_id":"3f870ebce23d6c5f7ece17d9774e087266f3955d","original_commit_id":"3f870ebce23d6c5f7ece17d9774e087266f3955d","user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"body":"I think I was wanting to evaluate something with the yeild, but I'll look at it and see if it's worth keeping. I'll probably get rid of it, and make it a one liner","created_at":"2015-09-07T18:50:11Z","updated_at":"2015-09-07T18:50:11Z","html_url":"https://github.com/rails-api/active_model_serializers/pull/1127#discussion_r38876807","pull_request_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127","_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/38876807"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1127#discussion_r38876807"},"pull_request":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127"}}},"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127","id":44369970,"html_url":"https://github.com/rails-api/active_model_serializers/pull/1127","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1127.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1127.patch","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1127","number":1127,"state":"open","locked":false,"title":"Support nested associations for json adapter","user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"body":"Originally #1114 (I messed up the history because I merged instead of rebased)\r\n\r\nThis PR is based off of #1111, cause I needed the refactoring.\r\n\r\nDiscussion of idea here: #1113\r\n\r\nRelates to:\r\n#921\r\n#1073\r\n\r\nBasically:\r\n\r\n```ruby\r\nBlogSerializer < #...\r\n  has_many :posts, include: { author: [:bio], comments: [:author]] }\r\nend\r\n```\r\nresulting json may look like:\r\n\r\n```\r\n{\r\n  blog: {\r\n    posts: [\r\n      {\r\n        author: {\r\n          bio: {...}\r\n        },\r\n        comments: [\r\n          {  author: {...} }\r\n        ]\r\n      }\r\n    ]\r\n  }\r\n}\r\n```\r\n\r\nSee the included tests for more examples.","created_at":"2015-09-07T16:34:52Z","updated_at":"2015-09-07T18:50:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"787f804625cb6c52e89a545e460fbd5f241771d7","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127/commits","review_comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127/comments","review_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1127/comments","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/3f870ebce23d6c5f7ece17d9774e087266f3955d","head":{"label":"NullVoxPopuli:support-nested-associations-for-json-adapter","ref":"support-nested-associations-for-json-adapter","sha":"3f870ebce23d6c5f7ece17d9774e087266f3955d","user":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"repo":{"id":25101596,"name":"active_model_serializers","full_name":"NullVoxPopuli/active_model_serializers","owner":{"login":"NullVoxPopuli","id":199018,"avatar_url":"https://avatars.githubusercontent.com/u/199018?v=3","gravatar_id":"","url":"https://api.github.com/users/NullVoxPopuli","html_url":"https://github.com/NullVoxPopuli","followers_url":"https://api.github.com/users/NullVoxPopuli/followers","following_url":"https://api.github.com/users/NullVoxPopuli/following{/other_user}","gists_url":"https://api.github.com/users/NullVoxPopuli/gists{/gist_id}","starred_url":"https://api.github.com/users/NullVoxPopuli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NullVoxPopuli/subscriptions","organizations_url":"https://api.github.com/users/NullVoxPopuli/orgs","repos_url":"https://api.github.com/users/NullVoxPopuli/repos","events_url":"https://api.github.com/users/NullVoxPopuli/events{/privacy}","received_events_url":"https://api.github.com/users/NullVoxPopuli/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/NullVoxPopuli/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":true,"url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers","forks_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/forks","keys_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/events","assignees_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/merges","archive_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/NullVoxPopuli/active_model_serializers/releases{/id}","created_at":"2014-10-12T00:59:37Z","updated_at":"2014-10-15T23:46:13Z","pushed_at":"2015-09-07T16:34:14Z","git_url":"git://github.com/NullVoxPopuli/active_model_serializers.git","ssh_url":"git@github.com:NullVoxPopuli/active_model_serializers.git","clone_url":"https://github.com/NullVoxPopuli/active_model_serializers.git","svn_url":"https://github.com/NullVoxPopuli/active_model_serializers","homepage":"","size":1753,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rails-api:master","ref":"master","sha":"1388ae82f26133d62f4e6cdfc40d1c659e121935","user":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"repo":{"id":2889230,"name":"active_model_serializers","full_name":"rails-api/active_model_serializers","owner":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rails-api/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":false,"url":"https://api.github.com/repos/rails-api/active_model_serializers","forks_url":"https://api.github.com/repos/rails-api/active_model_serializers/forks","keys_url":"https://api.github.com/repos/rails-api/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rails-api/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rails-api/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/rails-api/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/events","assignees_url":"https://api.github.com/repos/rails-api/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/rails-api/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/rails-api/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/rails-api/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/rails-api/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/rails-api/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/rails-api/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rails-api/active_model_serializers/merges","archive_url":"https://api.github.com/repos/rails-api/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rails-api/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/rails-api/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/rails-api/active_model_serializers/releases{/id}","created_at":"2011-12-01T06:39:26Z","updated_at":"2015-09-06T23:26:16Z","pushed_at":"2015-09-07T16:35:22Z","git_url":"git://github.com/rails-api/active_model_serializers.git","ssh_url":"git@github.com:rails-api/active_model_serializers.git","clone_url":"https://github.com/rails-api/active_model_serializers.git","svn_url":"https://github.com/rails-api/active_model_serializers","homepage":"","size":5946,"stargazers_count":2944,"watchers_count":2944,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":673,"mirror_url":null,"open_issues_count":155,"forks":673,"open_issues":155,"watchers":2944,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1127"},"issue":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1127"},"comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1127/comments"},"review_comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127/comments"},"review_comment":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1127/commits"},"statuses":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/3f870ebce23d6c5f7ece17d9774e087266f3955d"}}}}