slim_form_object 0.3.1 → 0.4.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: 6019f9dbb3ed83560faee79ecd06b76a7474c26f
4
- data.tar.gz: 0a007f576b563a08a6dff13fbcfa23dde13e8dc2
3
+ metadata.gz: 12cea860eec0074e7737bb46424261f647672057
4
+ data.tar.gz: 6af0624727291ad8f5f80379ba864e7da3d77f2e
5
5
  SHA512:
6
- metadata.gz: 13f9a38fd56a7b1b377df6d1baa3338bc0908057528101d1f08faa5e3f9044c1159f7317ba1d0327df29b330de9316dc69d6c8eba8de3ac7ad719f2ec39a0da7
7
- data.tar.gz: a68b19d0fc7bc3f03d3d7f02679731f066c0927b7962f6f36786199b5d8a3d4eebcb1fd25d9c9b681164ac5f1c8ba64b0bfff1c0b47926ddc137ee6e8c9213d5
6
+ metadata.gz: 0275aaf993739ab2263a636a3b4dabf788206b6c719c4f5114cbb68cb56f9bdd6d072e20f7852be42157a8fcfcd373c726d3bf6a01bd306927cd958c98580176
7
+ data.tar.gz: 01e985532ef0b11295528582e75fd37cd01af7b662f8801f6b85f0288c21a97aa7a84a22438f7a9e91529e0770cd92b3ba3159bc2f57ba76e0683a7bf5f78f4e
@@ -1,3 +1,3 @@
1
1
  module SlimFormObject
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -8,21 +8,23 @@ module SlimFormObject
8
8
 
9
9
  module ClassMethods
10
10
  def init_models(*args)
11
- @models = args
12
- add_attributes
11
+ self.instance_eval do
12
+ define_method(:array_of_models) { args }
13
+ end
14
+ add_attributes(args)
13
15
  end
14
16
 
15
17
  def snake(string)
16
18
  string.gsub(/((\w)([A-Z]))/,'\2_\3').downcase
17
19
  end
18
20
 
19
- def add_attributes
21
+ def add_attributes(models)
20
22
  #attr_accessor for models and env params
21
23
  attr_accessor :params
22
- @models.each{ |model| attr_accessor snake(model.to_s).to_sym }
24
+ models.each{ |model| attr_accessor snake(model.to_s).to_sym }
23
25
 
24
26
  #delegate attributes of models
25
- @models.each do |model|
27
+ models.each do |model|
26
28
  model.column_names.each do |attr|
27
29
  delegate attr.to_sym, "#{attr}=".to_sym,
28
30
  to: snake(model.to_s).to_sym,
@@ -40,7 +42,7 @@ module SlimFormObject
40
42
  if valid?
41
43
  models = get_model_for_save
42
44
  while model1 = models.delete( models[0] )
43
- get_models.each{ |model2| save_models(model1, model2) }
45
+ array_of_models.each{ |model2| save_models(model1, model2) }
44
46
  end
45
47
 
46
48
  return true
@@ -50,10 +52,6 @@ module SlimFormObject
50
52
 
51
53
  private
52
54
 
53
- def get_models
54
- self.class.instance_variable_get(:@models)
55
- end
56
-
57
55
  def save_models(model1, model2)
58
56
  self_model1 = method( self.class.snake(model1.to_s) ).call
59
57
  self_model2 = method( self.class.snake(model2.to_s) ).call
@@ -87,22 +85,31 @@ module SlimFormObject
87
85
  end
88
86
 
89
87
  def update_attributes
