activerecord_to_poro 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/activerecord_to_poro.gemspec +4 -3
- data/lib/activerecord_to_poro/metadata_enabled_ar.rb +29 -0
- data/lib/activerecord_to_poro/object_mapper.rb +4 -4
- data/lib/activerecord_to_poro/version.rb +1 -1
- data/spec/acceptance/map_ar_associations_spec.rb +11 -0
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69aba8f2a10463279cad3508895914d15e273f0a
|
4
|
+
data.tar.gz: 23c28ef06203deef5f487f83da6d0bca03e6db85
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69e06f848fc228e3500275833eb28519140b34568a6994673798909ceba8140de3aa585f398a3f0ddf48f22c8d1daf8ffc45fca537e5d973bd4c4257ec91e2b9
|
7
|
+
data.tar.gz: de404d5cac77aa930610899ed0d6acdefe52cd1ffd73a26c9da85bd27730baa66ff91c4d37a656ea783a4fbaea10b7a69dc248929026227670b642d5c56701ae
|
data/Gemfile
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency "bundler", "~> 1.5"
|
27
27
|
spec.add_development_dependency "rake"
|
28
28
|
spec.add_development_dependency 'sqlite3'
|
29
|
-
spec.add_development_dependency 'activerecord'
|
29
|
+
spec.add_development_dependency 'activerecord', '4.0.2'
|
30
30
|
spec.add_development_dependency 'database_cleaner'
|
31
31
|
|
32
32
|
|
@@ -45,8 +45,9 @@ Gem::Specification.new do |spec|
|
|
45
45
|
|
46
46
|
# https://github.com/pry/pry-stack_explorer
|
47
47
|
spec.add_development_dependency 'pry-stack_explorer'
|
48
|
-
# https://github.com/
|
49
|
-
|
48
|
+
# https://github.com/deivid-rodriguez/pry-byebug
|
49
|
+
# pre-debugger / debugger does not work with ruby 2.x
|
50
|
+
spec.add_development_dependency 'pry-byebug'
|
50
51
|
|
51
52
|
# https://github.com/dkubb/equalizer
|
52
53
|
spec.add_development_dependency 'equalizer'
|
@@ -8,9 +8,38 @@ module ActiverecordToPoro
|
|
8
8
|
record = pre_created_object || record_by_primary_key || new
|
9
9
|
|
10
10
|
record.tap do |new_obj|
|
11
|
+
|
11
12
|
new_obj.attributes = attrs
|
13
|
+
|
14
|
+
_patch_has_many_members(attrs, new_obj) unless new_obj.new_record?
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def _patch_has_many_members(attrs, new_obj)
|
20
|
+
has_many_attrs = attrs.slice(* _has_many_attr_names(new_obj))
|
21
|
+
|
22
|
+
has_many_attrs.each_pair do |name, updated_records|
|
23
|
+
|
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
|
27
|
+
|
28
|
+
_apply_change_set_to_record(attached_record, record_with_updated_values.changes)
|
29
|
+
end
|
30
|
+
|
12
31
|
end
|
32
|
+
end
|
13
33
|
|
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
|
38
|
+
|
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)
|
42
|
+
end
|
14
43
|
end
|
15
44
|
|
16
45
|
def _as_scope(attr)
|
@@ -81,11 +81,11 @@ module ActiverecordToPoro
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def dump_result_source=(new_dump_result)
|
84
|
-
|
85
|
-
|
86
|
-
source.send(:extend, MetadataEnabledAr)
|
87
|
-
end
|
84
|
+
unless new_dump_result.respond_to? :_from_attrs_with_metadata
|
85
|
+
new_dump_result.send(:extend, MetadataEnabledAr)
|
88
86
|
end
|
87
|
+
|
88
|
+
@dump_result_source = new_dump_result
|
89
89
|
end
|
90
90
|
|
91
91
|
def add_default_mapping_for_current_class
|
@@ -84,6 +84,17 @@ feature 'Map active record associations', %q{
|
|
84
84
|
expect(mapper.dump(poro).roles.last.permissions.size).to eq 1
|
85
85
|
end
|
86
86
|
|
87
|
+
scenario "updates an associated ActiveRecord object from a modified poro object" do
|
88
|
+
poro = mapper.load(a_active_record_object)
|
89
|
+
expect(mapper.dump(poro).roles.size).to eq 2
|
90
|
+
|
91
|
+
role_to_change = poro.roles.first
|
92
|
+
new_name = "#{role_to_change.name}_new"
|
93
|
+
role_to_change.name = new_name
|
94
|
+
|
95
|
+
expect(mapper.dump(poro).roles.first.name).to eq new_name
|
96
|
+
end
|
97
|
+
|
87
98
|
scenario "lazy loads associated objects" do
|
88
99
|
expect(a_active_record_object).not_to receive :salutation
|
89
100
|
expect(a_active_record_object).not_to receive :roles
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord_to_poro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dieter Späth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: abstract_type
|
@@ -84,16 +84,16 @@ dependencies:
|
|
84
84
|
name: activerecord
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '='
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: 4.0.2
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '='
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 4.0.2
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: database_cleaner
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -235,7 +235,7 @@ dependencies:
|
|
235
235
|
- !ruby/object:Gem::Version
|
236
236
|
version: '0'
|
237
237
|
- !ruby/object:Gem::Dependency
|
238
|
-
name: pry-
|
238
|
+
name: pry-byebug
|
239
239
|
requirement: !ruby/object:Gem::Requirement
|
240
240
|
requirements:
|
241
241
|
- - ">="
|