debian-mirror-gitlab/app/graphql/types/base_enum.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
module Types
class BaseEnum < GraphQL::Schema::Enum
2020-11-24 15:15:51 +05:30
extend GitlabStyleDeprecations
2019-12-26 22:10:19 +05:30
class << self
2021-03-08 18:12:59 +05:30
# Registers enum definition by the given DeclarativeEnum module
#
# @param enum_mod [Module] The enum module to be used
# @param use_name [Boolean] Does not override the name if set `false`
# @param use_description [Boolean] Does not override the description if set `false`
#
# Example:
#
# class MyEnum < BaseEnum
# declarative_enum MyDeclarativeEnum
# end
#
2021-06-08 01:23:25 +05:30
# Disabling descriptions rubocop for a false positive here
# rubocop: disable Graphql/Descriptions
#
2021-03-08 18:12:59 +05:30
def declarative_enum(enum_mod, use_name: true, use_description: true)
graphql_name(enum_mod.name) if use_name
description(enum_mod.description) if use_description
2021-04-29 21:17:54 +05:30
enum_mod.definition.each do |key, content|
value(key.to_s.upcase, **content)
end
end
2021-06-08 01:23:25 +05:30
# rubocop: enable Graphql/Descriptions
2021-04-29 21:17:54 +05:30
# Helper to define an enum member for each element of a Rails AR enum
def from_rails_enum(enum, description:)
enum.each_key do |name|
value name.to_s.upcase,
value: name,
description: format(description, name: name)
end
2021-03-08 18:12:59 +05:30
end
2019-12-26 22:10:19 +05:30
def value(*args, **kwargs, &block)
enum[args[0].downcase] = kwargs[:value] || args[0]
2021-04-29 21:17:54 +05:30
gitlab_deprecation(kwargs)
2019-12-26 22:10:19 +05:30
super(*args, **kwargs, &block)
end
# Returns an indifferent access hash with the key being the downcased name of the attribute
# and the value being the Ruby value (either the explicit `value` passed or the same as the value attr).
def enum
@enum_values ||= {}.with_indifferent_access
end
2021-04-29 21:17:54 +05:30
def authorization
@authorization ||= ::Gitlab::Graphql::Authorize::ObjectAuthorization.new(authorize)
end
def authorize(*abilities)
@abilities = abilities
end
def authorized?(object, context)
authorization.ok?(object, context[:current_user])
end
2019-12-26 22:10:19 +05:30
end
2018-11-08 19:23:39 +05:30
end
end