mongoid-pre 2.0.0.beta1 → 2.0.0.pre

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 (135) hide show
  1. data/.watchr +15 -10
  2. data/HISTORY +342 -0
  3. data/README.rdoc +8 -1
  4. data/Rakefile +2 -3
  5. data/VERSION +1 -1
  6. data/lib/mongoid/associations/{embedded_in.rb → belongs_to.rb} +4 -4
  7. data/lib/mongoid/associations/belongs_to_related.rb +9 -6
  8. data/lib/mongoid/associations/{embeds_many.rb → has_many.rb} +20 -33
  9. data/lib/mongoid/associations/has_many_related.rb +4 -28
  10. data/lib/mongoid/associations/{embeds_one.rb → has_one.rb} +6 -6
  11. data/lib/mongoid/associations/options.rb +6 -1
  12. data/lib/mongoid/associations.rb +32 -41
  13. data/lib/mongoid/attributes.rb +6 -13
  14. data/lib/mongoid/collection.rb +1 -2
  15. data/lib/mongoid/commands/delete.rb +1 -1
  16. data/lib/mongoid/commands/delete_all.rb +1 -4
  17. data/lib/mongoid/commands/destroy.rb +1 -1
  18. data/lib/mongoid/commands/destroy_all.rb +1 -3
  19. data/lib/mongoid/commands/save.rb +0 -1
  20. data/lib/mongoid/commands.rb +13 -2
  21. data/lib/mongoid/components.rb +1 -6
  22. data/lib/mongoid/config.rb +1 -5
  23. data/lib/mongoid/contexts/enumerable.rb +17 -54
  24. data/lib/mongoid/contexts/mongo.rb +38 -101
  25. data/lib/mongoid/contexts/paging.rb +2 -2
  26. data/lib/mongoid/contexts.rb +0 -21
  27. data/lib/mongoid/criteria.rb +73 -15
  28. data/lib/mongoid/criterion/inclusion.rb +0 -2
  29. data/lib/mongoid/criterion/optional.rb +2 -10
  30. data/lib/mongoid/document.rb +30 -41
  31. data/lib/mongoid/extensions.rb +0 -15
  32. data/lib/mongoid/field.rb +7 -20
  33. data/lib/mongoid/fields.rb +10 -15
  34. data/lib/mongoid/finders.rb +98 -10
  35. data/lib/mongoid/identity.rb +2 -8
  36. data/lib/mongoid/named_scope.rb +0 -2
  37. data/lib/mongoid/validations/associated.rb +8 -3
  38. data/lib/mongoid/validations/uniqueness.rb +7 -2
  39. data/lib/mongoid/validations.rb +2 -2
  40. data/lib/mongoid/versioning.rb +1 -1
  41. data/lib/mongoid.rb +5 -21
  42. data/mongoid.gemspec +19 -59
  43. data/spec/integration/mongoid/associations_spec.rb +3 -42
  44. data/spec/integration/mongoid/attributes_spec.rb +2 -2
  45. data/spec/integration/mongoid/commands_spec.rb +13 -27
  46. data/spec/integration/mongoid/contexts/enumerable_spec.rb +0 -13
  47. data/spec/integration/mongoid/criteria_spec.rb +3 -50
  48. data/spec/integration/mongoid/document_spec.rb +5 -72
  49. data/spec/integration/mongoid/finders_spec.rb +28 -85
  50. data/spec/models/address.rb +3 -3
  51. data/spec/models/animal.rb +2 -2
  52. data/spec/models/country_code.rb +2 -2
  53. data/spec/models/game.rb +1 -2
  54. data/spec/models/inheritance.rb +5 -5
  55. data/spec/models/location.rb +2 -2
  56. data/spec/models/name.rb +3 -3
  57. data/spec/models/namespacing.rb +2 -2
  58. data/spec/models/patient.rb +0 -2
  59. data/spec/models/person.rb +4 -6
  60. data/spec/models/pet.rb +3 -3
  61. data/spec/models/pet_owner.rb +3 -3
  62. data/spec/models/phone.rb +3 -3
  63. data/spec/models/post.rb +1 -1
  64. data/spec/models/translation.rb +2 -2
  65. data/spec/models/vet_visit.rb +2 -2
  66. data/spec/spec_helper.rb +2 -2
  67. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +0 -4
  68. data/spec/unit/mongoid/associations/{embedded_in_spec.rb → belongs_to_spec.rb} +11 -11
  69. data/spec/unit/mongoid/associations/has_many_related_spec.rb +14 -45
  70. data/spec/unit/mongoid/associations/{embeds_many_spec.rb → has_many_spec.rb} +34 -79
  71. data/spec/unit/mongoid/associations/{embeds_one_spec.rb → has_one_spec.rb} +18 -18
  72. data/spec/unit/mongoid/associations/options_spec.rb +19 -20
  73. data/spec/unit/mongoid/associations_spec.rb +12 -74
  74. data/spec/unit/mongoid/attributes_spec.rb +51 -83
  75. data/spec/unit/mongoid/collection_spec.rb +0 -46
  76. data/spec/unit/mongoid/commands/delete_all_spec.rb +8 -9
  77. data/spec/unit/mongoid/commands/delete_spec.rb +3 -6
  78. data/spec/unit/mongoid/commands/destroy_all_spec.rb +2 -0
  79. data/spec/unit/mongoid/commands/destroy_spec.rb +3 -10
  80. data/spec/unit/mongoid/commands_spec.rb +11 -20
  81. data/spec/unit/mongoid/config_spec.rb +0 -18
  82. data/spec/unit/mongoid/contexts/enumerable_spec.rb +11 -198
  83. data/spec/unit/mongoid/contexts/mongo_spec.rb +54 -357
  84. data/spec/unit/mongoid/criteria_spec.rb +78 -107
  85. data/spec/unit/mongoid/criterion/exclusion_spec.rb +13 -3
  86. data/spec/unit/mongoid/criterion/inclusion_spec.rb +19 -25
  87. data/spec/unit/mongoid/criterion/optional_spec.rb +18 -25
  88. data/spec/unit/mongoid/document_spec.rb +34 -71
  89. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
  90. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +3 -0
  91. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -16
  92. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +1 -1
  93. data/spec/unit/mongoid/field_spec.rb +0 -62
  94. data/spec/unit/mongoid/fields_spec.rb +0 -33
  95. data/spec/unit/mongoid/finders_spec.rb +1 -37
  96. data/spec/unit/mongoid/identity_spec.rb +6 -47
  97. data/spec/unit/mongoid/named_scope_spec.rb +2 -15
  98. data/spec/unit/mongoid/scope_spec.rb +1 -1
  99. data/spec/unit/mongoid/validations/uniqueness_spec.rb +3 -3
  100. data/spec/unit/mongoid_spec.rb +2 -11
  101. metadata +14 -64
  102. data/lib/mongoid/associations/meta_data.rb +0 -28
  103. data/lib/mongoid/contexts/ids.rb +0 -25
  104. data/lib/mongoid/deprecation.rb +0 -22
  105. data/lib/mongoid/dirty.rb +0 -203
  106. data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
  107. data/lib/mongoid/extensions/binary/conversions.rb +0 -17
  108. data/lib/mongoid/extensions/objectid/conversions.rb +0 -15
  109. data/lib/mongoid/extras.rb +0 -61
  110. data/lib/mongoid/javascript/functions.yml +0 -37
  111. data/lib/mongoid/javascript.rb +0 -21
  112. data/lib/mongoid/observable.rb +0 -30
  113. data/lib/mongoid/paths.rb +0 -54
  114. data/lib/mongoid/persistence/command.rb +0 -20
  115. data/lib/mongoid/persistence/insert.rb +0 -71
  116. data/lib/mongoid/persistence/update.rb +0 -78
  117. data/lib/mongoid/persistence.rb +0 -27
  118. data/lib/mongoid/state.rb +0 -32
  119. data/spec/integration/mongoid/dirty_spec.rb +0 -70
  120. data/spec/integration/mongoid/persistence/update_spec.rb +0 -46
  121. data/spec/models/callbacks.rb +0 -18
  122. data/spec/unit/mongoid/associations/meta_data_spec.rb +0 -88
  123. data/spec/unit/mongoid/contexts_spec.rb +0 -25
  124. data/spec/unit/mongoid/deprecation_spec.rb +0 -24
  125. data/spec/unit/mongoid/dirty_spec.rb +0 -286
  126. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +0 -22
  127. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +0 -22
  128. data/spec/unit/mongoid/extras_spec.rb +0 -102
  129. data/spec/unit/mongoid/javascript_spec.rb +0 -48
  130. data/spec/unit/mongoid/observable_spec.rb +0 -46
  131. data/spec/unit/mongoid/paths_spec.rb +0 -124
  132. data/spec/unit/mongoid/persistence/insert_spec.rb +0 -175
  133. data/spec/unit/mongoid/persistence/update_spec.rb +0 -148
  134. data/spec/unit/mongoid/persistence_spec.rb +0 -40
  135. data/spec/unit/mongoid/state_spec.rb +0 -83
