mongoid 2.0.0.beta.13 → 2.0.0.beta.14

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.
@@ -33,6 +33,7 @@ require "active_model/callbacks"
33
33
  require "active_model/conversion"
34
34
  require "active_model/deprecated_error_methods"
35
35
  require "active_model/errors"
36
+ require "active_model/mass_assignment_security"
36
37
  require "active_model/naming"
37
38
  require "active_model/serialization"
38
39
  require "active_model/translation"
@@ -10,7 +10,7 @@ module Mongoid #:nodoc:
10
10
  def <<(*objects)
11
11
  load_target
12
12
  objects.flatten.each do |object|
13
- object.send("#{@foreign_key}=", @parent.id)
13
+ object.write_attribute(@foreign_key, @parent.id)
14
14
  @target << object
15
15
  object.save unless @parent.new_record?
16
16
  end
@@ -27,7 +27,8 @@ module Mongoid #:nodoc:
27
27
  def build(attributes = nil)
28
28
  load_target
29
29
  name = determine_name
30
- object = @klass.instantiate((attributes || {}).merge(name => @parent))
30
+ object = @klass.instantiate(attributes || {})
31
+ object.send("#{name}=", @parent)
31
32
  @target << object
32
33
  object
33
34
  end
@@ -125,15 +126,16 @@ module Mongoid #:nodoc:
125
126
  def nested_build(attributes, options = {})
126
127
  attributes.each do |index, attrs|
127
128
  begin
128
- document = find(index.to_i)
129
- if options && options[:allow_destroy] && attrs['_destroy']
129
+ _destroy = Boolean.set(attrs.delete('_destroy'))
130
+ document = find(attrs.delete("id"))
131
+ if options && options[:allow_destroy] && _destroy
130
132
  @target.delete(document)
131
133
  document.destroy
132
134
  else
133
- document.write_attributes(attrs)
135
+ document.update_attributes(attrs)
134
136
  end
135
137
  rescue Errors::DocumentNotFound
136
- build(attrs)
138
+ create(attrs)
137
139
  end
138
140
  end
139
141
  end
@@ -2,10 +2,6 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Attributes
4
4
  extend ActiveSupport::Concern
5
- included do
6
- class_inheritable_accessor :_protected_fields
7
- self._protected_fields = []
8
- end
9
5
 
10
6
  # Get the id associated with this object. This will pull the _id value out
11
7
  # of the attributes +Hash+.
@@ -38,10 +34,10 @@ module Mongoid #:nodoc:
38
34
  # attributes provided in the suppied +Hash+ so that no extra nil values get
39
35
  # put into the document's attributes.
40
36
  def process(attrs = nil)
41
- (attrs || {}).each_pair do |key, value|
37
+ sanitize_for_mass_assignment(attrs || {}).each_pair do |key, value|
42
38
  if set_allowed?(key)
43
39
  write_attribute(key, value)
44
- elsif write_allowed?(key)
40
+ else
45
41
  if associations.include?(key.to_s) and associations[key.to_s].embedded? and value.is_a?(Hash)
46
42
  if association = send(key)
47
43
  association.nested_build(value)
@@ -188,12 +184,6 @@ module Mongoid #:nodoc:
188
184
  end
189
185
  end
190
186
 
191
- # Return true if writing to the given field is allowed
192
- def write_allowed?(key)
193
- name = key.to_s
194
- !self._protected_fields.include?(name)
195
- end
196
-
197
187
  module ClassMethods
198
188
  # Defines attribute setters for the associations specified by the names.
199
189
  # This will work for a has one or has many association.
@@ -224,19 +214,6 @@ module Mongoid #:nodoc:
224
214
  end
225
215
  end
226
216
  end
227
-
228
- # Defines fields that cannot be set via mass assignment.
229
- #
230
- # Example:
231
- #
232
- # class Person
233
- # include Mongoid::Document
234
- # field :security_code
235
- # attr_protected :security_code
236
- # end
237
- def attr_protected(*names)
238
- _protected_fields.concat(names.flatten.map(&:to_s))
239
- end
240
217
  end
241
218
  end
242
219
  end
@@ -8,6 +8,7 @@ module Mongoid #:nodoc
8
8
  include ActiveModel::Conversion
9
9
  include ActiveModel::Naming
10
10
  include ActiveModel::Serialization
11
+ include ActiveModel::MassAssignmentSecurity
11
12
  include ActiveModel::Serializers::JSON
12
13
  include ActiveModel::Serializers::Xml
13
14
  include Mongoid::Associations
@@ -63,10 +63,12 @@ module Mongoid #:nodoc:
63
63
  return args if !using_object_ids? || args.is_a?(BSON::ObjectID) || !cast
64
64
  if args.is_a?(String)
65
65
  BSON::ObjectID(args)
66
- else
66
+ elsif args.is_a?(Array)
67
67
  args.map{ |a|
68
68
  a.is_a?(BSON::ObjectID) ? a : BSON::ObjectID(a)
69
69
  }
70
+ else
71
+ args
70
72
  end
71
73
  end
72
74
 
@@ -3,15 +3,6 @@ module Mongoid #:nodoc:
3
3
  class Field
4
4
  attr_reader :name, :type
5
5
 
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
6
  # Get the declared options for this field
16
7
  #
17
8
  # Returns:
@@ -46,7 +37,6 @@ module Mongoid #:nodoc:
46
37
  @type = options[:type] || String
47
38
  @name, @default = name, options[:default]
48
39
  @copyable = (@default.is_a?(Array) || @default.is_a?(Hash))
49
- @accessible = options.has_key?(:accessible) ? options[:accessible] : true
50
40
  @options = options
51
41
  check_default!
52
42
  end
@@ -26,16 +26,12 @@ module Mongoid #:nodoc:
26
26
  def persist
27
27
  return @document if @validate && @document.invalid?(:create)
28
28
  parent = @document._parent
29
- @document.run_callbacks(:create) do
30
- @document.run_callbacks(:save) do
31
- if parent.new_record?
32
- parent.insert
33
- else
34
- update = { @document._inserter => { @document._position => @document.raw_attributes } }
35
- @collection.update(parent._selector, update, @options.merge(:multi => false))
36
- @document.new_record = false
37
- end
38
- end
29
+ if parent.new_record?
30
+ parent.insert
31
+ else
32
+ update = { @document._inserter => { @document._position => @document.raw_attributes } }
33
+ @collection.update(parent._selector, update, @options.merge(:multi => false))
34
+ @document.new_record = false
39
35
  end
40
36
  @document
41
37
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
- VERSION = "2.0.0.beta.13"
3
+ VERSION = "2.0.0.beta.14"
4
4
  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: 62196473
4
+ hash: 62196479
5
5
  prerelease: true
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
9
  - 0
10
10
  - beta
11
- - 13
12
- version: 2.0.0.beta.13
11
+ - 14
12
+ version: 2.0.0.beta.14
13
13
  platform: ruby
14
14
  authors:
15
15
  - Durran Jordan