mongoid-with-auth 1.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. data/MIT_LICENSE +20 -0
  2. data/README.rdoc +49 -0
  3. data/lib/mongoid.rb +122 -0
  4. data/lib/mongoid/associations.rb +300 -0
  5. data/lib/mongoid/associations/belongs_to_related.rb +58 -0
  6. data/lib/mongoid/associations/embedded_in.rb +72 -0
  7. data/lib/mongoid/associations/embeds_many.rb +254 -0
  8. data/lib/mongoid/associations/embeds_one.rb +96 -0
  9. data/lib/mongoid/associations/has_many_related.rb +181 -0
  10. data/lib/mongoid/associations/has_one_related.rb +85 -0
  11. data/lib/mongoid/associations/meta_data.rb +29 -0
  12. data/lib/mongoid/associations/options.rb +57 -0
  13. data/lib/mongoid/associations/proxy.rb +24 -0
  14. data/lib/mongoid/attributes.rb +204 -0
  15. data/lib/mongoid/callbacks.rb +23 -0
  16. data/lib/mongoid/collection.rb +120 -0
  17. data/lib/mongoid/collections.rb +41 -0
  18. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  19. data/lib/mongoid/collections/master.rb +29 -0
  20. data/lib/mongoid/collections/operations.rb +41 -0
  21. data/lib/mongoid/collections/slaves.rb +45 -0
  22. data/lib/mongoid/components.rb +27 -0
  23. data/lib/mongoid/concern.rb +31 -0
  24. data/lib/mongoid/config.rb +205 -0
  25. data/lib/mongoid/contexts.rb +25 -0
  26. data/lib/mongoid/contexts/enumerable.rb +151 -0
  27. data/lib/mongoid/contexts/ids.rb +25 -0
  28. data/lib/mongoid/contexts/mongo.rb +285 -0
  29. data/lib/mongoid/contexts/paging.rb +50 -0
  30. data/lib/mongoid/criteria.rb +239 -0
  31. data/lib/mongoid/criterion/complex.rb +21 -0
  32. data/lib/mongoid/criterion/exclusion.rb +65 -0
  33. data/lib/mongoid/criterion/inclusion.rb +110 -0
  34. data/lib/mongoid/criterion/optional.rb +136 -0
  35. data/lib/mongoid/cursor.rb +81 -0
  36. data/lib/mongoid/deprecation.rb +22 -0
  37. data/lib/mongoid/dirty.rb +253 -0
  38. data/lib/mongoid/document.rb +311 -0
  39. data/lib/mongoid/errors.rb +108 -0
  40. data/lib/mongoid/extensions.rb +101 -0
  41. data/lib/mongoid/extensions/array/accessors.rb +17 -0
  42. data/lib/mongoid/extensions/array/aliasing.rb +4 -0
  43. data/lib/mongoid/extensions/array/assimilation.rb +26 -0
  44. data/lib/mongoid/extensions/array/conversions.rb +29 -0
  45. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  46. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  47. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  48. data/lib/mongoid/extensions/boolean/conversions.rb +22 -0
  49. data/lib/mongoid/extensions/date/conversions.rb +24 -0
  50. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  51. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  52. data/lib/mongoid/extensions/hash/accessors.rb +38 -0
  53. data/lib/mongoid/extensions/hash/assimilation.rb +39 -0
  54. data/lib/mongoid/extensions/hash/conversions.rb +45 -0
  55. data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
  56. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  57. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  58. data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
  59. data/lib/mongoid/extensions/object/conversions.rb +33 -0
  60. data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
  61. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  62. data/lib/mongoid/extensions/string/conversions.rb +15 -0
  63. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  64. data/lib/mongoid/extensions/symbol/inflections.rb +36 -0
  65. data/lib/mongoid/extensions/time_conversions.rb +35 -0
  66. data/lib/mongoid/extras.rb +61 -0
  67. data/lib/mongoid/factory.rb +20 -0
  68. data/lib/mongoid/field.rb +59 -0
  69. data/lib/mongoid/fields.rb +65 -0
  70. data/lib/mongoid/finders.rb +136 -0
  71. data/lib/mongoid/identity.rb +39 -0
  72. data/lib/mongoid/indexes.rb +30 -0
  73. data/lib/mongoid/javascript.rb +21 -0
  74. data/lib/mongoid/javascript/functions.yml +37 -0
  75. data/lib/mongoid/matchers.rb +36 -0
  76. data/lib/mongoid/matchers/all.rb +11 -0
  77. data/lib/mongoid/matchers/default.rb +26 -0
  78. data/lib/mongoid/matchers/exists.rb +13 -0
  79. data/lib/mongoid/matchers/gt.rb +11 -0
  80. data/lib/mongoid/matchers/gte.rb +11 -0
  81. data/lib/mongoid/matchers/in.rb +11 -0
  82. data/lib/mongoid/matchers/lt.rb +11 -0
  83. data/lib/mongoid/matchers/lte.rb +11 -0
  84. data/lib/mongoid/matchers/ne.rb +11 -0
  85. data/lib/mongoid/matchers/nin.rb +11 -0
  86. data/lib/mongoid/matchers/size.rb +11 -0
  87. data/lib/mongoid/memoization.rb +33 -0
  88. data/lib/mongoid/named_scope.rb +37 -0
  89. data/lib/mongoid/observable.rb +30 -0
  90. data/lib/mongoid/paths.rb +62 -0
  91. data/lib/mongoid/persistence.rb +222 -0
  92. data/lib/mongoid/persistence/command.rb +39 -0
  93. data/lib/mongoid/persistence/insert.rb +50 -0
  94. data/lib/mongoid/persistence/insert_embedded.rb +38 -0
  95. data/lib/mongoid/persistence/remove.rb +39 -0
  96. data/lib/mongoid/persistence/remove_all.rb +37 -0
  97. data/lib/mongoid/persistence/remove_embedded.rb +50 -0
  98. data/lib/mongoid/persistence/update.rb +63 -0
  99. data/lib/mongoid/scope.rb +75 -0
  100. data/lib/mongoid/state.rb +39 -0
  101. data/lib/mongoid/timestamps.rb +27 -0
  102. data/lib/mongoid/version.rb +4 -0
  103. data/lib/mongoid/versioning.rb +27 -0
  104. metadata +284 -0
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Associations #:nodoc:
4
+ # Represents a relational association to a "parent" object.
5
+ class BelongsToRelated < Proxy
6
+
7
+ # Initializing a related association only requires looking up the object
8
+ # by its id.
9
+ #
10
+ # Options:
11
+ #
12
+ # document: The +Document+ that contains the relationship.
13
+ # options: The association +Options+.
14
+ def initialize(document, foreign_key, options, target = nil)
15
+ @options = options
16
+ @target = target || options.klass.find(foreign_key)
17
+ extends(options)
18
+ end
19
+
20
+ class << self
21
+ # Instantiate a new +BelongsToRelated+ or return nil if the foreign key is
22
+ # nil. It is preferrable to use this method over the traditional call
23
+ # to new.
24
+ #
25
+ # Options:
26
+ #
27
+ # document: The +Document+ that contains the relationship.
28
+ # options: The association +Options+.
29
+ def instantiate(document, options, target = nil)
30
+ foreign_key = document.send(options.foreign_key)
31
+ foreign_key.blank? ? nil : new(document, foreign_key, options, target)
32
+ end
33
+
34
+ # Returns the macro used to create the association.
35
+ def macro
36
+ :belongs_to_related
37
+ end
38
+
39
+ # Perform an update of the relationship of the parent and child. This
40
+ # will assimilate the child +Document+ into the parent's object graph.
41
+ #
42
+ # Options:
43
+ #
44
+ # target: The target(parent) object
45
+ # document: The +Document+ to update.
46
+ # options: The association +Options+
47
+ #
48
+ # Example:
49
+ #
50
+ # <tt>BelongsToRelated.update(person, game, options)</tt>
51
+ def update(target, document, options)
52
+ document.send("#{options.foreign_key}=", target ? target.id : nil)
53
+ instantiate(document, options, target)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Associations #:nodoc:
4
+ # Represents an association that is embedded within another document in the
5
+ # database, either as one or many.
6
+ class EmbeddedIn < Proxy
7
+
8
+ # Creates the new association by setting the internal
9
+ # target as the passed in Document. This should be the
10
+ # parent.
11
+ #
12
+ # All method calls on this object will then be delegated
13
+ # to the internal document itself.
14
+ #
15
+ # Options:
16
+ #
17
+ # target: The parent +Document+
18
+ # options: The association options
19
+ def initialize(target, options)
20
+ @target, @options = target, options
21
+ extends(options)
22
+ end
23
+
24
+ # Returns the parent document. The id param is present for
25
+ # compatibility with rails, however this could be overwritten
26
+ # in the future.
27
+ def find(id)
28
+ @target
29
+ end
30
+
31
+ class << self
32
+ # Creates the new association by setting the internal
33
+ # document as the passed in Document. This should be the
34
+ # parent.
35
+ #
36
+ # Options:
37
+ #
38
+ # document: The parent +Document+
39
+ # options: The association options
40
+ def instantiate(document, options)
41
+ target = document._parent
42
+ target.nil? ? nil : new(target, options)
43
+ end
44
+
45
+ # Returns the macro used to create the association.
46
+ def macro
47
+ :embedded_in
48
+ end
49
+
50
+ # Perform an update of the relationship of the parent and child. This
51
+ # is initialized by setting a parent object as the association on the
52
+ # +Document+. Will properly set an embeds_one or an embeds_many.
53
+ #
54
+ # Returns:
55
+ #
56
+ # A new +EmbeddedIn+ association proxy.
57
+ def update(target, child, options)
58
+ child.parentize(target, determine_name(target, options))
59
+ child.notify
60
+ instantiate(child, options)
61
+ end
62
+
63
+ protected
64
+ def determine_name(target, options)
65
+ inverse = options.inverse_of
66
+ return inverse unless inverse.is_a?(Array)
67
+ inverse.detect { |name| target.respond_to?(name) }
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,254 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Associations #:nodoc:
4
+ # Represents embedding many documents within a parent document, which will
5
+ # be an array as the underlying storage mechanism.
6
+ class EmbedsMany < Proxy
7
+
8
+ attr_accessor :association_name, :klass
9
+
10
+ # Appends the object to the +Array+, setting its parent in
11
+ # the process.
12
+ def <<(*documents)
13
+ documents.flatten.each do |doc|
14
+ doc.parentize(@parent, @association_name)
15
+ @target << doc
16
+ doc._index = @target.size - 1
17
+ doc.notify
18
+ end
19
+ end
20
+
21
+ alias :concat :<<
22
+ alias :push :<<
23
+
24
+ # Builds a new Document and adds it to the association collection. The
25
+ # document created will be of the same class as the others in the
26
+ # association, and the attributes will be passed into the constructor.
27
+ #
28
+ # Returns:
29
+ #
30
+ # The newly created Document.
31
+ def build(attrs = {}, type = nil)
32
+ document = type ? type.instantiate : @klass.instantiate
33
+ document.parentize(@parent, @association_name)
34
+ document.write_attributes(attrs)
35
+ @target << document
36
+ document._index = @target.size - 1
37
+ document
38
+ end
39
+
40
+ # Clears the association, and notifies the parents of the removal.
41
+ def clear
42
+ unless @target.empty?
43
+ document = @target.first
44
+ document.notify_observers(document, true)
45
+ @target.clear
46
+ end
47
+ end
48
+
49
+ # Creates a new Document and adds it to the association collection. The
50
+ # document created will be of the same class as the others in the
51
+ # association, and the attributes will be passed into the constructor and
52
+ # the new object will then be saved.
53
+ #
54
+ # Returns:
55
+ #
56
+ # The newly created Document.
57
+ def create(attrs = {}, type = nil)
58
+ document = build(attrs, type)
59
+ document.save; document
60
+ end
61
+
62
+ # Creates a new Document and adds it to the association collection. The
63
+ # document created will be of the same class as the others in the
64
+ # association, and the attributes will be passed into the constructor and
65
+ # the new object will then be saved. If validation fails an error will
66
+ # get raised.
67
+ #
68
+ # Returns:
69
+ #
70
+ # The newly created Document.
71
+ def create!(attrs = {}, type = nil)
72
+ document = create(attrs, type)
73
+ errors = document.errors
74
+ raise Errors::Validations.new(errors) unless errors.empty?
75
+ document
76
+ end
77
+
78
+ # Delete all the documents in the association without running callbacks.
79
+ #
80
+ # Example:
81
+ #
82
+ # <tt>addresses.delete_all</tt>
83
+ #
84
+ # Returns:
85
+ #
86
+ # The number of documents deleted.
87
+ def delete_all(conditions = {})
88
+ remove(:delete, conditions)
89
+ end
90
+
91
+ # Delete all the documents in the association and run destroy callbacks.
92
+ #
93
+ # Example:
94
+ #
95
+ # <tt>addresses.destroy_all</tt>
96
+ #
97
+ # Returns:
98
+ #
99
+ # The number of documents destroyed.
100
+ def destroy_all(conditions = {})
101
+ remove(:destroy, conditions)
102
+ end
103
+
104
+ # Finds a document in this association.
105
+ #
106
+ # If :all is passed, returns all the documents
107
+ #
108
+ # If an id is passed, will return the document for that id.
109
+ #
110
+ # Returns:
111
+ #
112
+ # Array or single Document.
113
+ def find(param)
114
+ return @target if param == :all
115
+ return detect { |document| document.id == param }
116
+ end
117
+
118
+ # Creates the new association by finding the attributes in
119
+ # the parent document with its name, and instantiating a
120
+ # new document for each one found. These will then be put in an
121
+ # internal array.
122
+ #
123
+ # This then delegated all methods to the array class since this is
124
+ # essentially a proxy to an array itself.
125
+ #
126
+ # Options:
127
+ #
128
+ # parent: The parent document to the association.
129
+ # options: The association options.
130
+ def initialize(parent, options, target_array = nil)
131
+ @parent, @association_name = parent, options.name
132
+ @klass, @options = options.klass, options
133
+ if target_array
134
+ build_children_from_target_array(target_array)
135
+ else
136
+ build_children_from_attributes(parent.raw_attributes[@association_name])
137
+ end
138
+ extends(options)
139
+ end
140
+
141
+
142
+
143
+ # If the target array does not respond to the supplied method then try to
144
+ # find a named scope or criteria on the class and send the call there.
145
+ #
146
+ # If the method exists on the array, use the default proxy behavior.
147
+ def method_missing(name, *args, &block)
148
+ unless @target.respond_to?(name)
149
+ object = @klass.send(name, *args)
150
+ object.documents = @target
151
+ return object
152
+ end
153
+ super
154
+ end
155
+
156
+ # Used for setting associations via a nested attributes setter from the
157
+ # parent +Document+.
158
+ #
159
+ # Options:
160
+ #
161
+ # attributes: A +Hash+ of integer keys and +Hash+ values.
162
+ #
163
+ # Returns:
164
+ #
165
+ # The newly build target Document.
166
+ def nested_build(attributes)
167
+ attributes.values.each do |attrs|
168
+ build(attrs)
169
+ end
170
+ end
171
+
172
+ # Paginate the association. Will create a new criteria, set the documents
173
+ # on it and execute in an enumerable context.
174
+ #
175
+ # Options:
176
+ #
177
+ # options: A +Hash+ of pagination options.
178
+ #
179
+ # Returns:
180
+ #
181
+ # A +WillPaginate::Collection+.
182
+ def paginate(options)
183
+ criteria = Mongoid::Criteria.translate(@klass, options)
184
+ criteria.documents = @target
185
+ criteria.paginate(options)
186
+ end
187
+
188
+ protected
189
+ # Initializes each of the attributes in the hash.
190
+ def build_children_from_attributes(attributes)
191
+ @target = []
192
+ if attributes
193
+ attributes.each_with_index do |attrs, index|
194
+ klass = attrs.klass
195
+ child = klass ? klass.instantiate(attrs) : @klass.instantiate(attrs)
196
+ child.parentize(@parent, @association_name)
197
+ child._index = index
198
+ @target << child
199
+ end
200
+ end
201
+ end
202
+
203
+ # Initializes the target array from an existing array of documents.
204
+ def build_children_from_target_array(target_array)
205
+ @target = target_array
206
+ @target.each_with_index do |child, index|
207
+ child._index = index
208
+ end
209
+ end
210
+
211
+ # Removes documents based on a method.
212
+ def remove(method, conditions)
213
+ criteria = @klass.find(conditions || {})
214
+ criteria.documents = @target
215
+ count = criteria.size
216
+ criteria.each do |doc|
217
+ @target.delete(doc); doc.send(method)
218
+ end; count
219
+ end
220
+
221
+ class << self
222
+
223
+ # Preferred method of creating a new +EmbedsMany+ association. It will
224
+ # delegate to new.
225
+ #
226
+ # Options:
227
+ #
228
+ # document: The parent +Document+
229
+ # options: The association options
230
+ def instantiate(document, options, target_array = nil)
231
+ new(document, options, target_array)
232
+ end
233
+
234
+ # Returns the macro used to create the association.
235
+ def macro
236
+ :embeds_many
237
+ end
238
+
239
+ # Perform an update of the relationship of the parent and child. This
240
+ # is initialized by setting the has_many to the supplied +Enumerable+
241
+ # and setting up the parentization.
242
+ def update(children, parent, options)
243
+ parent.raw_attributes.delete(options.name)
244
+ children.assimilate(parent, options)
245
+ if children && children.first.is_a?(Mongoid::Document)
246
+ instantiate(parent, options, children)
247
+ else
248
+ instantiate(parent, options)
249
+ end
250
+ end
251
+ end
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Associations #:nodoc:
4
+ # Represents an association that is embedded in a parent document as a
5
+ # one-to-one relationship.
6
+ class EmbedsOne < Proxy
7
+
8
+ # Build a new object for the association.
9
+ def build(attrs = {}, type = nil)
10
+ @target = attrs.assimilate(@parent, @options, type); self
11
+ end
12
+
13
+ # Creates the new association by finding the attributes in
14
+ # the parent document with its name, and instantiating a
15
+ # new document for it.
16
+ #
17
+ # All method calls on this object will then be delegated
18
+ # to the internal document itself.
19
+ #
20
+ # Options:
21
+ #
22
+ # document: The parent +Document+
23
+ # attributes: The attributes of the target object.
24
+ # options: The association options.
25
+ #
26
+ # Returns:
27
+ #
28
+ # A new +HashOne+ association proxy.
29
+ def initialize(document, attrs, options)
30
+ @parent, @options = document, options
31
+ @target = attrs.assimilate(@parent, @options, attrs.klass)
32
+ extends(options)
33
+ end
34
+
35
+ # Used for setting the association via a nested attributes setter on the
36
+ # parent +Document+. Called when using accepts_nested_attributes_for.
37
+ #
38
+ # Options:
39
+ #
40
+ # attributes: The attributes for the new association
41
+ #
42
+ # Returns:
43
+ #
44
+ # A new target document.
45
+ def nested_build(attributes)
46
+ build(attributes)
47
+ end
48
+
49
+ class << self
50
+ # Preferred method of instantiating a new +EmbedsOne+, since nil values
51
+ # will be handled properly.
52
+ #
53
+ # Options:
54
+ #
55
+ # document: The parent +Document+
56
+ # options: The association options.
57
+ #
58
+ # Returns:
59
+ #
60
+ # A new +EmbedsOne+ association proxy.
61
+ def instantiate(document, options)
62
+ attributes = document.raw_attributes[options.name]
63
+ return nil if attributes.blank?
64
+ new(document, attributes, options)
65
+ end
66
+
67
+ # Returns the macro used to create the association.
68
+ def macro
69
+ :embeds_one
70
+ end
71
+
72
+ # Perform an update of the relationship of the parent and child. This
73
+ # will assimilate the child +Document+ into the parent's object graph.
74
+ #
75
+ # Options:
76
+ #
77
+ # child: The child +Document+ or +Hash+.
78
+ # parent: The parent +Document+ to update.
79
+ # options: The association +Options+
80
+ #
81
+ # Example:
82
+ #
83
+ # <tt>EmbedsOne.update({:first_name => "Hank"}, person, options)</tt>
84
+ #
85
+ # Returns:
86
+ #
87
+ # A new +EmbedsOne+ association proxy.
88
+ def update(child, parent, options)
89
+ child.assimilate(parent, options)
90
+ instantiate(parent, options)
91
+ end
92
+ end
93
+
94
+ end
95
+ end
96
+ end