mongoid 2.0.0.beta.19 → 2.0.0.beta.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -193,12 +193,16 @@ module Mongoid # :nodoc:
193
193
  # Options:
194
194
  #
195
195
  # name: A +Symbol+ that is the related class name pluralized.
196
+ # default_order: A +Criteria+ that specifies the default sort order for
197
+ # this association. (e.g. :position.asc). If an explicit ordering is
198
+ # specified on a +Criteria+ object, the default order will NOT be used.
196
199
  #
197
200
  # Example:
198
201
  #
199
202
  # class Person
200
203
  # include Mongoid::Document
201
204
  # references_many :posts
205
+ # references_many :board_games, :default_order => :title.asc
202
206
  # end
203
207
  #
204
208
  def references_many(name, options = {}, &block)
@@ -68,6 +68,11 @@ module Mongoid #:nodoc:
68
68
  def stored_as
69
69
  self[:stored_as]
70
70
  end
71
+
72
+ # Used with references_many to define a default sorting order
73
+ def default_order
74
+ self[:default_order]
75
+ end
71
76
  end
72
77
  end
73
78
  end
@@ -24,10 +24,10 @@ module Mongoid #:nodoc:
24
24
  # association, and the attributes will be passed into the constructor.
25
25
  #
26
26
  # Returns the newly created object.
27
- def build(attributes = nil)
27
+ def build(attributes = {},type = nil)
28
28
  load_target
29
29
  name = determine_name
30
- object = @klass.instantiate(attributes || {})
30
+ object = (type || @klass).instantiate(attributes)
31
31
  object.send("#{name}=", @parent)
32
32
  @target << object
33
33
  object
@@ -39,16 +39,16 @@ module Mongoid #:nodoc:
39
39
  # the new object will then be saved.
40
40
  #
41
41
  # Returns the newly created object.
42
- def create(attributes = nil)
43
- build(attributes).tap(&:save)
42
+ def create(attributes = nil,type = nil)
43
+ build(attributes,type).tap(&:save)
44
44
  end
45
45
 
46
46
  # Creates a new Document and adds it to the association collection. If
47
47
  # validation fails an error is raised.
48
48
  #
49
49
  # Returns the newly created object.
50
- def create!(attributes = nil)
51
- build(attributes).tap(&:save!)
50
+ def create!(attributes = nil,type = nil)
51
+ build(attributes,type).tap(&:save!)
52
52
  end
53
53
 
54
54
  # Delete all the associated objects.
@@ -166,7 +166,11 @@ module Mongoid #:nodoc:
166
166
  # An +Array+ of objects if an ActiveRecord association
167
167
  # A +Collection+ if a DataMapper association.
168
168
  def query
169
- @query ||= lambda { @klass.all(:conditions => { @foreign_key => @parent.id }) }
169
+ @query ||= lambda {
170
+ @klass.all(:conditions => { @foreign_key => @parent.id }).tap do |crit|
171
+ crit.set_order_by(@options.default_order) if @options.default_order
172
+ end
173
+ }
170
174
  end
171
175
 
172
176
  # Remove the objects based on conditions.
@@ -45,9 +45,9 @@ module Mongoid #:nodoc:
45
45
  # association, and the attributes will be passed into the constructor.
46
46
  #
47
47
  # Returns the newly created object.
48
- def build(attributes = nil)
48
+ def build(attributes = {},type=nil)
49
49
  load_target
50
- document = @klass.instantiate(attributes || {})
50
+ document = (type || @klass).instantiate(attributes)
51
51
  push(document); document
52
52
  end
53
53
 
@@ -83,7 +83,7 @@ module Mongoid #:nodoc
83
83
  #
84
84
  # Options:
85
85
  #
86
- # map: The map javascript funcdtion.
86
+ # map: The map javascript function.
87
87
  # reduce: The reduce javascript function.
88
88
  def map_reduce(map, reduce, options = {})
89
89
  directed(options).map_reduce(map, reduce, options)
@@ -103,7 +103,10 @@ module Mongoid #:nodoc
103
103
  #
104
104
  # The master +Mongo::DB+
105
105
  def master
106
- raise Errors::InvalidDatabase.new(nil) unless @master
106
+ unless @master
107
+ _master(@settings) if @settings
108
+ raise Errors::InvalidDatabase.new(nil) unless @master
109
+ end
107
110
  if @reconnect
108
111
  @reconnect = false
109
112
  reconnect!
