Module: Graphoid::Utils

Defined in:
lib/graphoid/utils.rb

Class Method Summary collapse

Class Method Details

.build_update_attributes(data, model, context) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/graphoid/utils.rb', line 32

def build_update_attributes(data, model, context)
  user = context[:current_user]
  fields = Graphoid::Attribute.fieldnames_of(model)
  attrs = underscore(data, fields)
  attrs['updated_by_id'] = user.id if user && fields.include?('updated_by_id')
  attrs
end

.camelize(text) ⇒ Object



10
11
12
13
# File 'lib/graphoid/utils.rb', line 10

def camelize(text)
  # we are doing it twice because _id gets translated to Id the first time and to id the second time.
  graphqlize text.to_s.camelize(:lower).camelize(:lower)
end

.graphqlize(text) ⇒ Object



15
16
17
# File 'lib/graphoid/utils.rb', line 15

def graphqlize(text)
  text.to_s.gsub(/::/, '_')
end

.modelize(text) ⇒ Object



6
7
8
# File 'lib/graphoid/utils.rb', line 6

def modelize(text)
  graphqlize text.to_s.capitalize.camelize
end

.symbolize(fields) ⇒ Object



19
20
21
# File 'lib/graphoid/utils.rb', line 19

def symbolize(fields)
  fields.keys.map(&:underscore).map(&:to_sym)
end

.underscore(props, fields = []) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/graphoid/utils.rb', line 23

def underscore(props, fields = [])
  attrs = {}
  props.each do |key, value|
    key = key.underscore if fields.exclude?(key)
    attrs[key] = value
  end
  attrs
end