seomoz-ripple 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. data/Gemfile +20 -0
  2. data/Guardfile +15 -0
  3. data/Rakefile +88 -0
  4. data/lib/rails/generators/ripple/configuration/configuration_generator.rb +13 -0
  5. data/lib/rails/generators/ripple/configuration/templates/ripple.yml +24 -0
  6. data/lib/rails/generators/ripple/js/js_generator.rb +13 -0
  7. data/lib/rails/generators/ripple/js/templates/js/contrib.js +63 -0
  8. data/lib/rails/generators/ripple/js/templates/js/iso8601.js +76 -0
  9. data/lib/rails/generators/ripple/js/templates/js/ripple.js +132 -0
  10. data/lib/rails/generators/ripple/model/model_generator.rb +20 -0
  11. data/lib/rails/generators/ripple/model/templates/model.rb +10 -0
  12. data/lib/rails/generators/ripple/observer/observer_generator.rb +16 -0
  13. data/lib/rails/generators/ripple/observer/templates/observer.rb +4 -0
  14. data/lib/rails/generators/ripple/test/templates/test_server.rb +46 -0
  15. data/lib/rails/generators/ripple/test/test_generator.rb +39 -0
  16. data/lib/rails/generators/ripple_generator.rb +78 -0
  17. data/lib/ripple.rb +79 -0
  18. data/lib/ripple/associations.rb +356 -0
  19. data/lib/ripple/associations/embedded.rb +35 -0
  20. data/lib/ripple/associations/instantiators.rb +26 -0
  21. data/lib/ripple/associations/linked.rb +65 -0
  22. data/lib/ripple/associations/many.rb +38 -0
  23. data/lib/ripple/associations/many_embedded_proxy.rb +38 -0
  24. data/lib/ripple/associations/many_linked_proxy.rb +66 -0
  25. data/lib/ripple/associations/many_reference_proxy.rb +93 -0
  26. data/lib/ripple/associations/many_stored_key_proxy.rb +76 -0
  27. data/lib/ripple/associations/one.rb +20 -0
  28. data/lib/ripple/associations/one_embedded_proxy.rb +35 -0
  29. data/lib/ripple/associations/one_key_proxy.rb +58 -0
  30. data/lib/ripple/associations/one_linked_proxy.rb +22 -0
  31. data/lib/ripple/associations/one_stored_key_proxy.rb +43 -0
  32. data/lib/ripple/associations/proxy.rb +118 -0
  33. data/lib/ripple/attribute_methods.rb +118 -0
  34. data/lib/ripple/attribute_methods/dirty.rb +50 -0
  35. data/lib/ripple/attribute_methods/query.rb +34 -0
  36. data/lib/ripple/attribute_methods/read.rb +26 -0
  37. data/lib/ripple/attribute_methods/write.rb +25 -0
  38. data/lib/ripple/callbacks.rb +74 -0
  39. data/lib/ripple/conflict/basic_resolver.rb +82 -0
  40. data/lib/ripple/conflict/document_hooks.rb +20 -0
  41. data/lib/ripple/conflict/resolver.rb +71 -0
  42. data/lib/ripple/conflict/test_helper.rb +33 -0
  43. data/lib/ripple/conversion.rb +28 -0
  44. data/lib/ripple/core_ext.rb +2 -0
  45. data/lib/ripple/core_ext/casting.rb +148 -0
  46. data/lib/ripple/core_ext/object.rb +8 -0
  47. data/lib/ripple/document.rb +104 -0
  48. data/lib/ripple/document/bucket_access.rb +25 -0
  49. data/lib/ripple/document/finders.rb +131 -0
  50. data/lib/ripple/document/key.rb +43 -0
  51. data/lib/ripple/document/link.rb +30 -0
  52. data/lib/ripple/document/persistence.rb +115 -0
  53. data/lib/ripple/embedded_document.rb +64 -0
  54. data/lib/ripple/embedded_document/around_callbacks.rb +18 -0
  55. data/lib/ripple/embedded_document/finders.rb +26 -0
  56. data/lib/ripple/embedded_document/persistence.rb +77 -0
  57. data/lib/ripple/i18n.rb +2 -0
  58. data/lib/ripple/inspection.rb +32 -0
  59. data/lib/ripple/locale/en.yml +21 -0
  60. data/lib/ripple/nested_attributes.rb +265 -0
  61. data/lib/ripple/observable.rb +28 -0
  62. data/lib/ripple/properties.rb +73 -0
  63. data/lib/ripple/property_type_mismatch.rb +12 -0
  64. data/lib/ripple/railtie.rb +13 -0
  65. data/lib/ripple/serialization.rb +84 -0
  66. data/lib/ripple/timestamps.rb +27 -0
  67. data/lib/ripple/translation.rb +14 -0
  68. data/lib/ripple/validations.rb +67 -0
  69. data/lib/ripple/validations/associated_validator.rb +43 -0
  70. data/ripple.gemspec +46 -0
  71. data/seomoz-ripple.gemspec +46 -0
  72. data/spec/fixtures/config.yml +8 -0
  73. data/spec/integration/ripple/associations_spec.rb +220 -0
  74. data/spec/integration/ripple/conflict_resolution_spec.rb +293 -0
  75. data/spec/integration/ripple/nested_attributes_spec.rb +264 -0
  76. data/spec/integration/ripple/persistence_spec.rb +57 -0
  77. data/spec/integration/ripple/search_associations_spec.rb +42 -0
  78. data/spec/ripple/associations/many_embedded_proxy_spec.rb +122 -0
  79. data/spec/ripple/associations/many_linked_proxy_spec.rb +191 -0
  80. data/spec/ripple/associations/many_reference_proxy_spec.rb +170 -0
  81. data/spec/ripple/associations/many_stored_key_proxy_spec.rb +158 -0
  82. data/spec/ripple/associations/one_embedded_proxy_spec.rb +125 -0
  83. data/spec/ripple/associations/one_key_proxy_spec.rb +82 -0
  84. data/spec/ripple/associations/one_linked_proxy_spec.rb +91 -0
  85. data/spec/ripple/associations/one_stored_key_proxy_spec.rb +72 -0
  86. data/spec/ripple/associations/proxy_spec.rb +84 -0
  87. data/spec/ripple/associations_spec.rb +129 -0
  88. data/spec/ripple/attribute_methods/dirty_spec.rb +80 -0
  89. data/spec/ripple/attribute_methods_spec.rb +230 -0
  90. data/spec/ripple/bucket_access_spec.rb +25 -0
  91. data/spec/ripple/callbacks_spec.rb +176 -0
  92. data/spec/ripple/conflict/resolver_spec.rb +42 -0
  93. data/spec/ripple/conversion_spec.rb +22 -0
  94. data/spec/ripple/core_ext_spec.rb +103 -0
  95. data/spec/ripple/document/link_spec.rb +67 -0
  96. data/spec/ripple/document_spec.rb +96 -0
  97. data/spec/ripple/embedded_document/finders_spec.rb +29 -0
  98. data/spec/ripple/embedded_document/persistence_spec.rb +80 -0
  99. data/spec/ripple/embedded_document_spec.rb +84 -0
  100. data/spec/ripple/finders_spec.rb +217 -0
  101. data/spec/ripple/inspection_spec.rb +51 -0
  102. data/spec/ripple/key_spec.rb +30 -0
  103. data/spec/ripple/observable_spec.rb +121 -0
  104. data/spec/ripple/persistence_spec.rb +326 -0
  105. data/spec/ripple/properties_spec.rb +262 -0
  106. data/spec/ripple/ripple_spec.rb +71 -0
  107. data/spec/ripple/serialization_spec.rb +51 -0
  108. data/spec/ripple/timestamps_spec.rb +76 -0
  109. data/spec/ripple/validations/associated_validator_spec.rb +77 -0
  110. data/spec/ripple/validations_spec.rb +104 -0
  111. data/spec/spec_helper.rb +26 -0
  112. data/spec/support/associations.rb +1 -0
  113. data/spec/support/associations/proxies.rb +17 -0
  114. data/spec/support/integration_setup.rb +11 -0
  115. data/spec/support/mocks.rb +4 -0
  116. data/spec/support/models.rb +4 -0
  117. data/spec/support/models/address.rb +12 -0
  118. data/spec/support/models/box.rb +13 -0
  119. data/spec/support/models/car.rb +16 -0
  120. data/spec/support/models/cardboard_box.rb +3 -0
  121. data/spec/support/models/clock.rb +12 -0
  122. data/spec/support/models/clock_observer.rb +3 -0
  123. data/spec/support/models/company.rb +23 -0
  124. data/spec/support/models/customer.rb +4 -0
  125. data/spec/support/models/driver.rb +6 -0
  126. data/spec/support/models/email.rb +4 -0
  127. data/spec/support/models/engine.rb +5 -0
  128. data/spec/support/models/family.rb +16 -0
  129. data/spec/support/models/favorite.rb +4 -0
  130. data/spec/support/models/invoice.rb +7 -0
  131. data/spec/support/models/late_invoice.rb +3 -0
  132. data/spec/support/models/ninja.rb +9 -0
  133. data/spec/support/models/note.rb +5 -0
  134. data/spec/support/models/page.rb +4 -0
  135. data/spec/support/models/paid_invoice.rb +4 -0
  136. data/spec/support/models/passenger.rb +6 -0
  137. data/spec/support/models/profile.rb +10 -0
  138. data/spec/support/models/seat.rb +5 -0
  139. data/spec/support/models/subscription.rb +27 -0
  140. data/spec/support/models/tasks.rb +14 -0
  141. data/spec/support/models/team.rb +11 -0
  142. data/spec/support/models/transactions.rb +17 -0
  143. data/spec/support/models/tree.rb +4 -0
  144. data/spec/support/models/user.rb +10 -0
  145. data/spec/support/models/wheel.rb +6 -0
  146. data/spec/support/models/widget.rb +22 -0
  147. data/spec/support/test_server.rb +18 -0
  148. data/spec/support/test_server.yml.example +2 -0
  149. metadata +362 -0
