denormalize_mm 0.1.1 → 0.2.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.
@@ -20,20 +20,70 @@ module MongoMapper::Denormalization
20
20
 
21
21
  denormalize(source, dest, {
22
22
  :target_field => dest,
23
+ :is_association => true
23
24
  }.merge(options))
24
25
  end
25
26
 
26
27
  def denormalize(association, field, options={})
27
- method_name = "denormalize_#{association}_#{field}"
28
+ association = association.to_sym
29
+ field = field.to_sym
28
30
 
29
- validation_method = options[:on] || "before_validation"
30
- source_field_code = options[:source_field] || "#{association}.#{field}"
31
- target_field_code = options[:target_field] || "#{association}_#{field}"
31
+ validation_method = options[:on] || :before_validation
32
+ source_field_code = options[:source_field] || :"#{association}.#{field}"
33
+ target_field_code = options[:target_field] || :"#{association}_#{field}"
34
+ is_association = options[:is_association]
32
35
 
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)
38
+ end
39
+
40
+ private
41
+
42
+ def denormalize_on_update(association, field, is_association, target_field_code)
43
+ if is_association
44
+ field = :"#{field}_id"
45
+ target_field_code = :"#{target_field_code}_id"
46
+ end
47
+
48
+ klass = self.associations[association].klass
49
+
50
+ collection_name = self.collection_name
51
+ reverse_denormalization_method_name = "denormalize_#{collection_name}_#{association}_#{field}"
52
+
53
+ klass.class_eval(<<-CODE, __FILE__, __LINE__)
54
+ after_update :#{reverse_denormalization_method_name}
55
+
56
+ def #{reverse_denormalization_method_name}
57
+ if self.#{field}_changed?
58
+ db = MongoMapper.database
59
+
60
+ find_query = {
61
+ :#{association}_id => self.id
62
+ }
63
+ update_query = {
64
+ '$set' => {
65
+ :#{target_field_code} => self.#{field}
66
+ }
67
+ }
68
+
69
+ db["#{collection_name}"].update(find_query, update_query, :multi => true)
70
+ end
71
+
72
+ true
73
+ end
74
+
75
+ private :#{reverse_denormalization_method_name}
76
+ CODE
77
+ end
78
+
79
+ def denormalize_on_validation(association, field, validation_method, source_field_code, target_field_code)
80
+ validation_method_name = :"denormalize_#{association}_#{field}"
81
+
82
+ # denormalize the field
33
83
  self.class_eval <<-CODE, __FILE__, __LINE__
34
- #{validation_method} :#{method_name}
84
+ #{validation_method} :#{validation_method_name}
35
85
 
36
- def #{method_name}
86
+ def #{validation_method_name}
37
87
  if self.#{association}
38
88
  self.#{target_field_code} = #{source_field_code}
39
89
  end
@@ -41,7 +91,7 @@ module MongoMapper::Denormalization
41
91
  true
42
92
  end
43
93
 
44
- private :#{method_name}
94
+ private :#{validation_method_name}
45
95
  CODE
46
96
  end
47
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: denormalize_mm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-06-23 00:00:00.000000000 Z
13
+ date: 2014-06-24 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Helpers to denormalize fields easily on mongo mapper models
16
16
  email: scott@railsnewbie.com