Module: Vidibus::Inheritance::Mongoid::ClassMethods

Defined in:
lib/vidibus/inheritance/mongoid.rb

Instance Method Summary collapse

Instance Method Details

#inheritable_documents(object, options = {}) ⇒ Object

Returns embedded documents of given document. Provide :keys option if you want to return a list of document types instead of a hash of documents grouped by their type.

Example:

inheritable_documents(model)
# => { :children => [ <embeds_many collection> ], :location => <embeds_one object> }

inheritable_documents(model, :keys => true)
# => [:children, :location ]


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vidibus/inheritance/mongoid.rb', line 53

def inheritable_documents(object, options = {})
  keys = options[:keys]
  collection = keys ? [] : {}
  for name, association in object.associations
    next unless association.embedded?
    if keys
      collection << name
    else
      collection[name] = object.send(name)
    end
  end
  collection
end

#rootsObject

Returns all objects that have no ancestor. Accepts Mongoid criteria to reduce and sort matching set. Criteria API: mongoid.org/docs/querying/

Examples:

Model.roots                          # => All models without ancestor
Model.roots.where(:name => "Laura")  # => Only models named Laura
Model.roots.asc(:created_at)         # => All models, ordered by date of creation


77
78
79
# File 'lib/vidibus/inheritance/mongoid.rb', line 77

def roots
  where(:ancestor_uuid => nil)
end