slim_form_object 0.3.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0898d7922f9f9526969a136e2caab656ddf244c
4
+ data.tar.gz: a7cf53116e45fec970cb27645662043e6ae7b090
5
+ SHA512:
6
+ metadata.gz: fe29d133846bd9a47e78b5f8bd9b0ac8550f80e9f439066d0d1274595ace944227868943333844ba47caf15d309b6f5112dc437f3eefeed8e0aeb4bbc2eec03c
7
+ data.tar.gz: 6f11642c8d5caf9889e5c82112560e2abedce6ba6fe24e1f48d6adfa2141ee025398563783c6bb4c8aa64e8a078a41e6c0d9ecf909128c68ceac3b0f818f5a90
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/slim_form_object'
4
+
5
+ module GameCodebreaker
6
+
7
+ end
8
+
@@ -0,0 +1,3 @@
1
+ module SlimFormObject
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,120 @@
1
+ require "slim_form_object/version"
2
+
3
+ module SlimFormObject
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def init_models(*args)
11
+ @models = args
12
+ add_attributes
13
+ end
14
+
15
+ def snake(string)
16
+ string.gsub(/((\w)([A-Z]))/,'\2_\3').downcase
17
+ end
18
+
19
+ def add_attributes
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
+ end
34
+
35
+ def submit
36
+ update_attributes
37
+ end
38
+
39
+ def save
40
+ if valid?
41
+ models = get_model_for_save
42
+ while model1 = models.delete( models[0] )
43
+ get_models.each{ |model2| save_models(model1, model2) }
44
+ end
45
+
46
+ return true
47
+ end
48
+ false
49
+ end
50
+
51
+ private
52
+
53
+ def get_models
54
+ self.class.instance_variable_get(:@models)
55
+ end
56
+
57
+ def save_models(model1, model2)
58
+ self_model1 = method( self.class.snake(model1.to_s) ).call
59
+ self_model2 = method( self.class.snake(model2.to_s) ).call
60
+
61
+ case get_association(model1, model2)
62
+ when :belongs_to
63
+ self_model1.send( "#{self.class.snake(model2.to_s)}=", self_model2 )
64
+ self_model1.save
65
+ when :has_one
66
+ self_model1.send( "#{self.class.snake(model2.to_s)}=", self_model2 )
67
+ self_model1.save
68
+ when :has_many
69
+ self_model1.method("#{model2.table_name}").call << self_model2
70
+ self_model1.save
71
+ when :has_and_belongs_to_many
72
+ self_model1.method("#{model2.table_name}").call << self_model2
73
+ self_model1.save
74
+ end
75
+ end
76
+
77
+ def validation_models
78
+ get_model_for_save.each do |model|
79
+ set_errors( method(self.class.snake(model.to_s)).call.errors ) unless method( self.class.snake(model.to_s) ).call.valid?
80
+ end
81
+ end
82
+
83
+ def set_errors(model_errors)
84
+ model_errors.each do |attribute, message|
85
+ errors.add(attribute, message)
86
+ end
87
+ end
88
+
89
+ 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)
99
+ end
100
+ end
101
+
102
+ def get_model_for_save
103
+ keys = params.keys
104
+ models = []
105
+ get_models.each do |model|
106
+ model.column_names.each do |name|
107
+ keys.each do |key|
108
+ models << model if key.to_s == "#{self.class.snake(model.to_s)}_#{name}"
109
+ end
110
+ end
111
+ end
112
+ models.uniq!
113
+ models
114
+ end
115
+
116
+ def get_association(class1, class2)
117
+ class1.reflections.slice(self.class.snake(class2.to_s), class2.table_name).values.first.try(:macro)
118
+ end
119
+
120
+ end
@@ -0,0 +1,9 @@
1
+ test:
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ host: localhost
5
+ port: 5432
6
+ database: test_sof
7
+ pool: 5
8
+ username: rails_app
9
+ password: password1
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: postgresql
3
+ database: travis_ci_test
4
+ username: postgres
@@ -0,0 +1,3 @@
1
+ def dbconfig
2
+ YAML::load(File.open(File.join(File.dirname(__FILE__), '../db/database.yml')))
3
+ end
@@ -0,0 +1,26 @@
1
+ class TestOneModel < ActiveRecord::Base
2
+ has_one :test_two_model
3
+ has_many :test_three_models
4
+ has_many :test_one_four_models
5
+ has_many :test_four_models, through: :test_one_four_models
6
+ end
7
+
8
+ class TestTwoModel < ActiveRecord::Base
9
+ belongs_to :test_one_model
10
+ has_and_belongs_to_many :test_three_models
11
+ end
12
+
13
+ class TestThreeModel < ActiveRecord::Base
14
+ belongs_to :test_one_model
15
+ has_and_belongs_to_many :test_two_models
16
+ end
17
+
18
+ class TestFourModel < ActiveRecord::Base
19
+ has_many :test_one_four_models
20
+ has_many :test_one_models, through: :test_one_four_models
21
+ end
22
+
23
+ class TestOneFourModel < ActiveRecord::Base
24
+ belongs_to :test_one_model
25
+ belongs_to :test_four_model
26
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+ require 'helpers/models'
3
+
4
+ class TestModule
5
+ end
6
+
7
+ describe TestModule do
8
+
9
+ include SlimFormObject
10
+
11
+ it 'has a version number' do
12
+ expect(SlimFormObject::VERSION).not_to be nil
13
+ end
14
+
15
+ context 'get_association' do
16
+ it 'model1 has_one model2' do
17
+ association = send :get_association, TestOneModel, TestTwoModel
18
+ expect(association).to eq(:has_one)
19
+ end
20
+
21
+ it 'model2 belongs_to model1' do
22
+ association = send :get_association, TestTwoModel, TestOneModel
23
+ expect(association).to eq(:belongs_to)
24
+ end
25
+
26
+ it 'model1 has_many model3' do
27
+ association = send :get_association, TestOneModel, TestThreeModel
28
+ expect(association).to eq(:has_many)
29
+ end
30
+
31
+ it 'model3 belongs_to model1' do
32
+ association = send :get_association, TestThreeModel, TestOneModel
33
+ expect(association).to eq(:belongs_to)
34
+ end
35
+
36
+ it 'model1 has_many :test_four_models, through: :test_one_four_models' do
37
+ association = send :get_association, TestOneModel, TestFourModel
38
+ expect(association).to eq(:has_many)
39
+ end
40
+
41
+ it 'model2 has_and_belongs_to_many model3' do
42
+ association = send :get_association, TestTwoModel, TestThreeModel
43
+ expect(association).to eq(:has_and_belongs_to_many)
44
+ end
45
+
46
+ it 'model3 has_and_belongs_to_many model2' do
47
+ association = send :get_association, TestThreeModel, TestTwoModel
48
+ expect(association).to eq(:has_and_belongs_to_many)
49
+ end
50
+
51
+ it 'model3 has_and_belongs_to_many model2' do
52
+ association = send :get_association, TestThreeModel, TestTwoModel
53
+ expect(association).to eq(:has_and_belongs_to_many)
54
+ end
55
+
56
+ it 'model4 has_many :test_one_models, through: :test_one_four_models' do
57
+ association = send :get_association, TestFourModel, TestOneModel
58
+ expect(association).to eq(:has_many)
59
+ end
60
+
61
+ it 'model1-4 belongs_to model1' do
62
+ association = send :get_association, TestOneFourModel, TestOneModel
63
+ expect(association).to eq(:belongs_to)
64
+ end
65
+
66
+ it 'model1-4 belongs_to model4' do
67
+ association = send :get_association, TestOneFourModel, TestFourModel
68
+ expect(association).to eq(:belongs_to)
69
+ end
70
+
71
+ it 'model1 has_many model1-4' do
72
+ association = send :get_association, TestOneModel, TestOneFourModel
73
+ expect(association).to eq(:has_many)
74
+ end
75
+
76
+ it 'model4 has_many model1-4' do
77
+ association = send :get_association, TestFourModel, TestOneFourModel
78
+ expect(association).to eq(:has_many)
79
+ end
80
+
81
+ it 'model2 don\'t have model4' do
82
+ association = send :get_association, TestTwoModel, TestFourModel
83
+ expect(association).to eq(nil)
84
+ end
85
+
86
+ end
87
+
88
+ it 'a' do
89
+ # expect(TestOneModel).to eq(true)
90
+ end
91
+
92
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'slim_form_object'
3
+ require 'bundler/setup'
4
+ require 'active_record'
5
+ require 'rspec/collection_matchers'
6
+ require 'helpers/connect_to_base'
7
+
8
+ RSpec.configure do |config|
9
+
10
+ config.mock_with :rspec do |c|
11
+ c.syntax = [:should, :expect]
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slim_form_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - woodcrust
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.18.4
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.18.4
97
+ description: It's works
98
+ email:
99
+ - roboucrop@gmail.com
100
+ executables:
101
+ - slim_form_object
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - bin/slim_form_object
106
+ - lib/slim_form_object.rb
107
+ - lib/slim_form_object/version.rb
108
+ - spec/db/database.yml
109
+ - spec/db/database.yml.travis
110
+ - spec/helpers/connect_to_base.rb
111
+ - spec/helpers/models.rb
112
+ - spec/slim_form_object_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: http://inclouds.com.ua
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: This is form object
138
+ test_files:
139
+ - spec/db/database.yml
140
+ - spec/db/database.yml.travis
141
+ - spec/helpers/connect_to_base.rb
142
+ - spec/helpers/models.rb
143
+ - spec/slim_form_object_spec.rb
144
+ - spec/spec_helper.rb