module TweekCatalogue::Concerns::HasManyRelationship::ClassMethods

Public Instance Methods

it_has_many(field, opts = {}) click to toggle source

Hides all complexity to create one to many relationship between two models. Under the hood this will create a field with _ids suffix and it will make call to mongomapper many with the in option.

Paramters:

field - Name your target field names, ie, it_has_many :countries

Returns nil

# File lib/tweek_catalogue/concerns/has_many_relationship.rb, line 23
def it_has_many(field, opts = {})
  _key        = opts[:in]         || :"#{field.to_s.singularize}_ids"
  _class_name = opts[:class_name] || "#{self.model_namespace}::#{field.to_s.to_s.classify}"

  # Set field where all references will be stored
  key _key, Array

  # Set many relationship with in and class_name
  many field, in: _key, class_name: _class_name
end
set_model_namespace!(namespace) click to toggle source
# File lib/tweek_catalogue/concerns/has_many_relationship.rb, line 11
def set_model_namespace!(namespace)
  self.model_namespace = namespace
end