activerecord_to_poro 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69aba8f2a10463279cad3508895914d15e273f0a
4
- data.tar.gz: 23c28ef06203deef5f487f83da6d0bca03e6db85
3
+ metadata.gz: 8b997c823f571e6337d990f45915271b65bdf283
4
+ data.tar.gz: 43abefbf9e2a0aefef0db4a9b4c9198551cc91f5
5
5
  SHA512:
6
- metadata.gz: 69e06f848fc228e3500275833eb28519140b34568a6994673798909ceba8140de3aa585f398a3f0ddf48f22c8d1daf8ffc45fca537e5d973bd4c4257ec91e2b9
7
- data.tar.gz: de404d5cac77aa930610899ed0d6acdefe52cd1ffd73a26c9da85bd27730baa66ff91c4d37a656ea783a4fbaea10b7a69dc248929026227670b642d5c56701ae
6
+ metadata.gz: 12924d53f57249f4844e1a96d34ac60b67f5389082a95de2325ce85f02199f5ed3bfa0368ba39a2e7938f22ade7e246bd500916639498f5cd94b4e5187e3da46
7
+ data.tar.gz: 884ef768777721768959fa5f5d15270380452f3321b9a52c870158e95fbf4e00bb53f4df298cff3d11e253295e7bcc1c8a17075a33fa679513614e790b024736
@@ -34,19 +34,24 @@ module ActiverecordToPoro
34
34
  end
35
35
 
36
36
  def for_ar_class(ar_class_name)
37
- Set.new.find
38
37
  source_object_info.find(->{SourceObjectInfo.new}){|data|
39
38
  data.class_name == ar_class_name
40
39
  }
41
40
  end
42
41
 
43
42
  def set_source_info(ar_object)
44
- self.source_object_info << SourceObjectInfo.new(class_name: ar_object.class.name,
45
- column: ar_object.class.primary_key,
46
- value: ar_object.send(ar_object.class.primary_key),
47
- object_id: ar_object.object_id,
48
- lock_version: ar_object.respond_to?(:lock_version) ? ar_object.lock_version : nil
49
- )
43
+ new_info = create_source_info(ar_object)
44
+ self.source_object_info.delete_if { |info| info == new_info }
45
+ self.source_object_info << new_info
46
+ end
47
+
48
+ def create_source_info(ar_object)
49
+ SourceObjectInfo.new(class_name: ar_object.class.name,
50
+ column: ar_object.class.primary_key,
51
+ value: ar_object.send(ar_object.class.primary_key),
52
+ object_id: ar_object.object_id,
53
+ lock_version: ar_object.respond_to?(:lock_version) ? ar_object.lock_version : nil
54
+ )
50
55
  end
51
56
 
52
57
  def to_hash
@@ -1,59 +1,90 @@
1
1
  module ActiverecordToPoro
2
2
 
3
3
  module MetadataEnabledAr
4
+ require 'active_support/concern'
4
5
 
5
- def _from_attrs_with_metadata(attrs={}, pre_created_object= nil)
6
- record_by_primary_key = _record_from_metadata!(attrs)
6
+ extend ActiveSupport::Concern
7
7
 
8
- record = pre_created_object || record_by_primary_key || new
8
+ included do
9
+ after_save :_update_poro_metadata
10
+ end
11
+
12
+ def _referenced_poros
13
+ @_referenced_poros ||= []
14
+ end
9
15
 
10
- record.tap do |new_obj|
16
+ def _update_poro_metadata
11
17
 
12
- new_obj.attributes = attrs
18
+ _referenced_poros.each do |entity|
19
+ entity._set_metadata_from_ar = self if entity.respond_to? :_set_metadata_from_ar=
13
20
 
14
- _patch_has_many_members(attrs, new_obj) unless new_obj.new_record?
21
+ [:id, :updated_at, :created_at, :lock_version,].each do |magic_col|
22
+ entity.send("#{magic_col}=", send(magic_col)) if [entity, self].all? { |obj| obj.respond_to? magic_col }
23
+ end
15
24
  end
16
25
 
26
+ @_referenced_poros = nil # one way method to prevent circular references
27
+
28
+ true
17
29
  end
18
30
 
19
- def _patch_has_many_members(attrs, new_obj)
20
- has_many_attrs = attrs.slice(* _has_many_attr_names(new_obj))
21
31
 
22
- has_many_attrs.each_pair do |name, updated_records|
23
32
 
24
- new_obj.public_send(name).each do |attached_record|
25
- record_with_updated_values = updated_records.find {|r| r == attached_record}
26
- next unless record_with_updated_values
33
+ module ClassMethods
34
+
35
+ def _from_attrs_with_metadata(attrs={}, pre_created_object= nil)
36
+ record_by_primary_key = _record_from_metadata!(attrs)
37
+
38
+ record = pre_created_object || record_by_primary_key || new
27
39
 
