object_attorney 2.2.5 → 2.2.6
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.
- checksums.yaml +8 -8
- data/lib/object_attorney.rb +5 -2
- data/lib/object_attorney/nested_objects.rb +31 -9
- data/lib/object_attorney/version.rb +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
---
|
|
2
2
|
!binary "U0hBMQ==":
|
|
3
3
|
metadata.gz: !binary |-
|
|
4
|
-
|
|
4
|
+
NjVkNTljNjRiNzdkYzJhMTU2N2ZkM2UzYTU2ZmFkZTQ5ODUxMmQ4Mw==
|
|
5
5
|
data.tar.gz: !binary |-
|
|
6
|
-
|
|
6
|
+
MWQwZGRhNzkzNmYyMGY5ZGIxMWVhZDQ2ZDQ3ZGY0ZDQxYzViNzI0ZA==
|
|
7
7
|
SHA512:
|
|
8
8
|
metadata.gz: !binary |-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
M2U0Mjc0NWIwYzVmZTUzNDA5Y2I5NGQ0MDg1NDQxNzE0MGMwNmNiNWJkYTc3
|
|
10
|
+
NGViYzY3MmYxMmU3MzQ0MGE3MjBjYmM2ZjNkOWQ1OGViZjVjZTRhMDYzYTNj
|
|
11
|
+
ODc5YTFjYjk5OGQxNzMzYjAxZTU3NjA3YWY3YmQ4OTYwNWZiYTA=
|
|
12
12
|
data.tar.gz: !binary |-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
M2UwZmQ2MWJiZGMwNTc5NTIxZjQ0YjA5MzJlZjFkYTE2NjAzNGQyN2U1NjZh
|
|
14
|
+
ZjJmZjE1MzAzNzkzNmY5OGMzODZiZDFlMDkzMjliOTUwYWQ1MDdiMTg5NDUx
|
|
15
|
+
YTRjOTNmMDI4ODZhZTIwNGEwYjRhZDYxNjgyYTBhMTQ1MjM4YWM=
|
data/lib/object_attorney.rb
CHANGED
|
@@ -8,6 +8,8 @@ require 'active_record'
|
|
|
8
8
|
module ObjectAttorney
|
|
9
9
|
|
|
10
10
|
def initialize(attributes = {}, object = nil)
|
|
11
|
+
before_initialize(attributes)
|
|
12
|
+
|
|
11
13
|
if !attributes.is_a?(Hash) && object.blank?
|
|
12
14
|
object = attributes
|
|
13
15
|
attributes = nil
|
|
@@ -20,7 +22,7 @@ module ObjectAttorney
|
|
|
20
22
|
assign_attributes attributes
|
|
21
23
|
mark_for_destruction_if_necessary(self, attributes)
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
after_initialize(attributes)
|
|
24
26
|
end
|
|
25
27
|
|
|
26
28
|
def assign_attributes(attributes = {})
|
|
@@ -43,7 +45,8 @@ module ObjectAttorney
|
|
|
43
45
|
|
|
44
46
|
protected #################### PROTECTED METHODS DOWN BELOW ######################
|
|
45
47
|
|
|
46
|
-
def
|
|
48
|
+
def before_initialize(attributes); end
|
|
49
|
+
def after_initialize(attributes); end
|
|
47
50
|
|
|
48
51
|
def allowed_attribute(attribute)
|
|
49
52
|
respond_to?("#{attribute}=")
|
|
@@ -185,11 +185,14 @@ module ObjectAttorney
|
|
|
185
185
|
def existing_nested_objects(nested_object_name)
|
|
186
186
|
nested_relection = self.class.reflect_on_association(nested_object_name)
|
|
187
187
|
|
|
188
|
-
existing = represented_object.blank? ? nested_relection.klass.all : represented_object.send(nested_object_name)
|
|
189
|
-
existing ||= (nested_relection.has_many? ? [] : nil)
|
|
188
|
+
existing = represented_object.blank? ? nested_relection.klass.all : (represented_object.send(nested_object_name) || (nested_relection.has_many? ? [] : nil))
|
|
190
189
|
|
|
191
|
-
if represented_object.present? &&
|
|
192
|
-
|
|
190
|
+
if represented_object.present? && nested_relection.klass != self.class.represented_object_class.reflect_on_association(nested_object_name).try(:klass)
|
|
191
|
+
if existing.respond_to?(:map)
|
|
192
|
+
existing = existing.map { |existing_nested_object| nested_relection.klass.new({}, existing_nested_object) }
|
|
193
|
+
else
|
|
194
|
+
existing = nested_relection.klass.new({}, existing)
|
|
195
|
+
end
|
|
193
196
|
end
|
|
194
197
|
|
|
195
198
|
existing
|
|
@@ -198,15 +201,15 @@ module ObjectAttorney
|
|
|
198
201
|
module ClassMethods
|
|
199
202
|
|
|
200
203
|
def has_many(nested_object_name, options = {})
|
|
201
|
-
|
|
204
|
+
accepts_nested_objects_overwrite_macro(nested_object_name, options, :has_many)
|
|
202
205
|
end
|
|
203
206
|
|
|
204
207
|
def has_one(nested_object_name, options = {})
|
|
205
|
-
|
|
208
|
+
accepts_nested_objects_overwrite_macro(nested_object_name, options, :has_one)
|
|
206
209
|
end
|
|
207
210
|
|
|
208
211
|
def belongs_to(nested_object_name, options = {})
|
|
209
|
-
|
|
212
|
+
accepts_nested_objects_overwrite_macro(nested_object_name, options, :belongs_to)
|
|
210
213
|
end
|
|
211
214
|
|
|
212
215
|
def accepts_nested_objects(nested_object_name, options = {})
|
|
@@ -215,11 +218,13 @@ module ObjectAttorney
|
|
|
215
218
|
self.instance_variable_set("@#{nested_object_name}_reflection", reflection)
|
|
216
219
|
self.instance_variable_set("@association_reflections", association_reflections | [reflection])
|
|
217
220
|
|
|
218
|
-
|
|
221
|
+
define_nested_attributes_accessor(nested_object_name)
|
|
219
222
|
|
|
220
223
|
define_method(nested_object_name) { nested_getter(nested_object_name) }
|
|
221
224
|
define_method("build_#{reflection.single_name}") { |attributes = {}, nested_object = nil| build_nested_object(nested_object_name, attributes) }
|
|
222
225
|
define_method("existing_#{nested_object_name}") { existing_nested_objects(nested_object_name) }
|
|
226
|
+
|
|
227
|
+
define_nested_ids_accessor(nested_object_name, reflection)
|
|
223
228
|
end
|
|
224
229
|
|
|
225
230
|
def association_reflections
|
|
@@ -237,7 +242,24 @@ module ObjectAttorney
|
|
|
237
242
|
|
|
238
243
|
private ############################### PRIVATE METHODS ###########################
|
|
239
244
|
|
|
240
|
-
def
|
|
245
|
+
def define_nested_attributes_accessor(nested_object_name)
|
|
246
|
+
self.send(:attr_writer, "#{nested_object_name}_attributes".to_sym)
|
|
247
|
+
module_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
|
248
|
+
def #{nested_object_name}_attributes; @#{nested_object_name}_attributes ||= {}; end
|
|
249
|
+
RUBY_EVAL
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def define_nested_ids_accessor(nested_object_name, reflection)
|
|
253
|
+
return nil unless reflection.has_many?
|
|
254
|
+
|
|
255
|
+
module_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
|
256
|
+
def #{reflection.single_name}_ids
|
|
257
|
+
@#{reflection.single_name}_ids ||= nested_object_name.map(&:#{reflection.primary_key})
|
|
258
|
+
end
|
|
259
|
+
RUBY_EVAL
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def accepts_nested_objects_overwrite_macro(nested_object_name, options, macro)
|
|
241
263
|
default_options = { macro: macro }
|
|
242
264
|
options = options.is_a?(Hash) ? options.merge(default_options) : default_options
|
|
243
265
|
accepts_nested_objects(nested_object_name, options)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: object_attorney
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.2.
|
|
4
|
+
version: 2.2.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- João Gonçalves
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-01-
|
|
11
|
+
date: 2014-01-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -157,4 +157,3 @@ test_files:
|
|
|
157
157
|
- spec/support/models/comment.rb
|
|
158
158
|
- spec/support/models/post.rb
|
|
159
159
|
- spec/support/models/user.rb
|
|
160
|
-
has_rdoc:
|