slim_form_object 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fd0b9f7cfacc36f4b8942582d15823dc758eb5c
4
- data.tar.gz: cce413cf2d88d6ef030c30815d8256c8cd4fd5cb
3
+ metadata.gz: 3de1e0db09d9602bafd6f0cc1450a17fdc71e86a
4
+ data.tar.gz: bab8598dda0efe82bdebc1b95e7c44bc6445cef4
5
5
  SHA512:
6
- metadata.gz: f9edb2bb9b7710fb6e5bc2f88d9922b3569a67589a563e02efb8287afa81330d30d7d78a0b664e764731ad4b3696812cf01ca7828be0ab2eda12ca7b69c28e2a
7
- data.tar.gz: 5be93c1ad8b14b667be358db84669a0611901b9c1f15d992c22d5624b4153ef3850184530cf02eaf9d62d9dedb121224c12635348b77a706ad9eba7831704c61
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
- associate_all_objects(all_updated_objects)
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 associate_all_objects(nested_array, object=nil)
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
- associate_all_objects(hash[:nested], hash[:essence][:object])
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, object_hash, @all_updated_objects)
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, object_hash, result_array)
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, object_hash, result_array)
78
- object = get_class_of_snake_model_name(model_name).new
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 ActionView
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
- def sfo_collection_ads_regexp
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
- def sfo_attr?(method)
28
- sfo_single_attr?(method)
29
- end
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
- def sfo_single_attr?(method)
32
- method.to_s[sfo_single_attr_regexp] ? true : false
33
- end
9
+ def sfo_single_attr_regexp
10
+ /^([^-]+)-([^-]+)$/
11
+ end
34
12
 
35
- def sfo_date_attr?(tag_name)
36
- tag_name.to_s[sfo_date_attr_regexp] ? true : false
37
- end
13
+ def sfo_date_attr_regexp
14
+ /^([^-]+)-([^-]+)(\([\s\S]+\))$/
15
+ end
38
16
 
39
- def sfo_collection_ads_attr?(tag_name)
40
- tag_name.to_s[sfo_collection_ads_regexp] ? true : false
41
- end
17
+ def sfo_collection_ads_regexp
18
+ /_ids$/
19
+ end
42
20
 
43
- def sfo_get_tag_name(object_name, method, multiple, options)
44
- model_name, attr_name = apply_expression_text(method, sfo_single_attr_regexp)
21
+ def sfo_form_attribute?(object)
22
+ object.class.ancestors[1] == SlimFormObject::Base if object
23
+ end
45
24
 
46
- if options[:sfo_nested]
47
- if options[:sfo_main]
48
- tag_name = "#{object_name}[#{model_name}][][#{attr_name}]#{"[]" if multiple}"
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
- tag_name
57
- end
29
+ def sfo_single_attr?(method)
30
+ method.to_s[sfo_single_attr_regexp] ? true : false
31
+ end
58
32
 
59
- def sfo_get_method_name(method)
60
- if sfo_single_attr?(method) and !sfo_collection_ads_attr?(method)
61
- model_name, attr_name = apply_expression_text(method, sfo_single_attr_regexp)
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
- method
66
- end
37
+ def sfo_collection_ads_attr?(tag_name)
38
+ tag_name.to_s[sfo_collection_ads_regexp] ? true : false
39
+ end
67
40
 
68
- def sfo_get_date_tag_name(prefix, tag_name, options)
69
- model_name, attr_name, date_type = apply_expression_date(tag_name, sfo_date_attr_regexp)
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
- if options[:sfo_nested]
72
- tag_name = "#{prefix}[#{model_name}][][#{attr_name}#{date_type}]"
73
- else
74
- tag_name = "#{prefix}[#{model_name}][#{attr_name}#{date_type}]"
75
- end
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
- tag_name
78
- end
56
+ method
57
+ end
79
58
 
80
- def apply_expression_date(string, exp)
81
- string[exp]
82
- model_name = $1
83
- attr_name = $2
84
- date_type = $3
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
- [model_name, attr_name, date_type]
87
- end
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
- def apply_expression_text(string, exp)
90
- string[exp]
91
- model_name = $1
92
- attr_name = $2
74
+ [model_name, attr_name, date_type]
75
+ end
93
76
 
94
- [model_name, attr_name]
95
- end
96
- end
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 = 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 get_class_of_snake_model_name(snake_model_name)
7
- pref = if self.base_module
8
- self.base_module.to_s + '::'
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
- Object.const_get( pref + snake_model_name.to_s.split('_').map(&:capitalize).join )
19
+
20
+ object_1
13
21
  end
14
22
 
15
- def apply_expression_text(string, exp)
16
- string[exp]
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
- [model_name, attr_name]
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 snake(string)
24
- string = string.to_s
25
- string.gsub!(/((\w)([A-Z]))/,'\2_\3')
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 to_bind_models(object_1, object_2)
30
- association = get_association(object_1.class, object_2.class)
38
+ def get_reflection(class1, class2)
39
+ class1.reflections.select{ |k,v| v.klass == class2 }.values.first
40
+ end
31
41
 
32
- if association == :belongs_to or association == :has_one
33
- object_1.send( "#{snake(object_2.class.to_s)}=", object_2 )
34
- elsif association == :has_many or association == :has_and_belongs_to_many
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
- object_1
46
+ def method_name_association(reflection)
47
+ reflection&.name
39
48
  end
40
49
 
41
- def get_association(class1, class2)
42
- class1.reflections.slice(snake(class2.to_s), class2.table_name).values.first&.macro
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
- private
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(after_initialize before_save after_save before_validation after_validation).each do |method_name|
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
- if validator.valid_model_for_save?(object_of_model.class)
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 get_association(object_1.class, hash[:essence][:object].class) }
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
@@ -43,7 +43,6 @@ module SlimFormObject
43
43
  form_object.errors.add(object_name, { attribute => message})
44
44
  end
45
45
  end
46
-
47
46
  end
48
47
  end
49
48
 
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "2.0.3"
2
+ VERSION = "2.0.4"
3
3
  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.3
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-12 00:00:00.000000000 Z
11
+ date: 2017-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel