aform 0.0.6 → 0.0.7
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 +4 -4
- data/README.md +97 -7
- data/lib/aform/builder.rb +5 -1
- data/lib/aform/model.rb +1 -1
- data/lib/aform/version.rb +1 -1
- data/test/model_test.rb +17 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9df2b57aeb8971ff9e39d745dcd5eff638dda62
|
4
|
+
data.tar.gz: e571e37db8431d9058396d5ffa2400ead9ccb501
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6179f8685ab941adbe1bae32685857787978c7931cb47698968aee0d51e82915132526205afe2ec28e3eff30d276e35f7adab6fa4ead45811f184f74b80a6749
|
7
|
+
data.tar.gz: 07d14ba691df39bc0f35f0ad77f6a2d8caddfebe36146f5ea9f8400cf8bb0c9da9970095d2f81db77f78f714cdad1b649e159d932e2aabe27169209f66c9b60a
|
data/README.md
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
# Aform
|
2
|
-
[](https://travis-ci.org/antonversal/aform)
|
1
|
+
# Aform [](https://travis-ci.org/antonversal/aform) [](https://codeclimate.com/github/antonversal/aform)
|
3
2
|
|
4
|
-
|
3
|
+
Aform lets you define Form Object with validatins and ability save difficult `jsons` or `form params` to models. You can create Form object with nested forms for storing `has_many` associations with parent model.
|
5
4
|
|
6
|
-
|
5
|
+
It was developed for [rails-api](https://github.com/rails-api/rails-api) and ActiveRecord.
|
7
6
|
|
8
7
|
## Installation
|
9
8
|
|
10
9
|
Add this line to your application's Gemfile:
|
11
10
|
|
12
|
-
gem 'aform'
|
11
|
+
gem 'aform', '~>0.0.7'
|
13
12
|
|
14
13
|
And then execute:
|
15
14
|
|
@@ -20,12 +19,103 @@ Or install it yourself as:
|
|
20
19
|
$ gem install aform
|
21
20
|
|
22
21
|
## Usage
|
22
|
+
### Define Form
|
23
23
|
|
24
|
-
|
24
|
+
Definning form is pretty simple, for instance you have posts wich have comments and comments have likes:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class PostForm < Aform::Form
|
28
|
+
param :title, :author
|
29
|
+
validates_presence_of :title, :author
|
30
|
+
|
31
|
+
has_many :comments do
|
32
|
+
param :message, :author
|
33
|
+
validates_presence_of :message, :author
|
34
|
+
|
35
|
+
has_many :likes do
|
36
|
+
param :author
|
37
|
+
validates_presence_of :author
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
Method `param` is used for adding params wich will be used for saving models. You can add any validation you need for each param.
|
43
|
+
|
44
|
+
### Setup controller
|
45
|
+
``` ruby
|
46
|
+
class PostsController < ApplicationController
|
47
|
+
|
48
|
+
def create
|
49
|
+
form = PostForm.new(Post.new, params[:post])
|
50
|
+
if form.save
|
51
|
+
render json: form.model, status: :created
|
52
|
+
else
|
53
|
+
render json: {errors: form.errors}, status: :unprocessable_entity
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def update
|
58
|
+
post = Post.find(params[:id])
|
59
|
+
form = PostForm.new(post, params[:post])
|
60
|
+
if form.save
|
61
|
+
render json: form.model
|
62
|
+
else
|
63
|
+
render json: {errors: form.errors}, status: :unprocessable_entity
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
### Delete nested records
|
71
|
+
|
72
|
+
For deleting nested records you should set `_destroy` key to `true` in nested params:
|
73
|
+
```ruby
|
74
|
+
post = {
|
75
|
+
title: "Very Cool Post",
|
76
|
+
author: "John Doe",
|
77
|
+
comments: [ {id: comment.id, _destroy: true}]
|
78
|
+
}
|
79
|
+
```
|
80
|
+
|
81
|
+
### Params
|
82
|
+
Before looking up the param in a given hash, a form object will check for the presence of a method with the name of the param:
|
83
|
+
```ruby
|
84
|
+
post = {
|
85
|
+
title: "Cool Post",
|
86
|
+
first_author: "John Doe",
|
87
|
+
second_author: "Mr. Author"
|
88
|
+
}
|
89
|
+
```
|
90
|
+
Form Object:
|
91
|
+
``` ruby
|
92
|
+
class OtherPostForm < Aform::Form
|
93
|
+
param :title, :author
|
94
|
+
|
95
|
+
def author(attributes)
|
96
|
+
"#{attributes[:first_author]} and #{attributes[:second_author]}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
When you need just save method with other name you can use `:model_field` option:
|
102
|
+
|
103
|
+
``` ruby
|
104
|
+
param :first_author, model_field: :author
|
105
|
+
```
|
106
|
+
|
107
|
+
### Validations
|
108
|
+
Aform curruntly support standart ActiveModel validations like:
|
109
|
+
```ruby
|
110
|
+
validates_presence_of :title
|
111
|
+
validates :count, presence: true, inclusion: [1..100]
|
112
|
+
```
|
113
|
+
And ActiveRecord uniqueness validation `validates_uniqueness_of` or `validates :title, uniqueness: true`
|
114
|
+
But it doesn't support yet validation with block and a symbol pointing to a method, supporting will be added in short future.
|
25
115
|
|
26
116
|
## Contributing
|
27
117
|
|
28
|
-
1. Fork it ( https://github.com/
|
118
|
+
1. Fork it ( https://github.com/antonversal/aform )
|
29
119
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
120
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
121
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/aform/builder.rb
CHANGED
@@ -12,7 +12,11 @@ class Aform::Builder
|
|
12
12
|
self.params = params
|
13
13
|
|
14
14
|
validations.each do |v|
|
15
|
-
|
15
|
+
if v[:block]
|
16
|
+
send(v[:method], v[:block])
|
17
|
+
else
|
18
|
+
send(v[:method], *v[:options])
|
19
|
+
end
|
16
20
|
end if validations
|
17
21
|
|
18
22
|
params.each do |p|
|
data/lib/aform/model.rb
CHANGED
data/lib/aform/version.rb
CHANGED
data/test/model_test.rb
CHANGED
@@ -48,8 +48,15 @@ describe Aform::Model do
|
|
48
48
|
[{method: :validate, block: ->{errors.add(:base, "must be foo")}}]
|
49
49
|
end
|
50
50
|
|
51
|
+
let(:model) {subject.new(mock_ar_model, mock_form, {})}
|
52
|
+
|
51
53
|
it "is not valid" do
|
52
|
-
|
54
|
+
model.wont_be :valid?
|
55
|
+
end
|
56
|
+
|
57
|
+
it "adds errors" do
|
58
|
+
model.valid?
|
59
|
+
model.errors.messages.must_equal(base: ["must be foo"])
|
53
60
|
end
|
54
61
|
end
|
55
62
|
end
|
@@ -82,6 +89,15 @@ describe Aform::Model do
|
|
82
89
|
end
|
83
90
|
end
|
84
91
|
|
92
|
+
context "when field with boolean false value" do
|
93
|
+
let(:form_model) { subject.new(model, mock_form, name: "name", count: false)}
|
94
|
+
|
95
|
+
it "calls assigns attributes" do
|
96
|
+
form_model.save
|
97
|
+
model.attributes.must_equal(name: "name", count: false)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
85
101
|
context "when fields with model_field option" do
|
86
102
|
let(:fields){ [{field: :name}, {field: :count, options: {model_field: :size}}] }
|
87
103
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Versal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07
|
11
|
+
date: 2014-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|