@@ -0,0 +1,64 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/hash/except'
3
+ require 'ripple/translation'
4
+ require 'ripple/embedded_document/around_callbacks'
5
+ require 'ripple/embedded_document/finders'
6
+ require 'ripple/embedded_document/persistence'
7
+ require 'ripple/properties'
8
+ require 'ripple/attribute_methods'
9
+ require 'ripple/timestamps'
10
+ require 'ripple/validations'
11
+ require 'ripple/associations'
12
+ require 'ripple/callbacks'
13
+ require 'ripple/conversion'
14
+ require 'ripple/inspection'
15
+ require 'ripple/nested_attributes'
16
+ require 'ripple/serialization'
17
+
18
+ module Ripple
19
+ # Represents a document model that is composed into or stored in a parent
20
+ # Document. Embedded documents may also embed other documents, have
21
+ # callbacks and validations, but are solely dependent on the parent Document.
22
+ module EmbeddedDocument
23
+ extend ActiveSupport::Concern
24
+ include Translation
25
+
26
+ included do
27
+ extend ActiveModel::Naming
28
+ include Persistence
29
+ extend Ripple::Properties
30
+ include Ripple::AttributeMethods
31
+ include Ripple::Timestamps
32
+ include Ripple::Validations
33
+ include Ripple::Associations
34
+ include Ripple::Callbacks
35
+ include Ripple::EmbeddedDocument::AroundCallbacks
36
+ include Ripple::Conversion
37
+ include Finders
38
+ include Ripple::Inspection
39
+ include Ripple::NestedAttributes
40
+ include Ripple::Serialization
41
+ end
42
+
43
+ module ClassMethods
44
+ def embeddable?
45
+ true
46
+ end
47
+ end
48
+
49
+ module InstanceMethods
50
+ def ==(other)
51
+ self.class == other.class &&
52
+ _parent_document == other._parent_document &&
53
+ serializable_hash == other.serializable_hash
54
+ end
55
+ alias eql? ==
56
+
57
+ def hash
58
+ parent = [_parent_document.class]
59
+ parent << [_parent_document.key] if _parent_document.respond_to?(:key)
60
+ [ self.class, parent, serializable_hash ].hash
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ require 'ripple/callbacks'
2
+
3
+ module Ripple
4
+ module EmbeddedDocument
5
+ module AroundCallbacks
6
+ extend ActiveSupport::Concern
7
+ extend Translation
8
+
9
+ included do
10
+ Ripple::Callbacks::CALLBACK_TYPES.each do |type|
11
+ define_singleton_method "around_#{type}" do |*args|
12
+ raise NotImplementedError.new(t("around_callbacks_not_supported", :type => type))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/inflector'
3
+
4
+ module Ripple
5
+ module EmbeddedDocument
6
+ # @private
7
+ module Finders
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def instantiate(attrs)
12
+ begin
13
+ klass = attrs['_type'].present? ? attrs.delete('_type').constantize : self
14
+ rescue NameError
15
+ klass = self
16
+ end
17
+ klass.new.tap do |object|
18
+ object.raw_attributes = attrs
19
+ object.changed_attributes.clear
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,77 @@
1
+ require 'active_support/concern'
2
+ require 'ripple/translation'
3
+
4
+ module Ripple
5
+ # Exception raised when save is called on an EmbeddedDocument that
6
+ # is not attached to a root Document.
7
+ class NoRootDocument < StandardError
8
+ include Translation
9
+ def initialize(doc, method)
10
+ super(t("no_root_document", :doc => doc.inspect, :method => method))
11
+ end
12
+ end
13
+
14
+ module EmbeddedDocument
15
+ # Adds methods to {Ripple::EmbeddedDocument} that delegate storage
16
+ # operations to the parent document.
17
+ module Persistence
18
+ extend ActiveSupport::Concern
19
+
20
+ module ClassMethods
21
+ # Creates a method that points to the parent document.
22
+ def embedded_in(parent)
23
+ define_method(parent) { @_parent_document }
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ # The parent document to this embedded document. This may be a
29
+ # {Ripple::Document} or another {Ripple::EmbeddedDocument}.
30
+ attr_accessor :_parent_document
31
+
32
+ # Whether the root document is unsaved.
33
+ def new?
34
+ if _root_document
35
+ _root_document.new?
36
+ else
37
+ true
38
+ end
39
+ end
40
+
41
+ # Sets this embedded documents attributes and saves the root document.
42
+ def update_attributes(attrs)
43
+ self.attributes = attrs
44
+ save
45
+ end
46
+
47
+ # Updates this embedded document's attribute and saves the
48
+ # root document, skipping validations.
49
+ def update_attribute(attribute, value)
50
+ send("#{attribute}=", value)
51
+ save(:validate => false)
52
+ end
53
+
54
+ # Saves this embedded document by delegating to the root document.
55
+ def save(*args)
56
+ if _root_document
57
+ run_save_callbacks do
58
+ _root_document.save(*args)
59
+ end
60
+ else
61
+ raise NoRootDocument.new(self, :save)
62
+ end
63
+ end
64
+
65
+ # @private
66
+ def attributes_for_persistence
67
+ raw_attributes.merge("_type" => self.class.name)
68
+ end
69
+
70
+ # The root {Ripple::Document} to which this embedded document belongs.
71
+ def _root_document
72
+ @_parent_document.try(:_root_document)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,2 @@
1
+ require 'active_support/i18n'
2
+ I18n.load_path << File.expand_path("../locale/en.yml", __FILE__)
@@ -0,0 +1,32 @@
1
+ require 'ripple'
2
+
3
+ module Ripple
4
+ # Makes IRB and other inspect output a bit friendlier
5
+ module Inspection
6
+
7
+ # A human-readable version of the {Ripple::Document} or {Ripple::EmbeddedDocument}
8
+ # plus attributes
9
+ def inspect
10
+ attribute_list = attributes_for_persistence.except("_type").map {|k,v| "#{k}=#{v.inspect}" }.join(' ')
11
+ inspection_string(attribute_list)
12
+ end
13
+
14
+ # A string representation of the {Ripple::Document} or {Ripple::EmbeddedDocument}
15
+ def to_s
16
+ inspection_string
17
+ end
18
+
19
+ private
20
+
21
+ def inspection_string(extra = nil)
22
+ body = self.class.name + persistance_identifier
23
+ body += " #{extra}" if extra
24
+ "<#{body}>"
25
+ end
26
+
27
+ def persistance_identifier
28
+ self.class.embeddable? ? "" : ":#{key || '[new]'}"
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ en:
2
+ ripple:
3
+ around_callbacks_not_supported: "around_%{type} callbacks are not supported on embedded documents. See https://github.com/seancribbs/ripple/issues/188 for more detail."
4
+ associated_document_error_summary: "for %{doc_type} %{doc_id}: %{errors}"
5
+ attribute_hash: "value of attributes must be a Hash"
6
+ conflict_handler_not_implemented: "No on_conflict handler has been implemented for %{document}"
7
+ document_invalid: "Validation failed: %{errors}"
8
+ document_not_found:
9
+ no_key: "Couldn't find document without a key"
10
+ one_key: "Couldn't find document with key: %{key}"
11
+ many_keys: "Couldn't find documents with keys: %{keys}"
12
+ invalid_association_value: "Invalid value %{value} for association %{name} of type %{klass} on %{owner}"
13
+ many_association_validation_error: "are invalid (%{association_errors})"
14
+ many_key_association: "You cannot use a many association using :key"
15
+ missing_configuration: "You are missing your ripple configuration file that should be at %{file}"
16
+ no_root_document: "You cannot call %{method} on %{doc} without a root document"
17
+ one_association_validation_error: "is invalid (%{association_errors})"
18
+ property_type_mismatch: "Cannot cast %{value} into a %{class}"
19
+ undefined_property: "Undefined property :%{prop} for class '%{class}'"
20
+ unexpected_conflicts: "Conflict cannot be resolved for %{conflicts} (for %{document})"
21
+
@@ -0,0 +1,265 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/class/inheritable_attributes'
3
+
4
+ module Ripple
5
+ module NestedAttributes #:nodoc:
6
+ extend ActiveSupport::Concern
7
+
8
+ UNASSIGNABLE_KEYS = %w{ _destroy }
9
+ TRUE_VALUES = [ true, "true", 1, "1", "yes", "ok", "y" ]
10
+
11
+ included do
12
+ class_inheritable_accessor :nested_attributes_options, :instance_writer => false
13
+ self.nested_attributes_options = {}
14
+ end
15
+
16
+ # = Nested Attributes
17
+ #
18
+ # This is similar to the `accepts_nested_attributes` functionality
19
+ # as found in AR. This allows the use update attributes and create
20
+ # new child records through the parent. It also allows the use of
21
+ # the `fields_for` form view helper, using a presenter pattern.
22
+ #
23
+ # To enable in the model, call the class method, using the same
24
+ # relationship as defined in the `one` or `many`.
25
+ #
26
+ # class Shipment
27
+ # include Ripple::Document
28
+ # one :box
29
+ # many :addresses
30
+ # accepts_nested_attributes_for :box, :addresses
31
+ # end
32
+ #
33
+ # == One
34
+ #
35
+ # Given this model:
36
+ #
37
+ # class Shipment
38
+ # include Ripple::Document
39
+ # one :box
40
+ # accepts_nested_attributes_for :box
41
+ # end
42
+ #
43
+ # This allows creating a box child during creation:
44
+ #
45
+ # shipment = Shipment.create(:box_attributes => { :shape => 'square' })
46
+ # shipment.box.shape # => 'square'
47
+ #
48
+ # This also allows updating box attributes:
49
+ #
50
+ # shipment.update_attributes(:box_attributes => { :key => 'xxx', :shape => 'triangle' })
51
+ # shipment.box.shape # => 'triangle'
52
+ #
53
+ # == Many
54
+ #
55
+ # Given this model
56
+ #
57
+ # class Manifest
58
+ # include Ripple::Document
59
+ # many :shipments
60
+ # accepts_nested_attributes_for :shipments
61
+ # end
62
+ #
63
+ # This allows creating several shipments during manifest creation:
64
+ #
65
+ # manifest = Manifest.create(:shipments_attributes => [ { :reference => "foo1" }, { :reference => "foo2" } ])
66
+ # manifest.shipments.size # => 2
67
+ # manifest.shipments.first.reference # => foo1
68
+ # manifest.shipments.second.reference # => foo2
69
+ #
70
+ # And updating shipment attributes:
71
+ #
72
+ # manifest.update_attributes(:shipment_attributes => [ { :key => 'xxx', :reference => 'updated foo1' },
73
+ # { :key => 'yyy', :reference => 'updated foo2' } ])
74
+ # manifest.shipments.first.reference # => updated foo1
75
+ # manifest.shipments.second.reference # => updated foo2
76
+ #
77
+ # NOTE: On many embedded, then entire collection of embedded documents is replaced, as there
78
+ # is no key to specifically update.
79
+ #
80
+ # Given
81
+ #
82
+ # class Manifest
83
+ # include Ripple::Documnet
84
+ # many :signatures
85
+ # accepts_nested_attributes_for :signatures
86
+ # end
87
+ #
88
+ # class Signature
89
+ # include Ripple::EmbeddedDocument
90
+ # property :esignature, String
91
+ # end
92
+ #
93
+ # The assigning of attributes replaces existing:
94
+ #
95
+ # manifest = Manifest.create(:signature_attributes => [ { :esig => 'a00001' }, { :esig => 'b00001' } ]
96
+ # manifest.signatures # => [<Signature esig="a00001">, <Signature esig="b00001">]
97
+ #
98
+ # manifest.signature_attributes = [ { :esig => 'c00001' } ]
99
+ # manifest.signatures # => [<Signature esig="c00001">]
100
+ #
101
+ module ClassMethods
102
+
103
+ def accepts_nested_attributes_for(*attr_names)
104
+ options = { :allow_destroy => false }
105
+ options.update(attr_names.extract_options!)
106
+
107
+ attr_names.each do |association_name|
108
+ if association = self.associations[association_name]
109
+ nested_attributes_options[association_name.to_sym] = options
110
+
111
+ class_eval %{
112
+ def #{association_name}_attributes=(attributes)
113
+ assign_nested_attributes_for_#{association.type}_association(:#{association_name}, attributes)
114
+ end
115
+
116
+ before_save :autosave_nested_attributes_for_#{association_name}
117
+ before_save :destroy_marked_for_destruction
118
+
119
+ private
120
+
121
+ def autosave_nested_attributes_for_#{association_name}
122
+ save_nested_attributes_for_#{association.type}_association(:#{association_name}) if self.autosave[:#{association_name}]
123
+ end
124
+ }, __FILE__, __LINE__
125
+ else
126
+ raise ArgumentError, "Association #{association_name} not found!"
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ module InstanceMethods
133
+
134
+ protected
135
+
136
+ def autosave
137
+ @autosave_nested_attributes_for ||= {}
138
+ end
139
+
140
+ def marked_for_destruction
141
+ @marked_for_destruction ||= {}
142
+ end
143
+
144
+ private
145
+
146
+ def save_nested_attributes_for_one_association(association_name)
147
+ send(association_name).save
148
+ end
149
+
150
+ def save_nested_attributes_for_many_association(association_name)
151
+ send(association_name).map(&:save)
152
+ end
153
+
154
+ def destroy_marked_for_destruction
155
+ self.marked_for_destruction.each_pair do |association_name, resources|
156
+ resources.map(&:destroy)
157
+ send(association_name).reload
158
+ end
159
+ end
160
+
161
+ def destroy_nested_many_association(association_name)
162
+ send(association_name).map(&:destroy)
163
+ end
164
+
165
+ def assign_nested_attributes_for_one_association(association_name, attributes)
166
+ association = self.class.associations[association_name]
167
+ if association.embedded?
168
+ assign_nested_attributes_for_one_embedded_association(association_name, attributes)
169
+ else
170
+ self.autosave[association_name] = true
171
+ assign_nested_attributes_for_one_linked_association(association_name, attributes)
172
+ end
173
+ end
174
+
175
+ def assign_nested_attributes_for_one_embedded_association(association_name, attributes)
176
+ send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
177
+ end
178
+
179
+ def assign_nested_attributes_for_one_linked_association(association_name, attributes)
180
+ attributes = attributes.stringify_keys
181
+ options = nested_attributes_options[association_name]
182
+
183
+ if attributes[key_attr.to_s].blank? && !reject_new_record?(association_name, attributes)
184
+ send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
185
+ else
186
+ if ((existing_record = send(association_name)).key.to_s == attributes[key_attr.to_s].to_s)
187
+ assign_to_or_mark_for_destruction(existing_record, attributes, association_name, options[:allow_destroy])
188
+ else
189
+ raise ArgumentError, "Attempting to update a child that isn't already associated to the parent."
190
+ end
191
+ end
192
+ end
193
+
194
+ def assign_nested_attributes_for_many_association(association_name, attributes_collection)
195
+ unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
196
+ raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
197
+ end
198
+
199
+ if attributes_collection.is_a? Hash
200
+ attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes }
201
+ end
202
+
203
+ association = self.class.associations[association_name]
204
+ if association.embedded?
205
+ assign_nested_attributes_for_many_embedded_association(association_name, attributes_collection)
206
+ else
207
+ self.autosave[association_name] = true
208
+ assign_nested_attributes_for_many_linked_association(association_name, attributes_collection)
209
+ end
210
+ end
211
+
212
+ def assign_nested_attributes_for_many_embedded_association(association_name, attributes_collection)
213
+ options = nested_attributes_options[association_name]
214
+ send(:"#{association_name}=", []) # Clobber existing
215
+ attributes_collection.each do |attributes|
216
+ attributes = attributes.stringify_keys
217
+ if !reject_new_record?(association_name, attributes)
218
+ send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
219
+ end
220
+ end
221
+ end
222
+
223
+ def assign_nested_attributes_for_many_linked_association(association_name, attributes_collection)
224
+ options = nested_attributes_options[association_name]
225
+ attributes_collection.each do |attributes|
226
+ attributes = attributes.stringify_keys
227
+
228
+ if attributes[key_attr.to_s].blank? && !reject_new_record?(association_name, attributes)
229
+ send(association_name).build(attributes.except(*UNASSIGNABLE_KEYS))
230
+ elsif existing_record = send(association_name).detect { |record| record.key.to_s == attributes[key_attr.to_s].to_s }
231
+ assign_to_or_mark_for_destruction(existing_record, attributes, association_name, options[:allow_destroy])
232
+ end
233
+ end
234
+ end
235
+ end
236
+
237
+ def assign_to_or_mark_for_destruction(record, attributes, association_name, allow_destroy)
238
+ if has_destroy_flag?(attributes) && allow_destroy
239
+ (self.marked_for_destruction[association_name] ||= []) << record
240
+ else
241
+ record.attributes = attributes.except(*UNASSIGNABLE_KEYS)
242
+ end
243
+ end
244
+
245
+ def has_destroy_flag?(hash)
246
+ TRUE_VALUES.include?(hash.stringify_keys['_destroy'])
247
+ end
248
+
249
+ def reject_new_record?(association_name, attributes)
250
+ has_destroy_flag?(attributes) || call_reject_if(association_name, attributes)
251
+ end
252
+
253
+ def call_reject_if(association_name, attributes)
254
+ attributes = attributes.stringify_keys
255
+ case callback = nested_attributes_options[association_name][:reject_if]
256
+ when Symbol
257
+ method(callback).arity == 0 ? send(callback) : send(callback, attributes)
258
+ when Proc
259
+ callback.call(attributes)
260
+ end
261
+ end
262
+
263
+ end
264
+
265
+ end