90
- get_models.each do |model|
91
- model_attributes = []
92
- model.column_names.each do |name|
93
- model_attributes << "#{self.class.snake(model.to_s)}_#{name}"
94
- end
95
- update_attributes = {}
96
- hash_attributes = params.slice(*model_attributes)
97
- hash_attributes.each{ |attr, val| update_attributes[attr.gsub(/#{self.class.snake(model.to_s)}_(.*)/, '\1')] = val }
98
- method( self.class.snake(model.to_s) ).call.assign_attributes(update_attributes)
88
+ array_of_models.each do |model|
89
+ model_attributes = make_attributes_of_model(model)
90
+ method( self.class.snake(model.to_s) ).call.assign_attributes( get_attributes_for_update(model_attributes, model) )
99
91
  end
100
92
  end
101
93
 
94
+ def make_attributes_of_model(model)
95
+ model_attributes = []
96
+ model.column_names.each do |name|
97
+ model_attributes << "#{self.class.snake(model.to_s)}_#{name}"
98
+ end
99
+ model_attributes
100
+ end
101
+
102
+ def get_attributes_for_update(model_attributes, model)
103
+ update_attributes = {}
104
+ hash_attributes = params.slice(*model_attributes)
105
+ hash_attributes.each{ |attr, val| update_attributes[attr.gsub(/#{self.class.snake(model.to_s)}_(.*)/, '\1')] = val }
106
+ update_attributes
107
+ end
108
+
102
109
  def get_model_for_save
103
110
  keys = params.keys
104
111
  models = []
105
- get_models.each do |model|
112
+ array_of_models.each do |model|
106
113
  model.column_names.each do |name|
107
114
  keys.each do |key|
108
115
  models << model if key.to_s == "#{self.class.snake(model.to_s)}_#{name}"
@@ -0,0 +1,13 @@
1
+
2
+ class CreateModelOne < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :test_one_models do |t|
5
+ t.string :title
6
+ t.string :descr
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :test_one_model
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+
2
+ class CreateModelTwo < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :test_two_models do |t|
5
+ t.string :title
6
+ t.string :descr
7
+ t.integer :test_one_model_id
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :test_two_model
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+
2
+ class CreateModelThree < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :test_three_models do |t|
5
+ t.string :title
6
+ t.string :descr
7
+ t.integer :test_one_model_id
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :test_three_model
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+
2
+ class CreateModelFour < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :test_four_models do |t|
5
+ t.string :title
6
+ t.string :descr
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :test_four_model
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+
2
+ class CreateModelOneFour < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :test_one_four_models do |t|
5
+ t.integer :test_one_model_id
6
+ t.integer :test_four_model_id
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :test_one_four_model
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+
2
+ class CreateModelTwoThree < ActiveRecord::Migration
3
+ def self.up
4
+ create_table :test_three_model_test_two_models do |t|
5
+ t.integer :test_three_model_id
6
+ t.integer :test_two_model_id
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :test_three_model_test_two_model
12
+ end
13
+ end
@@ -3,21 +3,29 @@ class TestOneModel < ActiveRecord::Base
3
3
  has_many :test_three_models
4
4
  has_many :test_one_four_models
5
5
  has_many :test_four_models, through: :test_one_four_models
6
+
7
+ validates :title, :descr, presence: true
6
8
  end
7
9
 
8
10
  class TestTwoModel < ActiveRecord::Base
9
11
  belongs_to :test_one_model
10
12
  has_and_belongs_to_many :test_three_models
13
+
14
+ validates :title, :descr, presence: true
11
15
  end
12
16
 
13
17
  class TestThreeModel < ActiveRecord::Base
14
18
  belongs_to :test_one_model
15
19
  has_and_belongs_to_many :test_two_models
20
+
21
+ validates :title, :descr, presence: true
16
22
  end
17
23
 
18
24
  class TestFourModel < ActiveRecord::Base
19
25
  has_many :test_one_four_models
20
26
  has_many :test_one_models, through: :test_one_four_models
27
+
28
+ validates :title, :descr, presence: true
21
29
  end
22
30
 
23
31
  class TestOneFourModel < ActiveRecord::Base
@@ -1,12 +1,18 @@
1
1
  require 'spec_helper'
2
2
  require 'helpers/models'
3
+ require 'byebug'
3
4
 
4
5
  class TestModule
6
+ include ActiveModel::Model
7
+ include SlimFormObject
5
8
  end
6
9
 
7
10
  describe TestModule do
8
11
 
9
- include SlimFormObject
12
+ let!(:object) { TestModule.new }
13
+
14
+ it { expect(TestModule).to include(ActiveModel::Model) }
15
+ it { expect(TestModule).to include(SlimFormObject) }
10
16
 
11
17
  it 'has a version number' do
12
18
  expect(SlimFormObject::VERSION).not_to be nil
@@ -14,79 +20,208 @@ describe TestModule do
14
20
 
15
21
  context 'get_association' do
16
22
  it 'model1 has_one model2' do
17
- association = send :get_association, TestOneModel, TestTwoModel
23
+ association = object.send :get_association, TestOneModel, TestTwoModel
18
24
  expect(association).to eq(:has_one)
19
25
  end
20
26
 
21
27
  it 'model2 belongs_to model1' do
22
- association = send :get_association, TestTwoModel, TestOneModel
28
+ association = object.send :get_association, TestTwoModel, TestOneModel
23
29
  expect(association).to eq(:belongs_to)
24
30
  end
25
31
 
26
32
  it 'model1 has_many model3' do
27
- association = send :get_association, TestOneModel, TestThreeModel
33
+ association = object.send :get_association, TestOneModel, TestThreeModel
28
34
  expect(association).to eq(:has_many)
29
35
  end
30
36
 
31
37
  it 'model3 belongs_to model1' do
32
- association = send :get_association, TestThreeModel, TestOneModel
38
+ association = object.send :get_association, TestThreeModel, TestOneModel
33
39
  expect(association).to eq(:belongs_to)
34
40
  end
35
41
 
36
42
  it 'model1 has_many :test_four_models, through: :test_one_four_models' do
37
- association = send :get_association, TestOneModel, TestFourModel
43
+ association = object.send :get_association, TestOneModel, TestFourModel
38
44
  expect(association).to eq(:has_many)
39
45
  end
40
46
 
41
47
  it 'model2 has_and_belongs_to_many model3' do
42
- association = send :get_association, TestTwoModel, TestThreeModel
48
+ association = object.send :get_association, TestTwoModel, TestThreeModel
43
49
  expect(association).to eq(:has_and_belongs_to_many)
44
50
  end
45
51
 
46
52
  it 'model3 has_and_belongs_to_many model2' do
47
- association = send :get_association, TestThreeModel, TestTwoModel
53
+ association = object.send :get_association, TestThreeModel, TestTwoModel
48
54
  expect(association).to eq(:has_and_belongs_to_many)
49
55
  end
50
56
 
51
57
  it 'model3 has_and_belongs_to_many model2' do
52
- association = send :get_association, TestThreeModel, TestTwoModel
58
+ association = object.send :get_association, TestThreeModel, TestTwoModel
53
59
  expect(association).to eq(:has_and_belongs_to_many)
54
60
  end
55
61
 
56
62
  it 'model4 has_many :test_one_models, through: :test_one_four_models' do
57
- association = send :get_association, TestFourModel, TestOneModel
63
+ association = object.send :get_association, TestFourModel, TestOneModel
58
64
  expect(association).to eq(:has_many)
59
65
  end
60
66
 
61
67
  it 'model1-4 belongs_to model1' do
62
- association = send :get_association, TestOneFourModel, TestOneModel
68
+ association = object.send :get_association, TestOneFourModel, TestOneModel
63
69
  expect(association).to eq(:belongs_to)
64
70
  end
65
71
 
66
72
  it 'model1-4 belongs_to model4' do
67
- association = send :get_association, TestOneFourModel, TestFourModel
73
+ association = object.send :get_association, TestOneFourModel, TestFourModel
68
74
  expect(association).to eq(:belongs_to)
69
75
  end
70
76
 
71
77
  it 'model1 has_many model1-4' do
72
- association = send :get_association, TestOneModel, TestOneFourModel
78
+ association = object.send :get_association, TestOneModel, TestOneFourModel
73
79
  expect(association).to eq(:has_many)
74
80
  end
75
81
 
76
82
  it 'model4 has_many model1-4' do
77
- association = send :get_association, TestFourModel, TestOneFourModel
83
+ association = object.send :get_association, TestFourModel, TestOneFourModel
78
84
  expect(association).to eq(:has_many)
79
85
  end
80
86
 
81
87
  it 'model2 don\'t have model4' do
82
- association = send :get_association, TestTwoModel, TestFourModel
88
+ association = object.send :get_association, TestTwoModel, TestFourModel
83
89
  expect(association).to eq(nil)
84
90
  end
85
91
 
86
92
  end
87
93
 
88
- it 'a' do
89
- # expect(TestOneModel).to eq(true)
94
+ context 'init_models' do
95
+ it 'init variable @models' do
96
+ object.class.stub(:add_attributes).and_return(true)
97
+ object.class.init_models(TestOneModel, TestTwoModel)
98
+
99
+ expect( object.array_of_models ).to eq([TestOneModel, TestTwoModel])
100
+ end
101
+
102
+ it 'must be called add_attributes' do
103
+ expect(object.class).to receive(:add_attributes).and_return(true)
104
+ object.class.init_models(TestOneModel, TestTwoModel)
105
+ end
106
+ end
107
+
108
+ context 'snake' do
109
+ it { expect( object.class.snake(TestOneModel.to_s) ).to eq('test_one_model') }
110
+ it { expect( object.class.snake('123_Asd') ).to eq('123__asd') }
111
+ it { expect( object.class.snake('_zA') ).to eq('_z_a') }
112
+ it { expect( object.class.snake('_zA12') ).to eq('_z_a12') }
113
+ it { expect( object.class.snake('_zA-12_') ).to eq('_z_a-12_') }
114
+ end
115
+
116
+ context 'add_attributes' do
117
+ it 'attributes do not exist' do
118
+ expect(object.respond_to? :params).to eq(false)
119
+ expect(object.respond_to? :test_one_model_title).to eq(false)
120
+ expect(object.respond_to? :test_two_model_title).to eq(false)
121
+ expect(object.respond_to? :test_three_model_title).to eq(false)
122
+ expect(object.respond_to? :test_four_model_title).to eq(false)
123
+ end
124
+
125
+ it 'attributes exist' do
126
+ object.class.add_attributes([TestOneModel, TestTwoModel])
127
+
128
+ expect(object.respond_to? :params).to eq(true)
129
+ expect(object.respond_to? :test_one_model_title).to eq(true)
130
+ expect(object.respond_to? :test_one_model_descr).to eq(true)
131
+ expect(object.respond_to? :test_two_model_title).to eq(true)
132
+ expect(object.respond_to? :test_two_model_descr).to eq(true)
133
+ expect(object.respond_to? :test_two_model_test_one_model_id).to eq(true)
134
+ end
135
+ end
136
+
137
+ context 'set_errors' do
138
+ before :each do
139
+ @test_object = TestModule.new
140
+ end
141
+
142
+ it 'errors is present' do
143
+ error = {title: "can't be blank"}
144
+ @test_object.send :set_errors, error
145
+
146
+ expect( @test_object.errors.messages ).to eq( {title: ["can't be blank"]} )
147
+ end
148
+
149
+ it 'errors is not exist' do
150
+ expect( @test_object.errors.messages ).to eq( {} )
151
+ end
152
+ end
153
+
154
+ context 'validation_models' do
155
+ it 'errors is present' do
156
+ object.stub(:test_one_model).and_return( TestOneModel.new(descr: 'desc') )
157
+
158
+ expect(object).to receive(:get_model_for_save).and_return([TestOneModel])
159
+ expect(object).to receive(:set_errors).and_return( true )
160
+ object.send :validation_models
161
+ end
162
+
163
+ it 'errors is not exist' do
164
+ object.stub(:test_one_model).and_return( TestOneModel.new(title: 'title', descr: 'desc') )
165
+
166
+ expect(object).to receive(:get_model_for_save).and_return([TestOneModel])
167
+ expect(object).not_to receive(:set_errors)
168
+ object.send :validation_models
169
+ end
170
+ end
171
+
172
+ context 'update_attributes' do
173
+ it 'must call this methods' do
174
+ attributes_of_model = ["test_one_model_id", "test_one_model_title", "test_one_model_descr"]
175
+ attributes_for_update = {"title"=>"Test Title", "descr"=>"Test Descr"}
176
+
177
+ expect(object).to receive(:array_of_models).and_return( [TestOneModel] )
178
+ expect(object).to receive(:make_attributes_of_model).and_return( attributes_of_model )
179
+ expect(object).to receive(:get_attributes_for_update).and_return( attributes_for_update )
180
+ object.stub(:test_one_model).and_return( TestOneModel.new )
181
+ object.send :update_attributes
182
+ end
183
+ end
184
+
185
+ context 'get_attributes_for_update' do
186
+ it 'make attributes for update model' do
187
+ object.stub(:params).and_return( {'test_one_model_title'=>'Test Title', 'test_one_model_descr'=>'Test Descr'} )
188
+ model_attributes = ["test_one_model_id", "test_one_model_title", "test_one_model_descr"]
189
+ update_attributes = object.send :get_attributes_for_update, model_attributes, TestOneModel
190
+
191
+ expect(update_attributes).to eq( {"title"=>"Test Title", "descr"=>"Test Descr"} )
192
+ end
193
+ end
194
+
195
+ context 'make_attributes_of_model' do
196
+ it 'make attributes of model' do
197
+ update_attributes = object.send :make_attributes_of_model, TestOneModel
198
+
199
+ expect(update_attributes).to eq( ["test_one_model_id", "test_one_model_title", "test_one_model_descr"] )
200
+ end
201
+ end
202
+
203
+ context 'get_model_for_save' do
204
+ it 'get models from params (exist)' do
205
+ object.stub(:array_of_models).and_return( [TestOneModel, TestTwoModel, TestThreeModel] )
206
+ object.stub(:params).and_return( {'test_one_model_title'=>'Test Title', 'test_two_model_descr'=>'Test Descr'} )
207
+
208
+ expect(object.send :get_model_for_save).to eq( [TestOneModel, TestTwoModel] )
209
+ end
210
+
211
+ it 'get models from params (must be return empty array)' do
212
+ object.stub(:array_of_models).and_return( [TestOneModel, TestTwoModel, TestThreeModel] )
213
+ object.stub(:params).and_return( {} )
214
+
215
+ expect(object.send :get_model_for_save).to eq( [] )
216
+ end
90
217
  end
91
218
 
92
219
  end
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,7 @@ require 'slim_form_object'
3
3
  require 'bundler/setup'
4
4
  require 'active_record'
5
5
  require 'rspec/collection_matchers'
6
+ require 'database_cleaner'
6
7
  require 'helpers/connect_to_base'
7
8
 
8
9
  RSpec.configure do |config|
@@ -11,4 +12,10 @@ RSpec.configure do |config|
11
12
  c.syntax = [:should, :expect]
12
13
  end
13
14
 
15
+ config.before(:suite) do
16
+ ActiveRecord::Base.establish_connection dbconfig['test']
17
+ DatabaseCleaner.strategy = :transaction
18
+ DatabaseCleaner.clean_with(:truncation)
19
+ end
20
+
14
21
  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: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-29 00:00:00.000000000 Z
11
+ date: 2016-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: activerecord
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,20 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: 0.18.4
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  description: It's works
98
126
  email:
99
127
  - roboucrop@gmail.com
@@ -107,6 +135,12 @@ files:
107
135
  - lib/slim_form_object/version.rb
108
136
  - spec/db/database.yml
109
137
  - spec/db/database.yml.travis
138
+ - spec/db/migrate/001_create_model_one.rb
139
+ - spec/db/migrate/002_create_model_two.rb
140
+ - spec/db/migrate/003_create_model_three.rb
141
+ - spec/db/migrate/004_create_model_four.rb
142
+ - spec/db/migrate/005_create_model_one_four.rb
143
+ - spec/db/migrate/006_create_model_two_three.rb
110
144
  - spec/helpers/connect_to_base.rb
111
145
  - spec/helpers/models.rb
112
146
  - spec/slim_form_object_spec.rb
@@ -138,6 +172,12 @@ summary: This is form object
138
172
  test_files:
139
173
  - spec/db/database.yml
140
174
  - spec/db/database.yml.travis
175
+ - spec/db/migrate/001_create_model_one.rb
176
+ - spec/db/migrate/002_create_model_two.rb
177
+ - spec/db/migrate/003_create_model_three.rb
178
+ - spec/db/migrate/004_create_model_four.rb
179
+ - spec/db/migrate/005_create_model_one_four.rb
180
+ - spec/db/migrate/006_create_model_two_three.rb
141
181
  - spec/helpers/connect_to_base.rb
142
182
  - spec/helpers/models.rb
143
183
  - spec/slim_form_object_spec.rb