@@ -142,6 +145,7 @@ module Mongoid #:nodoc
142
145
  #
143
146
  # The slave +Mongo::DBs+
144
147
  def slaves
148
+ _slaves(@settings) unless @slaves || !@settings
145
149
  @slaves
146
150
  end
147
151
 
@@ -194,8 +198,7 @@ module Mongoid #:nodoc
194
198
  settings.except("database", "slaves").each_pair do |name, value|
195
199
  send("#{name}=", value) if respond_to?("#{name}=")
196
200
  end
197
- _master(settings)
198
- _slaves(settings)
201
+ @settings = settings.dup
199
202
  end
200
203
 
201
204
  # Adds a new I18n locale file to the load path
@@ -16,6 +16,19 @@ module Mongoid #:nodoc:
16
16
  def initialize(opts = {})
17
17
  @key, @operator = opts[:key], opts[:operator]
18
18
  end
19
+
20
+ def hash
21
+ [@key, @operator].hash
22
+ end
23
+
24
+ def eql?(other)
25
+ self == (other)
26
+ end
27
+
28
+ def ==(other)
29
+ return false unless other.is_a?(self.class)
30
+ self.key == other.key && self.operator == other.operator
31
+ end
19
32
  end
20
33
  end
21
34
  end
@@ -3,6 +3,16 @@ module Mongoid #:nodoc:
3
3
  module Criterion #:nodoc:
4
4
  module Optional
5
5
 
6
+ def using_default_sort?
7
+ @use_default_sort = true if @use_default_sort.nil? # TODO: move initialization elsewhere
8
+ return @use_default_sort
9
+ end
10
+
11
+ def remove_default_sort
12
+ @options[:sort] = nil if using_default_sort?
13
+ @use_default_sort = false
14
+ end
15
+
6
16
  # Adds fields to be sorted in ascending order. Will add them in the order
7
17
  # they were passed into the method.
8
18
  #
@@ -10,6 +20,8 @@ module Mongoid #:nodoc:
10
20
  #
11
21
  # <tt>criteria.ascending(:title, :dob)</tt>
12
22
  def ascending(*fields)
23
+ remove_default_sort
24
+
13
25
  @options[:sort] = [] unless @options[:sort] || fields.first.nil?
14
26
  fields.flatten.each { |field| @options[:sort] << [ field, :asc ] }
15
27
  self
@@ -45,6 +57,8 @@ module Mongoid #:nodoc:
45
57
  #
46
58
  # <tt>criteria.descending(:title, :dob)</tt>
47
59
  def descending(*fields)
60
+ remove_default_sort
61
+
48
62
  @options[:sort] = [] unless @options[:sort] || fields.first.nil?
49
63
  fields.flatten.each { |field| @options[:sort] << [ field, :desc ] }
50
64
  self
@@ -149,6 +163,11 @@ module Mongoid #:nodoc:
149
163
  #
150
164
  # Returns: <tt>self</tt>
151
165
  def order_by(*args)
166
+ remove_default_sort
167
+ set_order_by(*args)
168
+ end
169
+
170
+ def set_order_by(*args)
152
171
  @options[:sort] = [] unless @options[:sort] || args.first.nil?
153
172
  arguments = args.first
154
173
  case arguments
@@ -200,6 +219,7 @@ module Mongoid #:nodoc:
200
219
  types = [types] unless types.is_a?(Array)
201
220
  self.in(:_type => types)
202
221
  end
222
+
203
223
  end
204
224
  end
205
225
  end
@@ -94,7 +94,7 @@ module Mongoid #:nodoc:
94
94
  # an empty +Hash+.
95
95
  #
96
96
  # If a primary key is defined, the document's id will be set to that key,
97
- # otherwise it will be set to a fresh +BSON::ObjectID+ string.
97
+ # otherwise it will be set to a fresh +BSON::ObjectId+ string.
98
98
  #
99
99
  # Options:
100
100
  #
@@ -114,7 +114,6 @@ end
114
114
 
115
115
  class BSON::ObjectId #:nodoc
116
116
  extend Mongoid::Extensions::ObjectId::Conversions
117
-
118
117
  def as_json(options = nil)
119
118
  to_s
120
119
  end
@@ -8,7 +8,8 @@ module Mongoid #:nodoc:
8
8
  end
9
9
  # Return true if the attribute and value are equal.
10
10
  def matches?(value)
11
- value === @attribute
11
+ @attribute.is_a?(Array) && value.is_a?(String) ?
12
+ @attribute.include?(value) : value === @attribute
12
13
  end
