denormalize_mm 0.2.1 → 0.4.0
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.
- checksums.yaml +7 -0
- data/lib/mongo_mapper/denormalization.rb +31 -15
- metadata +11 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bf1db10ff517e44686a15dcf6bace3ca0d2acb24754f926cb3cd415a21220709
|
4
|
+
data.tar.gz: 5177ab282baba767bd3f65778d33611e1862b66e5dfe5ee9693f7bdb19504300
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e618a1cab3b3be5d2bdca176764615d5e504c65b1e4f7925215735f3a003c01d349981e54bd9debfe9b93d8aee955535187ee0110785eece5f78f907ea967068
|
7
|
+
data.tar.gz: 02d943a65ce25b05b177d52b0dd2c7bf16eb0090f90e68b992d43b71278dfaaa8fc3dd1113b164dbe3f0bdf24ae15a5b96a2acad105649c387c97de1a4ff50ef
|
@@ -1,6 +1,12 @@
|
|
1
1
|
require 'mongo_mapper'
|
2
2
|
|
3
3
|
module MongoMapper::Denormalization
|
4
|
+
if defined?(ActiveModel::Validations::ClassMethods::VALID_OPTIONS_FOR_VALIDATE)
|
5
|
+
VALID_OPTIONS_FOR_VALIDATE = ActiveModel::Validations::ClassMethods::VALID_OPTIONS_FOR_VALIDATE
|
6
|
+
else
|
7
|
+
VALID_OPTIONS_FOR_VALIDATE = [:on, :if, :unless, :prepend].freeze
|
8
|
+
end
|
9
|
+
|
4
10
|
def self.included(mod)
|
5
11
|
mod.extend(MongoMapper::Denormalization::ClassMethods)
|
6
12
|
end
|
@@ -10,31 +16,38 @@ module MongoMapper::Denormalization
|
|
10
16
|
denormalize(association, field, options)
|
11
17
|
end
|
12
18
|
|
13
|
-
def
|
14
|
-
options =
|
19
|
+
def denormalize_associations(*destinations)
|
20
|
+
options = destinations.last.is_a?(Hash) ? destinations.pop.dup : {}
|
15
21
|
source = options.delete(:from)
|
16
22
|
|
17
23
|
if !source
|
18
24
|
raise "denormalize_association must take a from (source) option"
|
19
25
|
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
27
|
+
destinations.each do |dest|
|
28
|
+
denormalize(source, dest, {
|
29
|
+
:target_field => dest,
|
30
|
+
:is_association => true
|
31
|
+
}.merge(options))
|
32
|
+
end
|
25
33
|
end
|
26
34
|
|
35
|
+
alias_method :denormalize_association, :denormalize_associations
|
36
|
+
|
27
37
|
def denormalize(association, field, options={})
|
28
38
|
association = association.to_sym
|
29
39
|
field = field.to_sym
|
30
40
|
|
31
|
-
validation_method = options[:
|
41
|
+
validation_method = options[:method] || :before_validation
|
42
|
+
validation_method_params = options.slice(*VALID_OPTIONS_FOR_VALIDATE)
|
43
|
+
|
32
44
|
source_field_code = options[:source_field] || :"#{association}.#{field}"
|
33
45
|
target_field_code = options[:target_field] || :"#{association}_#{field}"
|
34
46
|
is_association = options[:is_association]
|
47
|
+
reflect_updates = options.has_key?(:reflect_updates) ? options[:reflect_updates] : true
|
35
48
|
|
36
|
-
denormalize_on_validation(association, field, validation_method, source_field_code, target_field_code)
|
37
|
-
denormalize_on_update(association, field, is_association, target_field_code)
|
49
|
+
denormalize_on_validation(association, field, validation_method, source_field_code, target_field_code, validation_method_params)
|
50
|
+
denormalize_on_update(association, field, is_association, target_field_code) if reflect_updates
|
38
51
|
end
|
39
52
|
|
40
53
|
private
|
@@ -48,9 +61,9 @@ module MongoMapper::Denormalization
|
|
48
61
|
klass = self.associations[association].klass
|
49
62
|
|
50
63
|
collection_name = self.collection_name
|
51
|
-
reverse_denormalization_method_name = "
|
64
|
+
reverse_denormalization_method_name = "_denormalize__#{collection_name}__#{association}__#{field}".gsub(/[^A-Za-z0-9_]/, '_')
|
52
65
|
|
53
|
-
klass.class_eval(<<-CODE, __FILE__, __LINE__)
|
66
|
+
klass.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
|
54
67
|
after_update :#{reverse_denormalization_method_name}
|
55
68
|
|
56
69
|
def #{reverse_denormalization_method_name}
|
@@ -59,16 +72,19 @@ module MongoMapper::Denormalization
|
|
59
72
|
if self.#{field}_changed?
|
60
73
|
db = MongoMapper.database
|
61
74
|
|
75
|
+
new_value = self.#{field}
|
76
|
+
new_value = new_value.utc if new_value.is_a?(Time) && new_value.respond_to?(:utc)
|
77
|
+
|
62
78
|
find_query = {
|
63
79
|
:#{association}_id => self.id
|
64
80
|
}
|
65
81
|
update_query = {
|
66
82
|
'$set' => {
|
67
|
-
:#{target_field_code} =>
|
83
|
+
:#{target_field_code} => new_value
|
68
84
|
}
|
69
85
|
}
|
70
86
|
|
71
|
-
db["#{collection_name}"].
|
87
|
+
db["#{collection_name}"].update_many(find_query, update_query)
|
72
88
|
end
|
73
89
|
|
74
90
|
true
|
@@ -78,12 +94,12 @@ module MongoMapper::Denormalization
|
|
78
94
|
CODE
|
79
95
|
end
|
80
96
|
|
81
|
-
def denormalize_on_validation(association, field, validation_method, source_field_code, target_field_code)
|
97
|
+
def denormalize_on_validation(association, field, validation_method, source_field_code, target_field_code, validation_method_params={})
|
82
98
|
validation_method_name = :"denormalize_#{association}_#{field}"
|
83
99
|
|
84
100
|
# denormalize the field
|
85
101
|
self.class_eval <<-CODE, __FILE__, __LINE__
|
86
|
-
#{validation_method} :#{validation_method_name}
|
102
|
+
#{validation_method} :#{validation_method_name}, #{validation_method_params.inspect}
|
87
103
|
|
88
104
|
def #{validation_method_name}
|
89
105
|
if self.#{association}
|
metadata
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: denormalize_mm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Scott Taylor
|
9
8
|
- Andrew Pariser
|
10
|
-
autorequire:
|
9
|
+
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
date:
|
12
|
+
date: 2020-09-20 00:00:00.000000000 Z
|
14
13
|
dependencies: []
|
15
14
|
description: Helpers to denormalize fields easily on mongo mapper models
|
16
15
|
email: scott@railsnewbie.com
|
@@ -19,29 +18,27 @@ extensions: []
|
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
21
20
|
- lib/mongo_mapper/denormalization.rb
|
22
|
-
homepage: http://github.com/
|
21
|
+
homepage: http://github.com/smtlaissezfaire/denormalize_mm
|
23
22
|
licenses:
|
24
23
|
- MIT
|
25
|
-
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
26
|
rdoc_options: []
|
27
27
|
require_paths:
|
28
28
|
- lib
|
29
29
|
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
-
none: false
|
31
30
|
requirements:
|
32
|
-
- -
|
31
|
+
- - ">="
|
33
32
|
- !ruby/object:Gem::Version
|
34
33
|
version: '0'
|
35
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
-
none: false
|
37
35
|
requirements:
|
38
|
-
- -
|
36
|
+
- - ">="
|
39
37
|
- !ruby/object:Gem::Version
|
40
38
|
version: '0'
|
41
39
|
requirements: []
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
specification_version: 3
|
40
|
+
rubygems_version: 3.1.4
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
46
43
|
summary: Denormalize fields easily in mongo mapper
|
47
44
|
test_files: []
|