slim_form_object 2.0.4 → 2.1.0

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: 3de1e0db09d9602bafd6f0cc1450a17fdc71e86a
4
- data.tar.gz: bab8598dda0efe82bdebc1b95e7c44bc6445cef4
3
+ metadata.gz: 5117a69b875232f6d8bc23f95e09ff8b84e49f9d
4
+ data.tar.gz: ceac637e0890c87c2eb25340f1f41c7f0054fcfb
5
5
  SHA512:
6
- metadata.gz: 6cc0412036eb444986de4106610fb8583c09835e2dd1fd043da1412db77d0ac8bade9fc4daffcee70280223f407ed880a6c6ee88da5a775934369262bc6e866c
7
- data.tar.gz: 439ff3ebeaf41d1f2d58927f03452435a1d9d1f73be2744eaf76fe50da2f60c6a2d8b809894542defbf7749b1490c4cbcf7ff569f84610fee6f7c75267bbfb19
6
+ metadata.gz: aa2987d5ce904144c14c0e09b791599a0bc012136f081233dabcf81280356d7e76bfca1adeae98a52001bcad1fd585397dcddb6e496edea4160b10ef97a05613
7
+ data.tar.gz: ef5e893bdb0fd7d491fd50bbc421ae925c4aec47290a2348a6073944b0721224230af95712266bd82ec37abfb869fb67c0b6e5a6e30abe7163b1ab24b1a2adc9
@@ -2,7 +2,7 @@ module SlimFormObject
2
2
  class Assign
3
3
  include ::HelperMethods
4
4
 
5
- attr_reader :form_object, :params, :all_updated_objects, :base_module, :validator
5
+ attr_reader :form_object, :params, :data_for_save, :base_module, :validator
6
6
 
7
7
  def initialize(form_object)
8
8
  @form_object = form_object
@@ -10,29 +10,29 @@ module SlimFormObject
10
10
  @params = form_object.params
11
11
  @array_all_objects_for_save = form_object.array_all_objects_for_save
12
12
  @validator = Validator.new(form_object)
13
- @all_updated_objects = []
13
+ @data_for_save = []
14
14
  end
15
15
 
16
16
  def apply_parameters
17
17
  make_all_objects_with_attributes
18
18
  clear_nil_objects
19
19
 
20
- all_updated_objects
20
+ data_for_save
21
21
  end
22
22
 
23
23
  def associate_objects
24
- associate_all_nested_objects(all_updated_objects)
25
- associate_all_main_objects(all_updated_objects)
24
+ associate_all_nested_objects(data_for_save)
25
+ associate_all_main_objects(data_for_save)
26
26
 
27
- all_updated_objects
27
+ data_for_save
28
28
  end
29
29
 
30
30
  private
31
31
 
32
32
  def clear_nil_objects
33
33
  arr_ids_nil_object = []
34
- find_ids_nil_object(arr_ids_nil_object, all_updated_objects)
35
- delete_hash_nil_objects(arr_ids_nil_object, all_updated_objects)
34
+ find_ids_nil_object(arr_ids_nil_object, data_for_save)
35
+ delete_hash_nil_objects(arr_ids_nil_object, data_for_save)
36
36
  end
37
37
 
38
38
  def delete_hash_nil_objects(ids_arr, nested_array)
@@ -51,13 +51,13 @@ module SlimFormObject
51
51
 
52
52
  def associate_all_nested_objects(nested_array, object=nil)
53
53
  nested_array.each do |hash|
54
- to_bind_models(object, hash[:essence][:object]) if object
54
+ to_bind_models(object, hash[:essence][:object]) if object and validator.allow_to_associate_objects?(object, hash[:essence][:object])
55
55
  associate_all_nested_objects(hash[:nested], hash[:essence][:object])
56
56
  end
57
57
  end
58
58
 
59
- def associate_all_main_objects(all_updated_objects)
60
- objects = Array.new(all_updated_objects)
59
+ def associate_all_main_objects(data_for_save)
60
+ objects = Array.new(data_for_save)
61
61
  while object = objects.delete( objects[0] )
62
62
  object_1 = object[:essence][:object]
63
63
  objects.each do |hash|
@@ -70,7 +70,7 @@ module SlimFormObject
70
70
 
71
71
  def make_all_objects_with_attributes
72
72
  params.each do |main_model_name, hash|
73
- assign_objects_attributes(main_model_name, hash, all_updated_objects, :main)
73
+ assign_objects_attributes(main_model_name, hash, data_for_save, :main)
74
74
  end
75
75
  end
76
76
 
@@ -123,7 +123,7 @@ module SlimFormObject
123
123
  # }
124
124
  # }
125
125
 
126
- # make_all_objects_with_attributes() EXAMPLE @all_updated_objects FORMAT
126
+ # make_all_objects_with_attributes() EXAMPLE @data_for_save FORMAT
127
127
  #
128
128
  # [
129
129
  # {
@@ -47,13 +47,11 @@ module SlimFormObject
47
47
  end
48
48
 
49
49
  # CALLBACKS
