aform 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +9 -0
- data/README.md +3 -0
- data/Rakefile +31 -1
- data/lib/aform/form.rb +7 -7
- data/lib/aform/form_saver.rb +1 -1
- data/lib/aform/version.rb +1 -1
- data/test/form_saver_test.rb +4 -4
- data/test/integration/integration_helper.rb +0 -20
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b539a689687d10ce372376c984749ac30cbe65cc
|
4
|
+
data.tar.gz: addef8dbcb1055f19ce403ae320c4e37532d6ee3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b6c8024ce914f97a27845459d2003c18d949974a86703a0e75949d38b1588aeb70da76533464eda580563396bea4942d36e9c6d7d37a7e679ea94a01d4e7f4b
|
7
|
+
data.tar.gz: 95fb5e0da238837daf95f78ca910f098b984d700b46d049873571ff20ca2f4214ca98a077fbdd90640a75a3e6d8a7f3df4edfd1273af0d64b1de770dc19b1e83
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# Aform
|
2
|
+
[![Build Status](https://travis-ci.org/antonversal/aform.svg?branch=master)](https://travis-ci.org/antonversal/aform)
|
3
|
+
|
4
|
+
[![Code Quality](https://codeclimate.com/github/antonversal/aform.png)](https://codeclimate.com/github/antonversal/aform)
|
2
5
|
|
3
6
|
TODO: Write a gem description
|
4
7
|
|
data/Rakefile
CHANGED
@@ -11,4 +11,34 @@ Rake::TestTask.new do |t|
|
|
11
11
|
t.libs << 'test'
|
12
12
|
t.name = "test:integration"
|
13
13
|
t.pattern = 'test/integration/*_test.rb'
|
14
|
-
end
|
14
|
+
end
|
15
|
+
|
16
|
+
namespace :test do
|
17
|
+
desc "Prepare test environment"
|
18
|
+
task :prepare do
|
19
|
+
require 'active_record'
|
20
|
+
ActiveRecord::Base.establish_connection(
|
21
|
+
:adapter => "sqlite3",
|
22
|
+
:database => "#{Dir.pwd}/database.sqlite3"
|
23
|
+
)
|
24
|
+
|
25
|
+
ActiveRecord::Schema.define do
|
26
|
+
create_table :posts do |t|
|
27
|
+
t.column :title, :string
|
28
|
+
t.column :author, :string
|
29
|
+
t.timestamps
|
30
|
+
end
|
31
|
+
|
32
|
+
create_table :comments do |t|
|
33
|
+
t.column :message, :string
|
34
|
+
t.column :author, :string
|
35
|
+
t.belongs_to :post
|
36
|
+
end
|
37
|
+
|
38
|
+
create_table :likes do |t|
|
39
|
+
t.column :author, :string
|
40
|
+
t.belongs_to :comment
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/aform/form.rb
CHANGED
@@ -2,12 +2,12 @@ module Aform
|
|
2
2
|
class Form
|
3
3
|
class_attribute :params, :pkey, :validations, :nested_form_klasses
|
4
4
|
|
5
|
-
attr_reader :form_model, :attributes, :nested_forms, :
|
5
|
+
attr_reader :form_model, :attributes, :nested_forms, :model, :parent
|
6
6
|
|
7
|
-
def initialize(
|
7
|
+
def initialize(model, attributes, parent = nil, opts = {})
|
8
8
|
@opts = opts
|
9
9
|
@attributes = attributes
|
10
|
-
@
|
10
|
+
@model = model
|
11
11
|
@parent = parent
|
12
12
|
assign_opts_instances
|
13
13
|
initialize_nested
|
@@ -83,7 +83,7 @@ module Aform
|
|
83
83
|
@form_saver = @opts[:form_saver] || Aform::FormSaver.new(self)
|
84
84
|
@form_model = @opts[:form_model] || Aform::Model.\
|
85
85
|
build_klass(self.params, self.validations).\
|
86
|
-
new(
|
86
|
+
new(model, self, attributes)
|
87
87
|
end
|
88
88
|
|
89
89
|
def initialize_nested
|
@@ -92,7 +92,7 @@ module Aform
|
|
92
92
|
nested_form_klasses.inject({}) do |memo, (k, v)|
|
93
93
|
if attributes[k]
|
94
94
|
nested = attributes[k].map do |attrs|
|
95
|
-
v.new(
|
95
|
+
v.new(nested_model(k, attrs, v.pkey), attrs, self, @opts)
|
96
96
|
end
|
97
97
|
memo.merge(k => nested)
|
98
98
|
else
|
@@ -102,9 +102,9 @@ module Aform
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
def
|
105
|
+
def nested_model(association, attrs, key)
|
106
106
|
key = key || :id
|
107
|
-
|
107
|
+
model.send(association).find_by(key => attrs[key]) || \
|
108
108
|
association.to_s.classify.constantize.new
|
109
109
|
end
|
110
110
|
end
|
data/lib/aform/form_saver.rb
CHANGED
data/lib/aform/version.rb
CHANGED
data/test/form_saver_test.rb
CHANGED
@@ -21,10 +21,10 @@ describe Aform::FormSaver do
|
|
21
21
|
form_model: form_model)
|
22
22
|
end
|
23
23
|
|
24
|
-
def mock_form(nested_forms: nil,
|
24
|
+
def mock_form(nested_forms: nil, model: nil, save: true)
|
25
25
|
OpenStruct.new(nested_forms: nested_forms,
|
26
26
|
form_model: OpenStruct.new(save: save),
|
27
|
-
|
27
|
+
model: model)
|
28
28
|
end
|
29
29
|
|
30
30
|
def form_saver(form)
|
@@ -49,14 +49,14 @@ describe Aform::FormSaver do
|
|
49
49
|
it "success" do
|
50
50
|
nested_forms = [mock_nested_form, mock_nested_form]
|
51
51
|
form = mock_form(nested_forms: {comments: nested_forms},
|
52
|
-
|
52
|
+
model: OpenStruct.new(comments: []))
|
53
53
|
form_saver(form).save.must_equal(true)
|
54
54
|
end
|
55
55
|
|
56
56
|
it "failure" do
|
57
57
|
nested_forms = [mock_nested_form(false), mock_nested_form]
|
58
58
|
form = mock_form(nested_forms: {comments: nested_forms},
|
59
|
-
|
59
|
+
model: OpenStruct.new(comments: []))
|
60
60
|
form_saver(form).save.must_equal(false)
|
61
61
|
end
|
62
62
|
end
|
@@ -7,26 +7,6 @@ ActiveRecord::Base.establish_connection(
|
|
7
7
|
:database => "#{Dir.pwd}/database.sqlite3"
|
8
8
|
)
|
9
9
|
|
10
|
-
#TODO: create task
|
11
|
-
#ActiveRecord::Schema.define do
|
12
|
-
# create_table :posts do |t|
|
13
|
-
# t.column :title, :string
|
14
|
-
# t.column :author, :string
|
15
|
-
# t.timestamps
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# create_table :comments do |t|
|
19
|
-
# t.column :message, :string
|
20
|
-
# t.column :author, :string
|
21
|
-
# t.belongs_to :post
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# create_table :likes do |t|
|
25
|
-
# t.column :author, :string
|
26
|
-
# t.belongs_to :comment
|
27
|
-
# end
|
28
|
-
#end
|
29
|
-
|
30
10
|
class Post < ActiveRecord::Base
|
31
11
|
has_many :comments
|
32
12
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Versal
|
@@ -172,6 +172,7 @@ extensions: []
|
|
172
172
|
extra_rdoc_files: []
|
173
173
|
files:
|
174
174
|
- ".gitignore"
|
175
|
+
- ".travis.yml"
|
175
176
|
- Gemfile
|
176
177
|
- LICENSE.txt
|
177
178
|
- README.md
|