object_attorney 2.10.7 → 2.10.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjdkN2UzMGQ0YjgwNTMxN2Q4YjEyMDY1OWE1YmZiN2JjYmZhMDI4Mw==
4
+ ZTZjYmJlOTBjNzg4MWNmZTUxMTI2MzZjZGFkZmE4NTlhOTMxNjUyNg==
5
5
  data.tar.gz: !binary |-
6
- MWM2YzBmZTg1NzBmMTRkZWM2YmE1YWQ5YzVkZjc5OWUxZTU1YzZkYg==
6
+ YjdiNzA3MTlhMDMxYjZlNzM0NmFjMDMzMzdiYTMzNmRiZGYwZGFjZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NmUwODRmNmEzOWY4Zjg2YTNiY2QyZWYwNDMwMjlkNjBmODJlMWIzOWZjOTUw
10
- N2E0MzgyZGI4MTMyYTNmZDRkNDBkYjYyM2E2ZDdjYjQ5YThhNGEyMjFhZDY3
11
- ODc4MzhmMmRhNmI2YjZkMGU3N2Y4ZTljYTkxOGJmNmNkMWEwZTc=
9
+ NDEyY2NmYTM0ODVkMjA2NDZkMWJhZWFiYTBmMjFmODVmNmIzZWQ0NDc3MGRm
10
+ YjhkZWVmNjFhZTc5NGUxMThlNDI2Njg2NDRjODQ0MTA1ZDNlNmYzZmFmYTE0
11
+ ZDc4NTUwN2M0YjI2ZGNjZWU1MGRhMTg4MjYyMzVkYmQzNDFlYTk=
12
12
  data.tar.gz: !binary |-
13
- NGJjM2ZhNDdkMGZiZDc1YzMyMWI2M2ZlOTU5M2QzMjQ4YThlYWMzNjkzNTVi
14
- Y2ExNzhhYTIyZjExYTBmZjQwZjdhMWRlZjM2Nzk0NGUwZWIxZDQyNjAwYjI2
15
- M2E1ZGU5NTkxNjY4YjFmMmRjNDhjN2U3N2JlMmEyNmVlYzdkN2E=
13
+ OTFkNjY0NmNkNzZmYzU4NjIwMGM1NDgxYTNiMjZlYzU0OTY2ZDU4MTM5OGRj
14
+ NjNiZTU2NDI1OWMzN2MyMGFiNjAyODhmMDVkMzY0NTZmZGU0Yjg2MWMxYzA0
15
+ MWUwM2UyYTBlNmNiOGVjMjc2ZTBhNzBlNGU4YmNkNTZhODEwYmE=
data/console.rb ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << './'
4
+ $LOAD_PATH << './lib'
5
+
6
+ require 'spec/require_helper'
7
+
8
+ address_attributes = {
9
+ street: 'street',
10
+ city: 'city'
11
+ }
12
+
13
+ p = PostWithCommentsAndAddressForm.new(address_attributes: address_attributes)
14
+
15
+ a = AddressForm.new(post_attributes: { title: 'asd', body: 'body' })
16
+
17
+ binding.pry
@@ -6,6 +6,7 @@ module ObjectAttorney
6
6
  return if attributes.blank?
7
7
 
8
8
  attributes.each do |name, value|
9
+ name, value = check_for_hidden_nested_attributes(name, value)
9
10
  send("#{name}=", value) if allowed_attribute(name)
10
11
  end
11
12
 
@@ -14,6 +15,25 @@ module ObjectAttorney
14
15
 
15
16
  protected #################### PROTECTED METHODS DOWN BELOW ######################
16
17
 
18
+ def check_for_hidden_nested_attributes(name, value)
19
+ name_sym = name.to_sym
20
+
21
+ reflection = self.class.reflect_on_association(name_sym)
22
+
23
+ if reflection
24
+ if reflection.has_many? && value.is_a?(Array)
25
+ hash = {}
26
+ value.each_with_index do |_value, index|
27
+ hash[index.to_s] = _value
28
+ end
29
+ value = hash
30
+ end
31
+ name = "#{name}_attributes"
32
+ end
33
+
34
+ [name, value]
35
+ end
36
+
17
37
  def parsing_arguments(attributes, object)
18
38
  if !attributes.is_a?(Hash) && object.blank?
19
39
  object = attributes
@@ -21,7 +41,7 @@ module ObjectAttorney
21
41
  end
22
42
 
23
43
  attributes = {} if attributes.blank?
24
-
44
+
25
45
  [attributes.symbolize_keys, object]
26
46
  end
27
47
 
@@ -48,7 +48,7 @@ module ObjectAttorney
48
48
 
49
49
  def save_or_destroy_nested_objects(save_method, association_macro)
50
50
  nested_objects(association_macro).map do |reflection, nested_object|
51
-
51
+
52
52
  populate_foreign_key(self, nested_object, reflection, :has_many)
53
53
  populate_foreign_key(self, nested_object, reflection, :has_one)
54
54
 
@@ -73,7 +73,7 @@ module ObjectAttorney
73
73
 
74
74
  def get_attributes_for_existing(nested_object_name, existing_nested_object)
75
75
  attributes = send("#{nested_object_name}_attributes")
76
-
76
+
77
77
  if attributes.present?
78
78
  (attributes.values.select { |x| x[:id].to_i == existing_nested_object.id }.first || {}).symbolize_keys
79
79
  else
@@ -134,7 +134,7 @@ module ObjectAttorney
134
134
 
135
135
  def get_existing_and_new_nested_objects(nested_object_name)
136
136
  existing_and_new_nested_objects = []
137
-
137
+
138
138
  update_existing_nested_objects(existing_and_new_nested_objects, nested_object_name)
139
139
  build_new_nested_objects(existing_and_new_nested_objects, nested_object_name)
140
140
 
@@ -169,7 +169,7 @@ module ObjectAttorney
169
169
  reflection = self.class.reflect_on_association(nested_object_name)
170
170
 
171
171
  return nil if reflection.options[:new_records] == false
172
-
172
+
173
173
  if new_nested_object.present?
174
174
  new_nested_object = assign_attributes_or_build_nested_object(reflection, attributes, new_nested_object)
175
175
 
@@ -200,7 +200,7 @@ module ObjectAttorney
200
200
 
201
201
  def can_represented_object_build_nested?(reflection, nested_object_name)
202
202
  return false if represented_object.blank?
203
-
203
+
204
204
  represented_object.respond_to?("build_#{nested_object_name}") || represented_object.send(nested_object_name).respond_to?(:build)
205
205
  end
206
206
 
@@ -218,18 +218,18 @@ module ObjectAttorney
218
218
  nested_relection = self.class.reflect_on_association(nested_object_name)
219
219
 
220
220
  return [] if nested_relection.options[:existing_records] == false
221
-
221
+
222
222
  if nested_relection.options[:standalone] == true
223
223
  return nested_relection.has_many? ? (nested_relection.klass.try(:all) || []) : nil
224
224
  end
225
225
 
226
226
  existing = represented_object.nil? ? (nested_relection.klass.try(:all) || []) : (represented_object.send(nested_object_name) || (nested_relection.has_many? ? [] : nil))
227
-
227
+
228
228
  if represented_object.present?
229
229
  if self.class.represented_object_class.respond_to?(:reflect_on_association)
230
230
  existing = _existing_in_form_objects(existing, nested_relection) if nested_relection.klass != self.class.represented_object_class.reflect_on_association(nested_object_name).try(:klass)
231
231
  else
232
- existing = _existing_in_form_objects(existing, nested_relection)
232
+ existing = _existing_in_form_objects(existing, nested_relection)
233
233
  end
234
234
  end
235
235
 