50
- %w(before_save after_save before_validation after_validation).each do |method_name|
51
- define_singleton_method("#{method_name}_form".to_sym) do |&block|
52
- if block_given?
53
- self.instance_eval do
54
- define_method("#{method_name}_block".to_sym) { block }
55
- end
56
- end
50
+ %w(allow_to_save_object allow_to_associate_objects before_save_form after_save_form before_validation_form after_validation_form).each do |method_name|
51
+ define_method("#{method_name}".to_sym) do |&block|
52
+ self.instance_eval do
53
+ define_method("#{method_name}_block".to_sym) { block }
54
+ end if block
57
55
  end
58
56
  end
59
57
  # END CALLBACKS
@@ -104,9 +102,9 @@ module SlimFormObject
104
102
  end
105
103
 
106
104
  def apply
107
- assign = Assign.new(self)
108
- @data_for_save = assign.apply_parameters
109
- @data_for_save = assign.associate_objects
105
+ assign = Assign.new(self)
106
+ self.data_for_save = assign.apply_parameters
107
+ self.data_for_save = assign.associate_objects
110
108
  end
111
109
 
112
110
  def get_or_add_default_objects
@@ -121,12 +119,13 @@ module SlimFormObject
121
119
 
122
120
  def default_settings
123
121
  define_singleton_method(:array_models_which_not_save_if_empty) { [] } unless respond_to?(:array_models_which_not_save_if_empty)
124
- define_singleton_method(:before_save_block) { Proc.new {} } unless respond_to?(:before_save_block)
125
- define_singleton_method(:after_save_block) { Proc.new {} } unless respond_to?(:after_save_block)
126
- define_singleton_method(:before_validation_block) { Proc.new {} } unless respond_to?(:before_validation_block)
127
- define_singleton_method(:after_validation_block) { Proc.new {} } unless respond_to?(:after_validation_block)
122
+ define_singleton_method(:allow_to_associate_objects_block) { Proc.new { true } } unless respond_to?(:allow_to_associate_objects_block)
123
+ define_singleton_method(:allow_to_save_object_block) { Proc.new { true } } unless respond_to?(:allow_to_save_object_block)
124
+ define_singleton_method(:before_save_form_block) { Proc.new {} } unless respond_to?(:before_save_form_block)
125
+ define_singleton_method(:after_save_form_block) { Proc.new {} } unless respond_to?(:after_save_form_block)
126
+ define_singleton_method(:before_validation_form_block) { Proc.new {} } unless respond_to?(:before_validation_form_block)
127
+ define_singleton_method(:after_validation_form_block) { Proc.new {} } unless respond_to?(:after_validation_form_block)
128
128
  end
129
-
130
129
  end
131
130
  end
132
131
 
@@ -33,9 +33,10 @@ module SlimFormObject
33
33
 
34
34
  def save_all
35
35
  ActiveRecord::Base.transaction do
36
- form_object.before_save_block.call(form_object)
36
+ form_object.before_save_form_block.call(form_object)
37
37
  save_main_objects
38
- form_object.after_save_block.call(form_object)
38
+ save_nested_objects
39
+ form_object.after_save_form_block.call(form_object)
39
40
  end
40
41
  end
41
42
 
@@ -48,20 +49,27 @@ module SlimFormObject
48
49
  end
49
50
  end
50
51
 
52
+ def save_nested_objects
53
+ data_for_save.each do |main_object|
54
+ main_object[:nested].each do |object|
55
+ save_object(object[:essence][:object])
56
+ end
57
+ end
58
+ end
59
+
51
60
  def save_objects(object_1, object_2)
52
61
  object_for_save = to_bind_models(object_1, object_2)
53
62
  save_object(object_for_save)
54
63
  end
55
64
 
56
65
  def save_object(object_of_model)
57
- object_of_model.save!
66
+ object_of_model.save! if validator.allow_to_save_object?(object_of_model)
58
67
  end
59
68
 
60
69
  def save_last_model_if_not_associations(object_1)
61
70
  association_trigger = false
62
71
  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
72
+ save_object(object_1) unless association_trigger
64
73
  end
65
-
66
74
  end
67
75
  end
@@ -13,15 +13,23 @@ module SlimFormObject
13
13
  end
14
14
 
15
15
  def validate_form_object
16
- form_object.before_validation_block.call(form_object)
16
+ form_object.before_validation_form_block.call(form_object)
17
17
  validation_objects(data_for_save)
18
- form_object.after_validation_block.call(form_object)
18
+ form_object.after_validation_form_block.call(form_object)
19
19
  end
20
20
 
21
21
  def save_if_object_is_empty?(object)
22
22
  !(all_attributes_is_nil?(object) and array_models_which_not_save_if_empty.include?(object.class))
23
23
  end
24
24
 
25
+ def allow_to_save_object?(object)
26
+ form_object.allow_to_save_object_block.call(object)
27
+ end
28
+
29
+ def allow_to_associate_objects?(object_1, object_2)
30
+ form_object.allow_to_associate_objects_block.call(object_1, object_2)
31
+ end
32
+
25
33
  private
26
34
 
27
35
  def validation_objects(nested_array)
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "2.0.4"
2
+ VERSION = "2.1.0"
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.4
4
+ version: 2.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: 2017-10-13 00:00:00.000000000 Z
11
+ date: 2017-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel