slim_form_object 1.0.5 → 1.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ae4155e3a53b282d248dd3328278d852ace1e28
4
- data.tar.gz: a34baa8a40b0604775ce5db1f35b5c49b796c464
3
+ metadata.gz: 10f6c5f47c07161c3e2d610f4094fdfb648cdfbf
4
+ data.tar.gz: 95fe4c99fc97171eabe7abbbeeff5a15de41f8bd
5
5
  SHA512:
6
- metadata.gz: eb8a312ebbc9fec0ea66c14d5c0e17f21a176c0eda0e7f7369d2d055939043a859acfc742b610504235887b244a6f0d9f18b7b806e358b44b66ed3db554acb52
7
- data.tar.gz: 55a50c5f168ef4fec9015f8112d88acbffc9d45174f7311e9c63e25b97613bfc2a0361fb598f8be45a2eef62867f6bb50dc4bb7169175f2cb27857d22f684db2
6
+ metadata.gz: 3a00c88bf1314247263aa0bda2c615090ea4b61e117ce2c812bd349804b3ecb7674709c8878d893726b1997d3ebbe4981aab55f38c7843fd074d8cb093d1a7bf
7
+ data.tar.gz: a5c3def15d7c99d6a2fa1a6cb3bb760ba82dac27289bb7c4373bf36e719d441373166a1638c7317fae6beb27f6b779cc040dd9f63a1df5f3fb27dbc2793a9c4c
@@ -31,6 +31,10 @@ module ActionView
31
31
  /^([^-]+)-([^-]+)(\([\s\S]+\))$/
32
32
  end
33
33
 
34
+ def sfo_collection_ads_regexp
35
+ /_ids$/
36
+ end
37
+
34
38
  def sfo_form_attribute?(object)
35
39
  object.class.ancestors[1] == SlimFormObject::Base if object
36
40
  end
@@ -51,6 +55,10 @@ module ActionView
51
55
  tag_name.to_s[sfo_date_attr_regexp] ? true : false
52
56
  end
53
57
 
58
+ def sfo_collection_ads_attr?(tag_name)
59
+ tag_name.to_s[sfo_collection_ads_regexp] ? true : false
60
+ end
61
+
54
62
  def sfo_get_tag_name(object_name, method, multiple)
55
63
  model_name, attr_name = apply_expression_text(method, sfo_single_attr_regexp)
56
64
 
@@ -64,7 +72,7 @@ module ActionView
64
72
  end
65
73
 
66
74
  def sfo_get_method_name(method)
67
- if sfo_single_attr?(method)
75
+ if sfo_single_attr?(method) and !sfo_collection_ads_attr?(method)
68
76
  model_name, attr_name = apply_expression_text(method, sfo_single_attr_regexp)
69
77
  method = "#{model_name}_#{attr_name}"
70
78
  end
@@ -129,6 +137,7 @@ module ActionView
129
137
 
130
138
  def value(object)
131
139
  method_name = sfo_get_method_name(@method_name)
140
+ # byebug
132
141
  object.public_send method_name if object
133
142
  end
134
143
 
@@ -1,4 +1,6 @@
1
1
  module HelperMethods
2
+ private
3
+
2
4
  def snake(string)
3
5
  string = string.to_s
4
6
  string.gsub!(/((\w)([A-Z]))/,'\2_\3')
@@ -17,4 +19,26 @@ module HelperMethods
17
19
  def get_class_of_snake_model_name(snake_model_name)
18
20
  Object.const_get( snake_model_name.to_s.split('_').map(&:capitalize).join )
19
21
  end
22
+
23
+ def get_model_and_method_names(method)
24
+ if sfo_single_attr?(method)
25
+ apply_expression_text(method, sfo_single_attr_regexp)
26
+ end
27
+ end
28
+
29
+ def sfo_single_attr?(method)
30
+ method.to_s[sfo_single_attr_regexp] ? true : false
31
+ end
32
+
33
+ def sfo_single_attr_regexp
34
+ /^([^-]+)-([^-]+)$/
35
+ end
36
+
37
+ def apply_expression_text(string, exp)
38
+ string[exp]
39
+ model_name = $1
40
+ attr_name = $2
41
+
42
+ [model_name, attr_name]
43
+ end
20
44
  end
@@ -1,5 +1,3 @@
1
- require 'byebug'
2
-
3
1
  module SlimFormObject
4
2
  class Base
5
3
  include ActiveModel::Model
@@ -52,23 +50,20 @@ module SlimFormObject
52
50
  end
53
51
  end
54
52
 