@@ -239,8 +239,10 @@ module ObjectAttorney
239
239
  def _existing_in_form_objects(existing, nested_relection)
240
240
  if existing.respond_to?(:map)
241
241
  existing.map { |existing_nested_object| nested_relection.klass.new({}, existing_nested_object) }
242
- else
242
+ elsif existing.present?
243
243
  nested_relection.klass.new({}, existing)
244
+ else
245
+ nil
244
246
  end
245
247
  end
246
248
 
@@ -1,3 +1,3 @@
1
1
  module ObjectAttorney
2
- VERSION = "2.10.7"
2
+ VERSION = "2.10.9"
3
3
  end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe AddressForm do
4
+
5
+ it "FormObject with a belongs_to with a differente class then the represented_object's relation" do
6
+ params = {
7
+ address: {
8
+ post_attributes: { title: 'asd', body: 'body' }
9
+ }
10
+ }
11
+
12
+ address_form = AddressForm.new(params[:address])
13
+
14
+ address_form.address.post.should == nil
15
+ address_form.post
16
+ address_form.address.post.should_not == nil
17
+ end
18
+
19
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe UserForm do
4
+
5
+ it "..." do
6
+ params = {
7
+ user: {
8
+ email: 'email@gmail.com',
9
+ address: { street: "street1" },
10
+ comments: [
11
+ { body: "body1" },
12
+ { body: "body2" }
13
+ ]
14
+ }
15
+ }
16
+
17
+ user_form = UserAndCommentsForm.new(params[:user])
18
+
19
+ expect(user_form.address.nil?).to eq(false)
20
+ expect(user_form.comments.length).to eq(2)
21
+ end
22
+
23
+ end
@@ -0,0 +1,32 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require 'bundler'
5
+ Bundler.setup
6
+ require 'rspec'
7
+ require 'pry'
8
+ #require 'database_cleaner'
9
+
10
+ require 'object_attorney'
11
+ require 'support/database_setup'
12
+ require 'support/active_model/validations'
13
+ require 'support/models/address'
14
+ require 'support/models/comment'
15
+ require 'support/models/post'
16
+ require 'support/models/user'
17
+
18
+ require 'support/form_objects/post_form'
19
+ require 'support/form_objects/address_form'
20
+ require 'support/form_objects/post_validations_form'
21
+ require 'support/form_objects/comment_form'
22
+ require 'support/form_objects/post_with_comment_form'
23
+ require 'support/form_objects/post_with_comment_validations_form'
24
+ require 'support/form_objects/post_with_comments_and_address_form'
25
+ require 'support/form_objects/post_with_only_new_comments_form'
26
+ require 'support/form_objects/post_with_only_existing_comments_form'
27
+ require 'support/form_objects/bulk_posts_form'
28
+ require 'support/form_objects/bulk_posts_allow_only_existing_form'
29
+ require 'support/form_objects/bulk_posts_allow_only_new_form'
30
+ require 'support/form_objects/bulk_posts_with_form_objects_form'
31
+ require 'support/form_objects/user_and_comments_form'
32
+ require 'support/form_objects/user_form'
data/spec/spec_helper.rb CHANGED
@@ -18,6 +18,7 @@ require 'support/models/post'
18
18
  require 'support/models/user'
19
19
 
20
20
  require 'support/form_objects/post_form'
21
+ require 'support/form_objects/address_form'
21
22
  require 'support/form_objects/post_validations_form'
22
23
  require 'support/form_objects/comment_form'
23
24
  require 'support/form_objects/post_with_comment_form'
@@ -35,7 +36,7 @@ require 'support/form_objects/user_form'
35
36
  RSpec.configure do |config|
36
37
  #config.treat_symbols_as_metadata_keys_with_true_values = true
37
38
  #config.filter_run :current
38
-
39
+
39
40
  I18n.enforce_available_locales = false
40
41
 
