snusnu-dm-accepts_nested_attributes 0.0.3 → 0.0.4
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.
data/Manifest.txt
CHANGED
@@ -9,6 +9,7 @@ lib/dm-accepts_nested_attributes.rb
|
|
9
9
|
lib/dm-accepts_nested_attributes/association_proxies.rb
|
10
10
|
lib/dm-accepts_nested_attributes/association_validation.rb
|
11
11
|
lib/dm-accepts_nested_attributes/nested_attributes.rb
|
12
|
+
lib/dm-accepts_nested_attributes/resource.rb
|
12
13
|
lib/dm-accepts_nested_attributes/version.rb
|
13
14
|
spec/fixtures/person.rb
|
14
15
|
spec/fixtures/profile.rb
|
@@ -2,64 +2,26 @@ module DataMapper
|
|
2
2
|
module NestedAttributes
|
3
3
|
|
4
4
|
module AssociationValidation
|
5
|
-
|
6
|
-
|
7
|
-
# overwriting Resource#save like this breaks the before(:save) hook stack
|
8
|
-
# this hopefully is no problem, since the current implementation doesn't rely on
|
9
|
-
# a before(:save) hook, but rather overwrites this hook with a no-op, and adds
|
10
|
-
# the desired behavior via overwriting Resource#save directly. I'd really appreciate
|
11
|
-
# any ideas for doing this differently, though. Anyways, I'm not really sure if this
|
12
|
-
# is the right approach. I don't even know if it works with custom validations,
|
13
|
-
# or maybe breaks other things. It's also really not well specced at all atm.
|
14
|
-
# Use at your own risk :-)
|
15
|
-
|
16
|
-
def save(context = :default)
|
17
|
-
|
18
|
-
# -----------------------------------------------------------------
|
19
|
-
# ORIGINAL CODE from Resource#save
|
20
|
-
# -----------------------------------------------------------------
|
21
|
-
#
|
22
|
-
# associations_saved = false
|
23
|
-
# child_associations.each { |a| associations_saved |= a.save }
|
24
|
-
#
|
25
|
-
# saved = new_record? ? create : update
|
26
|
-
#
|
27
|
-
# if saved
|
28
|
-
# original_values.clear
|
29
|
-
# end
|
30
|
-
#
|
31
|
-
# parent_associations.each { |a| associations_saved |= a.save }
|
32
|
-
#
|
33
|
-
# # We should return true if the model (or any of its associations)
|
34
|
-
# # were saved.
|
35
|
-
# (saved | associations_saved) == true
|
36
|
-
#
|
37
|
-
# -----------------------------------------------------------------
|
38
|
-
|
5
|
+
|
6
|
+
def save_child_associations(saved, context)
|
39
7
|
return super if context.nil? # preserve save! behavior
|
40
|
-
|
41
|
-
associations_saved = false
|
42
|
-
|
43
8
|
child_associations.each do |a|
|
44
|
-
|
45
9
|
if a.respond_to?(:valid?)
|
46
10
|
a.errors.each { |e| self.errors.add(:general, e) } unless a.valid?(context)
|
47
11
|
else
|
48
12
|
self.errors.add(:general, "child association is missing")
|
49
13
|
end
|
50
|
-
|
51
|
-
associations_saved |= a.save
|
52
|
-
|
14
|
+
saved |= a.save
|
53
15
|
end
|
16
|
+
saved
|
17
|
+
end
|
54
18
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
original_values.clear
|
59
|
-
end
|
19
|
+
def save_self
|
20
|
+
self.valid? && super
|
21
|
+
end
|
60
22
|
|
23
|
+
def save_parent_associations(saved, context)
|
61
24
|
parent_associations.each do |a|
|
62
|
-
|
63
25
|
if a.respond_to?(:each)
|
64
26
|
a.each do |r|
|
65
27
|
r.errors.each { |e| self.errors.add(:general, e) } unless r.valid?(context)
|
@@ -67,20 +29,16 @@ module DataMapper
|
|
67
29
|
else
|
68
30
|
a.errors.each { |e| self.errors.add(:general, e) } unless a.valid?(context)
|
69
31
|
end
|
70
|
-
|
71
|
-
associations_saved |= a.save
|
72
|
-
|
32
|
+
saved |= a.save
|
73
33
|
end
|
74
|
-
|
75
|
-
(saved | associations_saved) == true
|
76
|
-
|
34
|
+
saved
|
77
35
|
end
|
78
36
|
|
79
37
|
# everything works the same if this method isn't overwritten with a no-op
|
80
38
|
# however, i suspect that this is the case because the registered before(:save) hook
|
81
39
|
# somehow gets lost when overwriting Resource#save here in this module.
|
82
40
|
# I'll leave it in for now, to make the purpose clear
|
83
|
-
|
41
|
+
|
84
42
|
def check_validations(context = :default)
|
85
43
|
true # no-op, validations are checked inside #save
|
86
44
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module Resource
|
3
|
+
|
4
|
+
# basic extract method refactorings to work around a bug in extlib
|
5
|
+
# see http://sick.snusnu.info/2009/04/29/extlibhook-breaks-if-hooked-method-is-redefined/
|
6
|
+
# maybe they are worth considering even when the bug in extlib (hopefully) gets fixed
|
7
|
+
|
8
|
+
def save(context = :default)
|
9
|
+
|
10
|
+
associations_saved = false
|
11
|
+
associations_saved = save_child_associations(associations_saved, context)
|
12
|
+
|
13
|
+
saved = save_self
|
14
|
+
|
15
|
+
if saved
|
16
|
+
original_values.clear
|
17
|
+
end
|
18
|
+
|
19
|
+
associations_saved = save_parent_associations(associations_saved, context)
|
20
|
+
|
21
|
+
# We should return true if the model (or any of its associations) were saved.
|
22
|
+
(saved | associations_saved) == true
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
def save_child_associations(saved, context)
|
28
|
+
child_associations.each { |a| saved |= a.save }
|
29
|
+
saved
|
30
|
+
end
|
31
|
+
|
32
|
+
def save_self
|
33
|
+
new_record? ? create : update
|
34
|
+
end
|
35
|
+
|
36
|
+
def save_parent_associations(saved, context)
|
37
|
+
parent_associations.each { |a| saved |= a.save }
|
38
|
+
saved
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -10,9 +10,10 @@ require 'dm-core'
|
|
10
10
|
require 'dm-validations'
|
11
11
|
|
12
12
|
# Require plugin-files
|
13
|
-
|
14
|
-
|
15
|
-
require
|
13
|
+
dir = Pathname(__FILE__).dirname.expand_path / 'dm-accepts_nested_attributes'
|
14
|
+
require dir / 'resource'
|
15
|
+
require dir / 'association_proxies'
|
16
|
+
require dir / 'nested_attributes'
|
16
17
|
|
17
18
|
# Include the plugin in Model
|
18
19
|
DataMapper::Model.append_extensions DataMapper::NestedAttributes::ClassMethods
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snusnu-dm-accepts_nested_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Martin Gamsj\xC3\xA4ger"
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- lib/dm-accepts_nested_attributes/association_proxies.rb
|
55
55
|
- lib/dm-accepts_nested_attributes/association_validation.rb
|
56
56
|
- lib/dm-accepts_nested_attributes/nested_attributes.rb
|
57
|
+
- lib/dm-accepts_nested_attributes/resource.rb
|
57
58
|
- lib/dm-accepts_nested_attributes/version.rb
|
58
59
|
- spec/fixtures/person.rb
|
59
60
|
- spec/fixtures/profile.rb
|