53
+ def method_missing(name, *args, &block)
54
+ if name[/_ids$/]
55
+ model_name, attr_name = get_model_and_method_names(name)
56
+ return self.send(model_name.to_sym).send(attr_name.to_sym)
57
+ end
58
+ super(name, args, block)
59
+ end
60
+
55
61
  def initialize(params: {})
56
62
  self.params = params
57
63
  get_or_add_default_objects
58
64
  end
59
-
60
- def get_or_add_default_objects
61
- array_of_all_models.map do |model|
62
- if get_self_object(model) == nil
63
- method( "#{snake(model.to_s)}=" ).call(model.new)
64
- else
65
- get_self_object(model)
66
- end
67
- end
68
- end
69
65
  # INIT END
70
66
 
71
-
72
67
  def apply_parameters
73
68
  check_array_settings_with_settings
74
69
  apply
@@ -94,6 +89,16 @@ module SlimFormObject
94
89
  assign = Assign.new(self)
95
90
  @hash_objects_for_save = assign.apply_parameters
96
91
  end
92
+
93
+ def get_or_add_default_objects
94
+ array_of_all_models.map do |model|
95
+ if get_self_object(model) == nil
96
+ method( "#{snake(model.to_s)}=" ).call(model.new)
97
+ else
98
+ get_self_object(model)
99
+ end
100
+ end
101
+ end
97
102
 
98
103
  def check_array_settings_with_settings
99
104
  define_singleton_method(:not_save_this_model) { [] } unless respond_to?(:not_save_this_model)
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "1.0.5"
2
+ VERSION = "1.0.6"
3
3
  end
data/spec/db/database.yml CHANGED
@@ -3,7 +3,7 @@ test:
3
3
  encoding: unicode
4
4
  host: localhost
5
5
  port: 5432
6
- database: test_sof
6
+ database: test_sfo
7
7
  pool: 5
8
8
  username: rails_app
9
9
  password: password1
@@ -1,17 +1,17 @@
1
1
  require 'spec_helper'
2
2
  require 'helpers/models'
3
3
 
4
- class TestModule
5
- include ActiveModel::Model
6
- include SlimFormObject
4
+ class TestModule < SlimFormObject::Base
5
+ init_models TestOneModel, TestTwoModel, TestThreeModel, TestFourModel
7
6
  end
8
7
 
9
- describe TestModule do
8
+ describe TestModule, enviroment: :test do
10
9
 
11
10
  let!(:object) { TestModule.new }
11
+ let!(:saver) { SlimFormObject::Saver.new(object) }
12
12
 
13
13
  it { expect(TestModule).to include(ActiveModel::Model) }
14
- it { expect(TestModule).to include(SlimFormObject) }
14
+ it { expect(TestModule).to be < SlimFormObject::Base }
15
15
 
16
16
  it 'has a version number' do
17
17
  expect(SlimFormObject::VERSION).not_to be nil
@@ -19,87 +19,88 @@ describe TestModule do
19
19
 
20
20
  context 'get_association' do
21
21
  it 'model1 has_one model2' do
22
- association = object.send :get_association, TestOneModel, TestTwoModel
22
+ association = saver.send :get_association, TestOneModel, TestTwoModel
23
23
  expect(association).to eq(:has_one)
24
24
  end
25
25
 
26
26
  it 'model2 belongs_to model1' do
27
- association = object.send :get_association, TestTwoModel, TestOneModel
27
+ association = saver.send :get_association, TestTwoModel, TestOneModel
28
28
  expect(association).to eq(:belongs_to)
29
29
  end
30
30
 
31
31
  it 'model1 has_many model3' do
32
- association = object.send :get_association, TestOneModel, TestThreeModel
32
+ association = saver.send :get_association, TestOneModel, TestThreeModel
33
33
  expect(association).to eq(:has_many)
34
34
  end
35
35
 
36
36
  it 'model3 belongs_to model1' do
37
- association = object.send :get_association, TestThreeModel, TestOneModel
37
+ association = saver.send :get_association, TestThreeModel, TestOneModel
38
38
  expect(association).to eq(:belongs_to)
39
39
  end
40
40
 
41
41
  it 'model1 has_many :test_four_models, through: :test_one_four_models' do
42
- association = object.send :get_association, TestOneModel, TestFourModel
42
+ association = saver.send :get_association, TestOneModel, TestFourModel
43
43
  expect(association).to eq(:has_many)
44
44
  end
45
45
 
46
46
  it 'model2 has_and_belongs_to_many model3' do
47
- association = object.send :get_association, TestTwoModel, TestThreeModel
47
+ association = saver.send :get_association, TestTwoModel, TestThreeModel
48
48
  expect(association).to eq(:has_and_belongs_to_many)
49
49
  end
50
50
 
51
51
  it 'model3 has_and_belongs_to_many model2' do
52
- association = object.send :get_association, TestThreeModel, TestTwoModel
52
+ association = saver.send :get_association, TestThreeModel, TestTwoModel
53
53
  expect(association).to eq(:has_and_belongs_to_many)
54
54
  end
55
55
 
56
56
  it 'model3 has_and_belongs_to_many model2' do
57
- association = object.send :get_association, TestThreeModel, TestTwoModel
57
+ association = saver.send :get_association, TestThreeModel, TestTwoModel
58
58
  expect(association).to eq(:has_and_belongs_to_many)
59
59
  end
60
60
 
61
61
  it 'model4 has_many :test_one_models, through: :test_one_four_models' do
62
- association = object.send :get_association, TestFourModel, TestOneModel
62
+ association = saver.send :get_association, TestFourModel, TestOneModel
63
63
  expect(association).to eq(:has_many)
64
64
  end
65
65
 
66
66
  it 'model1-4 belongs_to model1' do
67
- association = object.send :get_association, TestOneFourModel, TestOneModel
67
+ association = saver.send :get_association, TestOneFourModel, TestOneModel
68
68
  expect(association).to eq(:belongs_to)
69
69
  end
70
70
 
71
71
  it 'model1-4 belongs_to model4' do
72
- association = object.send :get_association, TestOneFourModel, TestFourModel
72
+ association = saver.send :get_association, TestOneFourModel, TestFourModel
73
73
  expect(association).to eq(:belongs_to)
74
74
  end
75
75
 
76
76
  it 'model1 has_many model1-4' do
77
- association = object.send :get_association, TestOneModel, TestOneFourModel
77
+ association = saver.send :get_association, TestOneModel, TestOneFourModel
78
78
  expect(association).to eq(:has_many)
79
79
  end
80
80
 
81
81
  it 'model4 has_many model1-4' do
82
- association = object.send :get_association, TestFourModel, TestOneFourModel
82
+ association = saver.send :get_association, TestFourModel, TestOneFourModel
83
83
  expect(association).to eq(:has_many)
84
84
  end
85
85
 
86
86
  it 'model2 don\'t have model4' do
87
- association = object.send :get_association, TestTwoModel, TestFourModel
87
+ association = saver.send :get_association, TestTwoModel, TestFourModel
88
88
  expect(association).to eq(nil)
89
89
  end
90
90
 
91
91
  end
92
92
 
93
93
  context 'init_models' do
94
- it 'init variable @models' do
95
- object.class.stub(:add_attributes).and_return(true)
96
- object.class.init_models(TestOneModel, TestTwoModel)
94
+ class Test < SlimFormObject::Base; end
95
+ Test.init_models(TestOneModel, TestTwoModel)
96
+ object = Test.new
97
97
 
98
- expect( object.array_of_models ).to eq([TestOneModel, TestTwoModel])
98
+ it 'init variable @models' do
99
+ expect( object.array_of_all_models ).to eq([TestOneModel, TestTwoModel])
99
100
  end
100
101
 
101
102
  it 'must be called add_attributes' do
102
- expect(object.class).to receive(:add_attributes).and_return(true)
103
+ expect(object.class).to receive(:define_array_of_models).and_return(true)
103
104
  object.class.init_models(TestOneModel, TestTwoModel)
104
105
  end
105
106
  end
@@ -113,8 +114,12 @@ describe TestModule do
113
114
  end
114
115
 
115
116
  context 'add_attributes' do
117
+ class Test2 < SlimFormObject::Base; end
118
+ Test2.init_models()
119
+ object = Test2.new
120
+
116
121
  it 'attributes do not exist' do
117
- expect(object.respond_to? :params).to eq(false)
122
+ expect(object.respond_to? :params).to eq(true)
118
123
  expect(object.respond_to? :test_one_model_title).to eq(false)
119
124
  expect(object.respond_to? :test_two_model_title).to eq(false)
120
125
  expect(object.respond_to? :test_three_model_title).to eq(false)
@@ -122,7 +127,7 @@ describe TestModule do
122
127
  end
123
128
 
124
129
  it 'attributes exist' do
125
- object.class.add_attributes([TestOneModel, TestTwoModel])
130
+ object.class.init_models(TestOneModel, TestTwoModel)
126
131
 
127
132
  expect(object.respond_to? :params).to eq(true)
128
133
  expect(object.respond_to? :test_one_model_title).to eq(true)
@@ -136,13 +141,14 @@ describe TestModule do
136
141
  context 'set_errors' do
137
142
  before :each do
138
143
  @test_object = TestModule.new
144
+ @validator = SlimFormObject::Validator.new(@test_object)
139
145
  end
140
146
 
141
147
  it 'errors is present' do
142
148
  error = {title: "can't be blank"}
143
- @test_object.send :set_errors, error
149
+ @validator.send :set_errors, 'object_name', error
144
150
 
145
- expect( @test_object.errors.messages ).to eq( {title: ["can't be blank"]} )
151
+ expect( @test_object.errors.messages ).to eq( {object_name: [{title: "can't be blank"}]} )
146
152
  end
147
153
 
148
154
  it 'errors is not exist' do
@@ -150,129 +156,35 @@ describe TestModule do
150
156
  end
151
157
  end
152
158
 
153
- context 'validation_models' do
154
- before do
155
- object.instance_eval{ @array_of_models = [TestOneModel] }
156
- end
157
-
158
- it 'errors is present' do
159
- object.stub(:test_one_model).and_return( TestOneModel.new(descr: 'desc') )
160
- expect(object).to receive(:set_errors).and_return( true )
161
- object.send :validation_models
162
- end
163
-
164
- it 'errors is not exist' do
165
- object.stub(:test_one_model).and_return( TestOneModel.new(title: 'title', descr: 'desc') )
166
- expect(object).not_to receive(:set_errors)
167
- object.send :validation_models
168
- end
169
- end
170
-
171
159
  context 'update_attributes' do
172
- it 'must call this methods' do
173
- attributes_of_model = ["test_one_model_id", "test_one_model_title", "test_one_model_descr"]
174
- attributes_for_update = {"title"=>"Test Title", "descr"=>"Test Descr"}
175
-
176
- object.instance_eval{ @array_of_models = [TestOneModel] }
177
- expect(object).to receive(:make_attributes_of_model).and_return( attributes_of_model )
178
- expect(object).to receive(:get_attributes_for_update).and_return( attributes_for_update )
179
- object.stub(:test_one_model).and_return( TestOneModel.new )
180
- object.send :update_attributes
181
- end
160
+ # it 'must call this methods' do
161
+ # attributes_of_model = ["test_one_model_id", "test_one_model_title", "test_one_model_descr"]
162
+ # attributes_for_update = {"title"=>"Test Title", "descr"=>"Test Descr"}
163
+ #
164
+ # object.instance_eval{ @array_of_models = [TestOneModel] }
165
+ # expect(object).to receive(:make_attributes_of_model).and_return( attributes_of_model )
166
+ # expect(object).to receive(:get_attributes_for_update).and_return( attributes_for_update )
167
+ # object.stub(:test_one_model).and_return( TestOneModel.new )
168
+ # object.send :update_attributes
169
+ # end
182
170
  end
183
171
 
184
172
  context 'get_attributes_for_update' do
185
- it 'make attributes for update model' do
186
- object.stub(:params).and_return( {'test_one_model_title'=>'Test Title', 'test_one_model_descr'=>'Test Descr'} )
187
- model_attributes = ["test_one_model_id", "test_one_model_title", "test_one_model_descr"]
188
- update_attributes = object.send :get_attributes_for_update, model_attributes, TestOneModel
189
-
190
- expect(update_attributes).to eq( {"title"=>"Test Title", "descr"=>"Test Descr"} )
191
- end
173
+ # it 'make attributes for update model' do
174
+ # object.stub(:params).and_return( {'test_one_model_title'=>'Test Title', 'test_one_model_descr'=>'Test Descr'} )
175
+ # model_attributes = ["test_one_model_id", "test_one_model_title", "test_one_model_descr"]
176
+ # update_attributes = object.send :get_attributes_for_update, model_attributes, TestOneModel
177
+ #
178
+ # expect(update_attributes).to eq( {"title"=>"Test Title", "descr"=>"Test Descr"} )
179
+ # end
192
180
  end
193
181
 
194
182
  context 'make_attributes_of_model' do
195
- it 'make attributes of model' do
196
- update_attributes = object.send :make_attributes_of_model, TestOneModel
197
-
198
- expect(update_attributes).to eq( ["test_one_model_id", "test_one_model_title", "test_one_model_descr"] )
199
- end
200
- end
201
-
202
- context 'assign_attributes_for_collection' do
203
- before :each do
204
- @test_object = TestOneModel.create(title:'title', descr:'descr')
205
- (1...4).each { TestFourModel.create(title:'title', descr:'descr') }
206
- end
207
-
208
- it 'assign attributes for test_one_model' do
209
- object.stub(:params).and_return( {test_one_model_test_four_model_ids: [1, 2, 3]} )
210
- object.stub_chain(:method, :call) { @test_object }
211
- object.stub(:keys_of_collections).and_return( ['test_four_model_ids'] )
212
- object.send(:assign_attributes_for_collection, TestOneModel)
213
-
214
- expect(@test_object.test_four_model_ids).to eq( [1, 2, 3] )
215
- end
216
-
217
- it 'must don\'t assign attributes for test_one_model' do
218
- 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]} )
219
- object.stub_chain(:method, :call) { @test_object }
220
- object.stub(:keys_of_collections).and_return( [:test_ids, :_ids, :ids_test, :test_one_model_test_four_model_idss, :test_ids_one] )
221
- object.send(:assign_attributes_for_collection, TestOneModel)
222
-
223
- expect(@test_object.test_four_model_ids).to eq( [] )
224
- end
225
- end
226
-
227
- context 'keys_of_collections' do
228
- it 'must be return array with values' do
229
- object.instance_eval{ @array_of_models = [TestOneModel] }
230
- object.stub(:params).and_return( {test_one_model_test_four_model_ids: [1, 2, 3]} )
231
- object.stub_chain(:method, :call) { TestOneModel.create(title:'title', descr:'descr') }
232
-
233
- expect(object.send :keys_of_collections).to eq( ['test_four_model_ids'] )
234
- end
235
-
236
- it 'must be return array without values' do
237
- object.stub(:params).and_return( {_ids: [1], ids_test: [1], test_one_model_test_four_model_idss: [1], test_ids_one: [1]} )
238
-
239
- expect(object.send :keys_of_collections).to eq( [] )
240
- end
241
- end
242
-
243
- context 'exist_any_errors_without_collections?' do
244
- before :each do
245
- object.instance_eval do
246
- def test_one_model
247
- TestOneModel.create(title:'title', descr:'descr')
248
- end
249
- def test_four_model
250
- TestFourModel.create(title:'title', descr:'descr')
251
- end
252
- @array_of_models = [TestOneModel, TestFourModel]
253
- end
254
- end
255
-
256
- it 'must be return true' do
257
- object.stub(:keys_of_collections).and_return( ['test_one_model_ids', 'test_four_model_ids'] )
258
- object.stub(:valid?).and_return( false )
259
- object.stub_chain(:errors, :messages).and_return( {:test_one_models=>'error', :test_four_models=>'error'} )
260
-
261
- expect(object.send :exist_any_errors_without_collections?).to eq( true )
262
- end
263
-
264
- it 'must be return false' do
265
- object.instance_eval do
266
- def test_one_model
267
- TestThreeModel.create(title:'title', descr:'')
268
- end
269
- end
270
- object.stub(:keys_of_collections).and_return( ['test_one_model_ids', 'test_four_model_ids'] )
271
- object.stub(:valid?).and_return( false )
272
- object.stub_chain(:errors, :messages).and_return( {:test_one_models=>'error', :test_four_models=>'error', :descr=>'error'} )
273
-
274
- expect(object.send :exist_any_errors_without_collections?).to eq( false )
275
- end
183
+ # it 'make attributes of model' do
184
+ # update_attributes = object.send :make_attributes_of_model, TestOneModel
185
+ #
186
+ # expect(update_attributes).to eq( ["test_one_model_id", "test_one_model_title", "test_one_model_descr"] )
187
+ # end
276
188
  end
277
189
 
278
190
  end
data/spec/spec_helper.rb CHANGED
@@ -1,20 +1,22 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'slim_form_object'
3
2
  require 'bundler/setup'
3
+ require 'byebug'
4
+ require 'yaml'
4
5
  require 'active_record'
5
- require 'rspec/collection_matchers'
6
6
  require 'database_cleaner'
7
+ require 'rspec/collection_matchers'
7
8
  require 'helpers/connect_to_base'
8
- require 'byebug'
9
+ require 'slim_form_object'
9
10
 
10
11
  RSpec.configure do |config|
12
+ ActiveRecord::Base.establish_connection( dbconfig['test'] )
11
13
 
12
14
  config.mock_with :rspec do |c|
13
15
  c.syntax = [:should, :expect]
14
16
  end
15
17
 
16
18
  config.before(:suite) do
17
- ActiveRecord::Base.establish_connection dbconfig['test']
19
+ # ActiveRecord::Base.establish_connection( dbconfig['test'] )
18
20
  DatabaseCleaner.strategy = :transaction
19
21
  DatabaseCleaner.clean_with(:truncation)
20
22
  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: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-22 00:00:00.000000000 Z
11
+ date: 2017-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel