slim_form_object 1.0.4 → 1.0.5
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/assign.rb +7 -7
- data/lib/slim_form_object/form_helpers/extension_actionview.rb +24 -18
- data/lib/slim_form_object/processing.rb +5 -12
- data/lib/slim_form_object/saver.rb +7 -5
- data/lib/slim_form_object/validator.rb +17 -15
- data/lib/slim_form_object/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ae4155e3a53b282d248dd3328278d852ace1e28
|
4
|
+
data.tar.gz: a34baa8a40b0604775ce5db1f35b5c49b796c464
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb8a312ebbc9fec0ea66c14d5c0e17f21a176c0eda0e7f7369d2d055939043a859acfc742b610504235887b244a6f0d9f18b7b806e358b44b66ed3db554acb52
|
7
|
+
data.tar.gz: 55a50c5f168ef4fec9015f8112d88acbffc9d45174f7311e9c63e25b97613bfc2a0361fb598f8be45a2eef62867f6bb50dc4bb7169175f2cb27857d22f684db2
|
@@ -5,11 +5,11 @@ module SlimFormObject
|
|
5
5
|
attr_reader :form_object, :params, :array_all_objects_for_save, :not_save_this_model, :result_hash_updated_objects
|
6
6
|
|
7
7
|
def initialize(form_object)
|
8
|
-
@form_object
|
9
|
-
@params
|
10
|
-
@array_all_objects_for_save
|
11
|
-
@not_save_this_model
|
12
|
-
@result_hash_updated_objects
|
8
|
+
@form_object = form_object
|
9
|
+
@params = form_object.params
|
10
|
+
@array_all_objects_for_save = form_object.array_all_objects_for_save
|
11
|
+
@not_save_this_model = form_object.not_save_this_model
|
12
|
+
@result_hash_updated_objects = {objects: [], nested_objects: {}}
|
13
13
|
end
|
14
14
|
|
15
15
|
def apply_parameters
|
@@ -20,6 +20,8 @@ module SlimFormObject
|
|
20
20
|
result_hash_updated_objects
|
21
21
|
end
|
22
22
|
|
23
|
+
private
|
24
|
+
|
23
25
|
def filter_not_save_objects
|
24
26
|
array_all_objects_for_save.reject do |object|
|
25
27
|
not_save_this_model.include?(object.class)
|
@@ -36,7 +38,6 @@ module SlimFormObject
|
|
36
38
|
result_hash_updated_objects[:objects] = array_all_objects_for_save
|
37
39
|
end
|
38
40
|
|
39
|
-
|
40
41
|
def hash_params_of_object(object)
|
41
42
|
if force_permit(params[snake(object.class.to_s)])
|
42
43
|
params[snake(object.class.to_s)]
|
@@ -76,7 +77,6 @@ module SlimFormObject
|
|
76
77
|
end
|
77
78
|
end
|
78
79
|
end
|
79
|
-
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -52,15 +52,11 @@ module ActionView
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def sfo_get_tag_name(object_name, method, multiple)
|
55
|
+
model_name, attr_name = apply_expression_text(method, sfo_single_attr_regexp)
|
56
|
+
|
55
57
|
if sfo_multiple_attr?(object_name)
|
56
|
-
method.to_s[sfo_single_attr_regexp]
|
57
|
-
model_name = $1
|
58
|
-
attr_name = $2
|
59
58
|
tag_name = "#{object_name}[#{model_name}][][#{attr_name}]#{"[]" if multiple}"
|
60
59
|
elsif sfo_single_attr?(method)
|
61
|
-
method.to_s[sfo_single_attr_regexp]
|
62
|
-
model_name = $1
|
63
|
-
attr_name = $2
|
64
60
|
tag_name = "#{object_name}[#{model_name}][#{attr_name}]#{"[]" if multiple}"
|
65
61
|
end
|
66
62
|
|
@@ -69,31 +65,41 @@ module ActionView
|
|
69
65
|
|
70
66
|
def sfo_get_method_name(method)
|
71
67
|
if sfo_single_attr?(method)
|
72
|
-
method
|
73
|
-
|
74
|
-
attr_name = $2
|
75
|
-
method = "#{model_name}_#{attr_name}"
|
68
|
+
model_name, attr_name = apply_expression_text(method, sfo_single_attr_regexp)
|
69
|
+
method = "#{model_name}_#{attr_name}"
|
76
70
|
end
|
77
71
|
|
78
72
|
method
|
79
73
|
end
|
80
74
|
|
81
75
|
def sfo_get_date_tag_name(prefix, tag_name)
|
76
|
+
model_name, attr_name, date_type = apply_expression_date(tag_name, sfo_date_attr_regexp)
|
77
|
+
|
82
78
|
if sfo_multiple_attr?(prefix)
|
83
|
-
tag_name[sfo_date_attr_regexp]
|
84
|
-
model_name = $1
|
85
|
-
attr_name = $2
|
86
|
-
date_type = $3
|
87
79
|
tag_name = "#{prefix}[#{model_name}][][#{attr_name}#{date_type}]"
|
88
|
-
|
89
|
-
model_name = $1
|
90
|
-
attr_name = $2
|
91
|
-
date_type = $3
|
80
|
+
else
|
92
81
|
tag_name = "#{prefix}[#{model_name}][#{attr_name}#{date_type}]"
|
93
82
|
end
|
94
83
|
|
95
84
|
tag_name
|
96
85
|
end
|
86
|
+
|
87
|
+
def apply_expression_date(string, exp)
|
88
|
+
string[exp]
|
89
|
+
model_name = $1
|
90
|
+
attr_name = $2
|
91
|
+
date_type = $3
|
92
|
+
|
93
|
+
[model_name, attr_name, date_type]
|
94
|
+
end
|
95
|
+
|
96
|
+
def apply_expression_text(string, exp)
|
97
|
+
string[exp]
|
98
|
+
model_name = $1
|
99
|
+
attr_name = $2
|
100
|
+
|
101
|
+
[model_name, attr_name]
|
102
|
+
end
|
97
103
|
end
|
98
104
|
|
99
105
|
class DateTimeSelector
|
@@ -9,7 +9,6 @@ module SlimFormObject
|
|
9
9
|
attr_accessor :params, :array_objects_for_save, :hash_objects_for_save
|
10
10
|
|
11
11
|
class << self
|
12
|
-
|
13
12
|
def set_model_name(name)
|
14
13
|
define_method(:model_name) { ActiveModel::Name.new(self, nil, name) }
|
15
14
|
end
|
@@ -85,7 +84,11 @@ module SlimFormObject
|
|
85
84
|
Validator.new(self).validate_form_object
|
86
85
|
end
|
87
86
|
|
88
|
-
|
87
|
+
def array_all_objects_for_save
|
88
|
+
array_objects_for_save ||= get_or_add_default_objects
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
89
92
|
|
90
93
|
def apply
|
91
94
|
assign = Assign.new(self)
|
@@ -97,16 +100,6 @@ module SlimFormObject
|
|
97
100
|
define_singleton_method(:force_save_if_all_attr_is_nil) { [] } unless respond_to?(:force_save_if_all_attr_is_nil)
|
98
101
|
end
|
99
102
|
|
100
|
-
def set_errors(object_errors)
|
101
|
-
object_errors.each do |attribute, message|
|
102
|
-
errors.add(attribute, message)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def array_all_objects_for_save
|
107
|
-
array_objects_for_save ||= get_or_add_default_objects
|
108
|
-
end
|
109
|
-
|
110
103
|
end
|
111
104
|
end
|
112
105
|
|
@@ -5,10 +5,10 @@ module SlimFormObject
|
|
5
5
|
attr_reader :form_object, :params, :validator, :hash_objects_for_save
|
6
6
|
|
7
7
|
def initialize(form_object)
|
8
|
-
@form_object
|
9
|
-
@params
|
10
|
-
@hash_objects_for_save
|
11
|
-
@validator
|
8
|
+
@form_object = form_object
|
9
|
+
@params = form_object.params
|
10
|
+
@hash_objects_for_save = form_object.hash_objects_for_save
|
11
|
+
@validator = Validator.new(form_object)
|
12
12
|
end
|
13
13
|
|
14
14
|
def save
|
@@ -19,12 +19,14 @@ module SlimFormObject
|
|
19
19
|
end
|
20
20
|
return true
|
21
21
|
end
|
22
|
+
|
22
23
|
false
|
23
24
|
rescue
|
24
|
-
# p "ERROR"
|
25
25
|
false
|
26
26
|
end
|
27
27
|
|
28
|
+
private
|
29
|
+
|
28
30
|
def save_main_objects
|
29
31
|
objects = Array.new(hash_objects_for_save[:objects])
|
30
32
|
while object_1 = objects.delete( objects[0] )
|
@@ -5,9 +5,9 @@ module SlimFormObject
|
|
5
5
|
attr_reader :form_object, :params, :hash_objects_for_save
|
6
6
|
|
7
7
|
def initialize(form_object)
|
8
|
-
@form_object
|
9
|
-
@params
|
10
|
-
@hash_objects_for_save
|
8
|
+
@form_object = form_object
|
9
|
+
@params = form_object.params
|
10
|
+
@hash_objects_for_save = form_object.hash_objects_for_save
|
11
11
|
end
|
12
12
|
|
13
13
|
def validate_form_object
|
@@ -15,30 +15,26 @@ module SlimFormObject
|
|
15
15
|
|
16
16
|
hash_objects_for_save[:objects].each do |object|
|
17
17
|
next if all_attributes_is_nil?(object)
|
18
|
-
|
18
|
+
set_errors( snake(object.class), object.errors ) unless object.valid?
|
19
19
|
end
|
20
20
|
|
21
21
|
hash_objects_for_save[:nested_objects].keys.each do |snake_model_name|
|
22
22
|
hash_objects_for_save[:nested_objects][snake_model_name].each do |object|
|
23
|
-
|
23
|
+
set_errors( snake(object.class), object.errors ) unless object.valid?
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def filter_nil_objects
|
29
|
-
hash_objects_for_save[:objects].reject! do |object|
|
30
|
-
all_attributes_is_nil?(object)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
28
|
def valid_model_for_save?(object)
|
35
|
-
# ( (attributes_is_present?(object) and object.id != nil) or (object.id == nil and !all_attributes_is_empty?(object)) )
|
36
29
|
true
|
37
30
|
end
|
38
31
|
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
private
|
33
|
+
|
34
|
+
def filter_nil_objects
|
35
|
+
hash_objects_for_save[:objects].reject! do |object|
|
36
|
+
all_attributes_is_nil?(object)
|
37
|
+
end
|
42
38
|
end
|
43
39
|
|
44
40
|
def all_attributes_is_nil?(object)
|
@@ -48,6 +44,12 @@ module SlimFormObject
|
|
48
44
|
true
|
49
45
|
end
|
50
46
|
|
47
|
+
def set_errors(object_name, object_errors)
|
48
|
+
object_errors.each do |attribute, message|
|
49
|
+
form_object.errors.add(object_name, { attribute => message})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|