{"action":"created","comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/48460631","id":48460631,"diff_hunk":"@@ -0,0 +1,38 @@\n+module ActiveModel\n+  class Serializer\n+    Attribute = Struct.new(:name, :block, :key, :condition_type, :condition) do\n+      def initialize(name, block, options)\n+        super(name, block)\n+        self.key = options.fetch(:key, name)\n+        self.condition_type =\n+          if options.key?(:if)\n+            :if\n+          elsif options.key?(:unless)\n+            :unless\n+          else\n+            :none\n+          end\n+        self.condition = options[condition_type]\n+      end\n+\n+      def value(serializer)\n+        if block\n+          serializer.instance_eval(&block)\n+        else\n+          serializer.read_attribute_for_serialization(name)\n+        end\n+      end\n+\n+      def include?(serializer)\n+        case condition_type\n+        when :if\n+          serializer.send(condition)","path":"lib/active_model/serializer/attribute.rb","position":null,"original_position":29,"commit_id":"755516b875ff4d5a79af1cd4fc055e730998e514","original_commit_id":"58f598bf62b2c477bcbd36d1da464f010a925f11","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":"Just updated it.","created_at":"2015-12-28T01:57:11Z","updated_at":"2015-12-28T01:57:11Z","html_url":"https://github.com/rails-api/active_model_serializers/pull/1403#discussion_r48460631","pull_request_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403","_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/48460631"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1403#discussion_r48460631"},"pull_request":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403"}}},"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403","id":54626332,"html_url":"https://github.com/rails-api/active_model_serializers/pull/1403","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1403.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1403.patch","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1403","number":1403,"state":"open","locked":false,"title":"Conditional attributes/associations (if/unless)","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":"My take at conditional attributes/associations. Previous work has been done in this direction (#1266), but I figured it was easily expressed on top of #1370. \r\nThis PR adds support for `if/unless` options to attribute/association definitions in serializers. Those options take a symbol of a method name on the serializer.\r\n\r\nExample:\r\n```ruby\r\nclass UserSerializer < ActiveModel::Serializer\r\n  attributes :name, :description, :date_of_birth\r\n  attribute :private_data, if: :is_current_user?\r\n\r\n  def is_current_user?\r\n    object.id == current_user.id\r\n  end\r\nend\r\n```\r\n\r\nNote: if this gets traction, I'll add tests.\r\n\r\nRef #1245, #1266","created_at":"2015-12-28T00:11:21Z","updated_at":"2015-12-28T01:57:11Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5abd6e9049fc5f10c9c77b8da8cb3ea742494c64","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403/commits","review_comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403/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/1403/comments","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/755516b875ff4d5a79af1cd4fc055e730998e514","head":{"label":"beauby:conditional-attributes","ref":"conditional-attributes","sha":"755516b875ff4d5a79af1cd4fc055e730998e514","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},"repo":{"id":30326986,"name":"active_model_serializers","full_name":"beauby/active_model_serializers","owner":{"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},"private":false,"html_url":"https://github.com/beauby/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":true,"url":"https://api.github.com/repos/beauby/active_model_serializers","forks_url":"https://api.github.com/repos/beauby/active_model_serializers/forks","keys_url":"https://api.github.com/repos/beauby/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/beauby/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/beauby/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/beauby/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/beauby/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/beauby/active_model_serializers/events","assignees_url":"https://api.github.com/repos/beauby/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/beauby/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/beauby/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/beauby/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/beauby/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/beauby/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/beauby/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/beauby/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/beauby/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/beauby/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/beauby/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/beauby/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/beauby/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/beauby/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/beauby/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/beauby/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/beauby/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/beauby/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/beauby/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/beauby/active_model_serializers/merges","archive_url":"https://api.github.com/repos/beauby/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/beauby/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/beauby/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/beauby/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/beauby/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/beauby/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/beauby/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/beauby/active_model_serializers/releases{/id}","created_at":"2015-02-04T23:25:42Z","updated_at":"2015-02-04T23:25:43Z","pushed_at":"2015-12-28T01:57:02Z","git_url":"git://github.com/beauby/active_model_serializers.git","ssh_url":"git@github.com:beauby/active_model_serializers.git","clone_url":"https://github.com/beauby/active_model_serializers.git","svn_url":"https://github.com/beauby/active_model_serializers","homepage":"","size":2336,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":0,"forks":3,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rails-api:master","ref":"master","sha":"bb67735c79f739e7261382a2d553e0d1a0a9b975","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-12-28T00:35:14Z","pushed_at":"2015-12-28T01:57:02Z","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":3576,"stargazers_count":3223,"watchers_count":3223,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":755,"mirror_url":null,"open_issues_count":185,"forks":755,"open_issues":185,"watchers":3223,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1403"},"issue":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1403"},"comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1403/comments"},"review_comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1403/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/1403/commits"},"statuses":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/755516b875ff4d5a79af1cd4fc055e730998e514"}}}}