41
42
  # see: http://iain.nl/testing-activerecord-in-isolation
@@ -0,0 +1,9 @@
1
+ class AddressForm
2
+
3
+ include ObjectAttorney
4
+
5
+ represents :address, properties: [:street, :city]
6
+
7
+ belongs_to :post, class_name: PostForm::Base
8
+
9
+ end
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.10.7
4
+ version: 2.10.9
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-05-27 00:00:00.000000000 Z
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,7 @@ files:
67
67
  - LICENSE.txt
68
68
  - README.md
69
69
  - Rakefile
70
+ - console.rb
70
71
  - db/migrate/20131205114000_create_users.rb
71
72
  - db/migrate/20131205114900_create_posts.rb
72
73
  - db/migrate/20131205114901_create_comments.rb
@@ -89,6 +90,7 @@ files:
89
90
  - lib/object_attorney/version.rb
90
91
  - not_used/nested_uniqueness_validator.rb
91
92
  - object_attorney.gemspec
93
+ - spec/object_attorney/address_form_spec.rb
92
94
  - spec/object_attorney/bulk_post_form_spec.rb
93
95
  - spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb
94
96
  - spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
@@ -103,11 +105,14 @@ files:
103
105
  - spec/object_attorney/post_with_only_existing_comments_form_spec.rb
104
106
  - spec/object_attorney/post_with_only_new_comments_form_spec.rb
105
107
  - spec/object_attorney/serialization_spec.rb
108
+ - spec/object_attorney/test_spec.rb
106
109
  - spec/object_attorney/user_and_comments_form_spec.rb
107
110
  - spec/object_attorney/user_form_spec.rb
111
+ - spec/require_helper.rb
108
112
  - spec/spec_helper.rb
109
113
  - spec/support/active_model/validations.rb
110
114
  - spec/support/database_setup.rb
115
+ - spec/support/form_objects/address_form.rb
111
116
  - spec/support/form_objects/bulk_posts_allow_only_existing_form.rb
112
117
  - spec/support/form_objects/bulk_posts_allow_only_new_form.rb
113
118
  - spec/support/form_objects/bulk_posts_form.rb
@@ -146,13 +151,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
151
  version: '0'
147
152
  requirements: []
148
153
  rubyforge_project:
149
- rubygems_version: 2.1.10
154
+ rubygems_version: 2.2.2
150
155
  signing_key:
151
156
  specification_version: 4
152
157
  summary: This gem allows you to extract the code responsible for 'validations', 'nested
153
158
  objects' and 'strong parameters' from your model onto a specific class for a specific
154
159
  use case.
155
160
  test_files:
161
+ - spec/object_attorney/address_form_spec.rb
156
162
  - spec/object_attorney/bulk_post_form_spec.rb
157
163
  - spec/object_attorney/bulk_posts_allow_only_existing_form_spec.rb
158
164
  - spec/object_attorney/bulk_posts_allow_only_new_form_spec.rb
@@ -167,11 +173,14 @@ test_files:
167
173
  - spec/object_attorney/post_with_only_existing_comments_form_spec.rb
168
174
  - spec/object_attorney/post_with_only_new_comments_form_spec.rb
169
175
  - spec/object_attorney/serialization_spec.rb
176
+ - spec/object_attorney/test_spec.rb
170
177
  - spec/object_attorney/user_and_comments_form_spec.rb
171
178
  - spec/object_attorney/user_form_spec.rb
179
+ - spec/require_helper.rb
172
180
  - spec/spec_helper.rb
173
181
  - spec/support/active_model/validations.rb
174
182
  - spec/support/database_setup.rb
183
+ - spec/support/form_objects/address_form.rb
175
184
  - spec/support/form_objects/bulk_posts_allow_only_existing_form.rb
176
185
  - spec/support/form_objects/bulk_posts_allow_only_new_form.rb
177
186
  - spec/support/form_objects/bulk_posts_form.rb