Sha256: c58f273b41b4d2a88467d937140de98b71b65b3e4638e268e1d11562de7cf9e9
Contents?: true
Size: 1.87 KB
Versions: 14
Compression:
Stored size: 1.87 KB
Contents
module GraphQL module Models module ActiveRecordExtension class EnumTypeHash extend Forwardable attr_accessor :hash def initialize @hash = {}.with_indifferent_access end def [](attribute) type = hash[attribute] type = type.call if type.is_a?(Proc) type = type.constantize if type.is_a?(String) type end def_delegators :@hash, :[]=, :include?, :keys end extend ActiveSupport::Concern class_methods do def graphql_enum_types @_graphql_enum_types ||= EnumTypeHash.new end # Defines a GraphQL enum type on the model def graphql_enum(attribute, type: nil, upcase: true) # Case 1: Providing a type. Only thing to do is register the enum type. if type graphql_enum_types[attribute] = type return type end # Case 2: Automatically generating the type name = "#{self.name}#{attribute.to_s.classify}" description = "#{attribute.to_s.titleize} field on #{self.name.titleize}" if defined_enums.include?(attribute.to_s) values = defined_enums[attribute.to_s].keys else values = DefinitionHelpers.detect_inclusion_values(self, attribute) end if values.nil? fail ArgumentError.new("Could not auto-detect the values for enum #{attribute} on #{self.name}") end type = GraphQL::EnumType.define do name(name) description(description) values.each do |val| value(upcase ? val.upcase : val, val.to_s.titleize, value: val) end end graphql_enum_types[attribute] = type end end end end end ActiveRecord::Base.send(:include, GraphQL::Models::ActiveRecordExtension)
Version data entries
14 entries across 14 versions & 1 rubygems