28
- _apply_change_set_to_record(attached_record, record_with_updated_values.changes)
40
+ record.tap do |new_obj|
41
+
42
+ new_obj.attributes = attrs
43
+
44
+ _patch_has_many_members(attrs, new_obj) unless new_obj.new_record?
29
45
  end
30
46
 
31
47
  end
32
- end
33
48
 
34
- def _has_many_attr_names(obj_or_class)
35
- class_to_check = obj_or_class.respond_to?(:reflect_on_all_associations) ? obj_or_class : obj_or_class.class
36
- class_to_check.reflect_on_all_associations(:has_many).map(&:name)
37
- end
49
+ def _patch_has_many_members(attrs, new_obj)
50
+ has_many_attrs = attrs.slice(* _has_many_attr_names(new_obj))
51
+
52
+ has_many_attrs.each_pair do |name, updated_records|
53
+
54
+ new_obj.public_send(name).each do |attached_record|
55
+ record_with_updated_values = updated_records.find { |r| r == attached_record }
56
+ next unless record_with_updated_values
38
57
 
39
- def _apply_change_set_to_record(attached_record, changes)
40
- changes.each_pair do |attr_name, (_, new_value)|
41
- attached_record.public_send("#{attr_name}=", new_value)
58
+ _apply_change_set_to_record(attached_record, record_with_updated_values.changes)
59
+ end
60
+
61
+ end
42
62
  end
43
- end
44
63
 
45
- def _as_scope(attr)
46
- attr.empty? ? none : where(attr)
47
- end
64
+ def _has_many_attr_names(obj_or_class)
65
+ class_to_check = obj_or_class.respond_to?(:reflect_on_all_associations) ? obj_or_class : obj_or_class.class
66
+ class_to_check.reflect_on_all_associations(:has_many).map(&:name)
67
+ end
48
68
 
49
- def _extract_metadata!(attrs)
50
- metadata = (attrs || {}).delete(:_set_metadata_to_ar) {|*| Metadata.new}
51
- metadata.for_ar_class(self.name)
52
- end
69
+ def _apply_change_set_to_record(attached_record, changes)
70
+ changes.each_pair do |attr_name, (_, new_value)|
71
+ attached_record.public_send("#{attr_name}=", new_value)
72
+ end
73
+ end
74
+
75
+ def _as_scope(attr)
76
+ attr.empty? ? none : where(attr)
77
+ end
53
78
 
54
- def _record_from_metadata!(attrs)
55
- specific_metadata = _extract_metadata!(attrs)
56
- _as_scope(specific_metadata.as_scope_hash).first
79
+ def _extract_metadata!(attrs)
80
+ metadata = (attrs || {}).delete(:_set_metadata_to_ar) { |*| Metadata.new }
81
+ metadata.for_ar_class(self.name)
82
+ end
83
+
84
+ def _record_from_metadata!(attrs)
85
+ specific_metadata = _extract_metadata!(attrs)
86
+ _as_scope(specific_metadata.as_scope_hash).first
87
+ end
57
88
  end
58
89
 
59
90
  end
@@ -2,7 +2,11 @@ module ActiverecordToPoro
2
2
 
3
3
  module MappingToArClass
4
4
  def call(pre_created_object=nil)
5
- self.target_source._from_attrs_with_metadata(to_hash_or_array(), pre_created_object)
5
+ self.target_source._from_attrs_with_metadata(to_hash_or_array(), pre_created_object).tap do |new_obj|
6
+
7
+ new_obj._referenced_poros << to_convert
8
+
9
+ end
6
10
  end
7
11
  end
8
12
 
@@ -82,7 +86,7 @@ module ActiverecordToPoro
82
86
 
83
87
  def dump_result_source=(new_dump_result)
84
88
  unless new_dump_result.respond_to? :_from_attrs_with_metadata
85
- new_dump_result.send(:extend, MetadataEnabledAr)
89
+ new_dump_result.send(:include, MetadataEnabledAr)
86
90
  end
87
91
 
88
92
  @dump_result_source = new_dump_result
@@ -1,3 +1,3 @@
1
1
  module ActiverecordToPoro
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -42,6 +42,20 @@ feature "Map active record objects", %q{
42
42
  expect(mapper_with_custom_source.load(a_active_record_object)).to be_kind_of custom_poro_class
43
43
  end
44
44
 
45
+ scenario 'updates "_metadata" of the poro after save ' do
46
+ poro = mapper.load(a_active_record_object)
47
+ old_lock_version = poro._metadata.for_ar_class(a_active_record_object).lock_version || 0
48
+ ar_object = mapper.dump(poro)
49
+
50
+ ar_object.email = "1_#{ar_object.email}"
51
+
52
+ ar_object.save!
53
+
54
+ expect(poro._metadata.for_ar_class(ar_object.class.name).lock_version).to eq old_lock_version + 1
55
+ expect(poro.lock_version).to eq old_lock_version + 1
56
+
57
+ end
58
+
45
59
  scenario 'extend default mapping' do
46
60
  mapper_with_custom_source.extend_mapping do
47
61
  rule to: :lock_version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_to_poro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dieter Späth