denormalize_mm 0.2.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95a1a56bd68bc0bf5de29ed7ce522d50f532f7102fa6fcc65b1133183bc2f706
4
+ data.tar.gz: 1bfc7fe93d8192300898a0278986e338534e41796b90b51cd4a02d9adfa24325
5
+ SHA512:
6
+ metadata.gz: 410445dbf2a9c2f7708c8e712d00ab8416cfd296ead0fbfcd21b032b247f1602cbb7c76750b742d3a0361e25b7f68cc6e991aaf01cdcaf0a48e1af428231aa4c
7
+ data.tar.gz: 2ab3548c255ca3bd063d51664cedad5016286c3150eec57ff2be420f39b1ee365829dbb46249f52a5578d90dc8e6a50747615ccfdeb172bdacbc2d72cfc1ab30
@@ -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 denormalize_association(dest, options={})
14
- options = options.dup
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
- denormalize(source, dest, {
22
- :target_field => dest,
23
- :is_association => true
24
- }.merge(options))
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[:on] || :before_validation
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
@@ -50,7 +63,7 @@ module MongoMapper::Denormalization
50
63
  collection_name = self.collection_name
51
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} => self.#{field}
83
+ :#{target_field_code} => new_value
68
84
  }
69
85
  }
70
86
 
71
- db["#{collection_name}"].update(find_query, update_query, :multi => true)
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,17 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: denormalize_mm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
5
- prerelease:
4
+ version: 0.4.1
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: 2014-09-26 00:00:00.000000000 Z
14
- dependencies: []
12
+ date: 2020-09-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongo_mapper
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 0.15.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 0.15.0
15
28
  description: Helpers to denormalize fields easily on mongo mapper models
16
29
  email: scott@railsnewbie.com
17
30
  executables: []
@@ -19,29 +32,27 @@ extensions: []
19
32
  extra_rdoc_files: []
20
33
  files:
21
34
  - lib/mongo_mapper/denormalization.rb
22
- homepage: http://github.com/GoLearnUp/denormalize_mm
35
+ homepage: http://github.com/smtlaissezfaire/denormalize_mm
23
36
  licenses:
24
37
  - MIT
25
- post_install_message:
38
+ metadata: {}
39
+ post_install_message:
26
40
  rdoc_options: []
27
41
  require_paths:
28
42
  - lib
29
43
  required_ruby_version: !ruby/object:Gem::Requirement
30
- none: false
31
44
  requirements:
32
- - - ! '>='
45
+ - - ">="
33
46
  - !ruby/object:Gem::Version
34
47
  version: '0'
35
48
  required_rubygems_version: !ruby/object:Gem::Requirement
36
- none: false
37
49
  requirements:
38
- - - ! '>='
50
+ - - ">="
39
51
  - !ruby/object:Gem::Version
40
52
  version: '0'
41
53
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 1.8.24
44
- signing_key:
45
- specification_version: 3
54
+ rubygems_version: 3.1.4
55
+ signing_key:
56
+ specification_version: 4
46
57
  summary: Denormalize fields easily in mongo mapper
47
58
  test_files: []