data/lib/mongoid/field.rb CHANGED
@@ -1,25 +1,11 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  class Field
4
- attr_reader :name, :type
5
4
 
6
- # Determine if the field is able to be accessible via a mass update.
7
- #
8
- # Returns:
9
- #
10
- # true if accessible, false if not.
11
- def accessible?
12
- !!@accessible
13
- end
14
-
15
- # Get the default value for the field.
16
- #
17
- # Returns:
18
- #
19
- # The primitive value or a copy of the default.
20
- def default
21
- (@default.is_a?(Array) || @default.is_a?(Hash)) ? @default.dup : @default
22
- end
5
+ attr_reader \
6
+ :default,
7
+ :name,
8
+ :type
23
9
 
24
10
  # Create the new field with a name and optional additional options. Valid
25
11
  # options are :default
@@ -33,9 +19,9 @@ module Mongoid #:nodoc:
33
19
  #
34
20
  # <tt>Field.new(:score, :default => 0)</tt>
35
21
  def initialize(name, options = {})
36
- @name, @default = name, options[:default]
22
+ @name = name
23
+ @default = options[:default]
37
24
  @type = options[:type] || String
38
- @accessible = options.has_key?(:accessible) ? options[:accessible] : true
39
25
  end
40
26
 
41
27
  # Used for setting an object in the attributes hash. If nil is provided the
