syrup_form_object 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/gemfiles/rails3_2.gemfile +1 -0
- data/gemfiles/rails4_0.gemfile +1 -0
- data/lib/syrup/callbacks.rb +15 -0
- data/lib/syrup/form/wrapped.rb +15 -7
- data/lib/syrup/form_methods.rb +12 -0
- data/lib/syrup/form_object.rb +18 -99
- data/lib/syrup/persistence.rb +25 -0
- data/lib/syrup/relations.rb +70 -0
- data/lib/syrup/version.rb +1 -1
- data/lib/syrup.rb +4 -0
- data/spec/db/schema.rb +5 -0
- data/spec/lib/syrup/form_object_spec.rb +5 -33
- data/spec/spec_helper.rb +9 -0
- data/test.sh +1 -0
- metadata +8 -3
- data/Gemfile +0 -7
data/gemfiles/rails3_2.gemfile
CHANGED
data/gemfiles/rails4_0.gemfile
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Syrup::Callbacks
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
include ActiveModel::Validations::Callbacks
|
6
|
+
include ActiveSupport::Callbacks
|
7
|
+
include ActiveRecord::Callbacks
|
8
|
+
|
9
|
+
include InstanceMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module InstanceMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/lib/syrup/form/wrapped.rb
CHANGED
@@ -2,20 +2,30 @@ module Syrup::Form::Wrapped
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
alias_method :wrapped, @
|
6
|
-
alias_method :wrapped=, "#{@
|
5
|
+
alias_method :wrapped, @wrapped_class_name
|
6
|
+
alias_method :wrapped=, "#{@wrapped_class_name}="
|
7
|
+
@wrapped_class = @wrapped_class_name.to_s.camelize.constantize
|
8
|
+
|
9
|
+
def self.method_missing(*params)
|
10
|
+
@wrapped_class.send *params
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.respond_to?(*params)
|
14
|
+
super || @wrapped_class.respond_to?(*params)
|
15
|
+
end
|
7
16
|
|
8
17
|
def self.model_name
|
9
|
-
@wrapped_class.
|
18
|
+
@wrapped_class.model_name
|
10
19
|
end
|
20
|
+
|
11
21
|
end
|
12
22
|
|
13
23
|
def method_missing(*params)
|
14
24
|
wrapped.send *params
|
15
25
|
end
|
16
26
|
|
17
|
-
def
|
18
|
-
super || wrapped.
|
27
|
+
def respond_to?(*params)
|
28
|
+
super || wrapped.respond_to?(*params)
|
19
29
|
end
|
20
30
|
|
21
31
|
def find_relations(params)
|
@@ -38,6 +48,4 @@ module Syrup::Form::Wrapped
|
|
38
48
|
wrapped.persisted?
|
39
49
|
end
|
40
50
|
|
41
|
-
|
42
51
|
end
|
43
|
-
|
data/lib/syrup/form_object.rb
CHANGED
@@ -1,54 +1,15 @@
|
|
1
|
-
require 'virtus'
|
2
|
-
|
3
1
|
class Syrup::FormObject
|
4
|
-
include
|
5
|
-
|
6
|
-
extend ActiveModel::Naming
|
7
|
-
include ActiveModel::Conversion
|
2
|
+
include Syrup::FormMethods
|
8
3
|
extend ActiveModel::Callbacks
|
9
|
-
|
10
|
-
include
|
11
|
-
include
|
12
|
-
include ActiveRecord::Validations
|
13
|
-
include ActiveModel::Validations::Callbacks
|
14
|
-
include ActiveSupport::Callbacks
|
15
|
-
include ActiveRecord::Callbacks
|
4
|
+
include Syrup::Relations
|
5
|
+
include Syrup::Persistence
|
6
|
+
include Syrup::Callbacks
|
16
7
|
|
17
8
|
class << self
|
18
9
|
|
19
|
-
def accepts_nested_attributes_for(*relations)
|
20
|
-
relations.each do |relation|
|
21
|
-
build_attributes_setter relation
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
# build_attributes_setter 'address'
|
26
|
-
#
|
27
|
-
# def address_attributes=(attributes)
|
28
|
-
# address.attributes=attributes
|
29
|
-
# end
|
30
|
-
|
31
|
-
def build_attributes_setter(relation)
|
32
|
-
module_eval <<-EOH
|
33
|
-
def #{relation}_attributes=(attributes)
|
34
|
-
#{relation}.attributes=attributes
|
35
|
-
end
|
36
|
-
EOH
|
37
|
-
end
|
38
|
-
|
39
|
-
def relations
|
40
|
-
@relations ||= []
|
41
|
-
end
|
42
|
-
|
43
|
-
def has_one(klass)
|
44
|
-
relations << klass
|
45
|
-
attr_accessor klass
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
10
|
def wraps(klass)
|
50
11
|
has_one(klass)
|
51
|
-
@
|
12
|
+
@wrapped_class_name = klass
|
52
13
|
include Syrup::Form::Wrapped
|
53
14
|
end
|
54
15
|
|
@@ -62,77 +23,38 @@ class Syrup::FormObject
|
|
62
23
|
form.after_find(params)
|
63
24
|
form
|
64
25
|
end
|
65
|
-
|
66
|
-
def connection
|
67
|
-
ActiveRecord::Base.connection
|
68
|
-
end
|
69
|
-
|
70
26
|
end
|
71
27
|
|
72
28
|
def remember_transaction_record_state(*); end
|
73
29
|
def restore_transaction_record_state(*); end
|
74
30
|
def clear_transaction_record_state(*); end
|
75
31
|
def self.reflect_on_association(*); end
|
32
|
+
def add_to_transaction; end
|
33
|
+
def self.attributes_protected_by_default
|
34
|
+
[]
|
35
|
+
end
|
36
|
+
|
76
37
|
def initialize(params={})
|
77
38
|
build_relations
|
78
39
|
build(params)
|
79
|
-
self.
|
40
|
+
self.assign_attributes(params)
|
80
41
|
end
|
81
42
|
|
82
43
|
def update_attributes(params)
|
83
|
-
self.
|
44
|
+
self.assign_attributes(params)
|
84
45
|
self.save
|
85
46
|
end
|
86
47
|
|
87
|
-
def
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
def find_relations(params)
|
94
|
-
self.class.relations.each do |klass|
|
95
|
-
if params[klass]
|
96
|
-
self.send "#{klass}=", klass.to_s.camelize.constantize.find(params[klass])
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def attributes=(params)
|
102
|
-
@params = params
|
103
|
-
params.each do |key, value|
|
104
|
-
self.send "#{key}=", value
|
105
|
-
end
|
106
|
-
end
|
48
|
+
# def attributes=(params)
|
49
|
+
# @params = params
|
50
|
+
# params.each do |key, value|
|
51
|
+
# self.send "#{key}=", value
|
52
|
+
# end
|
53
|
+
# end
|
107
54
|
|
108
55
|
def build(params); end
|
109
56
|
def after_find(params); end
|
110
57
|
|
111
|
-
def before_save; end
|
112
|
-
def before_create; end
|
113
|
-
def after_create; end
|
114
|
-
def after_save; end
|
115
|
-
def after_commit; end
|
116
|
-
|
117
|
-
def transaction(&block)
|
118
|
-
first_related_object.transaction(&block)
|
119
|
-
end
|
120
|
-
|
121
|
-
def first_related_object
|
122
|
-
respond_to?(:wrapped) ? wrapped : self.send(self.class.relations.first)
|
123
|
-
end
|
124
|
-
|
125
|
-
def related_objects
|
126
|
-
related= {}
|
127
|
-
self.class.relations.collect do |klass|
|
128
|
-
related[klass] = self.send(klass)
|
129
|
-
end
|
130
|
-
related
|
131
|
-
end
|
132
|
-
|
133
|
-
|
134
|
-
def add_to_transaction; end
|
135
|
-
|
136
58
|
def run_validations!(*)
|
137
59
|
super
|
138
60
|
self.related_objects.each do |klass, related|
|
@@ -158,8 +80,5 @@ class Syrup::FormObject
|
|
158
80
|
run_callbacks(:update) { save_form }
|
159
81
|
end
|
160
82
|
|
161
|
-
def save_form
|
162
|
-
self.related_objects.all?{|klass, related| related.save }
|
163
|
-
end
|
164
83
|
|
165
84
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Syrup::Persistence
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
include ActiveRecord::Persistence
|
6
|
+
include ActiveRecord::Transactions
|
7
|
+
include ActiveRecord::Validations
|
8
|
+
|
9
|
+
extend ClassMethods
|
10
|
+
include InstanceMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def connection
|
15
|
+
ActiveRecord::Base.connection
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def save_form
|
21
|
+
self.related_objects.all?{|klass, related| related.save }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Syrup::Relations
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
extend ClassMethods
|
6
|
+
|
7
|
+
include InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def accepts_nested_attributes_for(*relations)
|
12
|
+
relations.each do |relation|
|
13
|
+
build_attributes_setter relation
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# build_attributes_setter 'address'
|
18
|
+
#
|
19
|
+
# def address_attributes=(attributes)
|
20
|
+
# address.attributes=attributes
|
21
|
+
# end
|
22
|
+
|
23
|
+
def build_attributes_setter(relation)
|
24
|
+
module_eval <<-EOH
|
25
|
+
def #{relation}_attributes=(attributes)
|
26
|
+
#{relation}.attributes=attributes
|
27
|
+
end
|
28
|
+
EOH
|
29
|
+
end
|
30
|
+
|
31
|
+
def relations
|
32
|
+
@relations ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def has_one(klass)
|
36
|
+
relations << klass
|
37
|
+
attr_accessor klass
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module InstanceMethods
|
42
|
+
def build_relations
|
43
|
+
self.class.relations.each do |klass|
|
44
|
+
self.send "#{klass}=", klass.to_s.camelize.constantize.new
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_relations(params)
|
49
|
+
self.class.relations.each do |klass|
|
50
|
+
if params[klass]
|
51
|
+
self.send "#{klass}=", klass.to_s.camelize.constantize.find(params[klass])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def first_related_object
|
57
|
+
respond_to?(:wrapped) ? wrapped : self.send(self.class.relations.first)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def related_objects
|
62
|
+
related= {}
|
63
|
+
self.class.relations.collect do |klass|
|
64
|
+
related[klass] = self.send(klass)
|
65
|
+
end
|
66
|
+
related
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
data/lib/syrup/version.rb
CHANGED
data/lib/syrup.rb
CHANGED
data/spec/db/schema.rb
ADDED
@@ -1,38 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class TestItem
|
4
|
-
include Virtus.model
|
5
|
-
attribute :test_item_value, Integer
|
6
|
-
|
7
|
-
def self.model_name
|
8
|
-
ActiveModel::Name.new(self, nil, "test_item")
|
9
|
-
end
|
10
|
-
|
11
|
-
def errors
|
12
|
-
[]
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.find(id)
|
16
|
-
end
|
17
|
-
|
18
|
-
def save
|
19
|
-
end
|
20
|
-
|
21
|
-
def valid?
|
22
|
-
true
|
23
|
-
end
|
24
|
-
|
25
|
-
def readonly?
|
26
|
-
false
|
27
|
-
end
|
28
|
-
|
29
|
-
def new_record?
|
30
|
-
true
|
31
|
-
end
|
32
|
-
|
33
|
-
def persisted?
|
34
|
-
false
|
35
|
-
end
|
3
|
+
class TestItem < ActiveRecord::Base
|
36
4
|
end
|
37
5
|
|
38
6
|
class TestSubclass < Syrup::FormObject
|
@@ -248,6 +216,8 @@ describe Syrup::FormObject do
|
|
248
216
|
expect(subject.test_item).to be_a(TestItem)
|
249
217
|
end
|
250
218
|
it 'calls the after_find method' do
|
219
|
+
TestItem.stub(:find).once { TestItem.new }
|
220
|
+
|
251
221
|
expect(subject).to have_called_after_find
|
252
222
|
end
|
253
223
|
end
|
@@ -380,6 +350,8 @@ describe Syrup::FormObject do
|
|
380
350
|
expect(subject.wrapped).to be test_item
|
381
351
|
end
|
382
352
|
it 'calls the after_find method' do
|
353
|
+
TestItem.stub(:find).once { TestItem.new }
|
354
|
+
|
383
355
|
expect(subject).to have_called_after_find
|
384
356
|
end
|
385
357
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,16 @@
|
|
1
1
|
require 'rspec'
|
2
|
+
require 'rails'
|
2
3
|
require 'active_record'
|
3
4
|
require 'active_record/errors'
|
4
5
|
require 'syrup'
|
6
|
+
require 'nulldb_rspec'
|
7
|
+
|
8
|
+
include NullDB::RSpec::NullifiedDatabase
|
9
|
+
|
10
|
+
NullDB.configure do |config|
|
11
|
+
config.project_root = File.expand_path("..", __FILE__)
|
12
|
+
end
|
13
|
+
|
5
14
|
|
6
15
|
RSpec.configure do |config|
|
7
16
|
end
|
data/test.sh
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
BUNDLE_GEMFILE=gemfiles/rails3_2.gemfile bundle exec rspec && BUNDLE_GEMFILE=gemfiles/rails4_0.gemfile bundle exec rspec
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syrup_form_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|
@@ -71,22 +71,27 @@ files:
|
|
71
71
|
- .gitignore
|
72
72
|
- .rspec
|
73
73
|
- .travis.yml
|
74
|
-
- Gemfile
|
75
74
|
- LICENSE
|
76
75
|
- README.md
|
77
76
|
- Rakefile
|
78
77
|
- gemfiles/rails3_2.gemfile
|
79
78
|
- gemfiles/rails4_0.gemfile
|
80
79
|
- lib/syrup.rb
|
80
|
+
- lib/syrup/callbacks.rb
|
81
81
|
- lib/syrup/form.rb
|
82
82
|
- lib/syrup/form/standalone.rb
|
83
83
|
- lib/syrup/form/wrapped.rb
|
84
|
+
- lib/syrup/form_methods.rb
|
84
85
|
- lib/syrup/form_object.rb
|
86
|
+
- lib/syrup/persistence.rb
|
87
|
+
- lib/syrup/relations.rb
|
85
88
|
- lib/syrup/version.rb
|
89
|
+
- spec/db/schema.rb
|
86
90
|
- spec/lib/syrup/callbacks_spec.rb
|
87
91
|
- spec/lib/syrup/form_object_spec.rb
|
88
92
|
- spec/spec_helper.rb
|
89
93
|
- syrup_form_object.gemspec
|
94
|
+
- test.sh
|
90
95
|
homepage: https://github.com/alexsiri7/syrup_form_object
|
91
96
|
licenses:
|
92
97
|
- MIT
|