abyme 0.1.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.DS_Store +0 -0
- data/.github/workflows/build.yml +60 -0
- data/.gitignore +9 -0
- data/.rspec +1 -3
- data/.simplecov +0 -0
- data/Gemfile.lock +183 -8
- data/README.md +248 -23
- data/Rakefile +47 -2
- data/abyme.gemspec +14 -2
- data/bin/rails +12 -0
- data/javascript/abyme_controller.js +146 -27
- data/lib/{generators/.DS_Store → .DS_Store} +0 -0
- data/lib/abyme/abyme_builder.rb +25 -38
- data/lib/abyme/version.rb +4 -2
- data/lib/abyme/view_helpers.rb +202 -36
- metadata +139 -12
- data/.travis.yml +0 -7
- data/abyme-0.1.2.gem +0 -0
- data/lib/generators/abyme/install_generator.rb +0 -25
- data/lib/generators/abyme/templates/abyme_controller.js +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 585adff10e80cf9646d03a7d2251188d6d8be083a3f9f9b3e7fe019187904c39
|
4
|
+
data.tar.gz: 1e17608e8bde56231bf57016d4d5f880f29915e7cf7c98cd51f46e21ff06609b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efc39dc12b33a25c5b5b61f5079b2b0f7605b7b6ae557cebad526c9efa88475ce4f3e3841fce0e3a04bc87d823e9b52c15a6a2655203cd324ff6985ef229bec8
|
7
|
+
data.tar.gz: fe4dd5956a8afe5ac9d48df970a5c71a0fbabd880f36b19e8de997dc0a622bee5f23c1064e7f1db8b876439909b48bccdbe66bc514bd51573945f950a66699a4
|
data/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: build
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ master ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ master ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
runs-on: ubuntu-latest
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v2
|
22
|
+
- name: Set up Ruby
|
23
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
24
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
25
|
+
# uses: ruby/setup-ruby@v1
|
26
|
+
uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
|
27
|
+
with:
|
28
|
+
ruby-version: 2.6
|
29
|
+
- name: Install sqlite headers
|
30
|
+
run: |
|
31
|
+
sudo apt-get update
|
32
|
+
sudo apt-get install libsqlite3-dev
|
33
|
+
|
34
|
+
- name: Install dependencies
|
35
|
+
run: bundle install
|
36
|
+
|
37
|
+
- name: Prepare Database
|
38
|
+
run: bundle exec rails db:create db:migrate
|
39
|
+
env:
|
40
|
+
DB_CONNECTION: sqlite
|
41
|
+
DB_DATABASE: db/test.sqlite3
|
42
|
+
RAILS_ENV: test
|
43
|
+
|
44
|
+
- name: Run tests
|
45
|
+
run: bundle exec rake
|
46
|
+
env:
|
47
|
+
DB_CONNECTION: sqlite
|
48
|
+
DB_DATABASE: db/test.sqlite3
|
49
|
+
RAILS_ENV: test
|
50
|
+
|
51
|
+
- name: Coveralls
|
52
|
+
uses: coverallsapp/github-action@master
|
53
|
+
with:
|
54
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
55
|
+
|
56
|
+
- name: Create Coverage Artifact
|
57
|
+
uses: actions/upload-artifact@v2
|
58
|
+
with:
|
59
|
+
name: code-coverage
|
60
|
+
path: coverage/
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.simplecov
ADDED
File without changes
|
data/Gemfile.lock
CHANGED
@@ -1,18 +1,145 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
abyme (0.
|
4
|
+
abyme (0.2.4)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
actioncable (6.0.3.4)
|
10
|
+
actionpack (= 6.0.3.4)
|
11
|
+
nio4r (~> 2.0)
|
12
|
+
websocket-driver (>= 0.6.1)
|
13
|
+
actionmailbox (6.0.3.4)
|
14
|
+
actionpack (= 6.0.3.4)
|
15
|
+
activejob (= 6.0.3.4)
|
16
|
+
activerecord (= 6.0.3.4)
|
17
|
+
activestorage (= 6.0.3.4)
|
18
|
+
activesupport (= 6.0.3.4)
|
19
|
+
mail (>= 2.7.1)
|
20
|
+
actionmailer (6.0.3.4)
|
21
|
+
actionpack (= 6.0.3.4)
|
22
|
+
actionview (= 6.0.3.4)
|
23
|
+
activejob (= 6.0.3.4)
|
24
|
+
mail (~> 2.5, >= 2.5.4)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
actionpack (6.0.3.4)
|
27
|
+
actionview (= 6.0.3.4)
|
28
|
+
activesupport (= 6.0.3.4)
|
29
|
+
rack (~> 2.0, >= 2.0.8)
|
30
|
+
rack-test (>= 0.6.3)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
33
|
+
actiontext (6.0.3.4)
|
34
|
+
actionpack (= 6.0.3.4)
|
35
|
+
activerecord (= 6.0.3.4)
|
36
|
+
activestorage (= 6.0.3.4)
|
37
|
+
activesupport (= 6.0.3.4)
|
38
|
+
nokogiri (>= 1.8.5)
|
39
|
+
actionview (6.0.3.4)
|
40
|
+
activesupport (= 6.0.3.4)
|
41
|
+
builder (~> 3.1)
|
42
|
+
erubi (~> 1.4)
|
43
|
+
rails-dom-testing (~> 2.0)
|
44
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
45
|
+
activejob (6.0.3.4)
|
46
|
+
activesupport (= 6.0.3.4)
|
47
|
+
globalid (>= 0.3.6)
|
48
|
+
activemodel (6.0.3.4)
|
49
|
+
activesupport (= 6.0.3.4)
|
50
|
+
activerecord (6.0.3.4)
|
51
|
+
activemodel (= 6.0.3.4)
|
52
|
+
activesupport (= 6.0.3.4)
|
53
|
+
activestorage (6.0.3.4)
|
54
|
+
actionpack (= 6.0.3.4)
|
55
|
+
activejob (= 6.0.3.4)
|
56
|
+
activerecord (= 6.0.3.4)
|
57
|
+
marcel (~> 0.3.1)
|
58
|
+
activesupport (6.0.3.4)
|
59
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
60
|
+
i18n (>= 0.7, < 2)
|
61
|
+
minitest (~> 5.1)
|
62
|
+
tzinfo (~> 1.1)
|
63
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
64
|
+
addressable (2.7.0)
|
65
|
+
public_suffix (>= 2.0.2, < 5.0)
|
66
|
+
builder (3.2.4)
|
67
|
+
capybara (3.33.0)
|
68
|
+
addressable
|
69
|
+
mini_mime (>= 0.1.3)
|
70
|
+
nokogiri (~> 1.8)
|
71
|
+
rack (>= 1.6.0)
|
72
|
+
rack-test (>= 0.6.3)
|
73
|
+
regexp_parser (~> 1.5)
|
74
|
+
xpath (~> 3.2)
|
75
|
+
childprocess (3.0.0)
|
76
|
+
concurrent-ruby (1.1.7)
|
77
|
+
crass (1.0.6)
|
78
|
+
database_cleaner (1.8.5)
|
79
|
+
database_cleaner-active_record (1.8.0)
|
80
|
+
activerecord
|
81
|
+
database_cleaner (~> 1.8.0)
|
9
82
|
diff-lcs (1.4.4)
|
83
|
+
docile (1.3.2)
|
84
|
+
erubi (1.9.0)
|
85
|
+
globalid (0.4.2)
|
86
|
+
activesupport (>= 4.2.0)
|
87
|
+
i18n (1.8.5)
|
88
|
+
concurrent-ruby (~> 1.0)
|
89
|
+
loofah (2.7.0)
|
90
|
+
crass (~> 1.0.2)
|
91
|
+
nokogiri (>= 1.5.9)
|
92
|
+
mail (2.7.1)
|
93
|
+
mini_mime (>= 0.1.1)
|
94
|
+
marcel (0.3.3)
|
95
|
+
mimemagic (~> 0.3.2)
|
96
|
+
method_source (1.0.0)
|
97
|
+
mimemagic (0.3.5)
|
98
|
+
mini_mime (1.0.2)
|
99
|
+
mini_portile2 (2.4.0)
|
100
|
+
minitest (5.14.2)
|
101
|
+
nio4r (2.5.4)
|
102
|
+
nokogiri (1.10.10)
|
103
|
+
mini_portile2 (~> 2.4.0)
|
104
|
+
public_suffix (4.0.6)
|
105
|
+
puma (4.3.6)
|
106
|
+
nio4r (~> 2.0)
|
107
|
+
rack (2.2.3)
|
108
|
+
rack-test (1.1.0)
|
109
|
+
rack (>= 1.0, < 3)
|
110
|
+
rails (6.0.3.4)
|
111
|
+
actioncable (= 6.0.3.4)
|
112
|
+
actionmailbox (= 6.0.3.4)
|
113
|
+
actionmailer (= 6.0.3.4)
|
114
|
+
actionpack (= 6.0.3.4)
|
115
|
+
actiontext (= 6.0.3.4)
|
116
|
+
actionview (= 6.0.3.4)
|
117
|
+
activejob (= 6.0.3.4)
|
118
|
+
activemodel (= 6.0.3.4)
|
119
|
+
activerecord (= 6.0.3.4)
|
120
|
+
activestorage (= 6.0.3.4)
|
121
|
+
activesupport (= 6.0.3.4)
|
122
|
+
bundler (>= 1.3.0)
|
123
|
+
railties (= 6.0.3.4)
|
124
|
+
sprockets-rails (>= 2.0.0)
|
125
|
+
rails-controller-testing (1.0.5)
|
126
|
+
actionpack (>= 5.0.1.rc1)
|
127
|
+
actionview (>= 5.0.1.rc1)
|
128
|
+
activesupport (>= 5.0.1.rc1)
|
129
|
+
rails-dom-testing (2.0.3)
|
130
|
+
activesupport (>= 4.2.0)
|
131
|
+
nokogiri (>= 1.6)
|
132
|
+
rails-html-sanitizer (1.3.0)
|
133
|
+
loofah (~> 2.3)
|
134
|
+
railties (6.0.3.4)
|
135
|
+
actionpack (= 6.0.3.4)
|
136
|
+
activesupport (= 6.0.3.4)
|
137
|
+
method_source
|
138
|
+
rake (>= 0.8.7)
|
139
|
+
thor (>= 0.20.3, < 2.0)
|
10
140
|
rake (13.0.1)
|
11
|
-
|
12
|
-
|
13
|
-
rspec-expectations (~> 3.9.0)
|
14
|
-
rspec-mocks (~> 3.9.0)
|
15
|
-
rspec-core (3.9.2)
|
141
|
+
regexp_parser (1.8.2)
|
142
|
+
rspec-core (3.9.3)
|
16
143
|
rspec-support (~> 3.9.3)
|
17
144
|
rspec-expectations (3.9.2)
|
18
145
|
diff-lcs (>= 1.2.0, < 2.0)
|
@@ -20,7 +147,46 @@ GEM
|
|
20
147
|
rspec-mocks (3.9.1)
|
21
148
|
diff-lcs (>= 1.2.0, < 2.0)
|
22
149
|
rspec-support (~> 3.9.0)
|
150
|
+
rspec-rails (4.0.1)
|
151
|
+
actionpack (>= 4.2)
|
152
|
+
activesupport (>= 4.2)
|
153
|
+
railties (>= 4.2)
|
154
|
+
rspec-core (~> 3.9)
|
155
|
+
rspec-expectations (~> 3.9)
|
156
|
+
rspec-mocks (~> 3.9)
|
157
|
+
rspec-support (~> 3.9)
|
23
158
|
rspec-support (3.9.3)
|
159
|
+
rubyzip (2.3.0)
|
160
|
+
selenium-webdriver (3.142.7)
|
161
|
+
childprocess (>= 0.5, < 4.0)
|
162
|
+
rubyzip (>= 1.2.2)
|
163
|
+
simplecov (0.19.0)
|
164
|
+
docile (~> 1.1)
|
165
|
+
simplecov-html (~> 0.11)
|
166
|
+
simplecov-html (0.12.2)
|
167
|
+
simplecov-lcov (0.8.0)
|
168
|
+
sprockets (4.0.2)
|
169
|
+
concurrent-ruby (~> 1.0)
|
170
|
+
rack (> 1, < 3)
|
171
|
+
sprockets-rails (3.2.2)
|
172
|
+
actionpack (>= 4.0)
|
173
|
+
activesupport (>= 4.0)
|
174
|
+
sprockets (>= 3.0.0)
|
175
|
+
sqlite3 (1.4.2)
|
176
|
+
thor (1.0.1)
|
177
|
+
thread_safe (0.3.6)
|
178
|
+
tzinfo (1.2.7)
|
179
|
+
thread_safe (~> 0.1)
|
180
|
+
webdrivers (4.4.1)
|
181
|
+
nokogiri (~> 1.6)
|
182
|
+
rubyzip (>= 1.3.0)
|
183
|
+
selenium-webdriver (>= 3.0, < 4.0)
|
184
|
+
websocket-driver (0.7.3)
|
185
|
+
websocket-extensions (>= 0.1.0)
|
186
|
+
websocket-extensions (0.1.5)
|
187
|
+
xpath (3.2.0)
|
188
|
+
nokogiri (~> 1.8)
|
189
|
+
zeitwerk (2.4.0)
|
24
190
|
|
25
191
|
PLATFORMS
|
26
192
|
ruby
|
@@ -28,8 +194,17 @@ PLATFORMS
|
|
28
194
|
DEPENDENCIES
|
29
195
|
abyme!
|
30
196
|
bundler (~> 2.0)
|
197
|
+
capybara
|
198
|
+
database_cleaner-active_record
|
199
|
+
puma
|
200
|
+
rails
|
201
|
+
rails-controller-testing
|
31
202
|
rake (~> 13.0)
|
32
|
-
rspec
|
203
|
+
rspec-rails
|
204
|
+
simplecov
|
205
|
+
simplecov-lcov
|
206
|
+
sqlite3
|
207
|
+
webdrivers
|
33
208
|
|
34
209
|
BUNDLED WITH
|
35
|
-
2.
|
210
|
+
2.1.4
|
data/README.md
CHANGED
@@ -2,6 +2,24 @@
|
|
2
2
|
|
3
3
|
abyme is a modern take on handling dynamic nested forms in Rails 6+ using StimulusJS.
|
4
4
|
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/abyme.svg)](https://badge.fury.io/rb/abyme)
|
6
|
+
![build](https://github.com/bear-in-mind/abyme/workflows/build/badge.svg)
|
7
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/f591a9e00f7cf5188ad5/maintainability)](https://codeclimate.com/github/bear-in-mind/abyme/maintainability)
|
8
|
+
[![Coverage Status](https://coveralls.io/repos/github/bear-in-mind/abyme/badge.svg)](https://coveralls.io/github/bear-in-mind/abyme?branch=master)
|
9
|
+
|
10
|
+
## Disclaimer
|
11
|
+
This project is still a work in progress and subject to change. We would advise not to use it in production code just yet.
|
12
|
+
|
13
|
+
Any enhancement proposition or bug report welcome !
|
14
|
+
|
15
|
+
## Demo app
|
16
|
+
|
17
|
+
![Demo preview](https://res.cloudinary.com/aux-belles-autos/image/upload/v1603040053/abyme-preview.gif)
|
18
|
+
|
19
|
+
Check out our demo app here : https://abyme-demo.herokuapp.com/
|
20
|
+
|
21
|
+
Source code is right here : https://github.com/bear-in-mind/abyme_demo
|
22
|
+
|
5
23
|
## Installation
|
6
24
|
|
7
25
|
Add this line to your application's Gemfile:
|
@@ -13,7 +31,7 @@ gem 'abyme'
|
|
13
31
|
And then execute:
|
14
32
|
|
15
33
|
$ bundle
|
16
|
-
$ yarn add
|
34
|
+
$ yarn add abyme
|
17
35
|
|
18
36
|
|
19
37
|
Assuming you [already installed Stimulus](https://stimulusjs.org/handbook/introduction), add this in `app/javascript/controllers/index.js` :
|
@@ -46,14 +64,14 @@ Let's consider a to-do application with Projects having many Taks, themselves ha
|
|
46
64
|
```ruby
|
47
65
|
# models/project.rb
|
48
66
|
class Project < ApplicationRecord
|
49
|
-
has_many :tasks
|
67
|
+
has_many :tasks
|
50
68
|
validates :title, :description, presence: true
|
51
69
|
end
|
52
70
|
|
53
71
|
# models/task.rb
|
54
72
|
class Task < ApplicationRecord
|
55
73
|
belongs_to :project
|
56
|
-
has_many :comments
|
74
|
+
has_many :comments
|
57
75
|
validates :title, :description, presence: true
|
58
76
|
end
|
59
77
|
|
@@ -63,60 +81,267 @@ class Comment < ApplicationRecord
|
|
63
81
|
validates :content, presence: true
|
64
82
|
end
|
65
83
|
```
|
66
|
-
The end-goal is to be able to create a project along with different tasks, and immediately add comments to some of these tasks ; all
|
67
|
-
What we'll have is a 2-level nested form. Thus, we'll need to
|
84
|
+
The end-goal here is to be able to create a project along with different tasks, and immediately add comments to some of these tasks ; all within a single form.
|
85
|
+
What we'll have is a 2-level nested form. Thus, we'll need to configure our `Project` and `Task` models like so :
|
68
86
|
```ruby
|
69
87
|
# models/project.rb
|
70
88
|
class Project < ApplicationRecord
|
71
89
|
include Abyme::Model
|
72
|
-
|
90
|
+
has_many :tasks, inverse_of: :project
|
91
|
+
# ...
|
73
92
|
abyme_for :tasks
|
74
93
|
end
|
75
94
|
|
76
95
|
# models/task.rb
|
77
96
|
class Task < ApplicationRecord
|
78
97
|
include Abyme::Model
|
79
|
-
|
98
|
+
has_many :comments, inverse_of: :task
|
99
|
+
# ...
|
80
100
|
abyme_for :comments
|
81
101
|
end
|
82
102
|
```
|
103
|
+
Note the use of the `inverse_of` option. It is needed for Rails to effectively associate children to their yet unsaved parent. Have a peek to the bottom of [this page](https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) for more info.
|
83
104
|
|
84
105
|
### Controller
|
85
106
|
Since we're dealing with one form, we're only concerned with one controller : the one the form routes to. In our example, this would be the `ProjectsController`.
|
86
|
-
The only configuration needed here will
|
107
|
+
The only configuration needed here will concern our strong params. Nested attributes require a very specific syntax to white-list the permitted attributes. It looks like this :
|
87
108
|
|
88
109
|
```ruby
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
]
|
110
|
+
def project_params
|
111
|
+
params.require(:project).permit(
|
112
|
+
:title, :description, tasks_attributes: [
|
113
|
+
:id, :title, :description, :_destroy, comments_attributes: [
|
114
|
+
:id, :content, :_destroy
|
95
115
|
]
|
96
|
-
|
97
|
-
|
116
|
+
]
|
117
|
+
)
|
118
|
+
end
|
98
119
|
```
|
99
120
|
A few explanations here.
|
100
121
|
|
101
122
|
* To permit a nested model attributes in your params, you'll need to pass the `association_attributes: [...]` hash at the end of your resource attributes. Key will always be `association_name` followed by `_attributes`, while the value will be an array of symbolized attributes, just like usual.
|
102
123
|
|
103
|
-
**Note**: if your association is a singular one (`has_one` or `belongs_to
|
124
|
+
> **Note**: if your association is a singular one (`has_one` or `belongs_to`) the association will be singular ; if a Project `has_one :owner`, you would then need to pass `owner_attributes: [...]`)
|
125
|
+
|
126
|
+
* You may have remarked the presence of `id` and `_destroy` among those params. These are necessary for edit actions : if you want to allow your users to destroy or update existing records, these are **mandatory**. Otherwise, Rails won't be able to recognize these records as existing ones, and will just create new ones. More info [here](https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).
|
127
|
+
|
128
|
+
## Basic Usage
|
129
|
+
|
130
|
+
Dealing with nested attributes means you'll generally have to handle a few things inside your form:
|
131
|
+
* Display fields for the **persisted records** (here, already existing `:tasks`)
|
132
|
+
* Display fields for the **new records** (future `:tasks` not yet persisted)
|
133
|
+
* A button to **trigger the addition** of fields for a new resource (an `Add a new task` button)
|
134
|
+
* A button to **remove fields** for a given resource (`Remove task`)
|
135
|
+
|
136
|
+
abyme provides helper methods for all these. Here's how our form for `Project` looks like when using default values:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# views/projects/_form.html.erb
|
140
|
+
<%= simple_form_for @project do |f| %>
|
141
|
+
<%= f.input :title %>
|
142
|
+
<%= f.input :description %>
|
143
|
+
<%= f.submit 'Save' %>
|
144
|
+
|
145
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
146
|
+
<%= abyme.records %>
|
147
|
+
<%= abyme.new_records %>
|
148
|
+
<%= add_association %>
|
149
|
+
<% end %>
|
150
|
+
<% end %>
|
151
|
+
```
|
152
|
+
|
153
|
+
`abyme.records` will contain the persisted associations fields, while `abyme.new_records` will contain fields for the new associations. `add_association` will by default generate a button with a text of type "Add `resource_name`". To work properly, this method **has** to be called **inside the block** passed to the `abymize` method.
|
154
|
+
|
155
|
+
Now where's the code for these fields ? abyme will assume a **partial** to be present in the directory `/views/abyme` with a *name respecting this naming convention* (just like with [cocoon](https://github.com/nathanvda/cocoon#basic-usage)): `_singular_association_name_fields.html.erb`.
|
156
|
+
|
157
|
+
This partial might look like this:
|
158
|
+
```ruby
|
159
|
+
# views/abyme/_task_fields.html.erb
|
160
|
+
<%= f.input :title %>
|
161
|
+
<%= f.input :description %>
|
162
|
+
<%= f.hidden_field :_destroy %>
|
163
|
+
|
164
|
+
<%= remove_association(tag: :div) do %>
|
165
|
+
<i class="fas fa-trash"></i>
|
166
|
+
<% end %>
|
167
|
+
```
|
168
|
+
|
169
|
+
Note the presence of the `remove_association` button. Here, we pass it an option to make it a `<div>`, as well as a block to customize its content. Don't forget the `_destroy` attribute, needed to mark items for destruction.
|
170
|
+
|
171
|
+
### What about the controller ?
|
172
|
+
|
173
|
+
What about it ? Well, not much. That's the actual magical thing about `nested_attributes`: once your model is aware of its acceptance of those for a given association, and your strong params are correctly configured, there's nothing else to do.
|
174
|
+
`@project.create(project_params)` is all you'll need to save a project along with its descendants 👨👧👧
|
175
|
+
|
176
|
+
### Auto mode
|
177
|
+
|
178
|
+
Let's now take care of our comments fields. We'll add these using our neat *automatic mode*: just stick this line at the end of the partial:
|
179
|
+
```ruby
|
180
|
+
# views/abyme/_task_fields.html.erb
|
181
|
+
# ... rest of the partial above
|
182
|
+
<%= abymize(:comments, f) %>
|
183
|
+
```
|
184
|
+
Where's the rest of the code ? Well, if the default configuration you saw above in the `_form.html.erb` suits you, and the order in which the different resources appear feels right (persisted first, new fields second, and the 'Add' button last), then you can just spare the block, and it will be taken care of for you. We'll just write our `_comment_fields.html.erb` partial in the `views/abyme` directory and we'll be all set.
|
185
|
+
|
186
|
+
## Advanced usage
|
187
|
+
### Models
|
188
|
+
In models, the `abyme_for :association` acts as an alias for this command :
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
accepts_nested_attributes_for :association, reject_if: :all_blank, :allow_destroy: true
|
192
|
+
```
|
193
|
+
|
194
|
+
Which is the way you would configure `nested_attributes` 90% of the time. Should you want to pass [any available options](https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) to this method or change those, you may just pass them as with the original method :
|
195
|
+
```ruby
|
196
|
+
abyme_for :association, limit: 3, allow_destroy: false
|
197
|
+
```
|
198
|
+
|
199
|
+
### Views
|
200
|
+
|
201
|
+
#### #records
|
202
|
+
A few options can be passed to `abyme.records`:
|
203
|
+
* `collection:` : allows you to pass a collection of your choice to only display specific objects.
|
204
|
+
```ruby
|
205
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
206
|
+
<%= abyme.records(collection: @project.tasks.where(done: false)) %>
|
207
|
+
<%= abyme.new_records %>
|
208
|
+
<%= add_association %>
|
209
|
+
<% end %>
|
210
|
+
```
|
211
|
+
* `order:` : allows you to pass an ActiveRecord `order` method to sort your instances the way you want.
|
212
|
+
```ruby
|
213
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
214
|
+
<%= abyme.records(order: { created_at: :asc }) %>
|
215
|
+
<%= abyme.new_records %>
|
216
|
+
<%= add_association %>
|
217
|
+
<% end %>
|
218
|
+
```
|
219
|
+
* `partial:` : allows you to indicate a custom partial, if one has not already been passed to `abymize`.
|
220
|
+
```ruby
|
221
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
222
|
+
<%= abyme.records %>
|
223
|
+
<%= abyme.new_records(partial: 'projects/task_fields') %>
|
224
|
+
<%= add_association %>
|
225
|
+
<% end %>
|
226
|
+
```
|
227
|
+
* `fields_html:` : gives you the possibility to add any HTML attribute you may want to each set of fields. By default, an `abyme--fields` and an `singular_association-fields` class are already present.
|
228
|
+
```ruby
|
229
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
230
|
+
<%= abyme.records(fields_html: { class: "some-class" }) %>
|
231
|
+
# Every set of persisted fields will have these 3 classes : 'abyme--fields', 'task-fields', and 'some-class'
|
232
|
+
<%= abyme.new_records %>
|
233
|
+
<%= add_association %>
|
234
|
+
<% end %>
|
235
|
+
```
|
236
|
+
* `wrapper_html:` : gives you the possibility to add any HTML attribute you may want to the wrapper containing all fields.
|
237
|
+
```ruby
|
238
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
239
|
+
<%= abyme.records(wrapper_html: { class: "persisted-records" }) %>
|
240
|
+
# The wrapper containing all persisted task fields will have an id "abyme-tasks-wrapper" and a class "persisted-records"
|
241
|
+
<%= abyme.new_records %>
|
242
|
+
<%= add_association %>
|
243
|
+
<% end %>
|
244
|
+
```
|
245
|
+
#### #new_records
|
246
|
+
Here are the options that can be passed to `abyme.new_records`:
|
247
|
+
* `position:` : allows you to specify whether new fields added dynamically should go at the top or at the bottom. `:end` is the default value.
|
248
|
+
```ruby
|
249
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
250
|
+
<%= abyme.records %>
|
251
|
+
<%= abyme.new_records(position: :start) %>
|
252
|
+
<%= add_association %>
|
253
|
+
<% end %>
|
254
|
+
```
|
255
|
+
* `partial:` : same as `#records`
|
256
|
+
* `fields_html:` : same as `#records`
|
257
|
+
* `wrapper_html:` : same as `#records`
|
258
|
+
|
259
|
+
#### #add_association, #remove_association
|
260
|
+
These 2 methods behave the same. Here are their options :
|
261
|
+
* `tag:` : allows you to specify a tag of your choosing, like `:a`, or `:div`. Default is `:button`.
|
262
|
+
* `content:` : the text to display inside the element. Default is `Add association_name`
|
263
|
+
* `html:` : gives you the possibility to add any HTML attribute you may want to the element.
|
264
|
+
```ruby
|
265
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
266
|
+
# ...
|
267
|
+
<%= add_association(tag: :a, content: "Add a super task", html: {id: "add-super-task"}) %>
|
268
|
+
<% end %>
|
269
|
+
```
|
270
|
+
|
271
|
+
As you may have seen above, you can also pass a block to the method to give it whatever HTML content you want :
|
272
|
+
```ruby
|
273
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
274
|
+
# ...
|
275
|
+
<%= add_association(tag: :div, html: {id: "add-super-task", class: "flex"}) do %>
|
276
|
+
<i class="fas fa-plus"></i>
|
277
|
+
<h2>Add a super task</h2>
|
278
|
+
<% end %>
|
279
|
+
<% end %>
|
280
|
+
```
|
104
281
|
|
105
|
-
|
282
|
+
#### #abymize(:association, form_object)
|
283
|
+
This is the container for all your nested fields. It takes two parameters (the symbolized association and the `form_builder`), and some optional ones. Please note an id is automatically added to this element, which value is : `abyme--association`.
|
284
|
+
* `partial:` : allows you to indicate a custom partial path for both `records` and `new_records`
|
285
|
+
```ruby
|
286
|
+
<%= abymize(:tasks, f, partial: 'projects/task_fields') do |abyme| %>
|
287
|
+
<%= abyme.records %>
|
288
|
+
<%= abyme.new_records %>
|
289
|
+
<%= add_association %>
|
290
|
+
<% end %>
|
291
|
+
```
|
292
|
+
* `limit:` : allows you to limit the number of new fields that can be created through JS. If you need to limit the number of associations in database, you will need to add validations. You can also pass an option [in your model as well](https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for).
|
293
|
+
```ruby
|
294
|
+
<%= abymize(:tasks, f, limit: 5) do |abyme| %>
|
295
|
+
# Beyond 5 tasks, the add button won't add any more fields. See events section below to see how to handle the 'abyme:limit-reached' event
|
296
|
+
<%= abyme.records %>
|
297
|
+
<%= abyme.new_records %>
|
298
|
+
<%= add_association %>
|
299
|
+
<% end %>
|
300
|
+
```
|
301
|
+
* `min_count` : by default, there won't be any blank fields added on page load. By passing a `min_count` option, you can set how many empty fields should appear in the form.
|
302
|
+
```ruby
|
303
|
+
<%= abymize(:tasks, f, min_count: 1) do |abyme| %>
|
304
|
+
# 1 blank task will automatically be added to the form.
|
305
|
+
<%= abyme.records %>
|
306
|
+
<%= abyme.new_records %>
|
307
|
+
<%= add_association %>
|
308
|
+
<% end %>
|
309
|
+
```
|
106
310
|
|
107
|
-
|
311
|
+
*When in auto mode*, the abymize method can take a few options:
|
312
|
+
* `button_text:` : this will set the `add_association` button text to the string of your choice.
|
313
|
+
* All options that should be passed to either `records` or `new_records` can be passed here and will be passed down.
|
108
314
|
|
109
|
-
|
315
|
+
## Events
|
316
|
+
This part is still a work in progress and subject to change. We're providing some basic self-explanatory events to attach to. These are emitted by the main container (created by the `abymize` method).
|
317
|
+
|
318
|
+
We're currently thinking about a way to attach to these via Stimulus. Coming soon !
|
319
|
+
|
320
|
+
### Lifecycle events
|
321
|
+
* `abyme:before-add`
|
322
|
+
* `abyme:after-add`
|
323
|
+
* `abyme:before-remove`
|
324
|
+
* `abyme:after-remove`
|
325
|
+
```javascript
|
326
|
+
document.getElementById('abyme--tasks').addEventListener('abyme:before-add', yourCallback)
|
327
|
+
```
|
328
|
+
|
329
|
+
### Other events
|
330
|
+
* `abyme:limit-reached`
|
331
|
+
```javascript
|
332
|
+
const tasksContainer = document.getElementById('abyme--tasks');
|
333
|
+
tasksContainer.addEventListener('abyme:limit-reached', () => {
|
334
|
+
alert('You reached the max number of tasks !')
|
335
|
+
});
|
336
|
+
```
|
110
337
|
|
111
338
|
## Development
|
112
339
|
|
113
340
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
114
341
|
|
115
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
|
-
|
117
342
|
## Contributing
|
118
343
|
|
119
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
344
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bear-in-mind/abyme.
|
120
345
|
|
121
346
|
## License
|
122
347
|
|