mongo_mapper-unstable 2010.3.8 → 2010.06.23
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -8
- data/bin/mmconsole +1 -1
- data/examples/keys.rb +37 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +11 -329
- data/lib/mongo_mapper/embedded_document.rb +9 -38
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +5 -6
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +25 -39
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +36 -46
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +5 -4
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +7 -7
- data/lib/mongo_mapper/plugins/associations/proxy.rb +16 -8
- data/lib/mongo_mapper/plugins/associations.rb +14 -22
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +17 -5
- data/lib/mongo_mapper/plugins/clone.rb +10 -4
- data/lib/mongo_mapper/plugins/descendants.rb +3 -2
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/{support/find.rb → plugins/dynamic_querying/dynamic_finder.rb} +3 -36
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +4 -10
- data/lib/mongo_mapper/plugins/identity_map.rb +29 -23
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/keys.rb +85 -110
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +41 -16
- data/lib/mongo_mapper/plugins/pagination.rb +5 -15
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +9 -1
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/rails.rb +6 -1
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +5 -4
- data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +9 -5
- data/lib/mongo_mapper/plugins.rb +1 -20
- data/lib/mongo_mapper/support/descendant_appends.rb +5 -6
- data/lib/mongo_mapper/version.rb +4 -0
- data/lib/mongo_mapper.rb +71 -128
- data/test/{NOTE_ON_TESTING → _NOTE_ON_TESTING} +0 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +5 -5
- data/test/functional/associations/test_belongs_to_proxy.rb +13 -21
- data/test/functional/associations/test_in_array_proxy.rb +7 -9
- data/test/functional/associations/test_many_documents_as_proxy.rb +5 -5
- data/test/functional/associations/test_many_documents_proxy.rb +186 -64
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +22 -22
- data/test/functional/associations/test_many_embedded_proxy.rb +32 -32
- data/test/functional/associations/test_many_polymorphic_proxy.rb +47 -47
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +70 -49
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +11 -11
- data/test/functional/test_binary.rb +5 -5
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +104 -34
- data/test/functional/test_dirty.rb +16 -16
- data/test/functional/test_document.rb +12 -924
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +88 -8
- data/test/functional/test_identity_map.rb +41 -43
- data/test/functional/{test_indexing.rb → test_indexes.rb} +3 -5
- data/test/functional/test_logger.rb +1 -1
- data/test/functional/test_modifiers.rb +275 -181
- data/test/functional/test_pagination.rb +13 -15
- data/test/functional/test_protected.rb +25 -11
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +11 -11
- data/test/functional/test_timestamps.rb +0 -2
- data/test/functional/test_userstamps.rb +0 -1
- data/test/functional/test_validations.rb +44 -31
- data/test/models.rb +18 -17
- data/test/{active_model_lint_test.rb → test_active_model_lint.rb} +3 -1
- data/test/test_helper.rb +59 -16
- data/test/unit/associations/test_base.rb +47 -42
- data/test/unit/associations/test_proxy.rb +15 -15
- data/test/unit/serializers/test_json_serializer.rb +29 -29
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +3 -3
- data/test/unit/test_document.rb +49 -67
- data/test/unit/test_dynamic_finder.rb +53 -51
- data/test/unit/test_embedded_document.rb +19 -38
- data/test/unit/{test_support.rb → test_extensions.rb} +136 -122
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +29 -147
- data/test/unit/test_mongo_mapper.rb +3 -48
- data/test/unit/test_pagination.rb +1 -150
- data/test/unit/test_rails.rb +77 -19
- data/test/unit/test_rails_compatibility.rb +12 -12
- data/test/unit/test_serialization.rb +5 -5
- data/test/unit/test_time_zones.rb +9 -9
- data/test/unit/test_validations.rb +46 -46
- metadata +157 -155
- data/.gitignore +0 -10
- data/Rakefile +0 -55
- data/VERSION +0 -1
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +0 -72
- data/lib/mongo_mapper/query.rb +0 -130
- data/lib/mongo_mapper/support.rb +0 -215
- data/mongo_mapper.gemspec +0 -196
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/support/custom_matchers.rb +0 -55
- data/test/support/timing.rb +0 -16
- data/test/unit/test_query.rb +0 -340
@@ -1,60 +1,31 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module EmbeddedDocument
|
3
4
|
extend Support::DescendantAppends
|
4
5
|
|
5
6
|
def self.included(model)
|
6
7
|
model.class_eval do
|
7
|
-
include InstanceMethods
|
8
|
-
extend ClassMethods
|
9
8
|
extend Plugins
|
10
9
|
|
10
|
+
plugin Plugins::EmbeddedDocument
|
11
11
|
plugin Plugins::Associations
|
12
|
+
plugin Plugins::Caching
|
12
13
|
plugin Plugins::Clone
|
13
14
|
plugin Plugins::Descendants
|
14
15
|
plugin Plugins::Equality
|
15
16
|
plugin Plugins::Inspect
|
16
17
|
plugin Plugins::Keys
|
17
18
|
plugin Plugins::Logger
|
19
|
+
plugin Plugins::Persistence
|
20
|
+
plugin Plugins::Accessible
|
18
21
|
plugin Plugins::Protected
|
19
22
|
plugin Plugins::Rails
|
23
|
+
plugin Plugins::Sci
|
20
24
|
plugin Plugins::Serialization
|
21
25
|
plugin Plugins::Validations
|
22
|
-
|
23
|
-
attr_reader :_root_document, :_parent_document
|
26
|
+
plugin Plugins::Callbacks
|
24
27
|
end
|
25
|
-
|
26
28
|
super
|
27
29
|
end
|
28
|
-
|
29
|
-
|
30
|
-
def embeddable?
|
31
|
-
true
|
32
|
-
end
|
33
|
-
|
34
|
-
def embedded_in(owner_name)
|
35
|
-
define_method(owner_name) { _parent_document }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
module InstanceMethods
|
40
|
-
def save(options={})
|
41
|
-
if result = _root_document.try(:save, options)
|
42
|
-
@new = false
|
43
|
-
end
|
44
|
-
result
|
45
|
-
end
|
46
|
-
|
47
|
-
def save!(options={})
|
48
|
-
if result = _root_document.try(:save!, options)
|
49
|
-
@new = false
|
50
|
-
end
|
51
|
-
result
|
52
|
-
end
|
53
|
-
|
54
|
-
def _parent_document=(value)
|
55
|
-
@_root_document = value._root_document
|
56
|
-
@_parent_document = value
|
57
|
-
end
|
58
|
-
end # InstanceMethods
|
59
|
-
end # EmbeddedDocument
|
60
|
-
end # MongoMapper
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
# generic MM error
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
# raised when key expected to exist but not found
|
7
|
+
class KeyNotFound < Error; end
|
8
|
+
|
9
|
+
# raised when document expected but not found
|
10
|
+
class DocumentNotFound < Error; end
|
11
|
+
|
12
|
+
# raised when trying to connect using uri with incorrect scheme
|
13
|
+
class InvalidScheme < Error; end
|
14
|
+
|
15
|
+
# raised when trying to do something not supported, mostly for edocs
|
16
|
+
class NotSupported < Error; end
|
17
|
+
|
18
|
+
# raised when document not valid and using !
|
19
|
+
class DocumentNotValid < Error
|
20
|
+
def initialize(document)
|
21
|
+
super("Validation failed: #{document.errors.full_messages.join(", ")}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class AccessibleOrProtected < Error
|
26
|
+
def initialize(name)
|
27
|
+
super("Declare either attr_protected or attr_accessible for #{name}, but not both.")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Array
|
5
|
+
def to_mongo(value)
|
6
|
+
value = value.respond_to?(:lines) ? value.lines : value
|
7
|
+
value.to_a
|
8
|
+
end
|
9
|
+
|
10
|
+
def from_mongo(value)
|
11
|
+
value || []
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Array
|
18
|
+
extend MongoMapper::Extensions::Array
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Binary
|
5
|
+
def to_mongo(value)
|
6
|
+
if value.is_a?(::BSON::Binary)
|
7
|
+
value
|
8
|
+
else
|
9
|
+
value.nil? ? nil : ::BSON::Binary.new(value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def from_mongo(value)
|
14
|
+
value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Binary
|
21
|
+
extend MongoMapper::Extensions::Binary
|
22
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Boolean
|
5
|
+
Mapping = {
|
6
|
+
true => true,
|
7
|
+
'true' => true,
|
8
|
+
'TRUE' => true,
|
9
|
+
'True' => true,
|
10
|
+
't' => true,
|
11
|
+
'T' => true,
|
12
|
+
'1' => true,
|
13
|
+
1 => true,
|
14
|
+
1.0 => true,
|
15
|
+
false => false,
|
16
|
+
'false' => false,
|
17
|
+
'FALSE' => false,
|
18
|
+
'False' => false,
|
19
|
+
'f' => false,
|
20
|
+
'F' => false,
|
21
|
+
'0' => false,
|
22
|
+
0 => false,
|
23
|
+
0.0 => false,
|
24
|
+
nil => nil
|
25
|
+
}
|
26
|
+
|
27
|
+
def to_mongo(value)
|
28
|
+
if value.is_a?(Boolean)
|
29
|
+
value
|
30
|
+
else
|
31
|
+
Mapping[value]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def from_mongo(value)
|
36
|
+
value.nil? ? nil : !!value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Boolean
|
43
|
+
extend MongoMapper::Extensions::Boolean
|
44
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Date
|
5
|
+
def to_mongo(value)
|
6
|
+
if value.nil? || value == ''
|
7
|
+
nil
|
8
|
+
else
|
9
|
+
date = value.is_a?(::Date) || value.is_a?(::Time) ? value : ::Date.parse(value.to_s)
|
10
|
+
::Time.utc(date.year, date.month, date.day)
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def from_mongo(value)
|
17
|
+
value.to_date if value.present?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Date
|
24
|
+
extend MongoMapper::Extensions::Date
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Integer
|
5
|
+
def to_mongo(value)
|
6
|
+
value_to_i = value.to_i
|
7
|
+
if value_to_i == 0 && value != value_to_i
|
8
|
+
value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
|
9
|
+
else
|
10
|
+
value_to_i
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Integer
|
18
|
+
extend MongoMapper::Extensions::Integer
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module NilClass
|
5
|
+
def to_mongo(value)
|
6
|
+
value
|
7
|
+
end
|
8
|
+
|
9
|
+
def from_mongo(value)
|
10
|
+
value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class NilClass
|
17
|
+
include MongoMapper::Extensions::NilClass
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Object
|
5
|
+
module ClassMethods
|
6
|
+
def to_mongo(value)
|
7
|
+
value
|
8
|
+
end
|
9
|
+
|
10
|
+
def from_mongo(value)
|
11
|
+
value
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
def to_mongo
|
17
|
+
self.class.to_mongo(self)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Object
|
25
|
+
extend MongoMapper::Extensions::Object::ClassMethods
|
26
|
+
include MongoMapper::Extensions::Object::InstanceMethods
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module ObjectId
|
5
|
+
def to_mongo(value)
|
6
|
+
Plucky.to_object_id(value)
|
7
|
+
end
|
8
|
+
|
9
|
+
def from_mongo(value)
|
10
|
+
value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ObjectId
|
17
|
+
extend MongoMapper::Extensions::ObjectId
|
18
|
+
end
|
19
|
+
|
20
|
+
class BSON::ObjectID
|
21
|
+
alias_method :original_to_json, :to_json
|
22
|
+
|
23
|
+
def as_json(options=nil)
|
24
|
+
to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_json(options = nil)
|
28
|
+
as_json.to_json
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
module MongoMapper
|
5
|
+
module Extensions
|
6
|
+
module Set
|
7
|
+
def to_mongo(value)
|
8
|
+
value.to_a
|
9
|
+
end
|
10
|
+
|
11
|
+
def from_mongo(value)
|
12
|
+
(value || []).to_set
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Set
|
19
|
+
extend MongoMapper::Extensions::Set
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module String
|
5
|
+
def to_mongo(value)
|
6
|
+
value.nil? ? nil : value.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def from_mongo(value)
|
10
|
+
value.nil? ? nil : value.to_s
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class String
|
17
|
+
extend MongoMapper::Extensions::String
|
18
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Time
|
5
|
+
def to_mongo(value)
|
6
|
+
if value.nil? || value == ''
|
7
|
+
nil
|
8
|
+
else
|
9
|
+
time_class = ::Time.try(:zone).present? ? ::Time.zone : ::Time
|
10
|
+
time = value.is_a?(::Time) ? value : time_class.parse(value.to_s)
|
11
|
+
# strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
|
12
|
+
at(time.to_i).utc if time
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def from_mongo(value)
|
17
|
+
if ::Time.try(:zone).present? && value.present?
|
18
|
+
value.in_time_zone(::Time.zone)
|
19
|
+
else
|
20
|
+
value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Time
|
28
|
+
extend MongoMapper::Extensions::Time
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Plugins
|
3
|
+
module Accessible
|
4
|
+
module ClassMethods
|
5
|
+
def attr_accessible(*attrs)
|
6
|
+
raise AccessibleOrProtected.new(name) if try(:protected_attributes?)
|
7
|
+
self.write_inheritable_attribute(:attr_accessible, Set.new(attrs) + (accessible_attributes || []))
|
8
|
+
end
|
9
|
+
|
10
|
+
def accessible_attributes?
|
11
|
+
!accessible_attributes.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def accessible_attributes
|
15
|
+
self.read_inheritable_attribute(:attr_accessible)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def assign(attrs={})
|
21
|
+
super(filter_inaccessible_attrs(attrs))
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_attributes(attrs={})
|
25
|
+
super(filter_inaccessible_attrs(attrs))
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_attributes!(attrs={})
|
29
|
+
super(filter_inaccessible_attrs(attrs))
|
30
|
+
end
|
31
|
+
|
32
|
+
def accessible_attributes
|
33
|
+
self.class.accessible_attributes
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
def filter_inaccessible_attrs(attrs)
|
38
|
+
return attrs if accessible_attributes.blank? || attrs.blank?
|
39
|
+
attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Associations
|
@@ -16,8 +17,8 @@ module MongoMapper
|
|
16
17
|
|
17
18
|
def class_name
|
18
19
|
return @class_name if defined?(@class_name)
|
19
|
-
|
20
|
-
@class_name =
|
20
|
+
|
21
|
+
@class_name =
|
21
22
|
if cn = options[:class_name]
|
22
23
|
cn
|
23
24
|
elsif many?
|
@@ -56,7 +57,7 @@ module MongoMapper
|
|
56
57
|
end
|
57
58
|
|
58
59
|
def embeddable?
|
59
|
-
many? && klass.embeddable?
|
60
|
+
(one? || many?) && klass.embeddable?
|
60
61
|
end
|
61
62
|
|
62
63
|
def type_key_name
|
@@ -78,8 +79,8 @@ module MongoMapper
|
|
78
79
|
# hate this, need to revisit
|
79
80
|
def proxy_class
|
80
81
|
return @proxy_class if defined?(@proxy_class)
|
81
|
-
|
82
|
-
@proxy_class =
|
82
|
+
|
83
|
+
@proxy_class =
|
83
84
|
if many?
|
84
85
|
if klass.embeddable?
|
85
86
|
polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
|
@@ -95,7 +96,7 @@ module MongoMapper
|
|
95
96
|
end
|
96
97
|
end
|
97
98
|
elsif one?
|
98
|
-
OneProxy
|
99
|
+
klass.embeddable? ? OneEmbeddedProxy : OneProxy
|
99
100
|
else
|
100
101
|
polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
|
101
102
|
end
|
@@ -1,24 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Associations
|
4
5
|
class BelongsToPolymorphicProxy < Proxy
|
5
|
-
undef_method :object_id
|
6
|
-
|
7
6
|
def replace(doc)
|
8
7
|
if doc
|
9
8
|
doc.save if doc.new?
|
10
9
|
id, type = doc.id, doc.class.name
|
11
10
|
end
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
proxy_owner[association.foreign_key] = id
|
13
|
+
proxy_owner[association.type_key_name] = type
|
15
14
|
reset
|
16
15
|
end
|
17
16
|
|
18
17
|
protected
|
19
18
|
def find_target
|
20
|
-
return nil if association_class.nil? ||
|
21
|
-
association_class.find_by_id(
|
19
|
+
return nil if association_class.nil? || proxy_owner[association.foreign_key].nil?
|
20
|
+
association_class.find_by_id(proxy_owner[association.foreign_key])
|
22
21
|
end
|
23
22
|
|
24
23
|
def association_class
|
@@ -1,23 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Associations
|
4
5
|
class BelongsToProxy < Proxy
|
5
|
-
undef_method :object_id
|
6
|
-
|
7
6
|
def replace(doc)
|
8
7
|
if doc
|
9
8
|
doc.save if doc.new?
|
10
9
|
id = doc.id
|
11
10
|
end
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
proxy_owner[association.foreign_key] = id
|
13
|
+
reload
|
15
14
|
end
|
16
15
|
|
17
16
|
protected
|
18
17
|
def find_target
|
19
|
-
return nil if
|
20
|
-
klass.find_by_id(
|
18
|
+
return nil if proxy_owner[association.foreign_key].nil?
|
19
|
+
klass.find_by_id(proxy_owner[association.foreign_key])
|
21
20
|
end
|
22
21
|
end
|
23
22
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Associations
|
@@ -31,7 +32,7 @@ module MongoMapper
|
|
31
32
|
|
32
33
|
private
|
33
34
|
def assign_references(*docs)
|
34
|
-
docs.each { |doc| doc._parent_document =
|
35
|
+
docs.each { |doc| doc._parent_document = proxy_owner }
|
35
36
|
end
|
36
37
|
end
|
37
38
|
end
|