Module: Types::EnumHelpers

Defined in:
app/graphql/types/enum_helpers.rb

Class Method Summary collapse

Class Method Details

.enum_from_array(array, graphql_enum_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/graphql/types/enum_helpers.rb', line 19

def self.enum_from_array(array, graphql_enum_name)
  enum_values = array.map(&:to_s)

  existing_enum = Types::GraphqlTypeUtils.get_or_check_existing_constant(graphql_enum_name)
  return existing_enum if existing_enum

  Object.const_set(graphql_enum_name, Class.new(Types::BaseEnum) do
    graphql_name(graphql_enum_name)

    enum_values.each do |val|
      value val, description: val.titleize
    end
  end)
end

.enum_from_model(model, enum_name, graphql_enum_name: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/graphql/types/enum_helpers.rb', line 3

def self.enum_from_model(model, enum_name, graphql_enum_name: nil)
  graphql_class_name = graphql_enum_name || "#{model.name}#{enum_name.to_s.camelize}Enum"

  existing_enum = Types::GraphqlTypeUtils.get_or_check_existing_constant(graphql_class_name)
  return existing_enum if existing_enum

  enum_values = model.send(enum_name.to_s.pluralize).keys
  Object.const_set(graphql_class_name, Class.new(Types::BaseEnum) do
    graphql_name(graphql_class_name)

    enum_values.each do |val|
      value val, description: "#{val.titleize} #{enum_name}"
    end
  end)
end