base_form 0.1.1 → 0.1.3
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 +5 -5
- data/README.md +32 -29
- data/Rakefile +2 -1
- data/lib/base_form.rb +2 -0
- data/lib/base_form/form.rb +2 -0
- data/lib/base_form/version.rb +3 -1
- metadata +101 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '040816d0c82a13955a6945f7939d4d3c4be7258bf19930182df388301ec76a6a'
|
4
|
+
data.tar.gz: ef90df7b6d22f743cdad184e228e26eac2dcf5610010b54cb16b6bc2b8346f5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b05411ff5f7ebfa308e4398ba4ad5d9b810b5809c6f66070192f3f24a7dce275e4a9b8166fc996ccc845336f3cff79cf0f89fa177381ac0bbc0426cf8e18119
|
7
|
+
data.tar.gz: 2f60ffe258f35c7968a0add5affa30da6f7c267f93c482bbf2d57af5534e14318ceef8c000d942e2b95d216f649beeadf237488c18b63f5a2747e88c2722e636
|
data/README.md
CHANGED
@@ -1,10 +1,22 @@
|
|
1
1
|
# BaseForm
|
2
2
|
[](https://codeclimate.com/github/andrerpbts/base_form)
|
3
3
|
[](https://codeclimate.com/github/andrerpbts/base_form/coverage)
|
4
|
-
[
|
4
|
+
[](https://travis-ci.org/andrerpbts/base_form)
|
5
5
|
|
6
6
|
A simple and small Form Objects Rails plugin for ActiveRecord based projects.
|
7
7
|
|
8
|
+
## Why?
|
9
|
+
In a development day-to-day basis, we commonly are confronted with situations where we
|
10
|
+
need to save data in more than one database table, running it's own validations, and
|
11
|
+
the validations of the all context together. In most cases a Form Object is a perfect
|
12
|
+
solution to deliver those records in a fun and maintenable code.
|
13
|
+
|
14
|
+
Actually, there's a lot of another gems to do that, like the great
|
15
|
+
[reform](https://github.com/apotonick/reform) or
|
16
|
+
[activeform-rails](https://github.com/GCorbel/activeform-rails), which are a more complete
|
17
|
+
solution for this problem. But, if you are looking for something lighter, maybe this
|
18
|
+
gem could fit well for you.
|
19
|
+
|
8
20
|
## Installation
|
9
21
|
Add this line to your application's Gemfile:
|
10
22
|
|
@@ -23,18 +35,7 @@ $ gem install base_form
|
|
23
35
|
```
|
24
36
|
|
25
37
|
## Usage
|
26
|
-
|
27
|
-
need to save data in more than one database table, running it's own validations, and
|
28
|
-
the validations of the all context together. In most cases a Form Object is a perfect
|
29
|
-
solution to deliver those records in a fun and maintenable code.
|
30
|
-
|
31
|
-
Actually, there's a lot of another gems to do that, like the great
|
32
|
-
[reform](https://github.com/apotonick/reform) or
|
33
|
-
[activeform-rails](https://github.com/GCorbel/activeform-rails), which are a more complete
|
34
|
-
solution for this problem. But, if you are looking for something lighter, maybe this
|
35
|
-
gem could fit well for you.
|
36
|
-
|
37
|
-
That said, let me show some simple examples to you. Let's suppose you want to create a
|
38
|
+
Let's suppose you want to create a
|
38
39
|
signup form (you can check this example in the dummy app on this gem specs), with
|
39
40
|
receiving a user email, a user password, a user password confirmation, and a plan. In your
|
40
41
|
signup form, you need to create an account for this user, associate it to a entrance plan
|
@@ -76,18 +77,19 @@ class SignupForm < BaseForm::Form
|
|
76
77
|
end
|
77
78
|
```
|
78
79
|
|
79
|
-
Now you may be asking: What about email and password?
|
80
|
-
Well, you could, in fact,
|
80
|
+
Now you may be asking: What about email and password? Shouldn't they be validated as well?
|
81
|
+
Well, you could, in fact, add all validations in this form instead put it in your models,
|
82
|
+
but sometimes you don't have much control of that.
|
81
83
|
Then, I'm showing here the case that `User` model has those validations. Don't be mad ok? :)
|
82
84
|
|
83
85
|
The form validations are the first validations tha are performed before it try to persist
|
84
86
|
something here. If this validation fails, for an example, the persist method will not even
|
85
|
-
be called, and
|
87
|
+
be called, and we're done with it. Otherwise, it wil try to persist your logic, which we'll
|
86
88
|
implement next.
|
87
89
|
|
88
90
|
Ok, now, you need to set the records that you will persist here.
|
89
91
|
In this case is the `:user` you want to save, and the `:account` you will want to associate
|
90
|
-
to this user. So, you
|
92
|
+
to this user. So, you add it there (I recommend you let this in the top of the class to make
|
91
93
|
it clear):
|
92
94
|
|
93
95
|
```ruby
|
@@ -98,16 +100,16 @@ class SignupForm < BaseForm::Form
|
|
98
100
|
end
|
99
101
|
```
|
100
102
|
|
101
|
-
This line will automatically generate `attr_readers` to each record there, and will add
|
103
|
+
This line will automatically generate `attr_readers` to each record there, and will add these
|
102
104
|
symbols in an array called `form_records` in your class. To understand it better, let's talk
|
103
105
|
about the `persist` implementation itself.
|
104
106
|
|
105
|
-
By the rule, `persist` method is obligatory, and not implementing it, will cause your form
|
107
|
+
By the rule, the `persist` method is obligatory, and not implementing it, will cause your form
|
106
108
|
raise a `NotImplementedError` when calling `save` to it.
|
107
109
|
|
108
|
-
All things written inside `persist` method
|
109
|
-
and if some record have its validation failed, this will perform a
|
110
|
-
to you with those
|
110
|
+
All things written inside `persist` method will automatically run in a ActiveRecord transaction,
|
111
|
+
and if some record have its validation failed, this will perform a rollback and deliver the form
|
112
|
+
to you with those errors grouped through `errors` method, like any AR model you are already
|
111
113
|
familiar with.
|
112
114
|
|
113
115
|
Let me stop to talk and show you something we can call as implementation of this:
|
@@ -134,13 +136,13 @@ class SignupForm < BaseForm::Form
|
|
134
136
|
end
|
135
137
|
```
|
136
138
|
|
137
|
-
So, here is the thing
|
138
|
-
form_records I've defined before. It tries to create an account
|
139
|
+
So, here is the thing: check the variables names I've associated there are the names of
|
140
|
+
form_records I've defined before. It tries to create an account setting a plan to it
|
139
141
|
and then tries to create a user associated to this brand new account.
|
140
142
|
|
141
|
-
|
142
|
-
and group it in `errors` object in your form itself in case of some validation
|
143
|
-
|
143
|
+
This `form_records` will call each object associated here to check its errors,
|
144
|
+
and group it in `errors` object in your form itself in case of some validation fails.
|
145
|
+
If all is fine, the form instance is returned to you and you will be able to call
|
144
146
|
methods like `persisted?`, `account`, `user`, `valid?` and etc...
|
145
147
|
|
146
148
|
Are you still there? :D
|
@@ -176,8 +178,9 @@ class SignupForm < BaseForm
|
|
176
178
|
end
|
177
179
|
```
|
178
180
|
|
179
|
-
|
180
|
-
|
181
|
+
Hmmm, this looks pretty nice!
|
182
|
+
|
183
|
+
I hope this helps someone in the same way it helped me. Thanks!
|
181
184
|
|
182
185
|
## Contributing
|
183
186
|
- Fork it
|
data/Rakefile
CHANGED
data/lib/base_form.rb
CHANGED
data/lib/base_form/form.rb
CHANGED
data/lib/base_form/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,113 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base_form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- andrerpbts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bootsnap
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: factory_bot_rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.77.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.77.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.4.1
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.4.1
|
13
111
|
- !ruby/object:Gem::Dependency
|
14
112
|
name: activesupport
|
15
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,10 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
168
|
- !ruby/object:Gem::Version
|
71
169
|
version: '0'
|
72
170
|
requirements: []
|
73
|
-
|
74
|
-
rubygems_version: 2.5.1
|
171
|
+
rubygems_version: 3.0.6
|
75
172
|
signing_key:
|
76
173
|
specification_version: 4
|
77
174
|
summary: A simple and small form objects Rails plugin
|
78
175
|
test_files: []
|
79
|
-
has_rdoc:
|