13
14
 
14
15
  protected
@@ -54,7 +54,7 @@ module Mongoid #:nodoc:
54
54
  # ensure :case_sensitive is true by default
55
55
  def unique_search_value(value)
56
56
  if options[:case_sensitive] == false
57
- value ? /^#{value}$/i : nil
57
+ value ? Regexp.new("^#{Regexp.escape(value.to_s)}$", Regexp::IGNORECASE) : nil
58
58
  else
59
59
  value
60
60
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
- VERSION = "2.0.0.beta.19"
3
+ VERSION = "2.0.0.beta.20"
4
4
  end
@@ -14,6 +14,8 @@ module Mongoid #:nodoc:
14
14
 
15
15
  module ClassMethods #:nodoc:
16
16
  attr_accessor :version_max
17
+
18
+ # the number of max_version should >= 0
17
19
  def max_versions(number)
18
20
  self.version_max = number.to_i
19
21
  end
@@ -26,10 +28,14 @@ module Mongoid #:nodoc:
26
28
  def revise
27
29
  last_version = self.class.first(:conditions => { :_id => id, :version => version })
28
30
  if last_version
31
+ old_versions = ( @attributes['versions'].duplicable? ? @attributes['versions'].dup : nil )
29
32
  self.versions << last_version.clone
30
- self.versions.shift if self.class.version_max.present? && self.versions.length > self.class.version_max
33
+ if self.class.version_max.present? && ( self.class.version_max >= 0 ) && ( self.versions.length > self.class.version_max )
34
+ self.versions.shift
35
+ @attributes['versions'].shift
36
+ end
31
37
  self.version = (version || 1 ) + 1
32
- @modifications["versions"] = [ nil, @attributes["versions"] ] if @modifications
38
+ @modifications["versions"] = [ old_versions, @attributes['versions'] ] if @modifications
33
39
  end
34
40
  end
35
41
  end
data/lib/rails/mongoid.rb CHANGED
@@ -7,7 +7,7 @@ module Rails #:nodoc:
7
7
  # that indexing and inheritance work in both development and production
8
8
  # with the same results.
9
9
  def load_models(app)
10
- app.config.paths.app.models.each do |path|
10
+ app.config.paths['app/models'].each do |path|
11
11
  Dir.glob("#{path}/**/*.rb").sort.each do |file|
12
12
  require_dependency(file)
13
13
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 62196421
4
+ hash: 62196427
5
5
  prerelease: true
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
9
  - 0
10
10
  - beta
11
- - 19
12
- version: 2.0.0.beta.19
11
+ - 20
12
+ version: 2.0.0.beta.20
13
13
  platform: ruby
14
14
  authors:
15
15
  - Durran Jordan
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-09-29 00:00:00 -07:00
20
+ date: 2010-11-02 00:00:00 -07:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -71,14 +71,13 @@ dependencies:
71
71
  version_requirements: &id004 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
- - - "="
74
+ - - ~>
75
75
  - !ruby/object:Gem::Version
76
- hash: 5
76
+ hash: 13
77
77
  segments:
78
78
  - 1
79
- - 0
80
- - 9
81
- version: 1.0.9
79
+ - 1
80
+ version: "1.1"
82
81
  requirement: *id004
83
82
  type: :runtime
84
83
  name: mongo
@@ -87,14 +86,13 @@ dependencies:
87
86
  version_requirements: &id005 !ruby/object:Gem::Requirement
88
87
  none: false
89
88
  requirements:
90
- - - "="
89
+ - - ~>
91
90
  - !ruby/object:Gem::Version
92
- hash: 5
91
+ hash: 13
93
92
  segments:
94
93
  - 1
95
- - 0
96
- - 9
97
- version: 1.0.9
94
+ - 1
95
+ version: "1.1"
98
96
  requirement: *id005
99
97
  type: :development
100
98
  name: bson_ext
@@ -121,14 +119,12 @@ dependencies:
121
119
  requirements:
122
120
  - - "="
123
121
  - !ruby/object:Gem::Version
124
- hash: 62196431
122
+ hash: 13
125
123
  segments:
126
124
  - 2
127
125
  - 0
128
- - 0
129
- - beta
130
- - 22
131
- version: 2.0.0.beta.22
126
+ - 1
127
+ version: 2.0.1
132
128
  requirement: *id007
133
129
  type: :development
134
130
  name: rspec