Module: NoBrainer::Document::References::ClassMethods

Defined in:
lib/no_brainer/document/references.rb

Instance Method Summary collapse

Instance Method Details

#references_many(name, model: nil, store_as: nil, inverse: nil, **opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/no_brainer/document/references.rb', line 27

def references_many(name, model: nil, store_as: nil, inverse: nil, **opts)
  name = name.to_s
  store_as ||= "#{name.singularize}_ids"
  model ||= name.singularize.classify.constantize
  if model.is_a?(String)
    name = model
    model = ->{ const_get(name) }
  end

  field name.to_sym, type: Array.of(Reference.to(model)), store_as: store_as, **opts
end

#references_one(name, model: nil, store_as: nil, inverse: nil, **opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/no_brainer/document/references.rb', line 15

def references_one(name, model: nil, store_as: nil, inverse: nil, **opts)
  name = name.to_s
  store_as ||= "#{name}_id"
  model ||= name.classify.constantize
  if model.is_a?(String)
    name = model
    model = ->{ const_get(name) }
  end

  field name.to_sym, type: Reference.to(model), store_as: store_as, **opts
end