@@ -48,5 +34,6 @@ module Mongoid #:nodoc:
48
34
  def get(object)
49
35
  type.get(object)
50
36
  end
37
+
51
38
  end
52
39
  end
@@ -5,9 +5,11 @@ module Mongoid #:nodoc
5
5
  included do
6
6
  # Set up the class attributes that must be available to all subclasses.
7
7
  # These include defaults, fields
8
- class_inheritable_accessor :fields
8
+ class_inheritable_accessor :defaults, :fields
9
9
 
10
+ self.defaults = {}
10
11
  self.fields = {}
12
+
11
13
  delegate :defaults, :fields, :to => "self.class"
12
14
  end
13
15
 
@@ -27,15 +29,7 @@ module Mongoid #:nodoc
27
29
  def field(name, options = {})
28
30
  access = name.to_s
29
31
  set_field(access, options)
30
- end
31
-
32
- # Returns the default values for the fields on the document
33
- def defaults
34
- fields.inject({}) do |defs,(field_name,field)|
35
- next(defs) if field.default.nil?
36
- defs[field_name.to_s] = field.default
37
- defs
38
- end
32
+ set_default(access, options)
39
33
  end
40
34
 
41
35
  protected
@@ -44,19 +38,20 @@ module Mongoid #:nodoc
44
38
  meth = options.delete(:as) || name
