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 +4 -4
- data/lib/slim_form_object.rb +34 -15
- data/lib/slim_form_object/version.rb +1 -1
- data/spec/slim_form_object_spec.rb +1 -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: da6582abfd769d9af8238cfad6ca79a890b62308
|
4
|
+
data.tar.gz: b6b7e88761e2684826122f454d8374413981f532
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f6b7b6a8bcc973b1ad647960fffd088e2a07ceb9efc34fa30af6d823398c80bf744ec492b1d16caaa6a06b9ba5e4398ccd4b281ed477e66d8730510bfdf425b
|
7
|
+
data.tar.gz: 1accda12ed241795b42024073ead360d49fb0a2cfbf15df6fa22fbec275587e5988fe3d65bcbdf65bd8338bd135e1711705ec3580b9502a92bce8d57176ee047
|
data/lib/slim_form_object.rb
CHANGED
@@ -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
|
@@ -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.
|
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.
|
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-
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|