slim_form_object 0.4.1 → 0.5.1

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: f76df6c6f6b9c3318cf166532415097aeeecc01e
4
- data.tar.gz: b3bb60a33463ba3b22fe3b3c11224536a1e0fc4c
3
+ metadata.gz: cb83b2555e0774293968cf63af140307e868227c
4
+ data.tar.gz: ea270672e17684d5db94c684b18e1debe00c9c84
5
5
  SHA512:
6
- metadata.gz: 8bbe5a41ad2ee548bb194acf4531a6ea2a46e07584c624e7417ee36f37ff104cd85ebbf59a7fd19bb844ab659920d0ec966fc3424330e9a1cb6823c7474ff2f6
7
- data.tar.gz: 60cef2c3cb0bb795ac8d22cd480f8d0d9a90eac3d1aedfde323c0e42b4731b0b1a41ab30972a8f129d1660548edf132c53166d52c514fcd9f29a6c8f27922c9b
6
+ metadata.gz: f7b059c728efba05791fa2e4594c8114e0b93a3cf9f29e01a098c352eb83cc508908045ca0ffbc964331689a1b39d73233d1a88e386b083bb1e621eef40dc27c
7
+ data.tar.gz: 278ecb3ff653d5568832d8c19772ac0e7938098b6af30c936a0372209a9db90ef132819b27a5eb346da7a96aac721ddd0ed0a88f3b3ea90ede68b5e5995ec9a5
@@ -0,0 +1,5 @@
1
+ module HelperMethods
2
+ def snake(string)
3
+ string.gsub(/((\w)([A-Z]))/,'\2_\3').downcase
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -1,13 +1,17 @@
1
1
  require "slim_form_object/version"
2
+ require "slim_form_object/helpers"
2
3
 
3
4
  module SlimFormObject
4
5
 
5
6
  def self.included(base)
6
- base.include(ActiveModel::Model)
7
- base.extend(ClassMethods)
7
+ base.include ActiveModel::Model
8
+ base.include HelperMethods
9
+ base.extend ClassMethods
10
+ base.extend HelperMethods
8
11
  end
9
12
 
10
13
  module ClassMethods
14
+
11
15
  def init_models(*args)
12
16
  self.instance_eval do
13
17
  define_method(:array_of_models) { args }
@@ -15,10 +19,6 @@ module SlimFormObject
15
19
  add_attributes(args)
16
20
  end
17
21
 
18
- def snake(string)
19
- string.gsub(/((\w)([A-Z]))/,'\2_\3').downcase
20
- end
21
-
22
22
  def add_attributes(models)
23
23
  #attr_accessor for models and env params
24
24
  attr_accessor :params
@@ -41,7 +41,7 @@ module SlimFormObject
41
41
 
42
42
  def save
43
43
  if valid?
44
- models = get_model_for_save
44
+ models = Array.new(array_of_models)
45
45
  while model1 = models.delete( models[0] )
46
46
  array_of_models.each{ |model2| save_models(model1, model2) }
47
47
  end
@@ -54,28 +54,28 @@ module SlimFormObject
54
54
  private
55
55
 
56
56
  def save_models(model1, model2)
57
- self_model1 = method( self.class.snake(model1.to_s) ).call
58
- self_model2 = method( self.class.snake(model2.to_s) ).call
57
+ self_object_of_model1 = method( snake(model1.to_s) ).call
58
+ self_object_of_model2 = method( snake(model2.to_s) ).call
59
59
 
60
60
  case get_association(model1, model2)
61
61
  when :belongs_to
62
- self_model1.send( "#{self.class.snake(model2.to_s)}=", self_model2 )
63
- self_model1.save
62
+ self_object_of_model1.send( "#{snake(model2.to_s)}=", self_object_of_model2 )
63
+ self_object_of_model1.save!
64
64
  when :has_one
65
- self_model1.send( "#{self.class.snake(model2.to_s)}=", self_model2 )
66
- self_model1.save
65
+ self_object_of_model1.send( "#{snake(model2.to_s)}=", self_object_of_model2 )
66
+ self_object_of_model1.save!
67
67
  when :has_many
68
- self_model1.method("#{model2.table_name}").call << self_model2
69
- self_model1.save
68
+ self_object_of_model1.method("#{model2.table_name}").call << self_object_of_model2
69
+ self_object_of_model1.save!
70
70
  when :has_and_belongs_to_many
71
- self_model1.method("#{model2.table_name}").call << self_model2
72
- self_model1.save
71
+ self_object_of_model1.method("#{model2.table_name}").call << self_object_of_model2
72
+ self_object_of_model1.save!
73
73
  end
74
74
  end
75
75
 
76
76
  def validation_models
77
- get_model_for_save.each do |model|
78
- set_errors( method(self.class.snake(model.to_s)).call.errors ) unless method( self.class.snake(model.to_s) ).call.valid?
77
+ array_of_models.each do |model|
78
+ set_errors( method(snake(model.to_s)).call.errors ) unless method( snake(model.to_s) ).call.valid?
79
79
  end
80
80
  end
81
81
 
@@ -88,14 +88,15 @@ module SlimFormObject
88
88
  def update_attributes
89
89
  array_of_models.each do |model|
90
90
  model_attributes = make_attributes_of_model(model)
91
- method( self.class.snake(model.to_s) ).call.assign_attributes( get_attributes_for_update(model_attributes, model) )
91
+ method( snake(model.to_s) ).call.assign_attributes( get_attributes_for_update(model_attributes, model) )
92
+ assign_attributes_for_collection(model)
92
93
  end
93
94
  end
94
95
 
95
96
  def make_attributes_of_model(model)
96
97
  model_attributes = []
97
98
  model.column_names.each do |name|
98
- model_attributes << "#{self.class.snake(model.to_s)}_#{name}"
99
+ model_attributes << "#{snake(model.to_s)}_#{name}"
99
100
  end
100
101
  model_attributes
101
102
  end
@@ -103,26 +104,27 @@ module SlimFormObject
103
104
  def get_attributes_for_update(model_attributes, model)
104
105
  update_attributes = {}
105
106
  hash_attributes = params.slice(*model_attributes)
