mark_mapper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.rdoc +39 -0
- data/examples/attr_accessible.rb +24 -0
- data/examples/attr_protected.rb +24 -0
- data/examples/cache_key.rb +26 -0
- data/examples/custom_types.rb +26 -0
- data/examples/identity_map.rb +30 -0
- data/examples/identity_map/automatic.rb +2 -0
- data/examples/keys.rb +42 -0
- data/examples/modifiers/set.rb +27 -0
- data/examples/plugins.rb +40 -0
- data/examples/querying.rb +39 -0
- data/examples/sample_app.rb +43 -0
- data/examples/scopes.rb +56 -0
- data/examples/validating/embedded_docs.rb +31 -0
- data/lib/mark_mapper.rb +125 -0
- data/lib/mark_mapper/config.rb +90 -0
- data/lib/mark_mapper/connection.rb +60 -0
- data/lib/mark_mapper/criteria_hash.rb +194 -0
- data/lib/mark_mapper/document.rb +46 -0
- data/lib/mark_mapper/embedded_document.rb +32 -0
- data/lib/mark_mapper/exceptions.rb +33 -0
- data/lib/mark_mapper/extensions/array.rb +27 -0
- data/lib/mark_mapper/extensions/boolean.rb +45 -0
- data/lib/mark_mapper/extensions/date.rb +29 -0
- data/lib/mark_mapper/extensions/duplicable.rb +86 -0
- data/lib/mark_mapper/extensions/float.rb +18 -0
- data/lib/mark_mapper/extensions/hash.rb +26 -0
- data/lib/mark_mapper/extensions/integer.rb +27 -0
- data/lib/mark_mapper/extensions/kernel.rb +11 -0
- data/lib/mark_mapper/extensions/nil_class.rb +18 -0
- data/lib/mark_mapper/extensions/object.rb +30 -0
- data/lib/mark_mapper/extensions/object_id.rb +18 -0
- data/lib/mark_mapper/extensions/set.rb +20 -0
- data/lib/mark_mapper/extensions/string.rb +31 -0
- data/lib/mark_mapper/extensions/symbol.rb +87 -0
- data/lib/mark_mapper/extensions/time.rb +29 -0
- data/lib/mark_mapper/locale/en.yml +5 -0
- data/lib/mark_mapper/middleware/identity_map.rb +41 -0
- data/lib/mark_mapper/normalizers/criteria_hash_key.rb +17 -0
- data/lib/mark_mapper/normalizers/criteria_hash_value.rb +66 -0
- data/lib/mark_mapper/normalizers/fields_value.rb +26 -0
- data/lib/mark_mapper/normalizers/hash_key.rb +19 -0
- data/lib/mark_mapper/normalizers/integer.rb +19 -0
- data/lib/mark_mapper/normalizers/options_hash_value.rb +83 -0
- data/lib/mark_mapper/normalizers/sort_value.rb +55 -0
- data/lib/mark_mapper/options_hash.rb +103 -0
- data/lib/mark_mapper/pagination.rb +6 -0
- data/lib/mark_mapper/pagination/collection.rb +32 -0
- data/lib/mark_mapper/pagination/paginator.rb +46 -0
- data/lib/mark_mapper/plugins.rb +22 -0
- data/lib/mark_mapper/plugins/accessible.rb +61 -0
- data/lib/mark_mapper/plugins/active_model.rb +18 -0
- data/lib/mark_mapper/plugins/associations.rb +96 -0
- data/lib/mark_mapper/plugins/associations/base.rb +98 -0
- data/lib/mark_mapper/plugins/associations/belongs_to_association.rb +63 -0
- data/lib/mark_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +35 -0
- data/lib/mark_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
- data/lib/mark_mapper/plugins/associations/collection.rb +29 -0
- data/lib/mark_mapper/plugins/associations/embedded_collection.rb +44 -0
- data/lib/mark_mapper/plugins/associations/in_array_proxy.rb +133 -0
- data/lib/mark_mapper/plugins/associations/many_association.rb +63 -0
- data/lib/mark_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mark_mapper/plugins/associations/many_documents_proxy.rb +142 -0
- data/lib/mark_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mark_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mark_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mark_mapper/plugins/associations/one_as_proxy.rb +22 -0
- data/lib/mark_mapper/plugins/associations/one_association.rb +48 -0
- data/lib/mark_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
- data/lib/mark_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
- data/lib/mark_mapper/plugins/associations/one_proxy.rb +95 -0
- data/lib/mark_mapper/plugins/associations/proxy.rb +138 -0
- data/lib/mark_mapper/plugins/associations/single_association.rb +46 -0
- data/lib/mark_mapper/plugins/caching.rb +21 -0
- data/lib/mark_mapper/plugins/callbacks.rb +42 -0
- data/lib/mark_mapper/plugins/clone.rb +24 -0
- data/lib/mark_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mark_mapper/plugins/dirty.rb +61 -0
- data/lib/mark_mapper/plugins/document.rb +41 -0
- data/lib/mark_mapper/plugins/dumpable.rb +22 -0
- data/lib/mark_mapper/plugins/dynamic_querying.rb +45 -0
- data/lib/mark_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mark_mapper/plugins/embedded_callbacks.rb +81 -0
- data/lib/mark_mapper/plugins/embedded_document.rb +53 -0
- data/lib/mark_mapper/plugins/equality.rb +23 -0
- data/lib/mark_mapper/plugins/identity_map.rb +144 -0
- data/lib/mark_mapper/plugins/indexable.rb +86 -0
- data/lib/mark_mapper/plugins/inspect.rb +16 -0
- data/lib/mark_mapper/plugins/keys.rb +470 -0
- data/lib/mark_mapper/plugins/keys/key.rb +134 -0
- data/lib/mark_mapper/plugins/keys/static.rb +45 -0
- data/lib/mark_mapper/plugins/logger.rb +18 -0
- data/lib/mark_mapper/plugins/modifiers.rb +140 -0
- data/lib/mark_mapper/plugins/pagination.rb +16 -0
- data/lib/mark_mapper/plugins/partial_updates.rb +77 -0
- data/lib/mark_mapper/plugins/persistence.rb +79 -0
- data/lib/mark_mapper/plugins/protected.rb +45 -0
- data/lib/mark_mapper/plugins/querying.rb +173 -0
- data/lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb +75 -0
- data/lib/mark_mapper/plugins/rails.rb +79 -0
- data/lib/mark_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
- data/lib/mark_mapper/plugins/sci.rb +82 -0
- data/lib/mark_mapper/plugins/scopes.rb +28 -0
- data/lib/mark_mapper/plugins/serialization.rb +109 -0
- data/lib/mark_mapper/plugins/timestamps.rb +29 -0
- data/lib/mark_mapper/plugins/touch.rb +18 -0
- data/lib/mark_mapper/plugins/userstamps.rb +18 -0
- data/lib/mark_mapper/plugins/validations.rb +96 -0
- data/lib/mark_mapper/query.rb +278 -0
- data/lib/mark_mapper/railtie.rb +52 -0
- data/lib/mark_mapper/railtie/database.rake +65 -0
- data/lib/mark_mapper/translation.rb +10 -0
- data/lib/mark_mapper/version.rb +4 -0
- data/lib/rails/generators/mark_mapper/config/config_generator.rb +37 -0
- data/lib/rails/generators/mark_mapper/config/templates/marklogic.yml +19 -0
- data/lib/rails/generators/mark_mapper/model/model_generator.rb +40 -0
- data/lib/rails/generators/mark_mapper/model/templates/model.rb +17 -0
- data/spec/config/mark_mapper.yml +6 -0
- data/spec/examples_spec.rb +25 -0
- data/spec/functional/accessible_spec.rb +198 -0
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +64 -0
- data/spec/functional/associations/belongs_to_proxy_spec.rb +255 -0
- data/spec/functional/associations/in_array_proxy_spec.rb +349 -0
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +230 -0
- data/spec/functional/associations/many_documents_proxy_spec.rb +968 -0
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +238 -0
- data/spec/functional/associations/many_embedded_proxy_spec.rb +288 -0
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +302 -0
- data/spec/functional/associations/one_as_proxy_spec.rb +489 -0
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +207 -0
- data/spec/functional/associations/one_embedded_proxy_spec.rb +100 -0
- data/spec/functional/associations/one_proxy_spec.rb +406 -0
- data/spec/functional/associations_spec.rb +48 -0
- data/spec/functional/caching_spec.rb +75 -0
- data/spec/functional/callbacks_spec.rb +330 -0
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +316 -0
- data/spec/functional/document_spec.rb +310 -0
- data/spec/functional/dumpable_spec.rb +24 -0
- data/spec/functional/dynamic_querying_spec.rb +75 -0
- data/spec/functional/embedded_document_spec.rb +316 -0
- data/spec/functional/equality_spec.rb +20 -0
- data/spec/functional/extensions_spec.rb +16 -0
- data/spec/functional/identity_map_spec.rb +483 -0
- data/spec/functional/keys_spec.rb +339 -0
- data/spec/functional/logger_spec.rb +20 -0
- data/spec/functional/modifiers_spec.rb +446 -0
- data/spec/functional/options_hash_spec.rb +41 -0
- data/spec/functional/pagination_spec.rb +89 -0
- data/spec/functional/partial_updates_spec.rb +530 -0
- data/spec/functional/protected_spec.rb +199 -0
- data/spec/functional/querying_spec.rb +984 -0
- data/spec/functional/rails_spec.rb +55 -0
- data/spec/functional/sci_spec.rb +374 -0
- data/spec/functional/scopes_spec.rb +204 -0
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/timestamps_spec.rb +97 -0
- data/spec/functional/touch_spec.rb +125 -0
- data/spec/functional/userstamps_spec.rb +46 -0
- data/spec/functional/validations_spec.rb +416 -0
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +150 -0
- data/spec/support/matchers.rb +15 -0
- data/spec/support/models.rb +256 -0
- data/spec/symbol_operator_spec.rb +70 -0
- data/spec/symbol_spec.rb +9 -0
- data/spec/unit/associations/base_spec.rb +146 -0
- data/spec/unit/associations/belongs_to_association_spec.rb +30 -0
- data/spec/unit/associations/many_association_spec.rb +64 -0
- data/spec/unit/associations/one_association_spec.rb +48 -0
- data/spec/unit/associations/proxy_spec.rb +103 -0
- data/spec/unit/clone_spec.rb +79 -0
- data/spec/unit/config_generator_spec.rb +24 -0
- data/spec/unit/criteria_hash_spec.rb +218 -0
- data/spec/unit/document_spec.rb +251 -0
- data/spec/unit/dynamic_finder_spec.rb +125 -0
- data/spec/unit/embedded_document_spec.rb +676 -0
- data/spec/unit/equality_spec.rb +38 -0
- data/spec/unit/exceptions_spec.rb +12 -0
- data/spec/unit/extensions_spec.rb +368 -0
- data/spec/unit/identity_map_middleware_spec.rb +134 -0
- data/spec/unit/inspect_spec.rb +47 -0
- data/spec/unit/key_spec.rb +276 -0
- data/spec/unit/keys_spec.rb +155 -0
- data/spec/unit/mark_mapper_spec.rb +37 -0
- data/spec/unit/model_generator_spec.rb +45 -0
- data/spec/unit/normalizers/criteria_hash_key_spec.rb +37 -0
- data/spec/unit/normalizers/criteria_hash_value_spec.rb +200 -0
- data/spec/unit/normalizers/fields_value_spec.rb +45 -0
- data/spec/unit/normalizers/hash_key_spec.rb +15 -0
- data/spec/unit/normalizers/integer_spec.rb +24 -0
- data/spec/unit/normalizers/options_hash_value_spec.rb +99 -0
- data/spec/unit/normalizers/sort_value_spec.rb +98 -0
- data/spec/unit/options_hash_spec.rb +64 -0
- data/spec/unit/pagination/collection_spec.rb +30 -0
- data/spec/unit/pagination/paginator_spec.rb +118 -0
- data/spec/unit/pagination_spec.rb +11 -0
- data/spec/unit/plugins_spec.rb +89 -0
- data/spec/unit/query_spec.rb +837 -0
- data/spec/unit/rails_compatibility_spec.rb +40 -0
- data/spec/unit/rails_reflect_on_association_spec.rb +118 -0
- data/spec/unit/rails_spec.rb +188 -0
- data/spec/unit/serialization_spec.rb +169 -0
- data/spec/unit/serializers/json_serializer_spec.rb +218 -0
- data/spec/unit/serializers/xml_serializer_spec.rb +198 -0
- data/spec/unit/time_zones_spec.rb +44 -0
- data/spec/unit/translation_spec.rb +27 -0
- data/spec/unit/validations_spec.rb +588 -0
- metadata +307 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class ManyAssociation < Base
|
6
|
+
|
7
|
+
def class_name
|
8
|
+
@class_name ||= options[:class_name] || name.to_s.singularize.camelize
|
9
|
+
end
|
10
|
+
|
11
|
+
# hate this, need to revisit
|
12
|
+
def proxy_class
|
13
|
+
@proxy_class ||= if klass.embeddable?
|
14
|
+
polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
|
15
|
+
else
|
16
|
+
if polymorphic?
|
17
|
+
ManyPolymorphicProxy
|
18
|
+
elsif as?
|
19
|
+
ManyDocumentsAsProxy
|
20
|
+
elsif in_array?
|
21
|
+
InArrayProxy
|
22
|
+
else
|
23
|
+
ManyDocumentsProxy
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup(model)
|
29
|
+
model.associations_module.module_eval <<-end_eval
|
30
|
+
def #{name}
|
31
|
+
get_proxy(associations[#{name.inspect}])
|
32
|
+
end
|
33
|
+
|
34
|
+
def #{name}=(value)
|
35
|
+
get_proxy(associations[#{name.inspect}]).replace(value)
|
36
|
+
value
|
37
|
+
end
|
38
|
+
end_eval
|
39
|
+
|
40
|
+
association = self
|
41
|
+
options = self.options
|
42
|
+
|
43
|
+
model.before_destroy do
|
44
|
+
if !association.embeddable?
|
45
|
+
case options[:dependent]
|
46
|
+
when :destroy
|
47
|
+
self.get_proxy(association).destroy_all
|
48
|
+
when :delete_all
|
49
|
+
self.get_proxy(association).delete_all
|
50
|
+
when :nullify
|
51
|
+
self.get_proxy(association).nullify
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def autosave?
|
58
|
+
options.fetch(:autosave, true)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class ManyDocumentsAsProxy < ManyDocumentsProxy
|
6
|
+
protected
|
7
|
+
def criteria
|
8
|
+
{type_key_name => proxy_owner.class.name, id_key_name => proxy_owner.id}
|
9
|
+
end
|
10
|
+
|
11
|
+
def apply_scope(doc)
|
12
|
+
ensure_owner_saved
|
13
|
+
criteria.each { |key, value| doc[key] = value }
|
14
|
+
doc
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def type_key_name
|
19
|
+
"#{options[:as]}_type"
|
20
|
+
end
|
21
|
+
|
22
|
+
def id_key_name
|
23
|
+
"#{options[:as]}_id"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class ManyDocumentsProxy < Collection
|
6
|
+
include DynamicQuerying::ClassMethods
|
7
|
+
|
8
|
+
def_delegators :query, *(Querying::Methods - [:each, :to_a, :size, :empty?])
|
9
|
+
|
10
|
+
def replace(docs)
|
11
|
+
load_target
|
12
|
+
(target - docs).each do |t|
|
13
|
+
case options[:dependent]
|
14
|
+
when :destroy then t.destroy
|
15
|
+
when :delete_all then t.delete
|
16
|
+
else t.update_attributes(self.foreign_key => nil)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
docs.each { |doc| prepare(doc).save }
|
21
|
+
reset
|
22
|
+
end
|
23
|
+
|
24
|
+
def <<(*docs)
|
25
|
+
ensure_owner_saved
|
26
|
+
flatten_deeper(docs).each { |doc| prepare(doc).save }
|
27
|
+
reset
|
28
|
+
end
|
29
|
+
alias_method :push, :<<
|
30
|
+
alias_method :concat, :<<
|
31
|
+
|
32
|
+
def build(attrs={})
|
33
|
+
doc = klass.new(attrs)
|
34
|
+
apply_scope(doc)
|
35
|
+
yield doc if block_given?
|
36
|
+
@target ||= [] unless loaded?
|
37
|
+
@target << doc
|
38
|
+
doc
|
39
|
+
end
|
40
|
+
|
41
|
+
def create(attrs={})
|
42
|
+
doc = klass.new(attrs)
|
43
|
+
apply_scope(doc)
|
44
|
+
yield doc if block_given?
|
45
|
+
doc.save
|
46
|
+
reset
|
47
|
+
doc
|
48
|
+
end
|
49
|
+
|
50
|
+
def create!(attrs={})
|
51
|
+
doc = klass.new(attrs)
|
52
|
+
apply_scope(doc)
|
53
|
+
yield doc if block_given?
|
54
|
+
doc.save!
|
55
|
+
reset
|
56
|
+
doc
|
57
|
+
end
|
58
|
+
|
59
|
+
def destroy_all(options={})
|
60
|
+
all(options).each { |doc| doc.destroy }
|
61
|
+
reset
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete_all(options={})
|
65
|
+
query(options).remove
|
66
|
+
reset
|
67
|
+
end
|
68
|
+
|
69
|
+
def nullify
|
70
|
+
all.each { |doc| doc.update_attributes(self.foreign_key => nil) }
|
71
|
+
reset
|
72
|
+
end
|
73
|
+
|
74
|
+
def save_to_collection(options={})
|
75
|
+
@target.each { |doc| doc.save(options) } if @target
|
76
|
+
end
|
77
|
+
|
78
|
+
def each(&block)
|
79
|
+
load_target.each(&block)
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
def query(options={})
|
84
|
+
klass.
|
85
|
+
query(association.query_options).
|
86
|
+
amend(options).amend(criteria)
|
87
|
+
end
|
88
|
+
|
89
|
+
def method_missing(method, *args, &block)
|
90
|
+
if klass.respond_to?(method)
|
91
|
+
result = klass.send(method, *args, &block)
|
92
|
+
case result
|
93
|
+
when MarkMapper::Query
|
94
|
+
query.merge result
|
95
|
+
|
96
|
+
# If we got a single record of this classas a result, return it
|
97
|
+
when klass
|
98
|
+
result
|
99
|
+
|
100
|
+
# If we got an array of this class as a result, return it
|
101
|
+
when Array
|
102
|
+
if result[0].is_a? klass
|
103
|
+
result
|
104
|
+
else
|
105
|
+
super
|
106
|
+
end
|
107
|
+
else
|
108
|
+
super
|
109
|
+
end
|
110
|
+
else
|
111
|
+
super
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def criteria
|
116
|
+
{self.foreign_key => proxy_owner.id}
|
117
|
+
end
|
118
|
+
|
119
|
+
def find_target
|
120
|
+
all
|
121
|
+
end
|
122
|
+
|
123
|
+
def ensure_owner_saved
|
124
|
+
proxy_owner.save unless proxy_owner.persisted?
|
125
|
+
end
|
126
|
+
|
127
|
+
def prepare(doc)
|
128
|
+
klass === doc ? apply_scope(doc) : build(doc)
|
129
|
+
end
|
130
|
+
|
131
|
+
def apply_scope(doc)
|
132
|
+
criteria.each { |key, value| doc[key] = value }
|
133
|
+
doc
|
134
|
+
end
|
135
|
+
|
136
|
+
def foreign_key
|
137
|
+
options[:foreign_key] || proxy_owner.class.name.foreign_key
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class ManyEmbeddedPolymorphicProxy < EmbeddedCollection
|
6
|
+
def replace(values)
|
7
|
+
@_values = values.map do |v|
|
8
|
+
v.respond_to?(:attributes) ? v.attributes.merge(association.type_key_name => v.class.name) : v
|
9
|
+
end
|
10
|
+
reset
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
def find_target
|
15
|
+
(@_values || []).map do |hash|
|
16
|
+
child = polymorphic_class(hash).load(hash, true)
|
17
|
+
assign_references(child)
|
18
|
+
child
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def polymorphic_class(doc)
|
23
|
+
if class_name = doc[association.type_key_name]
|
24
|
+
class_name.constantize
|
25
|
+
else
|
26
|
+
klass
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class ManyEmbeddedProxy < EmbeddedCollection
|
6
|
+
def replace(values)
|
7
|
+
@_values = (values || []).compact.map do |v|
|
8
|
+
v.respond_to?(:attributes) ? v.attributes : v
|
9
|
+
end
|
10
|
+
reset
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def find_target
|
15
|
+
(@_values ||= []).map do |attrs|
|
16
|
+
klass.load(attrs, true).tap do |child|
|
17
|
+
assign_references(child)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class OneAsProxy < OneProxy
|
6
|
+
protected
|
7
|
+
def criteria
|
8
|
+
{type_key_name => proxy_owner.class.name, id_key_name => proxy_owner.id}
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def type_key_name
|
13
|
+
"#{options[:as]}_type"
|
14
|
+
end
|
15
|
+
|
16
|
+
def id_key_name
|
17
|
+
"#{options[:as]}_id"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class OneAssociation < SingleAssociation
|
6
|
+
def embeddable?
|
7
|
+
klass.embeddable?
|
8
|
+
end
|
9
|
+
|
10
|
+
def proxy_class
|
11
|
+
@proxy_class ||=
|
12
|
+
if klass.embeddable?
|
13
|
+
polymorphic? ? OneEmbeddedPolymorphicProxy : OneEmbeddedProxy
|
14
|
+
elsif as?
|
15
|
+
OneAsProxy
|
16
|
+
else
|
17
|
+
OneProxy
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup(model)
|
22
|
+
super
|
23
|
+
|
24
|
+
association = self
|
25
|
+
options = self.options
|
26
|
+
|
27
|
+
model.before_destroy do
|
28
|
+
if !association.embeddable?
|
29
|
+
proxy = self.get_proxy(association)
|
30
|
+
|
31
|
+
unless proxy.nil?
|
32
|
+
case options[:dependent]
|
33
|
+
when :destroy then proxy.destroy
|
34
|
+
when :delete then proxy.delete
|
35
|
+
else proxy.nullify
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def autosave?
|
43
|
+
options.fetch(:autosave, embeddable?)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class OneEmbeddedPolymorphicProxy < OneEmbeddedProxy
|
6
|
+
def replace(value)
|
7
|
+
@value = value.respond_to?(:attributes) ? value.attributes.merge(association.type_key_name => value.class.name) : value
|
8
|
+
reset
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
def find_target
|
13
|
+
if @value
|
14
|
+
child = polymorphic_class(@value).load(@value, true)
|
15
|
+
assign_references(child)
|
16
|
+
child
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def polymorphic_class(doc)
|
21
|
+
if class_name = doc[association.type_key_name]
|
22
|
+
class_name.constantize
|
23
|
+
else
|
24
|
+
klass
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MarkMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class OneEmbeddedProxy < Proxy
|
6
|
+
def build(attributes={})
|
7
|
+
@target = klass.new(attributes)
|
8
|
+
assign_references(@target)
|
9
|
+
loaded
|
10
|
+
@target
|
11
|
+
end
|
12
|
+
|
13
|
+
def replace(doc)
|
14
|
+
if doc.respond_to?(:attributes)
|
15
|
+
@target = klass.load(doc.attributes, true)
|
16
|
+
else
|
17
|
+
@target = klass.load(doc, true)
|
18
|
+
end
|
19
|
+
assign_references(@target)
|
20
|
+
loaded
|
21
|
+
@target
|
22
|
+
end
|
23
|
+
|
24
|
+
def save_to_collection(options={})
|
25
|
+
@target.persist(options) if @target
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def find_target
|
31
|
+
if @value
|
32
|
+
klass.load(@value, true).tap do |child|
|
33
|
+
assign_references(child)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def assign_references(doc)
|
39
|
+
doc._parent_document = proxy_owner if doc
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|