slim_form_object 3.0.0 → 3.1.0
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/assign.rb +15 -9
- data/lib/slim_form_object/data_object.rb +14 -36
- data/lib/slim_form_object/form_helpers/extension_actionview.rb +0 -3
- data/lib/slim_form_object/helpers.rb +8 -0
- data/lib/slim_form_object/processing.rb +20 -24
- data/lib/slim_form_object/saver.rb +15 -8
- data/lib/slim_form_object/validator.rb +28 -8
- data/lib/slim_form_object/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c312bc3e24f891d533dd197c4b064a676b6bf18fd4f5c7d655830462e2c0d1b
|
4
|
+
data.tar.gz: cb1d25b857e75dd4b85021c348be78fb23ca16fcb263dbf68da5c3538eabc805
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f23ff7c1c057e3b662dfe0c506476db9fb978f939b05c2825cf3d3ab32742f34587c493c9d7eeb8b83c3e125dd0f623e0044c956eb9cba8ed506210ed59beb3
|
7
|
+
data.tar.gz: 8e29fadfdf6d4a153296161eb062bc6bc44582f02caeb4dba8083cc11e684b7d68fae19f8e659cc109f7fd98442d083941ab43f080cc74d8e439b5fe5984c7c2
|
@@ -2,13 +2,14 @@ module SlimFormObject
|
|
2
2
|
class Assign
|
3
3
|
include ::HelperMethods
|
4
4
|
|
5
|
-
attr_reader :form_object, :params, :data_for_assign
|
5
|
+
attr_reader :form_object, :params, :data_for_assign, :validator
|
6
6
|
attr_accessor :data_objects_arr
|
7
7
|
|
8
8
|
def initialize(form_object)
|
9
9
|
@form_object = form_object
|
10
10
|
@params = form_object.params
|
11
11
|
@structure = form_object.data_structure
|
12
|
+
@validator = Validator.new(form_object)
|
12
13
|
@data_for_assign = []
|
13
14
|
@data_objects_arr = []
|
14
15
|
end
|
@@ -16,6 +17,7 @@ module SlimFormObject
|
|
16
17
|
def apply_parameters_and_make_objects
|
17
18
|
parse_params
|
18
19
|
@data_objects_arr = make_data_objects(data_for_assign)
|
20
|
+
clean_data_objects_arr(data_objects_arr)
|
19
21
|
associate_all_objects(data_objects_arr)
|
20
22
|
|
21
23
|
data_objects_arr
|
@@ -43,10 +45,10 @@ module SlimFormObject
|
|
43
45
|
|
44
46
|
value_params.each do |key, value|
|
45
47
|
if is_nested?(value)
|
46
|
-
if
|
48
|
+
if nested_as_hash?(value)
|
49
|
+
value.values.each { |hash_params| nested << make_hash_objects_and_nested_objects(key, hash_params) }
|
50
|
+
elsif value.is_a?(Array)
|
47
51
|
value.each { |hash_params| nested << make_hash_objects_and_nested_objects(key, hash_params) }
|
48
|
-
elsif value.is_a?(ActionController::Parameters)
|
49
|
-
value.each { |index, hash_params| nested << make_hash_objects_and_nested_objects(key, hash_params) }
|
50
52
|
else
|
51
53
|
nested << make_hash_objects_and_nested_objects(key, value)
|
52
54
|
end
|
@@ -59,15 +61,19 @@ module SlimFormObject
|
|
59
61
|
{model: model, attributes: attributes, nested: nested}
|
60
62
|
end
|
61
63
|
|
64
|
+
def nested_as_hash?(values)
|
65
|
+
values.select{ |key, value| key.to_i.to_s == key }.size == values.size
|
66
|
+
end
|
67
|
+
|
62
68
|
def parse_params
|
63
|
-
params.each do |main_model_name, attributes|
|
69
|
+
params.to_h.each do |main_model_name, attributes|
|
64
70
|
data_for_assign << make_hash_objects_and_nested_objects(main_model_name, attributes)
|
65
71
|
end
|
66
72
|
end
|
67
73
|
|
68
74
|
def is_nested?(value)
|
69
75
|
def nested?(e)
|
70
|
-
e.class == ActionController::Parameters or e.class == Hash
|
76
|
+
e.class == ActionController::Parameters or e.class == Hash or e.class == ActiveSupport::HashWithIndifferentAccess
|
71
77
|
end
|
72
78
|
|
73
79
|
return true if nested?(value)
|
@@ -90,7 +96,7 @@ module SlimFormObject
|
|
90
96
|
while data_object_1 = objects.delete( objects[0] )
|
91
97
|
associate_all_objects(data_object_1.nested)
|
92
98
|
objects.each do |data_object_2|
|
93
|
-
data_object_1.associate_with(data_object_2.
|
99
|
+
data_object_1.associate_with(data_object_2.object)
|
94
100
|
end
|
95
101
|
end
|
96
102
|
end
|
@@ -100,7 +106,7 @@ module SlimFormObject
|
|
100
106
|
|
101
107
|
objects.each do |data_object|
|
102
108
|
data_object.nested.each do |nested_data_object|
|
103
|
-
data_object.associate_with(nested_data_object.
|
109
|
+
data_object.associate_with(nested_data_object.object)
|
104
110
|
end
|
105
111
|
end
|
106
112
|
end
|
@@ -108,7 +114,7 @@ module SlimFormObject
|
|
108
114
|
def clean_data_objects_arr(objects)
|
109
115
|
objects.select! do |data_object|
|
110
116
|
clean_data_objects_arr(data_object.nested)
|
111
|
-
|
117
|
+
validator.allow_object_processing?(data_object)
|
112
118
|
end
|
113
119
|
end
|
114
120
|
|
@@ -2,7 +2,7 @@ module SlimFormObject
|
|
2
2
|
class DataObject
|
3
3
|
include ::HelperMethods
|
4
4
|
|
5
|
-
attr_reader :name, :model, :attributes, :object, :
|
5
|
+
attr_reader :name, :model, :attributes, :object, :form_object, :validator
|
6
6
|
attr_accessor :nested
|
7
7
|
|
8
8
|
def initialize(name: nil, attributes: {}, form_object: nil)
|
@@ -11,57 +11,37 @@ module SlimFormObject
|
|
11
11
|
@attributes = attributes
|
12
12
|
@form_object = form_object
|
13
13
|
@object = make_object
|
14
|
-
@
|
14
|
+
@validator = Validator.new(form_object)
|
15
15
|
assign_attributes!
|
16
16
|
end
|
17
17
|
|
18
18
|
def associate_with(other_object, stage: nil)
|
19
|
-
|
20
|
-
when 1
|
21
|
-
obj = object
|
22
|
-
when 2
|
23
|
-
obj = object
|
24
|
-
else
|
25
|
-
obj = associated_object
|
26
|
-
end
|
19
|
+
return object if (stage != Saver::STAGE_3 and !object.new_record? and !other_object.new_record?)
|
27
20
|
|
28
|
-
|
29
|
-
|
30
|
-
to_bind_models(obj, other_object)
|
21
|
+
to_bind_models(object, other_object)
|
31
22
|
end
|
32
23
|
|
33
|
-
def
|
34
|
-
|
24
|
+
def blank_or_empty?(except_fileds: [])
|
25
|
+
validator.blank_or_empty_object?(self, except_fileds: except_fileds)
|
35
26
|
end
|
36
27
|
|
37
|
-
def
|
38
|
-
|
28
|
+
def empty_attributes?
|
29
|
+
validator.empty_attributes?(attributes)
|
39
30
|
end
|
40
31
|
|
41
|
-
def
|
42
|
-
|
32
|
+
def only_blank_strings_in_attributes?
|
33
|
+
validator.only_blank_strings_in_attributes?(attributes)
|
43
34
|
end
|
44
35
|
|
45
|
-
|
46
|
-
|
47
|
-
def empty_attributes?
|
48
|
-
return false if nil_attributes?
|
49
|
-
attributes.each { |key, value| return false if value&.to_s&.strip != '' }
|
50
|
-
true
|
51
|
-
end
|
52
|
-
|
53
|
-
def nil_attributes?
|
54
|
-
attributes.empty?
|
36
|
+
def regenerate_object
|
37
|
+
@object = make_object
|
55
38
|
end
|
56
39
|
|
57
40
|
def assign_attributes!
|
58
|
-
|
41
|
+
object.assign_attributes(attributes)
|
59
42
|
end
|
60
43
|
|
61
|
-
|
62
|
-
obj.assign_attributes(attributes)
|
63
|
-
obj
|
64
|
-
end
|
44
|
+
private
|
65
45
|
|
66
46
|
def make_object
|
67
47
|
attributes[:id].to_i > 0 ? model.find(attributes[:id]) : model.new
|
@@ -69,5 +49,3 @@ module SlimFormObject
|
|
69
49
|
end
|
70
50
|
end
|
71
51
|
|
72
|
-
|
73
|
-
|
@@ -54,7 +54,6 @@ module HelperMethods
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
|
58
57
|
if const_exists?('ActionView::Helpers')
|
59
58
|
|
60
59
|
module ActionView
|
@@ -76,10 +75,8 @@ if const_exists?('ActionView::Helpers')
|
|
76
75
|
return sfo_get_date_tag_name(prefix, field_name, options) if sfo_date_attr?(field_name)
|
77
76
|
|
78
77
|
@options[:discard_type] ? prefix : "#{prefix}[#{field_name}]"
|
79
|
-
|
80
78
|
end
|
81
79
|
end
|
82
|
-
|
83
80
|
|
84
81
|
end
|
85
82
|
end
|
@@ -61,4 +61,12 @@ module HelperMethods
|
|
61
61
|
return $1 if string =~ /^.+::(.+)$/
|
62
62
|
string
|
63
63
|
end
|
64
|
+
|
65
|
+
def define_classes_array_with_name(name, args)
|
66
|
+
args.each { |model| raise "#{model.to_s} - type is not a Class" if model.class != Class }
|
67
|
+
instance_eval do
|
68
|
+
define_method(name) { args }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
64
72
|
end
|
@@ -22,23 +22,15 @@ module SlimFormObject
|
|
22
22
|
define_array_of_models(:array_of_all_models, get_main_models_from_structure(structure))
|
23
23
|
end
|
24
24
|
|
25
|
-
# array_models_which_not_save_if_empty
|
26
|
-
def not_save_empty_object_for(*args)
|
27
|
-
args.each { |model| raise "#{model.to_s} - type is not a Class" if model.class != Class }
|
28
|
-
instance_eval do
|
29
|
-
define_method(:not_save_if_empty_arr) { args }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def not_save_nil_object_for(*args)
|
34
|
-
args.each { |model| raise "#{model.to_s} - type is not a Class" if model.class != Class }
|
35
|
-
instance_eval do
|
36
|
-
define_method(:not_save_if_nil_arr) { args }
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
25
|
# CALLBACKS
|
41
|
-
|
26
|
+
[
|
27
|
+
'after_validation_form',
|
28
|
+
'after_save_form',
|
29
|
+
'after_save_object',
|
30
|
+
'allow_to_save_object',
|
31
|
+
'allow_to_validate_object',
|
32
|
+
'allow_object_processing'
|
33
|
+
].each do |method_name|
|
42
34
|
define_method("#{method_name}".to_sym) do |&block|
|
43
35
|
instance_eval do
|
44
36
|
define_method("#{method_name}_block".to_sym) { block }
|
@@ -138,14 +130,18 @@ module SlimFormObject
|
|
138
130
|
end
|
139
131
|
|
140
132
|
def default_settings
|
141
|
-
define_singleton_method(:
|
142
|
-
define_singleton_method(:
|
143
|
-
define_singleton_method(:
|
144
|
-
define_singleton_method(:
|
145
|
-
|
146
|
-
define_singleton_method(:
|
147
|
-
|
148
|
-
|
133
|
+
define_singleton_method(:after_validation_form_block) { Proc.new {} } unless respond_to?(:after_validation_form_block)
|
134
|
+
define_singleton_method(:after_save_form_block) { Proc.new {} } unless respond_to?(:before_save_form_block)
|
135
|
+
define_singleton_method(:after_save_object_block) { Proc.new { true } } unless respond_to?(:allow_to_save_object_block)
|
136
|
+
define_singleton_method(:allow_to_validate_object_block) { Proc.new { true } } unless respond_to?(:allow_to_validate_object_block)
|
137
|
+
|
138
|
+
define_singleton_method(:allow_to_save_object_block) do
|
139
|
+
Proc.new { |object| object.valid? and object.changed? }
|
140
|
+
end unless respond_to?(:allow_to_save_object_block)
|
141
|
+
|
142
|
+
define_singleton_method(:allow_object_processing_block) do
|
143
|
+
Proc.new { |data_object| data_object.blank_or_empty? }
|
144
|
+
end unless respond_to?(:allow_object_processing_block)
|
149
145
|
end
|
150
146
|
|
151
147
|
end
|
@@ -4,6 +4,8 @@ module SlimFormObject
|
|
4
4
|
|
5
5
|
attr_reader :form_object, :params, :validator, :data_objects_arr
|
6
6
|
|
7
|
+
STAGE_3 = 3
|
8
|
+
|
7
9
|
def initialize(form_object)
|
8
10
|
@form_object = form_object
|
9
11
|
@params = form_object.params
|
@@ -30,9 +32,18 @@ module SlimFormObject
|
|
30
32
|
|
31
33
|
private
|
32
34
|
|
35
|
+
def regenerate_objects_with_attributes(objects)
|
36
|
+
objects.each do |object|
|
37
|
+
object.regenerate_object
|
38
|
+
object.assign_attributes!
|
39
|
+
regenerate_objects_with_attributes(object.nested)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
33
43
|
def _save
|
44
|
+
regenerate_objects_with_attributes(data_objects_arr)
|
45
|
+
|
34
46
|
ActiveRecord::Base.transaction do
|
35
|
-
form_object.before_save_form_block.call(form_object)
|
36
47
|
stage_1(data_objects_arr)
|
37
48
|
stage_2(data_objects_arr)
|
38
49
|
stage_3(data_objects_arr)
|
@@ -44,7 +55,7 @@ module SlimFormObject
|
|
44
55
|
def stage_1(objects)
|
45
56
|
objects.each do |data_object|
|
46
57
|
data_object.nested.each do |nested_data_object|
|
47
|
-
data_object.associate_with(nested_data_object.object
|
58
|
+
data_object.associate_with(nested_data_object.object)
|
48
59
|
end
|
49
60
|
stage_1(data_object.nested)
|
50
61
|
end
|
@@ -71,18 +82,14 @@ module SlimFormObject
|
|
71
82
|
objects = Array.new(data_objects)
|
72
83
|
while data_object_1 = objects.delete( objects[0] )
|
73
84
|
objects.each do |data_object_2|
|
74
|
-
obj = data_object_1.associate_with(data_object_2.object, stage:
|
85
|
+
obj = data_object_1.associate_with(data_object_2.object, stage: STAGE_3)
|
75
86
|
save_object(obj)
|
76
87
|
end
|
77
88
|
end
|
78
89
|
end
|
79
90
|
|
80
|
-
def allow_to_save(object_of_model)
|
81
|
-
object_of_model.valid? and object_of_model.changed? and validator.allow_to_save_object?(object_of_model)
|
82
|
-
end
|
83
|
-
|
84
91
|
def save_object(object_of_model)
|
85
|
-
if
|
92
|
+
if validator.allow_to_save_object?(object_of_model)
|
86
93
|
object_of_model.save!
|
87
94
|
end
|
88
95
|
end
|
@@ -2,18 +2,14 @@ module SlimFormObject
|
|
2
2
|
class Validator
|
3
3
|
include ::HelperMethods
|
4
4
|
|
5
|
-
attr_reader :form_object, :
|
5
|
+
attr_reader :form_object, :data_objects_arr
|
6
6
|
|
7
7
|
def initialize(form_object)
|
8
8
|
@form_object = form_object
|
9
|
-
@params = form_object.params
|
10
9
|
@data_objects_arr = form_object.data_objects_arr
|
11
|
-
@not_save_if_empty_arr = form_object.not_save_if_empty_arr
|
12
|
-
@not_save_if_nil_arr = form_object.not_save_if_nil_arr
|
13
10
|
end
|
14
11
|
|
15
12
|
def validate_form_object
|
16
|
-
form_object.before_validation_form_block.call(form_object)
|
17
13
|
validation_objects(data_objects_arr)
|
18
14
|
form_object.after_validation_form_block.call(form_object)
|
19
15
|
end
|
@@ -22,15 +18,39 @@ module SlimFormObject
|
|
22
18
|
form_object.allow_to_save_object_block.call(object)
|
23
19
|
end
|
24
20
|
|
25
|
-
def
|
26
|
-
form_object.
|
21
|
+
def allow_to_validate_object?(data_object)
|
22
|
+
form_object.allow_to_validate_object_block.call(data_object)
|
23
|
+
end
|
24
|
+
|
25
|
+
def empty_attributes?(attributes)
|
26
|
+
attributes.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
def only_blank_strings_in_attributes?(attributes)
|
30
|
+
return false if empty_attributes?(attributes)
|
31
|
+
attributes.each { |key, value| return false if value&.to_s&.strip != '' }
|
32
|
+
true
|
33
|
+
end
|
34
|
+
|
35
|
+
def only_blank_or_empty_attributes(attributes)
|
36
|
+
empty_attributes?(attributes) or only_blank_strings_in_attributes?(attributes)
|
37
|
+
end
|
38
|
+
|
39
|
+
def blank_or_empty_object?(data_object, except_fileds: [])
|
40
|
+
attributes = data_object.attributes.reject { |key| except_fileds.map{|name| name.to_s }.include?(key.to_s) }
|
41
|
+
only_blank_or_empty_attributes(attributes) and data_object.nested.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
def allow_object_processing?(data_object)
|
45
|
+
form_object.allow_object_processing_block.call(data_object)
|
27
46
|
end
|
28
47
|
|
29
48
|
private
|
30
49
|
|
31
50
|
def validation_objects(objects)
|
32
51
|
objects.each do |data_object|
|
33
|
-
|
52
|
+
next unless allow_to_validate_object?(data_object)
|
53
|
+
set_errors( snake(data_object.model), data_object.object.errors ) unless data_object.object.valid?
|
34
54
|
validation_objects(data_object.nested)
|
35
55
|
end
|
36
56
|
end
|
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: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- woodcrust
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|