Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 83 lines (69 sloc) 2.102 kB
55765b3 @jnunemaker Missing encoding comments for ruby 1.9.
jnunemaker authored
1 # encoding: UTF-8
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
2 module MongoMapper
3 module Plugins
4 module Sci
7c69e08 @bkeepers Deprecate plugin structure in favor of using ActiveSupport::Concern
bkeepers authored
5 extend ActiveSupport::Concern
6
cea80bd @kelsin Adding support to query all subclasses when using sci
kelsin authored
7 included do
8 extend ActiveSupport::DescendantsTracker
9 end
10
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
11 module ClassMethods
12 def inherited(subclass)
13 super
0cc9903 @cheald Fix issues with set_collection_name nullifying SCI on 1.8
cheald authored
14 if @collection_name == subclass.instance_variable_get("@collection_name")
ca70c43 @cheald Only add the _type key to inherited classes when they have the same c…
cheald authored
15 key :_type, String unless key?(:_type)
0cc9903 @cheald Fix issues with set_collection_name nullifying SCI on 1.8
cheald authored
16 subclass.single_collection_parent = self
17 subclass.instance_variable_set("@single_collection_inherited", true)
d8e7d18 @cheald More complete fix for the previous issue; remove the _type key when S…
cheald authored
18 else
19 remove_key :_type
0cc9903 @cheald Fix issues with set_collection_name nullifying SCI on 1.8
cheald authored
20 end
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
21 end
22
26cdfe0 @jnunemaker Added a few details to SCI.
jnunemaker authored
23 def single_collection_root
24 parent = single_collection_parent || self
25 root = parent
26
27 while parent
28 parent = parent.single_collection_parent
29 root = parent unless parent.nil?
30 end
31
32 root
33 end
34
c7856e4 @cheald Add critera_hash when single collection inherited. Closes #454
cheald authored
35 def criteria_hash(criteria={})
36 if single_collection_inherited?
37 super criteria.merge(:_type => name)
38 else
39 super
40 end
41 end
42
b6f75e0 @cheald Disable SCI when an inherited model explicitly changes its collection…
cheald authored
43 def set_collection_name(name)
44 if single_collection_inherited?
d8e7d18 @cheald More complete fix for the previous issue; remove the _type key when S…
cheald authored
45 single_collection_parent.remove_key :_type
b6f75e0 @cheald Disable SCI when an inherited model explicitly changes its collection…
cheald authored
46 single_collection_parent = nil
47 @single_collection_inherited = false
48 end
0cc9903 @cheald Fix issues with set_collection_name nullifying SCI on 1.8
cheald authored
49 @collection_defined = true
b6f75e0 @cheald Disable SCI when an inherited model explicitly changes its collection…
cheald authored
50 super
51 end
52
0cc9903 @cheald Fix issues with set_collection_name nullifying SCI on 1.8
cheald authored
53 def explicit_collection_defined?
54 @collection_defined == true
55 end
56
26cdfe0 @jnunemaker Added a few details to SCI.
jnunemaker authored
57 def single_collection_parent
58 @single_collection_parent
59 end
60
61 def single_collection_parent=(parent)
62 @single_collection_parent = parent
63 end
64
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
65 def single_collection_inherited?
a60b04c @cheald Minor improvements to type cast speeds. Important since they're calle…
cheald authored
66 !!(@single_collection_inherited ||= false)
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
67 end
3b568dc @jnunemaker Moved scoping :_type at query time to Sci plugin. Also, using name in…
jnunemaker authored
68
69 def query(options={})
70 super.tap do |query|
cea80bd @kelsin Adding support to query all subclasses when using sci
kelsin authored
71 query[:_type] = {'$in' => [name] + descendants.map(&:name)} if single_collection_inherited?
3b568dc @jnunemaker Moved scoping :_type at query time to Sci plugin. Also, using name in…
jnunemaker authored
72 end
73 end
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
74 end
75
d2333d9 @laserlemon ActiveSupport::Concern's automatic inclusion of the InstanceMethods m…
laserlemon authored
76 def initialize(*args)
77 super
78 write_key :_type, self.class.name if self.class.key?(:_type)
872b101 @jnunemaker Separated single collection inheritance into plugin.
jnunemaker authored
79 end
80 end
81 end
cea80bd @kelsin Adding support to query all subclasses when using sci
kelsin authored
82 end
Something went wrong with that request. Please try again.