slim_form_object 2.0.3 → 2.0.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/assign.rb +21 -9
- data/lib/slim_form_object/form_helpers/extension_actionview.rb +72 -83
- data/lib/slim_form_object/helpers.rb +39 -26
- data/lib/slim_form_object/processing.rb +1 -4
- data/lib/slim_form_object/saver.rb +3 -5
- data/lib/slim_form_object/validator.rb +0 -1
- data/lib/slim_form_object/version.rb +1 -1
- 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: 3de1e0db09d9602bafd6f0cc1450a17fdc71e86a
|
4
|
+
data.tar.gz: bab8598dda0efe82bdebc1b95e7c44bc6445cef4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cc0412036eb444986de4106610fb8583c09835e2dd1fd043da1412db77d0ac8bade9fc4daffcee70280223f407ed880a6c6ee88da5a775934369262bc6e866c
|
7
|
+
data.tar.gz: 439ff3ebeaf41d1f2d58927f03452435a1d9d1f73be2744eaf76fe50da2f60c6a2d8b809894542defbf7749b1490c4cbcf7ff569f84610fee6f7c75267bbfb19
|
@@ -21,7 +21,8 @@ module SlimFormObject
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def associate_objects
|
24
|
-
|
24
|
+
associate_all_nested_objects(all_updated_objects)
|
25
|
+
associate_all_main_objects(all_updated_objects)
|
25
26
|
|
26
27
|
all_updated_objects
|
27
28
|
end
|
@@ -48,24 +49,34 @@ module SlimFormObject
|
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
51
|
-
def
|
52
|
+
def associate_all_nested_objects(nested_array, object=nil)
|
52
53
|
nested_array.each do |hash|
|
53
54
|
to_bind_models(object, hash[:essence][:object]) if object
|
54
|
-
|
55
|
+
associate_all_nested_objects(hash[:nested], hash[:essence][:object])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def associate_all_main_objects(all_updated_objects)
|
60
|
+
objects = Array.new(all_updated_objects)
|
61
|
+
while object = objects.delete( objects[0] )
|
62
|
+
object_1 = object[:essence][:object]
|
63
|
+
objects.each do |hash|
|
64
|
+
object_2 = hash[:essence][:object]
|
65
|
+
next if !object_1.new_record? and !object_2.new_record?
|
66
|
+
to_bind_models(object_1, object_2)
|
67
|
+
end
|
55
68
|
end
|
56
69
|
end
|
57
70
|
|
58
71
|
def make_all_objects_with_attributes
|
59
|
-
object_hash = {}
|
60
72
|
params.each do |main_model_name, hash|
|
61
|
-
assign_objects_attributes(main_model_name, hash,
|
73
|
+
assign_objects_attributes(main_model_name, hash, all_updated_objects, :main)
|
62
74
|
end
|
63
75
|
end
|
64
76
|
|
65
77
|
def nested(model_name, nested_array, result_array)
|
66
|
-
object_hash = {}
|
67
78
|
nested_array.each do |nested_object|
|
68
|
-
assign_objects_attributes(model_name, nested_object,
|
79
|
+
assign_objects_attributes(model_name, nested_object, result_array, :nested)
|
69
80
|
end
|
70
81
|
end
|
71
82
|
|
@@ -74,8 +85,9 @@ module SlimFormObject
|
|
74
85
|
value.select{ |e| e.class == ActionController::Parameters or e.class == Hash }.size == value.size
|
75
86
|
end
|
76
87
|
|
77
|
-
def assign_objects_attributes(model_name, hash,
|
78
|
-
|
88
|
+
def assign_objects_attributes(model_name, hash, result_array, type)
|
89
|
+
object_hash = {}
|
90
|
+
object = type == :main ? form_object.send(model_name.to_sym) : get_class_of(model_name, base_module).new
|
79
91
|
object_hash[:nested] = []
|
80
92
|
object_attrs = {}
|
81
93
|
hash.each do |key, val|
|
@@ -1,101 +1,90 @@
|
|
1
|
-
module
|
2
|
-
module Helpers
|
3
|
-
module HelperMethods
|
4
|
-
|
5
|
-
private
|
6
|
-
|
7
|
-
def get_class_of_snake_model_name(snake_model_name)
|
8
|
-
Object.const_get( snake_model_name.split('_').map(&:capitalize).join )
|
9
|
-
end
|
10
|
-
|
11
|
-
def sfo_single_attr_regexp
|
12
|
-
/^([^-]+)-([^-]+)$/
|
13
|
-
end
|
14
|
-
|
15
|
-
def sfo_date_attr_regexp
|
16
|
-
/^([^-]+)-([^-]+)(\([\s\S]+\))$/
|
17
|
-
end
|
1
|
+
module HelperMethods
|
18
2
|
|
19
|
-
|
20
|
-
/_ids$/
|
21
|
-
end
|
22
|
-
|
23
|
-
def sfo_form_attribute?(object)
|
24
|
-
object.class.ancestors[1] == SlimFormObject::Base if object
|
25
|
-
end
|
3
|
+
private
|
26
4
|
|
27
|
-
|
28
|
-
|
29
|
-
|
5
|
+
def get_class_of_snake_model_name(snake_model_name)
|
6
|
+
Object.const_get( snake_model_name.split('_').map(&:capitalize).join )
|
7
|
+
end
|
30
8
|
|
31
|
-
|
32
|
-
|
33
|
-
|
9
|
+
def sfo_single_attr_regexp
|
10
|
+
/^([^-]+)-([^-]+)$/
|
11
|
+
end
|
34
12
|
|
35
|
-
|
36
|
-
|
37
|
-
|
13
|
+
def sfo_date_attr_regexp
|
14
|
+
/^([^-]+)-([^-]+)(\([\s\S]+\))$/
|
15
|
+
end
|
38
16
|
|
39
|
-
|
40
|
-
|
41
|
-
|
17
|
+
def sfo_collection_ads_regexp
|
18
|
+
/_ids$/
|
19
|
+
end
|
42
20
|
|
43
|
-
|
44
|
-
|
21
|
+
def sfo_form_attribute?(object)
|
22
|
+
object.class.ancestors[1] == SlimFormObject::Base if object
|
23
|
+
end
|
45
24
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
else
|
50
|
-
tag_name = "#{object_name}[][#{model_name}][][#{attr_name}]#{"[]" if multiple}"
|
51
|
-
end
|
52
|
-
elsif sfo_single_attr?(method)
|
53
|
-
tag_name = "#{object_name}[#{model_name}][#{attr_name}]#{"[]" if multiple}"
|
54
|
-
end
|
25
|
+
def sfo_attr?(method)
|
26
|
+
sfo_single_attr?(method)
|
27
|
+
end
|
55
28
|
|
56
|
-
|
57
|
-
|
29
|
+
def sfo_single_attr?(method)
|
30
|
+
method.to_s[sfo_single_attr_regexp] ? true : false
|
31
|
+
end
|
58
32
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
method = "#{model_name}_#{attr_name}"
|
63
|
-
end
|
33
|
+
def sfo_date_attr?(tag_name)
|
34
|
+
tag_name.to_s[sfo_date_attr_regexp] ? true : false
|
35
|
+
end
|
64
36
|
|
65
|
-
|
66
|
-
|
37
|
+
def sfo_collection_ads_attr?(tag_name)
|
38
|
+
tag_name.to_s[sfo_collection_ads_regexp] ? true : false
|
39
|
+
end
|
67
40
|
|
68
|
-
|
69
|
-
|
41
|
+
def sfo_get_tag_name(object_name, method, multiple, options)
|
42
|
+
model_name, attr_name = apply_expression_text(method)
|
43
|
+
if options[:sfo_nested]
|
44
|
+
"#{object_name}[#{model_name}][][#{attr_name}]#{"[]" if multiple}"
|
45
|
+
else
|
46
|
+
"#{object_name}[#{model_name}][#{attr_name}]#{"[]" if multiple}"
|
47
|
+
end
|
48
|
+
end
|
70
49
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
50
|
+
def sfo_get_method_name(method)
|
51
|
+
if sfo_single_attr?(method) and !sfo_collection_ads_attr?(method)
|
52
|
+
model_name, attr_name = apply_expression_text(method)
|
53
|
+
method = "#{model_name}_#{attr_name}"
|
54
|
+
end
|
76
55
|
|
77
|
-
|
78
|
-
|
56
|
+
method
|
57
|
+
end
|
79
58
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
59
|
+
def sfo_get_date_tag_name(prefix, tag_name, options)
|
60
|
+
model_name, attr_name, date_type = apply_expression_date(tag_name)
|
61
|
+
if options[:sfo_nested]
|
62
|
+
"#{prefix}[#{model_name}][][#{attr_name}#{date_type}]"
|
63
|
+
else
|
64
|
+
"#{prefix}[#{model_name}][#{attr_name}#{date_type}]"
|
65
|
+
end
|
66
|
+
end
|
85
67
|
|
86
|
-
|
87
|
-
|
68
|
+
def apply_expression_date(string)
|
69
|
+
string[sfo_date_attr_regexp]
|
70
|
+
model_name = $1
|
71
|
+
attr_name = $2
|
72
|
+
date_type = $3
|
88
73
|
|
89
|
-
|
90
|
-
|
91
|
-
model_name = $1
|
92
|
-
attr_name = $2
|
74
|
+
[model_name, attr_name, date_type]
|
75
|
+
end
|
93
76
|
|
94
|
-
|
95
|
-
|
96
|
-
|
77
|
+
def apply_expression_text(string)
|
78
|
+
string[sfo_single_attr_regexp]
|
79
|
+
model_name = $1
|
80
|
+
attr_name = $2
|
97
81
|
|
82
|
+
[model_name, attr_name]
|
83
|
+
end
|
84
|
+
end
|
98
85
|
|
86
|
+
module ActionView
|
87
|
+
module Helpers
|
99
88
|
# EXTENSIONS
|
100
89
|
|
101
90
|
class DateTimeSelector
|
@@ -124,7 +113,7 @@ module ActionView
|
|
124
113
|
private
|
125
114
|
|
126
115
|
def value(object)
|
127
|
-
method_name = sfo_get_method_name(@method_name)
|
116
|
+
method_name = @options[:sfo_nested] ? apply_expression_text(@method_name)[1] : sfo_get_method_name(@method_name)
|
128
117
|
object.public_send method_name if object
|
129
118
|
end
|
130
119
|
|
@@ -164,11 +153,11 @@ module ActionView
|
|
164
153
|
end
|
165
154
|
|
166
155
|
def fields_for(record_name, record_object = nil, fields_options = {}, &block)
|
167
|
-
(self.options[:sfo_nested] and record_object) ? record_object.merge!({sfo_main: false}) : record_object.merge!({sfo_main: true})
|
168
156
|
fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
|
169
157
|
fields_options[:builder] ||= options[:builder]
|
170
158
|
fields_options[:namespace] = options[:namespace]
|
171
159
|
fields_options[:parent_builder] = self
|
160
|
+
self.options[:sfo_nested] ? fields_options.merge!({sfo_main: false}) : fields_options.merge!({sfo_main: true})
|
172
161
|
|
173
162
|
case record_name
|
174
163
|
when String, Symbol
|
@@ -199,8 +188,8 @@ module ActionView
|
|
199
188
|
fields_options[:child_index] = index
|
200
189
|
|
201
190
|
if fields_options[:sfo_nested]
|
202
|
-
record_object = object
|
203
|
-
record_name
|
191
|
+
record_object = object if !record_object
|
192
|
+
record_name = record_name + '[][]' unless fields_options[:sfo_main]
|
204
193
|
end
|
205
194
|
@template.fields_for(record_name, record_object, fields_options, &block)
|
206
195
|
end
|
@@ -3,49 +3,62 @@ module HelperMethods
|
|
3
3
|
method( snake(model.to_s).to_sym ).call
|
4
4
|
end
|
5
5
|
|
6
|
-
def
|
7
|
-
pref =
|
8
|
-
|
6
|
+
def get_class_of(snake_model_name, base_modulenil)
|
7
|
+
pref = base_module ? (base_module.to_s + '::') : ''
|
8
|
+
Object.const_get( pref + snake_model_name.to_s.split('_').map(&:capitalize).join )
|
9
|
+
rescue
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_bind_models(object_1, object_2)
|
14
|
+
if object_2.new_record?
|
15
|
+
assignment_to_each_other(object_2, object_1)
|
9
16
|
else
|
10
|
-
|
17
|
+
assignment_to_each_other(object_1, object_2)
|
11
18
|
end
|
12
|
-
|
19
|
+
|
20
|
+
object_1
|
13
21
|
end
|
14
22
|
|
15
|
-
def
|
16
|
-
|
17
|
-
model_name = $1
|
18
|
-
attr_name = $2
|
23
|
+
def assignment_to_each_other(object_1, object_2)
|
24
|
+
type, method_name = get_type_and_name_of_association(object_1.class, object_2.class)
|
19
25
|
|
20
|
-
|
26
|
+
if type == :belongs_to or type == :has_one
|
27
|
+
object_1.send( "#{method_name.to_s}=", object_2 )
|
28
|
+
elsif type == :has_many or type == :has_and_belongs_to_many
|
29
|
+
object_1.method(method_name).call << object_2
|
30
|
+
end
|
21
31
|
end
|
22
32
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
26
|
-
class_name_if_module(string.downcase)
|
33
|
+
def get_type_and_name_of_association(class1, class2)
|
34
|
+
reflection = get_reflection(class1, class2)
|
35
|
+
[type_association(reflection), method_name_association(reflection)]
|
27
36
|
end
|
28
37
|
|
29
|
-
def
|
30
|
-
|
38
|
+
def get_reflection(class1, class2)
|
39
|
+
class1.reflections.select{ |k,v| v.klass == class2 }.values.first
|
40
|
+
end
|
31
41
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
object_1.method("#{object_2.class.table_name}").call << object_2
|
36
|
-
end
|
42
|
+
def type_association(reflection)
|
43
|
+
reflection&.macro
|
44
|
+
end
|
37
45
|
|
38
|
-
|
46
|
+
def method_name_association(reflection)
|
47
|
+
reflection&.name
|
39
48
|
end
|
40
49
|
|
41
|
-
def
|
42
|
-
class1
|
50
|
+
def type_and_name_of_association_back_and_forth(class1, class2)
|
51
|
+
get_type_and_name_of_association(class1, class2) + get_type_and_name_of_association(class2, class1)
|
43
52
|
end
|
44
53
|
|
45
|
-
|
54
|
+
def snake(string)
|
55
|
+
string = string.to_s
|
56
|
+
string.gsub!(/((\w)([A-Z]))/,'\2_\3')
|
57
|
+
class_name_if_module(string.downcase)
|
58
|
+
end
|
46
59
|
|
47
60
|
def class_name_if_module(string)
|
48
61
|
return $1 if string =~ /^.+::(.+)$/
|
49
62
|
string
|
50
63
|
end
|
51
|
-
end
|
64
|
+
end
|
@@ -47,7 +47,7 @@ module SlimFormObject
|
|
47
47
|
end
|
48
48
|
|
49
49
|
# CALLBACKS
|
50
|
-
%w(
|
50
|
+
%w(before_save after_save before_validation after_validation).each do |method_name|
|
51
51
|
define_singleton_method("#{method_name}_form".to_sym) do |&block|
|
52
52
|
if block_given?
|
53
53
|
self.instance_eval do
|
@@ -72,7 +72,6 @@ module SlimFormObject
|
|
72
72
|
self.params = params
|
73
73
|
get_or_add_default_objects
|
74
74
|
default_settings
|
75
|
-
self.after_initialize_block.call(self)
|
76
75
|
end
|
77
76
|
# END INIT
|
78
77
|
|
@@ -92,7 +91,6 @@ module SlimFormObject
|
|
92
91
|
|
93
92
|
def validation_models
|
94
93
|
Validator.new(self).validate_form_object
|
95
|
-
# self.after_validation_block.call(self)
|
96
94
|
end
|
97
95
|
|
98
96
|
def array_all_objects_for_save
|
@@ -123,7 +121,6 @@ module SlimFormObject
|
|
123
121
|
|
124
122
|
def default_settings
|
125
123
|
define_singleton_method(:array_models_which_not_save_if_empty) { [] } unless respond_to?(:array_models_which_not_save_if_empty)
|
126
|
-
define_singleton_method(:after_initialize_block) { Proc.new {} } unless respond_to?(:after_initialize_block)
|
127
124
|
define_singleton_method(:before_save_block) { Proc.new {} } unless respond_to?(:before_save_block)
|
128
125
|
define_singleton_method(:after_save_block) { Proc.new {} } unless respond_to?(:after_save_block)
|
129
126
|
define_singleton_method(:before_validation_block) { Proc.new {} } unless respond_to?(:before_validation_block)
|
@@ -54,15 +54,13 @@ module SlimFormObject
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def save_object(object_of_model)
|
57
|
-
|
58
|
-
object_of_model.save!
|
59
|
-
end
|
57
|
+
object_of_model.save!
|
60
58
|
end
|
61
59
|
|
62
60
|
def save_last_model_if_not_associations(object_1)
|
63
61
|
association_trigger = false
|
64
|
-
data_for_save.each { |hash| association_trigger = true if
|
65
|
-
object_1.save unless association_trigger
|
62
|
+
data_for_save.each { |hash| association_trigger = true if get_reflection(object_1.class, hash[:essence][:object].class) }
|
63
|
+
object_1.save! unless association_trigger
|
66
64
|
end
|
67
65
|
|
68
66
|
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: 2.0.
|
4
|
+
version: 2.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- woodcrust
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|