slim_form_object 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb83b2555e0774293968cf63af140307e868227c
4
- data.tar.gz: ea270672e17684d5db94c684b18e1debe00c9c84
3
+ metadata.gz: da6582abfd769d9af8238cfad6ca79a890b62308
4
+ data.tar.gz: b6b7e88761e2684826122f454d8374413981f532
5
5
  SHA512:
6
- metadata.gz: f7b059c728efba05791fa2e4594c8114e0b93a3cf9f29e01a098c352eb83cc508908045ca0ffbc964331689a1b39d73233d1a88e386b083bb1e621eef40dc27c
7
- data.tar.gz: 278ecb3ff653d5568832d8c19772ac0e7938098b6af30c936a0372209a9db90ef132819b27a5eb346da7a96aac721ddd0ed0a88f3b3ea90ede68b5e5995ec9a5
6
+ metadata.gz: 1f6b7b6a8bcc973b1ad647960fffd088e2a07ceb9efc34fa30af6d823398c80bf744ec492b1d16caaa6a06b9ba5e4398ccd4b281ed477e66d8730510bfdf425b
7
+ data.tar.gz: 1accda12ed241795b42024073ead360d49fb0a2cfbf15df6fa22fbec275587e5988fe3d65bcbdf65bd8338bd135e1711705ec3580b9502a92bce8d57176ee047
@@ -37,6 +37,7 @@ module SlimFormObject
37
37
 
38
38
  def submit
39
39
  update_attributes
40
+ update_attributes_for_collection
40
41
  end
41
42
 
42
43
  def save
@@ -89,10 +90,43 @@ module SlimFormObject
89
90
  array_of_models.each do |model|
90
91
  model_attributes = make_attributes_of_model(model)
91
92
  method( snake(model.to_s) ).call.assign_attributes( get_attributes_for_update(model_attributes, model) )
93
+ end
94
+ end
95
+
96
+ def update_attributes_for_collection
97
+ array_of_models.each do |model|
92
98
  assign_attributes_for_collection(model)
93
99
  end
94
100
  end
95
101
 
102
+ def keys_of_collections
103
+ @keys ||= params.keys.select{ |key| key =~ /^.+_ids$/ }
104
+ end
105
+
106
+ def exist_any_arrors_without_collections?(model)
107
+ keys_of_collections.each do |key|
108
+ name_of_model = key.to_s[/^#{snake(model.to_s)}_(.*)_ids$/, 1].split('_').map(&:capitalize).join
109
+ name_of_key_error = Object.const_get(name_of_model).table_name
110
+ errors.messages.delete(name_of_key_error.to_sym)
111
+ end unless valid?
112
+ errors.messages.empty?
113
+ end
114
+
115
+ def assign_attributes_for_collection(model)
116
+ self_object_of_model = method( snake(model.to_s) ).call
117
+
118
+ keys_of_collections.each do |key|
119
+ method_name = key.to_s[/#{snake(model.to_s)}_(.*)/, 1]
120
+ if self_object_of_model.respond_to? method_name.to_s
121
+ old_attribute = self_object_of_model.method( method_name ).call
122
+ unless self_object_of_model.update_attributes( {method_name.to_s => params[key]} )
123
+ set_errors(self_object_of_model.errors)
124
+ self_object_of_model.update_attributes( {method_name.to_s => old_attribute} )
125
+ end if exist_any_arrors_without_collections?(model)
126
+ end
127
+ end
128
+ end
129
+
96
130
  def make_attributes_of_model(model)
97
131
  model_attributes = []
98
132
  model.column_names.each do |name|
@@ -112,19 +146,4 @@ module SlimFormObject
112
146
  class1.reflections.slice(snake(class2.to_s), class2.table_name).values.first.try(:macro)
113
147
  end
114
148
 
115
- def keys_of_collections
116
- params.keys.select{ |key| key =~ /^.+_ids$/ }
117
- end
118
-
119
- def assign_attributes_for_collection(model)
120
- self_object_of_model = method( snake(model.to_s) ).call
121
-
122
- keys_of_collections.each do |key|
123
- key.to_s =~ /#{snake(model.to_s)}_(.*)/
124
- if self_object_of_model.respond_to? $1.to_s
125
- self_object_of_model.assign_attributes( {$1.to_s => params[key]} )
126
- end
127
- end
128
- end
129
-
130
149
  end
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -176,7 +176,6 @@ describe TestModule do
176
176
  expect(object).to receive(:array_of_models).and_return( [TestOneModel] )
177
177
  expect(object).to receive(:make_attributes_of_model).and_return( attributes_of_model )
178
178
  expect(object).to receive(:get_attributes_for_update).and_return( attributes_for_update )
179
- expect(object).to receive(:assign_attributes_for_collection).and_return( true )
180
179
  object.stub(:test_one_model).and_return( TestOneModel.new )
181
180
  object.send :update_attributes
182
181
  end
@@ -202,7 +201,7 @@ describe TestModule do
202
201
 
203
202
  context 'assign_attributes_for_collection' do
204
203
  before :each do
205
- @test_object = TestOneModel.new
204
+ @test_object = TestOneModel.create(title:'title', descr:'descr')
206
205
  (1...4).each { TestFourModel.create(title:'title', descr:'descr') }
207
206
  end
208
207
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim_form_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-18 00:00:00.000000000 Z
11
+ date: 2016-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord