summaryrefslogtreecommitdiff
path: root/lib/api/helpers/custom_attributes.rb
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2018-02-14 10:40:59 +0000
committerDouwe Maan <douwe@gitlab.com>2018-02-14 10:40:59 +0000
commit6085656bffa54cdbe21e776e283f87ece313991f (patch)
tree0798db9f44217da4ff340ab66844673e5e189839 /lib/api/helpers/custom_attributes.rb
parent5e829934f95bcae25f5c09583b6febe6e2e646b6 (diff)
parentb7cd99c376c2f953f30a4bf982b69780e3d6985b (diff)
downloadgitlab-ce-6085656bffa54cdbe21e776e283f87ece313991f.tar.gz
Merge branch 'feature/include-custom-attributes-in-api' into 'master'
Allow including custom attributes in API responses See merge request gitlab-org/gitlab-ce!16526
Diffstat (limited to 'lib/api/helpers/custom_attributes.rb')
-rw-r--r--lib/api/helpers/custom_attributes.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/api/helpers/custom_attributes.rb b/lib/api/helpers/custom_attributes.rb
new file mode 100644
index 00000000000..70e4eda95f8
--- /dev/null
+++ b/lib/api/helpers/custom_attributes.rb
@@ -0,0 +1,28 @@
+module API
+ module Helpers
+ module CustomAttributes
+ extend ActiveSupport::Concern
+
+ included do
+ helpers do
+ params :with_custom_attributes do
+ optional :with_custom_attributes, type: Boolean, default: false, desc: 'Include custom attributes in the response'
+ end
+
+ def with_custom_attributes(collection_or_resource, options = {})
+ options = options.merge(
+ with_custom_attributes: params[:with_custom_attributes] &&
+ can?(current_user, :read_custom_attribute)
+ )
+
+ if options[:with_custom_attributes] && collection_or_resource.is_a?(ActiveRecord::Relation)
+ collection_or_resource = collection_or_resource.includes(:custom_attributes)
+ end
+
+ [collection_or_resource, options]
+ end
+ end
+ end
+ end
+ end
+end