slim_form_object 0.5.3 → 0.5.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.
- checksums.yaml +4 -4
- data/lib/slim_form_object/version.rb +1 -1
- data/lib/slim_form_object.rb +22 -11
- data/spec/slim_form_object_spec.rb +39 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20e76ba7c91e9ca6496af6eddddec79358576136
|
4
|
+
data.tar.gz: 47d1ddf450ab45e020d63d0b5c43e16a54ce8946
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee29067605105c217d60e76cb3978ee26e38c83df2065ed4625e2e25d1a379b2d126ac3fbeb973acce18efd394ef4b685a236639de16c6151ecf1632c8c5baaf
|
7
|
+
data.tar.gz: 5b1b27cabb0f566e4cfed1a2c9ca65ffde2fef082814ec744d8231a5066cd336a1554965aa6cc00eca8d9cb5760b5dbcff696c242ef62e5af82bcb721c5fbb18
|
data/lib/slim_form_object.rb
CHANGED
@@ -100,14 +100,26 @@ module SlimFormObject
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def keys_of_collections
|
103
|
-
@keys ||=
|
103
|
+
@keys ||= []
|
104
|
+
params.keys.each do |key|
|
105
|
+
array_of_models.each do |model|
|
106
|
+
self_object_of_model = method( snake(model.to_s) ).call
|
107
|
+
method_name = key.to_s[/#{snake(model.to_s)}_(.*)/, 1]
|
108
|
+
@keys << method_name if self_object_of_model.respond_to? method_name.to_s
|
109
|
+
end if key[/^.+_ids$/]
|
110
|
+
end if @keys.empty?
|
111
|
+
@keys
|
104
112
|
end
|
105
113
|
|
106
|
-
def exist_any_arrors_without_collections?
|
107
|
-
keys_of_collections.each do |
|
108
|
-
|
109
|
-
|
110
|
-
|
114
|
+
def exist_any_arrors_without_collections?
|
115
|
+
keys_of_collections.each do |method_name|
|
116
|
+
array_of_models.each do |model|
|
117
|
+
self_object_of_model = method( snake(model.to_s) ).call
|
118
|
+
name_of_model = method_name.to_s[/^(.+)_ids$/, 1]
|
119
|
+
name_of_constant_model = name_of_model.split('_').map(&:capitalize).join
|
120
|
+
name_of_key_error = Object.const_get(name_of_constant_model).table_name
|
121
|
+
errors.messages.delete(name_of_key_error.to_sym)
|
122
|
+
end
|
111
123
|
end unless valid?
|
112
124
|
errors.messages.empty?
|
113
125
|
end
|
@@ -115,14 +127,13 @@ module SlimFormObject
|
|
115
127
|
def assign_attributes_for_collection(model)
|
116
128
|
self_object_of_model = method( snake(model.to_s) ).call
|
117
129
|
|
118
|
-
keys_of_collections.each do |
|
119
|
-
|
120
|
-
if self_object_of_model.respond_to? method_name.to_s
|
130
|
+
keys_of_collections.each do |method_name|
|
131
|
+
if self_object_of_model.respond_to? method_name
|
121
132
|
old_attribute = self_object_of_model.method( method_name ).call
|
122
|
-
unless self_object_of_model.update_attributes( {method_name.to_s => params[
|
133
|
+
unless self_object_of_model.update_attributes( {method_name.to_s => params["#{snake(model.to_s)}_#{method_name}".to_sym]} )
|
123
134
|
set_errors(self_object_of_model.errors)
|
124
135
|
self_object_of_model.update_attributes( {method_name.to_s => old_attribute} )
|
125
|
-
end if exist_any_arrors_without_collections?
|
136
|
+
end if exist_any_arrors_without_collections?
|
126
137
|
end
|
127
138
|
end
|
128
139
|
end
|
@@ -208,7 +208,7 @@ describe TestModule do
|
|
208
208
|
it 'assign attributes for test_one_model' do
|
209
209
|
object.stub(:params).and_return( {test_one_model_test_four_model_ids: [1, 2, 3]} )
|
210
210
|
object.stub_chain(:method, :call) { @test_object }
|
211
|
-
object.stub(:keys_of_collections).and_return( [
|
211
|
+
object.stub(:keys_of_collections).and_return( ['test_four_model_ids'] )
|
212
212
|
object.send(:assign_attributes_for_collection, TestOneModel)
|
213
213
|
|
214
214
|
expect(@test_object.test_four_model_ids).to eq( [1, 2, 3] )
|
@@ -227,8 +227,9 @@ describe TestModule do
|
|
227
227
|
context 'keys_of_collections' do
|
228
228
|
it 'must be return array with values' do
|
229
229
|
object.stub(:params).and_return( {test_one_model_test_four_model_ids: [1, 2, 3]} )
|
230
|
+
object.stub_chain(:method, :call) { TestOneModel.create(title:'title', descr:'descr') }
|
230
231
|
|
231
|
-
expect(object.send :keys_of_collections).to eq( [
|
232
|
+
expect(object.send :keys_of_collections).to eq( ['test_four_model_ids'] )
|
232
233
|
end
|
233
234
|
|
234
235
|
it 'must be return array without values' do
|
@@ -238,6 +239,42 @@ describe TestModule do
|
|
238
239
|
end
|
239
240
|
end
|
240
241
|
|
242
|
+
context 'exist_any_arrors_without_collections?' do
|
243
|
+
before :each do
|
244
|
+
object.instance_eval do
|
245
|
+
def test_one_model
|
246
|
+
TestOneModel.create(title:'title', descr:'descr')
|
247
|
+
end
|
248
|
+
def test_four_model
|
249
|
+
TestFourModel.create(title:'title', descr:'descr')
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
it 'must be return true' do
|
255
|
+
object.stub(:keys_of_collections).and_return( ['test_one_model_ids', 'test_four_model_ids'] )
|
256
|
+
object.stub(:array_of_models).and_return( [TestOneModel, TestFourModel] )
|
257
|
+
object.stub(:valid?).and_return( false )
|
258
|
+
object.stub_chain(:errors, :messages).and_return( {:test_one_models=>'error', :test_four_models=>'error'} )
|
259
|
+
|
260
|
+
expect(object.send :exist_any_arrors_without_collections?).to eq( true )
|
261
|
+
end
|
262
|
+
|
263
|
+
it 'must be return true' do
|
264
|
+
object.instance_eval do
|
265
|
+
def test_one_model
|
266
|
+
TestThreeModel.create(title:'title', descr:'')
|
267
|
+
end
|
268
|
+
end
|
269
|
+
object.stub(:keys_of_collections).and_return( ['test_one_model_ids', 'test_four_model_ids'] )
|
270
|
+
object.stub(:array_of_models).and_return( [TestOneModel, TestFourModel] )
|
271
|
+
object.stub(:valid?).and_return( false )
|
272
|
+
object.stub_chain(:errors, :messages).and_return( {:test_one_models=>'error', :test_four_models=>'error', :descr=>'error'} )
|
273
|
+
|
274
|
+
expect(object.send :exist_any_arrors_without_collections?).to eq( false )
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
241
278
|
end
|
242
279
|
|
243
280
|
|
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.
|
4
|
+
version: 0.5.4
|
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-
|
11
|
+
date: 2016-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|