{"action":"created","comment":{"url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/comments/75183221","id":75183221,"diff_hunk":"@@ -229,6 +229,144 @@ const format = body => {\n   return data;\n };\n \n+/*\n+ * @typedef Reference\n+ * @type object\n+ *\n+ * @property {type} Reference.id - Id of the content type\n+ * @property {attr} Reference.attr - Attribute Index\n+ * @property {input} Reference.input - Input of the reference\n+ * @property {length} Reference.length - Number of instances\n+ * @property {ct.id} Reference.ct.id - Id of the referenced content type\n+ * @property {ct.index} Reference.ct.index - Position of the reference content type\n+ */\n+\n+/*\n+ * Inititalizes reference\n+ *\n+ * @param {object} types - content types object\n+ *\n+ *\n+ * @returns {array[Reference]} - content type with reference data\n+ */\n+const references = (types) => {\n+  const cts = _.cloneDeep(types);\n+  const refs = [];\n+  const position = {};\n+\n+  // Saves position of each content type\n+  cts.forEach((type, index) => {\n+    if (!position.hasOwnProperty(type.id)) {\n+      position[type.id] = index;\n+    }\n+  });\n+\n+  // Iterates through each content type and attributes\n+  cts.map(ct => {\n+    ct.attributes.map((attr, index) => {\n+      let inputs;\n+      let length;\n+\n+      // Get the number of instances if repeatable\n+      if (Array.isArray(attr.inputs)) {\n+        inputs = attr.inputs[0];\n+        length = attr.inputs.length;\n+      }\n+      else {\n+        inputs = attr.inputs;\n+      }\n+\n+      // Iterate through each inputs\n+      Object.keys(inputs).forEach(input => {\n+        // Checks if input is a reference\n+        if (inputs[input].hasOwnProperty('reference') && inputs[input].reference === true) {\n+          // Throw error if contentType is not defined\n+          if (!inputs[input].settings.hasOwnProperty('contentType')) {\n+            throw new Error('Reference must have a content type');\n+          }\n+\n+          // Throw error if contentType is not valid\n+          if (!position.hasOwnProperty(inputs[input].settings.contentType)) {\n+            throw new Error(`Content Type ${inputs[input].settings.contentType} is not valid`);\n+          }\n+\n+          // Defines and push reference object\n+          refs.push({\n+            type: ct.id,\n+            attr: index,\n+            input,\n+            length,\n+            ct: {\n+              index: position[inputs[input].settings.contentType],\n+              id: inputs[input].settings.contentType,\n+            },\n+          });\n+        }\n+      });\n+    });\n+  });\n+\n+  return refs;\n+};\n+\n+\n+/*\n+ * Fills in content type data from database\n+ *\n+ * @param {object} types - content types object\n+ * @param {string} id - id of the requested content type\n+ * @param {array} refs - reference data\n+ * @param {object} database - database object\n+ *\n+ *\n+ * @returns {object} - content type object with reference data\n+ */\n+const fill = (types, id, refs, database) => {\n+  const ct = singleItem('id', id, types);\n+\n+  if (!ct) {\n+    return new Promise(resolve => {\n+      resolve(ct);\n+    });\n+  }\n+  const ref = refs.filter(reference => {\n+    return ct.id === reference.type;\n+  });\n+\n+  return Promise.map(ref, input => {\n+    // subquery for select\n+    const where = database\n+          .max('revision')\n+          .from(`content-type--${input.ct.id}`)\n+          .groupBy('id');\n+\n+    // Get the records from content-type\n+    return database\n+      .select('id', 'revision', 'value')\n+      .from(`content-type--${input.ct.id}`)\n+      .whereIn('revision', where).then(rws => {\n+        const rows = routes.identifier(rws, types[input.ct.index]);\n+        const options = rows.map(row => {\n+          return {\n+            value: row.id,","path":"lib/utils.js","position":141,"original_position":136,"commit_id":"ce9ff99f20e3fd4b6574d6eca3fc03f740aae3df","original_commit_id":"07e726dc901c0307dec9095538b11282b55d8f5e","user":{"login":"scottnath","id":216931,"avatar_url":"https://avatars.githubusercontent.com/u/216931?v=3","gravatar_id":"","url":"https://api.github.com/users/scottnath","html_url":"https://github.com/scottnath","followers_url":"https://api.github.com/users/scottnath/followers","following_url":"https://api.github.com/users/scottnath/following{/other_user}","gists_url":"https://api.github.com/users/scottnath/gists{/gist_id}","starred_url":"https://api.github.com/users/scottnath/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scottnath/subscriptions","organizations_url":"https://api.github.com/users/scottnath/orgs","repos_url":"https://api.github.com/users/scottnath/repos","events_url":"https://api.github.com/users/scottnath/events{/privacy}","received_events_url":"https://api.github.com/users/scottnath/received_events","type":"User","site_admin":false},"body":"OH. Duh, thank you for the clarification.","created_at":"2016-08-17T18:49:06Z","updated_at":"2016-08-17T18:49:06Z","html_url":"https://github.com/punchcard-cms/punchcard/pull/273#discussion_r75183221","pull_request_url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273","_links":{"self":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/comments/75183221"},"html":{"href":"https://github.com/punchcard-cms/punchcard/pull/273#discussion_r75183221"},"pull_request":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273"}}},"pull_request":{"url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273","id":81483011,"html_url":"https://github.com/punchcard-cms/punchcard/pull/273","diff_url":"https://github.com/punchcard-cms/punchcard/pull/273.diff","patch_url":"https://github.com/punchcard-cms/punchcard/pull/273.patch","issue_url":"https://api.github.com/repos/punchcard-cms/punchcard/issues/273","number":273,"state":"open","locked":false,"title":"Feature/reference","user":{"login":"ayush2k","id":7220478,"avatar_url":"https://avatars.githubusercontent.com/u/7220478?v=3","gravatar_id":"","url":"https://api.github.com/users/ayush2k","html_url":"https://github.com/ayush2k","followers_url":"https://api.github.com/users/ayush2k/followers","following_url":"https://api.github.com/users/ayush2k/following{/other_user}","gists_url":"https://api.github.com/users/ayush2k/gists{/gist_id}","starred_url":"https://api.github.com/users/ayush2k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayush2k/subscriptions","organizations_url":"https://api.github.com/users/ayush2k/orgs","repos_url":"https://api.github.com/users/ayush2k/repos","events_url":"https://api.github.com/users/ayush2k/events{/privacy}","received_events_url":"https://api.github.com/users/ayush2k/received_events","type":"User","site_admin":false},"body":"Content to Content Reference\r\n\r\n---\r\nResolves #45 \r\n\r\n`DCO 1.1 Signed-off-by: Ayush Gupta <ayush2k@gmail.com>`\r\n","created_at":"2016-08-16T14:37:48Z","updated_at":"2016-08-17T18:49:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4fe9a8375e94c1cd50cdffa6211323e217ac1539","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273/commits","review_comments_url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273/comments","review_comment_url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/comments{/number}","comments_url":"https://api.github.com/repos/punchcard-cms/punchcard/issues/273/comments","statuses_url":"https://api.github.com/repos/punchcard-cms/punchcard/statuses/ce9ff99f20e3fd4b6574d6eca3fc03f740aae3df","head":{"label":"ayush2k:feature/reference","ref":"feature/reference","sha":"ce9ff99f20e3fd4b6574d6eca3fc03f740aae3df","user":{"login":"ayush2k","id":7220478,"avatar_url":"https://avatars.githubusercontent.com/u/7220478?v=3","gravatar_id":"","url":"https://api.github.com/users/ayush2k","html_url":"https://github.com/ayush2k","followers_url":"https://api.github.com/users/ayush2k/followers","following_url":"https://api.github.com/users/ayush2k/following{/other_user}","gists_url":"https://api.github.com/users/ayush2k/gists{/gist_id}","starred_url":"https://api.github.com/users/ayush2k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayush2k/subscriptions","organizations_url":"https://api.github.com/users/ayush2k/orgs","repos_url":"https://api.github.com/users/ayush2k/repos","events_url":"https://api.github.com/users/ayush2k/events{/privacy}","received_events_url":"https://api.github.com/users/ayush2k/received_events","type":"User","site_admin":false},"repo":{"id":61636403,"name":"punchcard","full_name":"ayush2k/punchcard","owner":{"login":"ayush2k","id":7220478,"avatar_url":"https://avatars.githubusercontent.com/u/7220478?v=3","gravatar_id":"","url":"https://api.github.com/users/ayush2k","html_url":"https://github.com/ayush2k","followers_url":"https://api.github.com/users/ayush2k/followers","following_url":"https://api.github.com/users/ayush2k/following{/other_user}","gists_url":"https://api.github.com/users/ayush2k/gists{/gist_id}","starred_url":"https://api.github.com/users/ayush2k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ayush2k/subscriptions","organizations_url":"https://api.github.com/users/ayush2k/orgs","repos_url":"https://api.github.com/users/ayush2k/repos","events_url":"https://api.github.com/users/ayush2k/events{/privacy}","received_events_url":"https://api.github.com/users/ayush2k/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ayush2k/punchcard","description":"The Punchcard CMS","fork":true,"url":"https://api.github.com/repos/ayush2k/punchcard","forks_url":"https://api.github.com/repos/ayush2k/punchcard/forks","keys_url":"https://api.github.com/repos/ayush2k/punchcard/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ayush2k/punchcard/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ayush2k/punchcard/teams","hooks_url":"https://api.github.com/repos/ayush2k/punchcard/hooks","issue_events_url":"https://api.github.com/repos/ayush2k/punchcard/issues/events{/number}","events_url":"https://api.github.com/repos/ayush2k/punchcard/events","assignees_url":"https://api.github.com/repos/ayush2k/punchcard/assignees{/user}","branches_url":"https://api.github.com/repos/ayush2k/punchcard/branches{/branch}","tags_url":"https://api.github.com/repos/ayush2k/punchcard/tags","blobs_url":"https://api.github.com/repos/ayush2k/punchcard/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ayush2k/punchcard/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ayush2k/punchcard/git/refs{/sha}","trees_url":"https://api.github.com/repos/ayush2k/punchcard/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ayush2k/punchcard/statuses/{sha}","languages_url":"https://api.github.com/repos/ayush2k/punchcard/languages","stargazers_url":"https://api.github.com/repos/ayush2k/punchcard/stargazers","contributors_url":"https://api.github.com/repos/ayush2k/punchcard/contributors","subscribers_url":"https://api.github.com/repos/ayush2k/punchcard/subscribers","subscription_url":"https://api.github.com/repos/ayush2k/punchcard/subscription","commits_url":"https://api.github.com/repos/ayush2k/punchcard/commits{/sha}","git_commits_url":"https://api.github.com/repos/ayush2k/punchcard/git/commits{/sha}","comments_url":"https://api.github.com/repos/ayush2k/punchcard/comments{/number}","issue_comment_url":"https://api.github.com/repos/ayush2k/punchcard/issues/comments{/number}","contents_url":"https://api.github.com/repos/ayush2k/punchcard/contents/{+path}","compare_url":"https://api.github.com/repos/ayush2k/punchcard/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ayush2k/punchcard/merges","archive_url":"https://api.github.com/repos/ayush2k/punchcard/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ayush2k/punchcard/downloads","issues_url":"https://api.github.com/repos/ayush2k/punchcard/issues{/number}","pulls_url":"https://api.github.com/repos/ayush2k/punchcard/pulls{/number}","milestones_url":"https://api.github.com/repos/ayush2k/punchcard/milestones{/number}","notifications_url":"https://api.github.com/repos/ayush2k/punchcard/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ayush2k/punchcard/labels{/name}","releases_url":"https://api.github.com/repos/ayush2k/punchcard/releases{/id}","deployments_url":"https://api.github.com/repos/ayush2k/punchcard/deployments","created_at":"2016-06-21T13:33:50Z","updated_at":"2016-06-21T13:33:50Z","pushed_at":"2016-08-17T18:14:42Z","git_url":"git://github.com/ayush2k/punchcard.git","ssh_url":"git@github.com:ayush2k/punchcard.git","clone_url":"https://github.com/ayush2k/punchcard.git","svn_url":"https://github.com/ayush2k/punchcard","homepage":null,"size":413,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"punchcard-cms:master","ref":"master","sha":"00425d2db98f6ec2d985069797a061dc6d58ce90","user":{"login":"punchcard-cms","id":18744098,"avatar_url":"https://avatars.githubusercontent.com/u/18744098?v=3","gravatar_id":"","url":"https://api.github.com/users/punchcard-cms","html_url":"https://github.com/punchcard-cms","followers_url":"https://api.github.com/users/punchcard-cms/followers","following_url":"https://api.github.com/users/punchcard-cms/following{/other_user}","gists_url":"https://api.github.com/users/punchcard-cms/gists{/gist_id}","starred_url":"https://api.github.com/users/punchcard-cms/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/punchcard-cms/subscriptions","organizations_url":"https://api.github.com/users/punchcard-cms/orgs","repos_url":"https://api.github.com/users/punchcard-cms/repos","events_url":"https://api.github.com/users/punchcard-cms/events{/privacy}","received_events_url":"https://api.github.com/users/punchcard-cms/received_events","type":"Organization","site_admin":false},"repo":{"id":58833061,"name":"punchcard","full_name":"punchcard-cms/punchcard","owner":{"login":"punchcard-cms","id":18744098,"avatar_url":"https://avatars.githubusercontent.com/u/18744098?v=3","gravatar_id":"","url":"https://api.github.com/users/punchcard-cms","html_url":"https://github.com/punchcard-cms","followers_url":"https://api.github.com/users/punchcard-cms/followers","following_url":"https://api.github.com/users/punchcard-cms/following{/other_user}","gists_url":"https://api.github.com/users/punchcard-cms/gists{/gist_id}","starred_url":"https://api.github.com/users/punchcard-cms/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/punchcard-cms/subscriptions","organizations_url":"https://api.github.com/users/punchcard-cms/orgs","repos_url":"https://api.github.com/users/punchcard-cms/repos","events_url":"https://api.github.com/users/punchcard-cms/events{/privacy}","received_events_url":"https://api.github.com/users/punchcard-cms/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/punchcard-cms/punchcard","description":"The Punchcard CMS","fork":false,"url":"https://api.github.com/repos/punchcard-cms/punchcard","forks_url":"https://api.github.com/repos/punchcard-cms/punchcard/forks","keys_url":"https://api.github.com/repos/punchcard-cms/punchcard/keys{/key_id}","collaborators_url":"https://api.github.com/repos/punchcard-cms/punchcard/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/punchcard-cms/punchcard/teams","hooks_url":"https://api.github.com/repos/punchcard-cms/punchcard/hooks","issue_events_url":"https://api.github.com/repos/punchcard-cms/punchcard/issues/events{/number}","events_url":"https://api.github.com/repos/punchcard-cms/punchcard/events","assignees_url":"https://api.github.com/repos/punchcard-cms/punchcard/assignees{/user}","branches_url":"https://api.github.com/repos/punchcard-cms/punchcard/branches{/branch}","tags_url":"https://api.github.com/repos/punchcard-cms/punchcard/tags","blobs_url":"https://api.github.com/repos/punchcard-cms/punchcard/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/punchcard-cms/punchcard/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/punchcard-cms/punchcard/git/refs{/sha}","trees_url":"https://api.github.com/repos/punchcard-cms/punchcard/git/trees{/sha}","statuses_url":"https://api.github.com/repos/punchcard-cms/punchcard/statuses/{sha}","languages_url":"https://api.github.com/repos/punchcard-cms/punchcard/languages","stargazers_url":"https://api.github.com/repos/punchcard-cms/punchcard/stargazers","contributors_url":"https://api.github.com/repos/punchcard-cms/punchcard/contributors","subscribers_url":"https://api.github.com/repos/punchcard-cms/punchcard/subscribers","subscription_url":"https://api.github.com/repos/punchcard-cms/punchcard/subscription","commits_url":"https://api.github.com/repos/punchcard-cms/punchcard/commits{/sha}","git_commits_url":"https://api.github.com/repos/punchcard-cms/punchcard/git/commits{/sha}","comments_url":"https://api.github.com/repos/punchcard-cms/punchcard/comments{/number}","issue_comment_url":"https://api.github.com/repos/punchcard-cms/punchcard/issues/comments{/number}","contents_url":"https://api.github.com/repos/punchcard-cms/punchcard/contents/{+path}","compare_url":"https://api.github.com/repos/punchcard-cms/punchcard/compare/{base}...{head}","merges_url":"https://api.github.com/repos/punchcard-cms/punchcard/merges","archive_url":"https://api.github.com/repos/punchcard-cms/punchcard/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/punchcard-cms/punchcard/downloads","issues_url":"https://api.github.com/repos/punchcard-cms/punchcard/issues{/number}","pulls_url":"https://api.github.com/repos/punchcard-cms/punchcard/pulls{/number}","milestones_url":"https://api.github.com/repos/punchcard-cms/punchcard/milestones{/number}","notifications_url":"https://api.github.com/repos/punchcard-cms/punchcard/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/punchcard-cms/punchcard/labels{/name}","releases_url":"https://api.github.com/repos/punchcard-cms/punchcard/releases{/id}","deployments_url":"https://api.github.com/repos/punchcard-cms/punchcard/deployments","created_at":"2016-05-14T22:23:32Z","updated_at":"2016-08-15T17:19:34Z","pushed_at":"2016-08-17T18:14:42Z","git_url":"git://github.com/punchcard-cms/punchcard.git","ssh_url":"git@github.com:punchcard-cms/punchcard.git","clone_url":"https://github.com/punchcard-cms/punchcard.git","svn_url":"https://github.com/punchcard-cms/punchcard","homepage":null,"size":475,"stargazers_count":8,"watchers_count":8,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":10,"mirror_url":null,"open_issues_count":115,"forks":10,"open_issues":115,"watchers":8,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273"},"html":{"href":"https://github.com/punchcard-cms/punchcard/pull/273"},"issue":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/issues/273"},"comments":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/issues/273/comments"},"review_comments":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273/comments"},"review_comment":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/pulls/273/commits"},"statuses":{"href":"https://api.github.com/repos/punchcard-cms/punchcard/statuses/ce9ff99f20e3fd4b6574d6eca3fc03f740aae3df"}}}}
{"actor":{"display_login":"scottnath"}}