{"action":"created","comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/37604580","id":37604580,"diff_hunk":"@@ -4,9 +4,31 @@ module ActiveModel\n   class Serializer\n     class Adapter\n       class JsonApi < Adapter\n+        @root = :data\n+        DEFAULT_ATTRIBUTES = [:type, :id]\n+\n+        def self.params(permitted, associations)\n+          relationships = {}\n+          associations.each do |assoc|\n+             relationships[assoc] = {}\n+             relationships[assoc][@root] = DEFAULT_ATTRIBUTES\n+           end\n+\n+          return :type, {attributes: permitted}, {relationships: relationships}\n+        end\n+\n+        def self.parse(params)\n+          attrs, assoc = {}, {}\n+          attrs = params['attributes'] if params['attributes']\n+          assoc = params['relationships'].map {|a| {a.shift => a.first['data']['type'].camelize.safe_constantize.find(a.shift['data']['id'])}} if params['relationships']","path":"lib/active_model/serializer/adapter/json_api.rb","position":20,"original_position":20,"commit_id":"315067ee8232ec7272958161f1cccf8966a0c839","original_commit_id":"315067ee8232ec7272958161f1cccf8966a0c839","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},"body":"Also, as per the specification, the `data` attribute of a relationship object can be an array of resource identifiers. Hence, that part should actually be something along the lines of:\r\n```ruby                                                                                                                                                                                                       \r\n        def self.parse(params)\r\n          attrs, assoc = {}, {}\r\n          attrs = params['attributes'] if params['attributes']\r\n          assoc = params['relationships'].map do |rel|                                                                                                                                                      \r\n            key, data = rel.shift, rel.first['data']                                                                                                                                                        \r\n            value = if data.kind_of? Array                                                                                                                                                                  \r\n                      data.map { |ri| self.parse_resource_identifier ri }                                                                                                                                   \r\n                    elsif data                                                                                                                                                                              \r\n                      self.parse_resource_identifier data                                                                                                                                                   \r\n                    else                                                                                                                                                                                    \r\n                      nil                                                                                                                                                                                   \r\n                    end                                                                                                                                                                                     \r\n            {key => value}                                                                                                                                                                                  \r\n          end if params['relationships']                                                                                                                                                                    \r\n          assoc.reduce attrs, :merge\r\n        end\r\n\r\n      private:\r\n        def self.parse_resource_identifier(ri)                                                                                                                                                              \r\n          ri['type'].camelize.singularize.safe_constantize.find(ri['id'])                                                                                                                                   \r\n        end         \r\n```","created_at":"2015-08-21T03:46:28Z","updated_at":"2015-08-21T03:46:28Z","html_url":"https://github.com/rails-api/active_model_serializers/pull/950#discussion_r37604580","pull_request_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950","_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/37604580"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/950#discussion_r37604580"},"pull_request":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950"}}},"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950","id":37488938,"html_url":"https://github.com/rails-api/active_model_serializers/pull/950","diff_url":"https://github.com/rails-api/active_model_serializers/pull/950.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/950.patch","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950","number":950,"state":"open","locked":false,"title":"WIP - Deserializer implementation","user":{"login":"joaomdmoura","id":667063,"avatar_url":"https://avatars.githubusercontent.com/u/667063?v=3","gravatar_id":"","url":"https://api.github.com/users/joaomdmoura","html_url":"https://github.com/joaomdmoura","followers_url":"https://api.github.com/users/joaomdmoura/followers","following_url":"https://api.github.com/users/joaomdmoura/following{/other_user}","gists_url":"https://api.github.com/users/joaomdmoura/gists{/gist_id}","starred_url":"https://api.github.com/users/joaomdmoura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaomdmoura/subscriptions","organizations_url":"https://api.github.com/users/joaomdmoura/orgs","repos_url":"https://api.github.com/users/joaomdmoura/repos","events_url":"https://api.github.com/users/joaomdmoura/events{/privacy}","received_events_url":"https://api.github.com/users/joaomdmoura/received_events","type":"User","site_admin":false},"body":"We decided that it's time to work with deserialization. :tada::tada: :tada:  \r\n\r\nAMS aim to bring convention over configuration to JSON generation, but as it evolves into a more complete solution to build great APIs it makes sense to also handle deserialization.\r\n\r\nThe initial proposal of Deserialization is to convert your json input into something the ```ActiveRecord```  understands.\r\n\r\nIt will play really nice with json-api, enabling us to follow it conventions when creating and updating objects using AMS.\r\n\r\nHow it works:\r\n- [x] Automatically handle strong parameters, permitting the parameters that should be deserialized\r\n- [x] It identify the attributes that should be deserialized by your ```attributes``` method inside an existing serializer\r\n- [x] Allows you to override the parameters that should be serialized (ex. ```deserialize :title, :body```)\r\n- [ ] Work with the existing adapters conventions\r\n\r\n- JSON\r\n - [ ] Working CRUD\r\n - [ ] Parsing attributes\r\n - [ ] Parsing associations automatically\r\n\r\n- JSON API\r\n - [x] Working CRUD\r\n - [x] Parsing attributes\r\n - [x] Parsing associations automatically\r\n\r\nUsage:\r\n\r\n**post_serialization.rb**\r\n```ruby\r\nclass PostSerialization < ActiveModel::Serializer\r\n  attributes :id, :title, :body\r\n  belongs_to :user, root: :rows\r\n  params :title, :body\r\nend\r\n```\r\n\r\n**post_controller.rb**\r\n```ruby\r\nclass PostsController < ApplicationController\r\n\r\n  def create\r\n    @post = Post.new(post_params)\r\n\r\n    if @post.save\r\n      render json: @post, status: :created, location: @post\r\n    else\r\n      render json: @post.errors, status: :unprocessable_entity\r\n    end\r\n  end\r\n\r\n  private\r\n\r\n  def post_params\r\n    PostSerialization.deserialize(params)\r\n  end\r\nend\r\n```\r\n\r\n*This is a Working In Progress yet* :stuck_out_tongue_closed_eyes: ","created_at":"2015-06-11T17:37:47Z","updated_at":"2015-08-21T03:46:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4b96457c97364c2d0fc1e5c5c8c35f48dda7ccf7","assignee":{"login":"joaomdmoura","id":667063,"avatar_url":"https://avatars.githubusercontent.com/u/667063?v=3","gravatar_id":"","url":"https://api.github.com/users/joaomdmoura","html_url":"https://github.com/joaomdmoura","followers_url":"https://api.github.com/users/joaomdmoura/followers","following_url":"https://api.github.com/users/joaomdmoura/following{/other_user}","gists_url":"https://api.github.com/users/joaomdmoura/gists{/gist_id}","starred_url":"https://api.github.com/users/joaomdmoura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaomdmoura/subscriptions","organizations_url":"https://api.github.com/users/joaomdmoura/orgs","repos_url":"https://api.github.com/users/joaomdmoura/repos","events_url":"https://api.github.com/users/joaomdmoura/events{/privacy}","received_events_url":"https://api.github.com/users/joaomdmoura/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones/5","html_url":"https://github.com/rails-api/active_model_serializers/milestones/0.10","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones/5/labels","id":1011188,"number":5,"title":"0.10","description":"","creator":{"login":"kurko","id":70312,"avatar_url":"https://avatars.githubusercontent.com/u/70312?v=3","gravatar_id":"","url":"https://api.github.com/users/kurko","html_url":"https://github.com/kurko","followers_url":"https://api.github.com/users/kurko/followers","following_url":"https://api.github.com/users/kurko/following{/other_user}","gists_url":"https://api.github.com/users/kurko/gists{/gist_id}","starred_url":"https://api.github.com/users/kurko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kurko/subscriptions","organizations_url":"https://api.github.com/users/kurko/orgs","repos_url":"https://api.github.com/users/kurko/repos","events_url":"https://api.github.com/users/kurko/events{/privacy}","received_events_url":"https://api.github.com/users/kurko/received_events","type":"User","site_admin":false},"open_issues":18,"closed_issues":34,"state":"open","created_at":"2015-03-08T13:22:14Z","updated_at":"2015-08-18T17:36:35Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950/commits","review_comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950/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/950/comments","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/315067ee8232ec7272958161f1cccf8966a0c839","head":{"label":"joaomdmoura:deserializer-implementation","ref":"deserializer-implementation","sha":"315067ee8232ec7272958161f1cccf8966a0c839","user":{"login":"joaomdmoura","id":667063,"avatar_url":"https://avatars.githubusercontent.com/u/667063?v=3","gravatar_id":"","url":"https://api.github.com/users/joaomdmoura","html_url":"https://github.com/joaomdmoura","followers_url":"https://api.github.com/users/joaomdmoura/followers","following_url":"https://api.github.com/users/joaomdmoura/following{/other_user}","gists_url":"https://api.github.com/users/joaomdmoura/gists{/gist_id}","starred_url":"https://api.github.com/users/joaomdmoura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaomdmoura/subscriptions","organizations_url":"https://api.github.com/users/joaomdmoura/orgs","repos_url":"https://api.github.com/users/joaomdmoura/repos","events_url":"https://api.github.com/users/joaomdmoura/events{/privacy}","received_events_url":"https://api.github.com/users/joaomdmoura/received_events","type":"User","site_admin":false},"repo":{"id":25483024,"name":"active_model_serializers","full_name":"joaomdmoura/active_model_serializers","owner":{"login":"joaomdmoura","id":667063,"avatar_url":"https://avatars.githubusercontent.com/u/667063?v=3","gravatar_id":"","url":"https://api.github.com/users/joaomdmoura","html_url":"https://github.com/joaomdmoura","followers_url":"https://api.github.com/users/joaomdmoura/followers","following_url":"https://api.github.com/users/joaomdmoura/following{/other_user}","gists_url":"https://api.github.com/users/joaomdmoura/gists{/gist_id}","starred_url":"https://api.github.com/users/joaomdmoura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaomdmoura/subscriptions","organizations_url":"https://api.github.com/users/joaomdmoura/orgs","repos_url":"https://api.github.com/users/joaomdmoura/repos","events_url":"https://api.github.com/users/joaomdmoura/events{/privacy}","received_events_url":"https://api.github.com/users/joaomdmoura/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/joaomdmoura/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":true,"url":"https://api.github.com/repos/joaomdmoura/active_model_serializers","forks_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/forks","keys_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/events","assignees_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/merges","archive_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/joaomdmoura/active_model_serializers/releases{/id}","created_at":"2014-10-20T19:31:19Z","updated_at":"2015-05-05T00:58:23Z","pushed_at":"2015-08-21T02:28:10Z","git_url":"git://github.com/joaomdmoura/active_model_serializers.git","ssh_url":"git@github.com:joaomdmoura/active_model_serializers.git","clone_url":"https://github.com/joaomdmoura/active_model_serializers.git","svn_url":"https://github.com/joaomdmoura/active_model_serializers","homepage":"","size":2040,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rails-api:master","ref":"master","sha":"8568ed5913edf2c641e33b841dfa28ebbc97b3a4","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-08-21T00:55:24Z","pushed_at":"2015-08-21T03:03:35Z","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":5676,"stargazers_count":2913,"watchers_count":2913,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":665,"mirror_url":null,"open_issues_count":145,"forks":665,"open_issues":145,"watchers":2913,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/950"},"issue":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950"},"comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/950/comments"},"review_comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/950/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/950/commits"},"statuses":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/315067ee8232ec7272958161f1cccf8966a0c839"}}}}