45
39
  fields[name] = Field.new(name, options)
46
40
  create_accessors(name, meth, options)
47
- add_dirty_methods(name)
48
41
  end
49
42
 
50
43
  # Create the field accessors.
51
44
  def create_accessors(name, meth, options = {})
52
45
  define_method(meth) { read_attribute(name) }
53
46
  define_method("#{meth}=") { |value| write_attribute(name, value) }
54
- define_method("#{meth}?") do
55
- attr = read_attribute(name)
56
- (options[:type] == Boolean) ? attr == true : attr.present?
57
- end
47
+ define_method("#{meth}?") { read_attribute(name) == true } if options[:type] == Boolean
58
48
  end
59
49
 
50
+ # Set up a default value for a field.
51
+ def set_default(name, options = {})
52
+ value = options[:default]
53
+ defaults[name] = value if value
54
+ end
60
55
  end
61
56
  end
62
57
  end
@@ -1,16 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  module Finders #:nodoc:
4
-
5
- # Delegate to the criteria methods that are natural for creating a new
6
- # criteria.
7
- [ :all_in, :any_in, :avg, :excludes, :limit, :max, :min,
8
- :not_in, :only, :order_by, :skip, :sum, :where ].each do |name|
9
- define_method(name) do |*args|
10
- criteria.send(name, *args)
11
- end
12
- end
13
-
14
4
  # Find +Documents+ given the conditions.
15
5
  #
16
6
  # Options:
@@ -110,6 +100,54 @@ module Mongoid #:nodoc:
110
100
  find(:last, *args)
111
101
  end
112
102
 
103
+ # Convenience method for returning the max value of a field.
104
+ #
105
+ # Options:
106
+ #
107
+ # field: The field to use when calculating the max.
108
+ #
109
+ # Example:
110
+ #
111
+ # <tt>Person.max(:age)</tt>
112
+ #
113
+ # Returns: <tt>Float</tt> max value.
114
+ def max(field)
115
+ Criteria.new(self).max(field)
116
+ end
117
+
118
+ # Will execute a +Criteria+ based on the +DynamicFinder+ that gets
119
+ # generated.
120
+ #
121
+ # Options:
122
+ #
123
+ # name: The finder method name
124
+ # args: The arguments to pass to the method.
125
+ #
126
+ # Example:
127
+ #
128
+ # <tt>Person.find_all_by_title_and_age("Sir", 30)</tt>
129
+ # def method_missing(name, *args)
130
+ # dyna = DynamicFinder.new(name, *args)
131
+ # finder, conditions = dyna.finder, dyna.conditions
132
+ # results = find(finder, :conditions => conditions)
133
+ # results ? results : dyna.create(self)
134
+ # end
135
+
136
+ # Convenience method for returning the min value of a field.
137
+ #
138
+ # Options:
139
+ #
140
+ # field: The field to use when calculating the min.
141
+ #
142
+ # Example:
143
+ #
144
+ # <tt>Person.min(:age)</tt>
145
+ #
146
+ # Returns: <tt>Float</tt> min value.
147
+ def min(field)
148
+ Criteria.new(self).min(field)
149
+ end
150
+
113
151
  # Find all documents in paginated fashion given the supplied arguments.
114
152
  # If no parameters are passed just default to offset 0 and limit 20.
115
153
  #
@@ -127,6 +165,56 @@ module Mongoid #:nodoc:
127
165
  Criteria.translate(self, params).paginate
128
166
  end
129
167
 
168
+ # Entry point for creating a new criteria from a Document. This will
169
+ # instantiate a new +Criteria+ object with the supplied select criterion
170
+ # already added to it.
171
+ #
172
+ # Options:
173
+ #
174
+ # args: A list of field names to retrict the returned fields to.
175
+ #
176
+ # Example:
177
+ #
178
+ # <tt>Person.only(:field1, :field2, :field3)</tt>
179
+ #
180
+ # Returns: <tt>Criteria</tt>
181
+ def only(*args)
182
+ Criteria.new(self).only(*args)
183
+ end
184
+
185
+ # Convenience method for returning the sum of a specified field for all
186
+ # documents in the database.
187
+ #
188
+ # Options:
189
+ #
190
+ # field: The field to use when calculating the sum.
191
+ #
192
+ # Example:
193
+ #
194
+ # <tt>Person.sum(:age)</tt>
195
+ #
196
+ # Returns: <tt>Float</tt> of the sum.
197
+ def sum(field)
198
+ Criteria.new(self).sum(field)
199
+ end
200
+
201
+ # Entry point for creating a new criteria from a Document. This will
202
+ # instantiate a new +Criteria+ object with the supplied select criterion
203
+ # already added to it.
204
+ #
205
+ # Options:
206
+ #
207
+ # selector: A where criteria to initialize.
208
+ #
209
+ # Example:
210
+ #
211
+ # <tt>Person.where(:field1 => "Value")</tt>
212
+ #
213
+ # Returns: <tt>Criteria</tt>
214
+ def where(selector = nil)
215
+ Criteria.new(self).where(selector)
216
+ end
217
+
130
218
  protected
131
219
  # Find the first object or create/initialize it.
132
220
  def find_or(method, attrs = {})
@@ -13,21 +13,15 @@ module Mongoid #:nodoc:
13
13
  end
14
14
 
15
15
  protected
16
- # Return the proper id for the document.
17
- def generate_id
18
- id = Mongo::ObjectID.new
19
- Mongoid.use_object_ids ? id : id.to_s
20
- end
21
-
22
16
  # Set the id for the document.
23
17
  def identify(doc)
24
18
  doc.id = compose(doc).join(" ").identify if doc.primary_key
25
- doc.id = generate_id unless doc.id
19
+ doc.id = Mongo::ObjectID.new.to_s unless doc.id
26
20
  end
27
21
 
28
22
  # Set the _type field on the document.
29
23
  def type(doc)
30
- doc._type = doc.class.name if Mongoid.persist_types
24
+ doc._type = doc.class.name
31
25
  end
32
26
 
33
27
  # Generates the composite key for a document.
@@ -30,8 +30,6 @@ module Mongoid #:nodoc:
30
30
 
31
31
  alias :scope :named_scope
32
32
 
33
- alias :scope :named_scope
34
-
35
33
  # Return the scopes or default to an empty +Hash+.
36
34
  def scopes
37
35
  read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
@@ -8,8 +8,8 @@ module Mongoid #:nodoc:
8
8
  #
9
9
  # class Person
10
10
  # include Mongoid::Document
11
- # embeds_one :name
12
- # embeds_many :addresses
11
+ # has_one :name
12
+ # has_many :addresses
13
13
  #
14
14
  # validates_associated :name, :addresses
15
15
  # end
@@ -25,7 +25,12 @@ module Mongoid #:nodoc:
25
25
  def validate_each(document, attribute, value)
26
26
  values = value.is_a?(Array) ? value : [ value ]
27
27
  return if values.collect { |doc| doc.nil? || doc.valid? }.all?
28
- document.errors.add(attribute, :invalid, :default => options[:message], :value => value)
28
+ document.errors.add(
29
+ attribute,
30
+ :invalid,
31
+ :default => options[:message],
32
+ :value => value
33
+ )
29
34
  end
30
35
  end
31
36
  end
@@ -14,8 +14,13 @@ module Mongoid #:nodoc:
14
14
  # end
15
15
  class UniquenessValidator < ActiveModel::EachValidator
16
16
  def validate_each(document, attribute, value)
17
- return if document.class.where(attribute => value, :_id.ne => document._id).empty?
18
- document.errors.add(attribute, :taken, :default => options[:message], :value => value)
17
+ return if document.class.where(attribute => value).empty?
18
+ document.errors.add(
19
+ attribute,
20
+ :taken,
21
+ :default => options[:message],
22
+ :value => value
23
+ )
19
24
  end
20
25
  end
21
26
  end
