slim_form_object 0.5.14 → 0.5.19

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: 08415906154c7a4e386d9294d708acac0cc2afe0
4
- data.tar.gz: 6afcece85875f2388fcac37b1cf904a62492b4db
3
+ metadata.gz: 239fc8e0a836b0c614a1b7607a7775057f5f91cf
4
+ data.tar.gz: a1240a1e9dd26ca7bdb0d39744c7d99f07ebee6f
5
5
  SHA512:
6
- metadata.gz: 048b844bd6610e2886d9300484fb8f959dfc68cd704b4d45ae4cee8b8a271bf3ca49d7ca917ecef7bceb750049ff541654df5583f9cac6302837fb4fc4d776c9
7
- data.tar.gz: 06f1bcd3814b9374a2e4fe40ad5bcc983d39c3b7ca088fd2a1e0660fd7541122aab716daea8e94da3f0d846effbbc1ec15392592d13a41f3f9dea78a736bd990
6
+ metadata.gz: 1cb24a67ce6370d71bc00c7ae71a9720db254bca41c7992f0ac4b9f3d4023628f4860166895aa0e5d153f4515a05a095630a3c3f07da291e783b929edfaf148c
7
+ data.tar.gz: cae3003ff4b6f04744ab92c1ba96aa955cdf2e29ff2153e91c0c3eb0adbe7cbdfb438d5f494123af3a49f7d64224cf94986f9130af21c8d35537e23a120bbc11
@@ -1,171 +1,7 @@
1
1
  require "slim_form_object/version"
2
2
  require "slim_form_object/helpers"
3
+ require "slim_form_object/processing"
3
4
 
4
5
  module SlimFormObject
5
6
 
6
- def self.included(base)
7
- base.include ActiveModel::Model
8
- base.include HelperMethods
9
- base.extend ClassMethods
10
- base.extend HelperMethods
11
- end
12
-
13
- module ClassMethods
14
-
15
- def init_models(*args)
16
- self.instance_eval do
17
- define_method(:array_of_models) { args }
18
- end
19
- add_attributes(args)
20
- end
21
-
22
- def add_attributes(models)
23
- #attr_accessor for models and env params
24
- attr_accessor :params
25
- models.each{ |model| attr_accessor snake(model.to_s).to_sym }
26
-
27
- #delegate attributes of models
28
- models.each do |model|
29
- model.column_names.each do |attr|
30
- delegate attr.to_sym, "#{attr}=".to_sym,
31
- to: snake(model.to_s).to_sym,
32
- prefix: snake(model.to_s).to_sym
33
- end
34
- end
35
- end
36
-
37
- def set_model_name(name)
38
- @@set_name = name
39
- class << self
40
- def model_name
41
- ActiveModel::Name.new(self, nil, @@set_name.to_s)
42
- end
43
- end
44
- end
45
- end
46
-
47
- def submit
48
- update_attributes
49
- update_attributes_for_collection
50
- self
51
- end
52
-
53
- alias_method :apply_parameters, :submit
54
-
55
- def save
56
- if valid?
57
- models = Array.new(array_of_models)
58
- while model1 = models.delete( models[0] )
59
- array_of_models.each{ |model2| save_models(model1, model2) }
60
- end
61
-
62
- return true
63
- end
64
- false
65
- end
66
-
67
- private
68
-
69
- def save_models(model1, model2)
70
- self_object_of_model1 = method( snake(model1.to_s) ).call
71
- self_object_of_model2 = method( snake(model2.to_s) ).call
72
-
73
- case get_association(model1, model2)
74
- when :belongs_to
75
- self_object_of_model1.send( "#{snake(model2.to_s)}=", self_object_of_model2 )
76
- self_object_of_model1.save!
77
- when :has_one
78
- self_object_of_model1.send( "#{snake(model2.to_s)}=", self_object_of_model2 )
79
- self_object_of_model1.save!
80
- when :has_many
81
- self_object_of_model1.method("#{model2.table_name}").call << self_object_of_model2
82
- self_object_of_model1.save!
83
- when :has_and_belongs_to_many
84
- self_object_of_model1.method("#{model2.table_name}").call << self_object_of_model2
85
- self_object_of_model1.save!
86
- end
87
- end
88
-
89
- def validation_models
90
- array_of_models.each do |model|
91
- set_errors( method(snake(model.to_s)).call.errors ) unless method( snake(model.to_s) ).call.valid?
92
- end
93
- end
94
-
95
- def set_errors(model_errors)
96
- model_errors.each do |attribute, message|
97
- errors.add(attribute, message)
98
- end
99
- end
100
-
101
- def update_attributes
102
- array_of_models.each do |model|
103
- model_attributes = make_attributes_of_model(model)
104
- method( snake(model.to_s) ).call.assign_attributes( get_attributes_for_update(model_attributes, model) )
105
- end
106
- end
107
-
108
- def update_attributes_for_collection
109
- array_of_models.each do |model|
110
- assign_attributes_for_collection(model)
111
- end
112
- end
113
-
114
- def keys_of_collections
115
- @keys ||= []
116
- params.keys.each do |key|
117
- array_of_models.each do |model|
118
- self_object_of_model = method( snake(model.to_s) ).call
119
- method_name = key.to_s[/#{snake(model.to_s)}_(.*)/, 1]
120
- @keys << method_name if self_object_of_model.respond_to? method_name.to_s
121
- end if key[/^.+_ids$/]
122
- end if @keys.empty?
123
- @keys
124
- end
125
-
126
- def exist_any_arrors_without_collections?
127
- keys_of_collections.each do |method_name|
128
- array_of_models.each do |model|
129
- name_of_model = method_name.to_s[/^(.+)_ids$/, 1]
130
- name_of_constant_model = name_of_model.split('_').map(&:capitalize).join
131
- name_of_key_error = Object.const_get(name_of_constant_model).table_name
132
- errors.messages.delete(name_of_key_error.to_sym)
133
- end
134
- end unless valid?
135
- errors.messages.empty?
136
- end
137
-
138
- def assign_attributes_for_collection(model)
139
- self_object_of_model = method( snake(model.to_s) ).call
140
-
141
- keys_of_collections.each do |method_name|
142
- if self_object_of_model.respond_to? method_name
143
- old_attribute = self_object_of_model.method( method_name ).call
144
- unless self_object_of_model.update_attributes( {method_name.to_s => params["#{snake(model.to_s)}_#{method_name}".to_sym]} )
145
- set_errors(self_object_of_model.errors)
146
- self_object_of_model.update_attributes( {method_name.to_s => old_attribute} )
147
- end if exist_any_arrors_without_collections?
148
- end
149
- end
150
- end
151
-
152
- def make_attributes_of_model(model)
153
- model_attributes = []
154
- model.column_names.each do |name|
155
- model_attributes << "#{snake(model.to_s)}_#{name}"
156
- end
157
- model_attributes
158
- end
159
-
160
- def get_attributes_for_update(model_attributes, model)
161
- update_attributes = {}
162
- hash_attributes = params.slice(*model_attributes)
163
- hash_attributes.each{ |attr, val| update_attributes[attr.gsub(/#{snake(model.to_s)}_(.*)/, '\1')] = val }
164
- update_attributes
165
- end
166
-
167
- def get_association(class1, class2)
168
- class1.reflections.slice(snake(class2.to_s), class2.table_name).values.first.try(:macro)
169
- end
170
-
171
7
  end
@@ -1,5 +1,11 @@
1
1
  module HelperMethods
2
2
  def snake(string)
3
- string.gsub(/((\w)([A-Z]))/,'\2_\3').downcase
3
+ string.gsub!(/((\w)([A-Z]))/,'\2_\3')
4
+ class_name_if_module(string.downcase)
5
+ end
6
+
7
+ def class_name_if_module(string)
8
+ return $1 if string =~ /^.+::(.+)$/
9
+ string
4
10
  end
5
11
  end
@@ -0,0 +1,163 @@
1
+ module SlimFormObject
2
+
3
+ def self.included(base)
4
+ base.include ActiveModel::Model
5
+ base.include HelperMethods
6
+ base.extend ClassMethods
7
+ base.extend HelperMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def init_models(*args)
13
+ self.instance_eval do
14
+ define_method(:array_of_models) { args }
15
+ end
16
+ add_attributes(args)
17
+ end
18
+
19
+ def add_attributes(models)
20
+ #attr_accessor for models and env params
21
+ attr_accessor :params
22
+ models.each{ |model| attr_accessor snake(model.to_s).to_sym }
23
+
24
+ #delegate attributes of models
25
+ models.each do |model|
26
+ model.column_names.each do |attr|
27
+ delegate attr.to_sym, "#{attr}=".to_sym,
28
+ to: snake(model.to_s).to_sym,
29
+ prefix: snake(model.to_s).to_sym
30
+ end
31
+ end
32
+ end
33
+
34
+ def set_model_name(name)
35
+ define_method(:model_name) { ActiveModel::Name.new(self, nil, name) }
36
+ end
37
+ end
38
+
39
+ def submit
40
+ update_attributes
41
+ update_attributes_for_collection
42
+ self
43
+ end
44
+
45
+ alias_method :apply_parameters, :submit
46
+
47
+ def save
48
+ if valid?
49
+ models = Array.new(array_of_models)
50
+ while model1 = models.delete( models[0] )
51
+ array_of_models.each{ |model2| save_models(model1, model2) }
52
+ end
53
+
54
+ return true
55
+ end
56
+ false
57
+ end
58
+
59
+ private
60
+
61
+ def save_models(model1, model2)
62
+ self_object_of_model1 = method( snake(model1.to_s) ).call
63
+ self_object_of_model2 = method( snake(model2.to_s) ).call
64
+
65
+ case get_association(model1, model2)
66
+ when :belongs_to
67
+ self_object_of_model1.send( "#{snake(model2.to_s)}=", self_object_of_model2 )
68
+ self_object_of_model1.save!
69
+ when :has_one
70
+ self_object_of_model1.send( "#{snake(model2.to_s)}=", self_object_of_model2 )
71
+ self_object_of_model1.save!
72
+ when :has_many
73
+ self_object_of_model1.method("#{model2.table_name}").call << self_object_of_model2
74
+ self_object_of_model1.save!
75
+ when :has_and_belongs_to_many
76
+ self_object_of_model1.method("#{model2.table_name}").call << self_object_of_model2
77
+ self_object_of_model1.save!
78
+ end
79
+ end
80
+
81
+ def validation_models
82
+ array_of_models.each do |model|
83
+ set_errors( method(snake(model.to_s)).call.errors ) unless method( snake(model.to_s) ).call.valid?
84
+ end
85
+ end
86
+
87
+ def set_errors(model_errors)
88
+ model_errors.each do |attribute, message|
89
+ errors.add(attribute, message)
90
+ end
91
+ end
92
+
93
+ def update_attributes
94
+ array_of_models.each do |model|
95
+ model_attributes = make_attributes_of_model(model)
96
+ method( snake(model.to_s) ).call.assign_attributes( get_attributes_for_update(model_attributes, model) )
97
+ end
98
+ end
99
+
100
+ def update_attributes_for_collection
101
+ array_of_models.each do |model|
102
+ assign_attributes_for_collection(model)
103
+ end
104
+ end
105
+
106
+ def keys_of_collections
107
+ @keys ||= []
108
+ params.keys.each do |key|
109
+ array_of_models.each do |model|
110
+ self_object_of_model = method( snake(model.to_s) ).call
111
+ method_name = key.to_s[/#{snake(model.to_s)}_(.*)/, 1]
112
+ @keys << method_name if self_object_of_model.respond_to? method_name.to_s
113
+ end if key[/^.+_ids$/]
114
+ end if @keys.empty?
115
+ @keys
116
+ end
117
+
118
+ def exist_any_errors_without_collections?
119
+ keys_of_collections.each do |method_name|
120
+ array_of_models.each do |model|
121
+ name_of_model = method_name.to_s[/^(.+)_ids$/, 1]
122
+ name_of_constant_model = name_of_model.split('_').map(&:capitalize).join
123
+ name_of_key_error = Object.const_get(name_of_constant_model).table_name
124
+ errors.messages.delete(name_of_key_error.to_sym)
125
+ end
126
+ end unless valid?
127
+ errors.messages.empty?
128
+ end
129
+
130
+ def assign_attributes_for_collection(model)
131
+ self_object_of_model = method( snake(model.to_s) ).call
132
+
133
+ keys_of_collections.each do |method_name|
134
+ if self_object_of_model.respond_to? method_name
135
+ old_attribute = self_object_of_model.method( method_name ).call
136
+ unless self_object_of_model.update_attributes( {method_name.to_s => params["#{snake(model.to_s)}_#{method_name}".to_sym]} )
137
+ set_errors(self_object_of_model.errors)
138
+ self_object_of_model.update_attributes( {method_name.to_s => old_attribute} )
139
+ end if exist_any_errors_without_collections?
140
+ end
141
+ end
142
+ end
143
+
144
+ def make_attributes_of_model(model)
145
+ model_attributes = []
146
+ model.column_names.each do |name|
147
+ model_attributes << "#{snake(model.to_s)}_#{name}"
148
+ end
149
+ model_attributes
150
+ end
151
+
152
+ def get_attributes_for_update(model_attributes, model)
153
+ update_attributes = {}
154
+ hash_attributes = params.slice(*model_attributes)
155
+ hash_attributes.each{ |attr, val| update_attributes[attr.gsub(/#{snake(model.to_s)}_(.*)/, '\1')] = val }
156
+ update_attributes
157
+ end
158
+
159
+ def get_association(class1, class2)
160
+ class1.reflections.slice(snake(class2.to_s), class2.table_name).values.first.try(:macro)
161
+ end
162
+
163
+ end
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "0.5.14"
2
+ VERSION = "0.5.19"
3
3
  end
@@ -239,7 +239,7 @@ describe TestModule do
239
239
  end
240
240
  end
241
241
 
242
- context 'exist_any_arrors_without_collections?' do
242
+ context 'exist_any_errors_without_collections?' do
243
243
  before :each do
244
244
  object.instance_eval do
245
245
  def test_one_model
@@ -257,7 +257,7 @@ describe TestModule do
257
257
  object.stub(:valid?).and_return( false )
258
258
  object.stub_chain(:errors, :messages).and_return( {:test_one_models=>'error', :test_four_models=>'error'} )
259
259
 
260
- expect(object.send :exist_any_arrors_without_collections?).to eq( true )
260
+ expect(object.send :exist_any_errors_without_collections?).to eq( true )
261
261
  end
262
262
 
263
263
  it 'must be return false' do
@@ -271,7 +271,7 @@ describe TestModule do
271
271
  object.stub(:valid?).and_return( false )
272
272
  object.stub_chain(:errors, :messages).and_return( {:test_one_models=>'error', :test_four_models=>'error', :descr=>'error'} )
273
273
 
274
- expect(object.send :exist_any_arrors_without_collections?).to eq( false )
274
+ expect(object.send :exist_any_errors_without_collections?).to eq( false )
275
275
  end
276
276
  end
277
277
 
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: 0.5.14
4
+ version: 0.5.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-07 00:00:00.000000000 Z
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -136,8 +136,8 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
- description: Very simple automatic generation and saving of your models nested attributes.
140
- For ActiveModel.
139
+ description: Very simple automatic generation and saving nested attributes of models
140
+ from html form. ActiveModel.
141
141
  email:
142
142
  - roboucrop@gmail.com
143
143
  executables:
@@ -148,6 +148,7 @@ files:
148
148
  - bin/slim_form_object
149
149
  - lib/slim_form_object.rb
150
150
  - lib/slim_form_object/helpers.rb
151
+ - lib/slim_form_object/processing.rb
151
152
  - lib/slim_form_object/version.rb
152
153
  - spec/db/database.yml
153
154
  - spec/db/database.yml.travis