2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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'
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
optional :custom_attributes, type: Hash,
|
|
|
|
desc: 'Filter with custom attributes'
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|