@@ -23,8 +23,8 @@ module Mongoid #:nodoc:
23
23
  #
24
24
  # class Person
25
25
  # include Mongoid::Document
26
- # embeds_one :name
27
- # embeds_many :addresses
26
+ # has_one :name
27
+ # has_many :addresses
28
28
  #
29
29
  # validates_associated :name, :addresses
30
30
  # end
@@ -7,7 +7,7 @@ module Mongoid #:nodoc:
7
7
  extend ActiveSupport::Concern
8
8
  included do
9
9
  field :version, :type => Integer, :default => 1
10
- embeds_many :versions, :class_name => self.name
10
+ has_many :versions, :class_name => self.name
11
11
  before_save :revise
12
12
  end
13
13
  module InstanceMethods
data/lib/mongoid.rb CHANGED
@@ -23,9 +23,10 @@ require "rubygems"
23
23
 
24
24
  gem "activemodel", ">= 3.0.pre"
25
25
  gem "will_paginate", ">= 3.0.pre"
26
- gem "mongo", ">= 0.19.1"
26
+ gem "mongo", ">= 0.18.2"
27
27
 
28
28
  require "delegate"
29
+ require "observer"
29
30
  require "singleton"
30
31
  require "time"
31
32
  require "active_support/core_ext"
@@ -45,6 +46,7 @@ require "active_model/validations"
45
46
  require "will_paginate/collection"
46
47
  require "mongo"
47
48
  require "mongoid/associations"
49
+ require "mongoid/associations/options"
48
50
  require "mongoid/attributes"
49
51
  require "mongoid/callbacks"
50
52
  require "mongoid/collection"
@@ -53,10 +55,7 @@ require "mongoid/config"
53
55
  require "mongoid/contexts"
54
56
  require "mongoid/criteria"
55
57
  require "mongoid/cursor"
56
- require "mongoid/deprecation"
57
- require "mongoid/dirty"
58
58
  require "mongoid/extensions"
59
- require "mongoid/extras"
60
59
  require "mongoid/errors"
61
60
  require "mongoid/factory"
62
61
  require "mongoid/field"
@@ -64,15 +63,10 @@ require "mongoid/fields"
64
63
  require "mongoid/finders"
65
64
  require "mongoid/identity"
66
65
  require "mongoid/indexes"
67
- require "mongoid/javascript"
68
66
  require "mongoid/matchers"
69
67
  require "mongoid/memoization"
70
68
  require "mongoid/named_scope"
71
- require "mongoid/observable"
72
- require "mongoid/paths"
73
- require "mongoid/persistence"
74
69
  require "mongoid/scope"
75
- require "mongoid/state"
76
70
  require "mongoid/timestamps"
77
71
  require "mongoid/validations"
78
72
  require "mongoid/versioning"
@@ -102,20 +96,10 @@ module Mongoid #:nodoc
102
96
  #
103
97
  # The Mongoid +Config+ singleton instance.
104
98
  def configure
105
- config = Mongoid::Config.instance
99
+ config = Config.instance
106
100
  block_given? ? yield(config) : config
107
101
  end
108
102
 
109
- # Easy convenience method for having an alert generated from the
110
- # deprecation module.
111
- #
112
- # Example:
113
- #
114
- # <tt>Mongoid.deprecate("Method no longer used")</tt>
115
- def deprecate(message)
116
- Mongoid::Deprecation.instance.alert(message)
117
- end
118
-
119
103
  alias :config :configure
120
104
  end
121
105
 
@@ -125,7 +109,7 @@ module Mongoid #:nodoc
125
109
  # Example:
126
110
  #
127
111
  # <tt>Mongoid.database = Mongo::Connection.new.db("test")</tt>
128
- Mongoid::Config.public_instance_methods(false).each do |name|
112
+ Config.public_instance_methods(false).each do |name|
129
113
  (class << self; self; end).class_eval <<-EOT
130
114
  def #{name}(*args)
131
115
  configure.send("#{name}", *args)