diff options
author | Markus Koller <markus.koller.ext@siemens.com> | 2017-12-22 16:54:55 +0100 |
---|---|---|
committer | Markus Koller <markus.koller.ext@siemens.com> | 2018-02-08 13:30:34 +0100 |
commit | b7cd99c376c2f953f30a4bf982b69780e3d6985b (patch) | |
tree | 1a808da75fbc90fc332765cbe338a6ede7335a7a /lib/api/helpers | |
parent | bb2478c205932429e7e519aaaf2d9b655aecf860 (diff) | |
download | gitlab-ce-b7cd99c376c2f953f30a4bf982b69780e3d6985b.tar.gz |
Allow including custom attributes in API responses
Diffstat (limited to 'lib/api/helpers')
-rw-r--r-- | lib/api/helpers/custom_attributes.rb | 28 |
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 |