106
- hash_attributes.each{ |attr, val| update_attributes[attr.gsub(/#{self.class.snake(model.to_s)}_(.*)/, '\1')] = val }
107
+ hash_attributes.each{ |attr, val| update_attributes[attr.gsub(/#{snake(model.to_s)}_(.*)/, '\1')] = val }
107
108
  update_attributes
108
109
  end
109
110
 
110
- def get_model_for_save
111
- keys = params.keys
112
- models = []
113
- array_of_models.each do |model|
114
- model.column_names.each do |name|
115
- keys.each do |key|
116
- models << model if key.to_s == "#{self.class.snake(model.to_s)}_#{name}"
117
- end
118
- end
119
- end
120
- models.uniq!
121
- models
111
+ def get_association(class1, class2)
112
+ class1.reflections.slice(snake(class2.to_s), class2.table_name).values.first.try(:macro)
122
113
  end
123
114
 
124
- def get_association(class1, class2)
125
- class1.reflections.slice(self.class.snake(class2.to_s), class2.table_name).values.first.try(:macro)
115
+ def keys_of_collections
116
+ params.keys.select{ |key| key =~ /^.+_ids$/ }
117
+ end
118
+
119
+ def assign_attributes_for_collection(model)
120
+ self_object_of_model = method( snake(model.to_s) ).call
121
+
122
+ keys_of_collections.each do |key|
123
+ key.to_s =~ /#{snake(model.to_s)}_(.*)/
124
+ if self_object_of_model.respond_to? $1.to_s
125
+ self_object_of_model.assign_attributes( {$1.to_s => params[key]} )
126
+ end
127
+ end
126
128
  end
127
129
 
128
130
  end
@@ -154,7 +154,7 @@ describe TestModule do
154
154
  it 'errors is present' do
155
155
  object.stub(:test_one_model).and_return( TestOneModel.new(descr: 'desc') )
156
156
 
157
- expect(object).to receive(:get_model_for_save).and_return([TestOneModel])
157
+ expect(object).to receive(:array_of_models).and_return([TestOneModel])
158
158
  expect(object).to receive(:set_errors).and_return( true )
159
159
  object.send :validation_models
160
160
  end
@@ -162,7 +162,7 @@ describe TestModule do
162
162
  it 'errors is not exist' do
163
163
  object.stub(:test_one_model).and_return( TestOneModel.new(title: 'title', descr: 'desc') )
164
164
 
165
- expect(object).to receive(:get_model_for_save).and_return([TestOneModel])
165
+ expect(object).to receive(:array_of_models).and_return([TestOneModel])
166
166
  expect(object).not_to receive(:set_errors)
167
167
  object.send :validation_models
168
168
  end
@@ -176,6 +176,7 @@ describe TestModule do
176
176
  expect(object).to receive(:array_of_models).and_return( [TestOneModel] )
177
177
  expect(object).to receive(:make_attributes_of_model).and_return( attributes_of_model )
178
178
  expect(object).to receive(:get_attributes_for_update).and_return( attributes_for_update )
179
+ expect(object).to receive(:assign_attributes_for_collection).and_return( true )
179
180
  object.stub(:test_one_model).and_return( TestOneModel.new )
180
181
  object.send :update_attributes
181
182
  end
@@ -199,19 +200,42 @@ describe TestModule do
199
200
  end
200
201
  end
201
202
 
202
- context 'get_model_for_save' do
203
- it 'get models from params (exist)' do
204
- object.stub(:array_of_models).and_return( [TestOneModel, TestTwoModel, TestThreeModel] )
205
- object.stub(:params).and_return( {'test_one_model_title'=>'Test Title', 'test_two_model_descr'=>'Test Descr'} )
203
+ context 'assign_attributes_for_collection' do
204
+ before :each do
205
+ @test_object = TestOneModel.new
206
+ (1...4).each { TestFourModel.create(title:'title', descr:'descr') }
207
+ end
208
+
209
+ it 'assign attributes for test_one_model' do
210
+ object.stub(:params).and_return( {test_one_model_test_four_model_ids: [1, 2, 3]} )
211
+ object.stub_chain(:method, :call) { @test_object }
212
+ object.stub(:keys_of_collections).and_return( [:test_one_model_test_four_model_ids] )
213
+ object.send(:assign_attributes_for_collection, TestOneModel)
214
+
215
+ expect(@test_object.test_four_model_ids).to eq( [1, 2, 3] )
216
+ end
217
+
218
+ it 'must don\'t assign attributes for test_one_model' do
219
+ object.stub(:params).and_return( {test_ids: [1], _ids: [1], ids_test: [1], test_one_model_test_four_model_idss: [1], test_ids_one: [1]} )
220
+ object.stub_chain(:method, :call) { @test_object }
221
+ object.stub(:keys_of_collections).and_return( [:test_ids, :_ids, :ids_test, :test_one_model_test_four_model_idss, :test_ids_one] )
222
+ object.send(:assign_attributes_for_collection, TestOneModel)
223
+
224
+ expect(@test_object.test_four_model_ids).to eq( [] )
225
+ end
226
+ end
227
+
228
+ context 'keys_of_collections' do
229
+ it 'must be return array with values' do
230
+ object.stub(:params).and_return( {test_one_model_test_four_model_ids: [1, 2, 3]} )
206
231
 
207
- expect(object.send :get_model_for_save).to eq( [TestOneModel, TestTwoModel] )
232
+ expect(object.send :keys_of_collections).to eq( [:test_one_model_test_four_model_ids] )
208
233
  end
209
234
 
210
- it 'get models from params (must be return empty array)' do
211
- object.stub(:array_of_models).and_return( [TestOneModel, TestTwoModel, TestThreeModel] )
212
- object.stub(:params).and_return( {} )
235
+ it 'must be return array without values' do
236
+ object.stub(:params).and_return( {_ids: [1], ids_test: [1], test_one_model_test_four_model_idss: [1], test_ids_one: [1]} )
213
237
 
214
- expect(object.send :get_model_for_save).to eq( [] )
238
+ expect(object.send :keys_of_collections).to eq( [] )
215
239
  end
216
240
  end
217
241
 
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_record'
5
5
  require 'rspec/collection_matchers'
6
6
  require 'database_cleaner'
7
7
  require 'helpers/connect_to_base'
8
+ require 'byebug'
8
9
 
9
10
  RSpec.configure do |config|
10
11
 
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.4.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-01 00:00:00.000000000 Z
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -132,6 +132,7 @@ extra_rdoc_files: []
132
132
  files:
133
133
  - bin/slim_form_object
134
134
  - lib/slim_form_object.rb
135
+ - lib/slim_form_object/helpers.rb
135
136
  - lib/slim_form_object/version.rb
136
137
  - spec/db/database.yml
137
138
  - spec/db/database.yml.travis