Module: Mongoid::DocumentInclusion::ClassMethods

Defined in:
lib/mongoid-included/document_inclusion.rb

Instance Method Summary collapse

Instance Method Details

#included_in(relation_name, args = {}) ⇒ Object

Facade for Mongoid::Relations::Macros.embeded_in

Examples:

Skipping pluralization

class Entry::Image
  included_in :entry, :skip_path_pluralization => true
  image.to_partial_path #=> "entry/images/image"

With pluralization. This seems a more natural fit for partials placement.

class Entry::Image
  included_in :entry
  image.to_partial_path #=> "entries/images/image"

Parameters:

  • relation_name (Symbol)

    name for the parent association

  • args (Hash) (defaults to: {})

    (see Mongoid::Relations::Macros.embeded_in)

Options Hash (args):

  • :skip_path_pluralization (true, false)

    If Partial Paths should be kept as default, using the namespace in singular form

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mongoid-included/document_inclusion.rb', line 35

def included_in(relation_name, args = {})
  @skip_path_pluralization = args.delete(:skip_path_pluralization)
  
  relation_klass = if args[:class_name]
    infer_klass(args[:class_name])
  else
    infer_klass(relation_name)
  end 
  
  raise NotMongoidDocument,       "Parent must be Mongoid::Document" unless has_mongoid? self.parent
  raise DocumentAlreadyIncluded,  "Document already included" if (included_by | [ relation_klass ]).size > 1
  
  embedded_in relation_name, args
  self.included_by |= [ relation_klass ]
  
  self.extend ActiveModel::Naming
  _overwrite_model_name
end

#includes_many(relation_name, args = {}) ⇒ Object

Facade for Mongoid::Relations::Macros.embeds_many

Parameters:

  • relation_name (Symbol)

    Name for the child association

  • args (Hash) (defaults to: {})

    (see Mongoid::Relations::Macros.embeds_many)



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongoid-included/document_inclusion.rb', line 57

def includes_many(relation_name, args = {})
  relation_klass = if args[:class_name]
    infer_klass(args[:class_name])
  else
    klass_to_include(relation_name)
  end
  
  verify_inclusion_dependencies(relation_klass)
  self.including_many |= [ relation_klass ]

  embeds_many relation_name, args.merge(:class_name => relation_klass.name )
end

#includes_one(relation_name, args = {}) ⇒ Object

Facade for Mongoid::Relations::Macros.embeds_one

Parameters:

  • _model (Symbol)

    Name for the association

  • args (Hash) (defaults to: {})

    (see Mongoid::Relations::Macros.embeds_one)



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mongoid-included/document_inclusion.rb', line 74

def includes_one(relation_name, args = {})
  relation_klass = if args[:class_name]
    infer_klass(args[:class_name])
  else
    klass_to_include(relation_name)
  end
  
  verify_inclusion_dependencies(relation_klass)
  self.including_one |= [ relation_klass ]

  embeds_one relation_name, args.merge(:class_name => relation_klass.name )
end