Module: MongoMapper::Plugins::Sci::ClassMethods

Defined in:
lib/mongo_mapper/plugins/sci.rb

Instance Method Summary collapse

Instance Method Details

#criteria_hash(criteria = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/mongo_mapper/plugins/sci.rb', line 35

def criteria_hash(criteria={})
  if single_collection_inherited?
    super criteria.merge(:_type => name)
  else
    super
  end
end

#explicit_collection_defined?Boolean

Returns:



53
54
55
# File 'lib/mongo_mapper/plugins/sci.rb', line 53

def explicit_collection_defined?
  @collection_defined == true
end

#inherited(subclass) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/mongo_mapper/plugins/sci.rb', line 12

def inherited(subclass)
  super
  if @collection_name == subclass.instance_variable_get("@collection_name")
    key :_type, String unless key?(:_type)
    subclass.single_collection_parent = self
    subclass.instance_variable_set("@single_collection_inherited", true)
  else
    remove_key :_type
  end
end

#query(options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/mongo_mapper/plugins/sci.rb', line 69

def query(options={})
  super.tap do |query|
    query[:_type] = {'$in' => [name] + descendants.map(&:name)} if single_collection_inherited?
  end
end

#set_collection_name(name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/mongo_mapper/plugins/sci.rb', line 43

def set_collection_name(name)
  if single_collection_inherited?
    single_collection_parent.remove_key :_type
    single_collection_parent = nil
    @single_collection_inherited = false
  end
  @collection_defined = true
  super
end

#single_collection_inherited?Boolean

Returns:



65
66
67
# File 'lib/mongo_mapper/plugins/sci.rb', line 65

def single_collection_inherited?
  !!(@single_collection_inherited ||= false)
end

#single_collection_parentObject



57
58
59
# File 'lib/mongo_mapper/plugins/sci.rb', line 57

def single_collection_parent
  @single_collection_parent
end

#single_collection_parent=(parent) ⇒ Object



61
62
63
# File 'lib/mongo_mapper/plugins/sci.rb', line 61

def single_collection_parent=(parent)
  @single_collection_parent = parent
end

#single_collection_rootObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongo_mapper/plugins/sci.rb', line 23

def single_collection_root
  parent = single_collection_parent || self
  root = parent

  while parent
    parent = parent.single_collection_parent
    root = parent unless parent.nil?
  end

  root
end