abyme 0.1.2 → 0.2.3
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/.gitignore +21 -0
- data/.rspec +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +96 -0
- data/LICENSE.txt +21 -0
- data/README.md +341 -0
- data/Rakefile +28 -0
- data/abyme.gemspec +34 -0
- data/bin/console +14 -0
- data/bin/rails +12 -0
- data/bin/setup +8 -0
- data/javascript/abyme_controller.js +121 -0
- data/javascript/index.js +2 -0
- data/lib/.DS_Store +0 -0
- data/lib/abyme.rb +8 -0
- data/lib/abyme/abyme_builder.rb +32 -0
- data/lib/abyme/engine.rb +12 -0
- data/lib/abyme/model.rb +12 -0
- data/lib/abyme/version.rb +9 -0
- data/lib/abyme/view_helpers.rb +117 -0
- data/lib/generators/.DS_Store +0 -0
- data/lib/generators/abyme/install_generator.rb +25 -0
- data/lib/generators/abyme/templates/abyme_controller.js +17 -0
- data/package.json +25 -0
- data/yarn.lock +35 -0
- metadata +65 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c47483c64b5a49b57f83cf3e779b089aff6cfb218a6cc4cc56c35b7cf14ea44a
|
4
|
+
data.tar.gz: ad6152628fe3731f04a19bf3afabf45f1f9c2fc92f7af2637ce990cd8ad801e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fb02ef0524a28ae5a7445c1f51d3144e1ae11b4bbe710dd5e4d87b8a767d4b0e598f512293892cc114f06e492c73074a4c409a902a2911e29b0329bdf0a1a96
|
7
|
+
data.tar.gz: 6bb56a17944fd6f4577580bc19cb5a004903e7dc149c65aedbc3d6e34469565c66f26318147cdb7cde107d7adc06ad3176b875557c6cb8ef8412489d3131ea09
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
yarn-error.log
|
10
|
+
/node_modules
|
11
|
+
|
12
|
+
# rspec failure tracking
|
13
|
+
.rspec_status
|
14
|
+
node_modules
|
15
|
+
|
16
|
+
# Dummy app
|
17
|
+
spec/dummy/db/*.sqlite3
|
18
|
+
spec/dummy/db/*.sqlite3-journal
|
19
|
+
spec/dummy/db/log/*.log
|
20
|
+
spec/dummy/tmp/
|
21
|
+
spec/dummy/.sass-cache
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require rails_helper
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
abyme (0.2.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (6.0.3.4)
|
10
|
+
actionview (= 6.0.3.4)
|
11
|
+
activesupport (= 6.0.3.4)
|
12
|
+
rack (~> 2.0, >= 2.0.8)
|
13
|
+
rack-test (>= 0.6.3)
|
14
|
+
rails-dom-testing (~> 2.0)
|
15
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
16
|
+
actionview (6.0.3.4)
|
17
|
+
activesupport (= 6.0.3.4)
|
18
|
+
builder (~> 3.1)
|
19
|
+
erubi (~> 1.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
22
|
+
activesupport (6.0.3.4)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 0.7, < 2)
|
25
|
+
minitest (~> 5.1)
|
26
|
+
tzinfo (~> 1.1)
|
27
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
28
|
+
builder (3.2.4)
|
29
|
+
concurrent-ruby (1.1.7)
|
30
|
+
crass (1.0.6)
|
31
|
+
diff-lcs (1.4.4)
|
32
|
+
erubi (1.9.0)
|
33
|
+
i18n (1.8.5)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
loofah (2.7.0)
|
36
|
+
crass (~> 1.0.2)
|
37
|
+
nokogiri (>= 1.5.9)
|
38
|
+
method_source (1.0.0)
|
39
|
+
mini_portile2 (2.4.0)
|
40
|
+
minitest (5.14.2)
|
41
|
+
nokogiri (1.10.10)
|
42
|
+
mini_portile2 (~> 2.4.0)
|
43
|
+
rack (2.2.3)
|
44
|
+
rack-test (1.1.0)
|
45
|
+
rack (>= 1.0, < 3)
|
46
|
+
rails-dom-testing (2.0.3)
|
47
|
+
activesupport (>= 4.2.0)
|
48
|
+
nokogiri (>= 1.6)
|
49
|
+
rails-dummy (0.1.0)
|
50
|
+
railties
|
51
|
+
rails-html-sanitizer (1.3.0)
|
52
|
+
loofah (~> 2.3)
|
53
|
+
railties (6.0.3.4)
|
54
|
+
actionpack (= 6.0.3.4)
|
55
|
+
activesupport (= 6.0.3.4)
|
56
|
+
method_source
|
57
|
+
rake (>= 0.8.7)
|
58
|
+
thor (>= 0.20.3, < 2.0)
|
59
|
+
rake (13.0.1)
|
60
|
+
rspec-core (3.9.3)
|
61
|
+
rspec-support (~> 3.9.3)
|
62
|
+
rspec-expectations (3.9.2)
|
63
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
64
|
+
rspec-support (~> 3.9.0)
|
65
|
+
rspec-mocks (3.9.1)
|
66
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
67
|
+
rspec-support (~> 3.9.0)
|
68
|
+
rspec-rails (4.0.1)
|
69
|
+
actionpack (>= 4.2)
|
70
|
+
activesupport (>= 4.2)
|
71
|
+
railties (>= 4.2)
|
72
|
+
rspec-core (~> 3.9)
|
73
|
+
rspec-expectations (~> 3.9)
|
74
|
+
rspec-mocks (~> 3.9)
|
75
|
+
rspec-support (~> 3.9)
|
76
|
+
rspec-support (3.9.3)
|
77
|
+
sqlite3 (1.4.2)
|
78
|
+
thor (1.0.1)
|
79
|
+
thread_safe (0.3.6)
|
80
|
+
tzinfo (1.2.7)
|
81
|
+
thread_safe (~> 0.1)
|
82
|
+
zeitwerk (2.4.0)
|
83
|
+
|
84
|
+
PLATFORMS
|
85
|
+
ruby
|
86
|
+
|
87
|
+
DEPENDENCIES
|
88
|
+
abyme!
|
89
|
+
bundler (~> 2.0)
|
90
|
+
rails-dummy
|
91
|
+
rake (~> 13.0)
|
92
|
+
rspec-rails
|
93
|
+
sqlite3
|
94
|
+
|
95
|
+
BUNDLED WITH
|
96
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Louis Sommer
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,341 @@
|
|
1
|
+
# Abyme 🕳
|
2
|
+
|
3
|
+
abyme is a modern take on handling dynamic nested forms in Rails 6+ using StimulusJS.
|
4
|
+
|
5
|
+
## Disclaimer
|
6
|
+
This project is still a work in progress and subject to change. We encourage not to use it in production code just yet.
|
7
|
+
|
8
|
+
Any enhancement proposition or bug report welcome !
|
9
|
+
|
10
|
+
General remarks :
|
11
|
+
* A demo app will soon be online.
|
12
|
+
* For now, the gem is tested through our demo app. Specific autonomous tests will be transfered/written in the following days.
|
13
|
+
* Help is very much wanted on the Events part of the gem (see bottom of this documentation)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'abyme'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
$ yarn add abyme
|
27
|
+
|
28
|
+
|
29
|
+
Assuming you [already installed Stimulus](https://stimulusjs.org/handbook/introduction), add this in `app/javascript/controllers/index.js` :
|
30
|
+
```javascript
|
31
|
+
// app/javascript/controllers/index.js
|
32
|
+
import { Application } from "stimulus"
|
33
|
+
import { definitionsFromContext } from "stimulus/webpack-helpers"
|
34
|
+
// Add this line below
|
35
|
+
import { AbymeController } from 'abyme'
|
36
|
+
|
37
|
+
const application = Application.start()
|
38
|
+
const context = require.context("controllers", true, /_controller\.js$/)
|
39
|
+
application.load(definitionsFromContext(context))
|
40
|
+
// And this one
|
41
|
+
application.register('abyme', AbymeController)
|
42
|
+
```
|
43
|
+
|
44
|
+
## What are nested forms and why a new gem ?
|
45
|
+
|
46
|
+
Nested forms (or more accurately *nested fields* or *nested attributes*) are forms that deal with associated models. Let's picture a `Project` model that `has_many :tasks`. A nested form will allow you to create a project along with one or several tasks **within a single form**. If `Tasks` were to have associations on their own, like `:comments`, you could also, still in the same form, instantiate comments along with their parent models.
|
47
|
+
|
48
|
+
Rails provides [its own helper](https://api.rubyonrails.org/v6.0.1/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for) to handle nested attributes. **abyme** is basically a smart wrapper around it, offering easier syntax along with some fancy additions. To work properly, some configuration will be required in both models and controllers (see below).
|
49
|
+
|
50
|
+
What Rails doesn't provide natively is the possibility to **dynamically add new associations on the fly**, which requires Javascript implementation. What this means it that you would normally have to know in advance how many fields you'd like to display (1, 2 or any number of `:tasks`), which isn't very usable in this day and age. This is what the [cocoon gem](https://github.com/nathanvda/cocoon) has been helping with for the past 7 years. This gem still being implemented in JQuery (which [Rails dropped as a dependency](https://github.com/rails/rails/issues/25208)), we wanted to propose a more plug'n'play approach, using Basecamp's [Stimulus](https://stimulusjs.org/) instead.
|
51
|
+
|
52
|
+
## Basic Configuration
|
53
|
+
|
54
|
+
### Models
|
55
|
+
Let's consider a to-do application with Projects having many Taks, themselves having many Comments.
|
56
|
+
```ruby
|
57
|
+
# models/project.rb
|
58
|
+
class Project < ApplicationRecord
|
59
|
+
has_many :tasks
|
60
|
+
validates :title, :description, presence: true
|
61
|
+
end
|
62
|
+
|
63
|
+
# models/task.rb
|
64
|
+
class Task < ApplicationRecord
|
65
|
+
belongs_to :project
|
66
|
+
has_many :comments
|
67
|
+
validates :title, :description, presence: true
|
68
|
+
end
|
69
|
+
|
70
|
+
# models/comment.rb
|
71
|
+
class Comment < ApplicationRecord
|
72
|
+
belongs_to :task
|
73
|
+
validates :content, presence: true
|
74
|
+
end
|
75
|
+
```
|
76
|
+
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.
|
77
|
+
What we'll have is a 2-level nested form. Thus, we'll need to configure our `Project` and `Task` models like so :
|
78
|
+
```ruby
|
79
|
+
# models/project.rb
|
80
|
+
class Project < ApplicationRecord
|
81
|
+
include Abyme::Model
|
82
|
+
has_many :tasks, inverse_of: :project
|
83
|
+
# ...
|
84
|
+
abyme_for :tasks
|
85
|
+
end
|
86
|
+
|
87
|
+
# models/task.rb
|
88
|
+
class Task < ApplicationRecord
|
89
|
+
include Abyme::Model
|
90
|
+
has_many :comments, inverse_of: :task
|
91
|
+
# ...
|
92
|
+
abyme_for :comments
|
93
|
+
end
|
94
|
+
```
|
95
|
+
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.
|
96
|
+
|
97
|
+
### Controller
|
98
|
+
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`.
|
99
|
+
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 :
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
def project_params
|
103
|
+
params.require(:project).permit(
|
104
|
+
:title, :description, tasks_attributes: [
|
105
|
+
:id, :title, :description, :_destroy, comments_attributes: [
|
106
|
+
:id, :content, :_destroy
|
107
|
+
]
|
108
|
+
]
|
109
|
+
)
|
110
|
+
end
|
111
|
+
```
|
112
|
+
A few explanations here.
|
113
|
+
|
114
|
+
* 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.
|
115
|
+
|
116
|
+
> **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: [...]`)
|
117
|
+
|
118
|
+
* 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).
|
119
|
+
|
120
|
+
## Basic Usage
|
121
|
+
|
122
|
+
Dealing with nested attributes means you'll generally have to handle a few things inside your form:
|
123
|
+
* Display fields for the **persisted records** (here, already existing `:tasks`)
|
124
|
+
* Display fields for the **new records** (future `:tasks` not yet persisted)
|
125
|
+
* A button to **trigger the addition** of fields for a new resource (an `Add a new task` button)
|
126
|
+
* A button to **remove fields** for a given resource (`Remove task`)
|
127
|
+
|
128
|
+
abyme provides helper methods for all these. Here's how our form for `Project` looks like when using default values:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
# views/projects/_form.html.erb
|
132
|
+
<%= simple_form_for @project do |f| %>
|
133
|
+
<%= f.input :title %>
|
134
|
+
<%= f.input :description %>
|
135
|
+
<%= f.submit 'Save' %>
|
136
|
+
|
137
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
138
|
+
<%= abyme.records %>
|
139
|
+
<%= abyme.new_records %>
|
140
|
+
<%= add_association %>
|
141
|
+
<% end %>
|
142
|
+
<% end %>
|
143
|
+
```
|
144
|
+
|
145
|
+
`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.
|
146
|
+
|
147
|
+
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`.
|
148
|
+
|
149
|
+
This partial might look like this:
|
150
|
+
```ruby
|
151
|
+
# views/abyme/_task_fields.html.erb
|
152
|
+
<%= f.input :title %>
|
153
|
+
<%= f.input :description %>
|
154
|
+
<%= f.hidden_field :_destroy %>
|
155
|
+
|
156
|
+
<%= remove_association(tag: :div) do %>
|
157
|
+
<i class="fas fa-trash"></i>
|
158
|
+
<% end %>
|
159
|
+
```
|
160
|
+
|
161
|
+
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.
|
162
|
+
|
163
|
+
### What about the controller ?
|
164
|
+
|
165
|
+
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.
|
166
|
+
`@project.create(project_params)` is all you'll need to save a project along with its descendants 👨👧👧
|
167
|
+
|
168
|
+
### Auto mode
|
169
|
+
|
170
|
+
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:
|
171
|
+
```ruby
|
172
|
+
# views/abyme/_task_fields.html.erb
|
173
|
+
# ... rest of the partial above
|
174
|
+
<%= abymize(:comments, f) %>
|
175
|
+
```
|
176
|
+
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.
|
177
|
+
|
178
|
+
## Advanced usage
|
179
|
+
### Models
|
180
|
+
In models, the `abyme_for :association` acts as an alias for this command :
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
accepts_nested_attributes_for :association, reject_if: :all_blank, :allow_destroy: true
|
184
|
+
```
|
185
|
+
|
186
|
+
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 :
|
187
|
+
```ruby
|
188
|
+
abyme_for :association, limit: 3, allow_destroy: false
|
189
|
+
```
|
190
|
+
|
191
|
+
### Views
|
192
|
+
|
193
|
+
#### #records
|
194
|
+
A few options can be passed to `abyme.records`:
|
195
|
+
* `collection:` : allows you to pass a collection of your choice to only display specific objects.
|
196
|
+
```ruby
|
197
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
198
|
+
<%= abyme.records(collection: @project.tasks.where(done: false)) %>
|
199
|
+
<%= abyme.new_records %>
|
200
|
+
<%= add_association %>
|
201
|
+
<% end %>
|
202
|
+
```
|
203
|
+
* `order:` : allows you to pass an ActiveRecord `order` method to sort your instances the way you want.
|
204
|
+
```ruby
|
205
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
206
|
+
<%= abyme.records(order: { created_at: :asc }) %>
|
207
|
+
<%= abyme.new_records %>
|
208
|
+
<%= add_association %>
|
209
|
+
<% end %>
|
210
|
+
```
|
211
|
+
* `partial:` : allows you to indicate a custom partial, if one has not already been passed to `abymize`.
|
212
|
+
```ruby
|
213
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
214
|
+
<%= abyme.records %>
|
215
|
+
<%= abyme.new_records(partial: 'projects/task_fields') %>
|
216
|
+
<%= add_association %>
|
217
|
+
<% end %>
|
218
|
+
```
|
219
|
+
* `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.
|
220
|
+
```ruby
|
221
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
222
|
+
<%= abyme.records(fields_html: { class: "some-class" }) %>
|
223
|
+
# Every set of persisted fields will have these 3 classes : 'abyme--fields', 'task-fields', and 'some-class'
|
224
|
+
<%= abyme.new_records %>
|
225
|
+
<%= add_association %>
|
226
|
+
<% end %>
|
227
|
+
```
|
228
|
+
* `wrapper_html:` : gives you the possibility to add any HTML attribute you may want to the wrapper containing all fields.
|
229
|
+
```ruby
|
230
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
231
|
+
<%= abyme.records(wrapper_html: { class: "persisted-records" }) %>
|
232
|
+
# The wrapper containing all persisted task fields will have an id "abyme-tasks-wrapper" and a class "persisted-records"
|
233
|
+
<%= abyme.new_records %>
|
234
|
+
<%= add_association %>
|
235
|
+
<% end %>
|
236
|
+
```
|
237
|
+
#### #new_records
|
238
|
+
Here are the options that can be passed to `abyme.new_records`:
|
239
|
+
* `position:` : allows you to specify whether new fields added dynamically should go at the top or at the bottom. `:end` is the default value.
|
240
|
+
```ruby
|
241
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
242
|
+
<%= abyme.records %>
|
243
|
+
<%= abyme.new_records(position: :start) %>
|
244
|
+
<%= add_association %>
|
245
|
+
<% end %>
|
246
|
+
```
|
247
|
+
* `partial:` : same as `#records`
|
248
|
+
* `fields_html:` : same as `#records`
|
249
|
+
* `wrapper_html:` : same as `#records`
|
250
|
+
|
251
|
+
#### #add_association, #remove_association
|
252
|
+
These 2 methods behave the same. Here are their options :
|
253
|
+
* `tag:` : allows you to specify a tag of your choosing, like `:a`, or `:div`. Default is `:button`.
|
254
|
+
* `content:` : the text to display inside the element. Default is `Add association_name`
|
255
|
+
* `html:` : gives you the possibility to add any HTML attribute you may want to the element.
|
256
|
+
```ruby
|
257
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
258
|
+
# ...
|
259
|
+
<%= add_association(tag: :a, content: "Add a super task", html: {id: "add-super-task"}) %>
|
260
|
+
<% end %>
|
261
|
+
```
|
262
|
+
|
263
|
+
As you may have seen above, you can also pass a block to the method to give it whatever HTML content you want :
|
264
|
+
```ruby
|
265
|
+
<%= abymize(:tasks, f) do |abyme| %>
|
266
|
+
# ...
|
267
|
+
<%= add_association(tag: :div, html: {id: "add-super-task", class: "flex"}) do %>
|
268
|
+
<i class="fas fa-plus"></i>
|
269
|
+
<h2>Add a super task</h2>
|
270
|
+
<% end %>
|
271
|
+
<% end %>
|
272
|
+
```
|
273
|
+
|
274
|
+
|
275
|
+
#### #abymize(:association, form_object)
|
276
|
+
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`.
|
277
|
+
* `partial:` : allows you to indicate a custom partial path for both `records` and `new_records`
|
278
|
+
```ruby
|
279
|
+
<%= abymize(:tasks, f, partial: 'projects/task_fields') do |abyme| %>
|
280
|
+
<%= abyme.records %>
|
281
|
+
<%= abyme.new_records %>
|
282
|
+
<%= add_association %>
|
283
|
+
<% end %>
|
284
|
+
```
|
285
|
+
* `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).
|
286
|
+
```ruby
|
287
|
+
<%= abymize(:tasks, f, limit: 5) do |abyme| %>
|
288
|
+
# 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
|
289
|
+
<%= abyme.records %>
|
290
|
+
<%= abyme.new_records %>
|
291
|
+
<%= add_association %>
|
292
|
+
<% end %>
|
293
|
+
```
|
294
|
+
* `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.
|
295
|
+
```ruby
|
296
|
+
<%= abymize(:tasks, f, min_count: 1) do |abyme| %>
|
297
|
+
# 1 blank task will automatically be added to the form.
|
298
|
+
<%= abyme.records %>
|
299
|
+
<%= abyme.new_records %>
|
300
|
+
<%= add_association %>
|
301
|
+
<% end %>
|
302
|
+
```
|
303
|
+
|
304
|
+
*When in auto mode*, the abymize method can take a few options:
|
305
|
+
* `button_text:` : this will set the `add_association` button text to the string of your choice.
|
306
|
+
* All options that should be passed to either `records` or `new_records` can be passed here and will be passed down.
|
307
|
+
|
308
|
+
## Events
|
309
|
+
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).
|
310
|
+
|
311
|
+
We're currently thinking about a way to attach to these via Stimulus. Coming soon !
|
312
|
+
|
313
|
+
### Lifecycle events
|
314
|
+
* `abyme:before-add`
|
315
|
+
* `abyme:after-add`
|
316
|
+
* `abyme:before-remove`
|
317
|
+
* `abyme:after-remove`
|
318
|
+
```javascript
|
319
|
+
document.getElementById('abyme--tasks').addEventListener('abyme:before-add', yourCallback)
|
320
|
+
```
|
321
|
+
|
322
|
+
### Other events
|
323
|
+
* `abyme:limit-reached`
|
324
|
+
```javascript
|
325
|
+
const tasksContainer = document.getElementById('abyme--tasks');
|
326
|
+
tasksContainer.addEventListener('abyme:limit-reached', () => {
|
327
|
+
alert('You reached the max number of tasks !')
|
328
|
+
});
|
329
|
+
```
|
330
|
+
|
331
|
+
## Development
|
332
|
+
|
333
|
+
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.
|
334
|
+
|
335
|
+
## Contributing
|
336
|
+
|
337
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bear-in-mind/abyme.
|
338
|
+
|
339
|
+
## License
|
340
|
+
|
341
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require 'rails/dummy/tasks'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
|
9
|
+
load 'rails/tasks/engine.rake'
|
10
|
+
load 'rails/tasks/statistics.rake'
|
11
|
+
|
12
|
+
Bundler::GemHelper.install_tasks
|
13
|
+
|
14
|
+
begin
|
15
|
+
require 'bundler/setup'
|
16
|
+
rescue LoadError
|
17
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rdoc/task'
|
21
|
+
|
22
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
23
|
+
rdoc.rdoc_dir = 'rdoc'
|
24
|
+
rdoc.title = 'Abyme'
|
25
|
+
rdoc.options << '--line-numbers'
|
26
|
+
rdoc.rdoc_files.include('README.md')
|
27
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
|
+
end
|
data/abyme.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "abyme/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "abyme"
|
7
|
+
spec.version = Abyme::VERSION::STRING
|
8
|
+
spec.authors = ["Romain Sanson", "Louis Sommer"]
|
9
|
+
spec.email = ["louis.sommer@hey.com"]
|
10
|
+
|
11
|
+
spec.summary = "abyme is the modern way to handle dynamic nested forms in Rails 6+."
|
12
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
13
|
+
spec.homepage = "https://github.com/bear-in-mind/abyme"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/bear-in-mind/abyme"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/bear-in-mind/abyme"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
30
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
31
|
+
spec.add_development_dependency "rspec-rails"
|
32
|
+
spec.add_development_dependency "rails-dummy"
|
33
|
+
spec.add_development_dependency "sqlite3"
|
34
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "abyme"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/rails
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application.
|
3
|
+
|
4
|
+
ENGINE_ROOT = File.expand_path('..', __dir__)
|
5
|
+
ENGINE_PATH = File.expand_path('../lib/abyme/engine', __dir__)
|
6
|
+
|
7
|
+
# Set up gems listed in the Gemfile.
|
8
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
9
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
10
|
+
|
11
|
+
require 'rails/all'
|
12
|
+
require 'rails/engine/commands'
|
data/bin/setup
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
import { Controller } from 'stimulus';
|
2
|
+
|
3
|
+
export default class extends Controller {
|
4
|
+
static targets = ['template', 'associations', 'fields', 'newFields'];
|
5
|
+
|
6
|
+
connect() {
|
7
|
+
if (this.count) {
|
8
|
+
this.addDefaultAssociations();
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
get count() {
|
13
|
+
return this.element.dataset.minCount || 0;
|
14
|
+
}
|
15
|
+
|
16
|
+
get position() {
|
17
|
+
return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';
|
18
|
+
}
|
19
|
+
|
20
|
+
add_association(event) {
|
21
|
+
if (event) {
|
22
|
+
event.preventDefault();
|
23
|
+
}
|
24
|
+
// check for limit reached
|
25
|
+
if (this.element.dataset.limit && this.limit_check()) {
|
26
|
+
this.create_event('limit-reached')
|
27
|
+
return false
|
28
|
+
}
|
29
|
+
|
30
|
+
const html = this.build_html();
|
31
|
+
this.create_event('before-add');
|
32
|
+
this.associationsTarget.insertAdjacentHTML(this.position, html);
|
33
|
+
this.create_event('after-add');
|
34
|
+
}
|
35
|
+
|
36
|
+
remove_association(event) {
|
37
|
+
event.preventDefault();
|
38
|
+
this.create_event('before-remove');
|
39
|
+
this.mark_for_destroy(event);
|
40
|
+
this.create_event('after-remove');
|
41
|
+
}
|
42
|
+
|
43
|
+
// LIFECYCLE EVENTS RELATED
|
44
|
+
|
45
|
+
create_event(stage, html = null) {
|
46
|
+
const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });
|
47
|
+
this.element.dispatchEvent(event);
|
48
|
+
// WIP
|
49
|
+
this.dispatch(event, stage);
|
50
|
+
}
|
51
|
+
|
52
|
+
// WIP : Trying to integrate event handling through controller inheritance
|
53
|
+
dispatch(event, stage) {
|
54
|
+
if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);
|
55
|
+
if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);
|
56
|
+
if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);
|
57
|
+
if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);
|
58
|
+
}
|
59
|
+
|
60
|
+
abymeBeforeAdd(event) {
|
61
|
+
}
|
62
|
+
|
63
|
+
abymeAfterAdd(event) {
|
64
|
+
}
|
65
|
+
|
66
|
+
abymeBeforeRemove(event) {
|
67
|
+
}
|
68
|
+
|
69
|
+
abymeAfterRemove(event) {
|
70
|
+
}
|
71
|
+
|
72
|
+
// UTILITIES
|
73
|
+
|
74
|
+
// build html
|
75
|
+
build_html() {
|
76
|
+
let html = this.templateTarget.innerHTML.replace(
|
77
|
+
/NEW_RECORD/g,
|
78
|
+
new Date().getTime()
|
79
|
+
);
|
80
|
+
|
81
|
+
if (html.match(/<template[\s\S]+<\/template>/)) {
|
82
|
+
const template = html
|
83
|
+
.match(/<template[\s\S]+<\/template>/)[0]
|
84
|
+
.replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g, `[NEW_RECORD]$2`);
|
85
|
+
|
86
|
+
html = html.replace(/<template[\s\S]+<\/template>/g, template);
|
87
|
+
}
|
88
|
+
|
89
|
+
return html;
|
90
|
+
}
|
91
|
+
|
92
|
+
// mark association for destroy
|
93
|
+
mark_for_destroy(event) {
|
94
|
+
let item = event.target.closest('.abyme--fields');
|
95
|
+
item.querySelector("input[name*='_destroy']").value = 1;
|
96
|
+
item.style.display = 'none';
|
97
|
+
item.classList.add('abyme--marked-for-destroy')
|
98
|
+
}
|
99
|
+
|
100
|
+
// check if associations limit is reached
|
101
|
+
limit_check() {
|
102
|
+
return (this.newFieldsTargets
|
103
|
+
.filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length
|
104
|
+
>= parseInt(this.element.dataset.limit)
|
105
|
+
}
|
106
|
+
|
107
|
+
// Add default blank associations at page load
|
108
|
+
async addDefaultAssociations() {
|
109
|
+
let i = 0
|
110
|
+
while (i < this.count) {
|
111
|
+
this.add_association()
|
112
|
+
i++
|
113
|
+
// Sleep function to ensure uniqueness of timestamp
|
114
|
+
await this.sleep(1);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
sleep(ms) {
|
119
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
120
|
+
}
|
121
|
+
}
|
data/javascript/index.js
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/abyme.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Abyme
|
2
|
+
class AbymeBuilder < ActionView::Base
|
3
|
+
include ActionView
|
4
|
+
|
5
|
+
def initialize(association:, form:, lookup_context:, partial:, &block)
|
6
|
+
@association = association
|
7
|
+
@form = form
|
8
|
+
@lookup_context = lookup_context
|
9
|
+
@partial = partial
|
10
|
+
yield(self) if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
def records(options = {})
|
14
|
+
persisted_records_for(@association, @form, options) do |fields_for_association|
|
15
|
+
render_association_partial(fields_for_association, options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def new_records(options = {}, &block)
|
20
|
+
new_records_for(@association, @form, options) do |fields_for_association|
|
21
|
+
render_association_partial(fields_for_association, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def render_association_partial(fields, options)
|
28
|
+
partial = @partial || options[:partial] || "abyme/#{@association.to_s.singularize}_fields"
|
29
|
+
ActionController::Base.render(partial: partial, locals: { f: fields })
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/abyme/engine.rb
ADDED
data/lib/abyme/model.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module Abyme
|
2
|
+
module Model
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def abyme_for(association, options = {})
|
7
|
+
default_options = {reject_if: :all_blank, allow_destroy: true}
|
8
|
+
accepts_nested_attributes_for association, default_options.merge(options)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require_relative "abyme_builder"
|
2
|
+
|
3
|
+
module Abyme
|
4
|
+
module ViewHelpers
|
5
|
+
|
6
|
+
def abymize(association, form, options = {}, &block)
|
7
|
+
content_tag(:div, data: { controller: 'abyme', limit: options[:limit], min_count: options[:min_count] }, id: "abyme--#{association}") do
|
8
|
+
if block_given?
|
9
|
+
yield(Abyme::AbymeBuilder.new(
|
10
|
+
association: association, form: form, lookup_context: self.lookup_context, partial: options[:partial]
|
11
|
+
)
|
12
|
+
)
|
13
|
+
else
|
14
|
+
model = association.to_s.singularize.classify.constantize
|
15
|
+
concat(persisted_records_for(association, form, options))
|
16
|
+
concat(new_records_for(association, form, options))
|
17
|
+
concat(add_association(content: options[:button_text] || "Add #{model}"))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def new_records_for(association, form, options = {}, &block)
|
23
|
+
options[:wrapper_html] ||= {}
|
24
|
+
|
25
|
+
wrapper_default = {
|
26
|
+
data: {
|
27
|
+
target: 'abyme.associations',
|
28
|
+
association: association,
|
29
|
+
abyme_position: options[:position] || :end
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
fields_default = { data: { target: 'abyme.fields abyme.newFields' } }
|
34
|
+
|
35
|
+
content_tag(:div, build_attributes(wrapper_default, options[:wrapper_html])) do
|
36
|
+
content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: { target: 'abyme.template' }) do
|
37
|
+
form.fields_for association, association.to_s.classify.constantize.new, child_index: 'NEW_RECORD' do |f|
|
38
|
+
content_tag(:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
|
39
|
+
# Here, if a block is passed, we're passing the association fields to it, rather than the form itself
|
40
|
+
block_given? ? yield(f) : render(options[:partial] || "abyme/#{association.to_s.singularize}_fields", f: f)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def persisted_records_for(association, form, options = {})
|
48
|
+
records = options[:collection] || form.object.send(association)
|
49
|
+
options[:wrapper_html] ||= {}
|
50
|
+
fields_default = { data: { target: 'abyme.fields' } }
|
51
|
+
|
52
|
+
if options[:order].present?
|
53
|
+
records = records.order(options[:order])
|
54
|
+
# Get invalid records
|
55
|
+
invalids = form.object.send(association).reject(&:persisted?)
|
56
|
+
records = records.to_a.concat(invalids) if invalids.any?
|
57
|
+
end
|
58
|
+
|
59
|
+
content_tag(:div, options[:wrapper_html]) do
|
60
|
+
form.fields_for(association, records) do |f|
|
61
|
+
content_tag(:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
|
62
|
+
block_given? ? yield(f) : render(options[:partial] || "abyme/#{association.to_s.singularize}_fields", f: f)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def add_association(options = {}, &block)
|
69
|
+
action = 'click->abyme#add_association'
|
70
|
+
create_button(action, options, &block)
|
71
|
+
end
|
72
|
+
|
73
|
+
def remove_association(options = {}, &block)
|
74
|
+
action = 'click->abyme#remove_association'
|
75
|
+
create_button(action, options, &block)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def create_button(action, options, &block)
|
81
|
+
options[:html] ||= {}
|
82
|
+
options[:tag] ||= :button
|
83
|
+
options[:content] ||= 'Add Association'
|
84
|
+
|
85
|
+
if block_given?
|
86
|
+
content_tag(options[:tag], { data: { action: action } }.merge(options[:html])) do
|
87
|
+
capture(&block)
|
88
|
+
end
|
89
|
+
else
|
90
|
+
content_tag(options[:tag], options[:content], { data: { action: action } }.merge(options[:html]))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def basic_fields_markup(html, association = nil)
|
95
|
+
if html && html[:class]
|
96
|
+
html[:class] = "abyme--fields #{association.to_s.singularize}-fields #{html[:class]}"
|
97
|
+
else
|
98
|
+
html ||= {}
|
99
|
+
html[:class] = "abyme--fields #{association.to_s.singularize}-fields"
|
100
|
+
end
|
101
|
+
html
|
102
|
+
end
|
103
|
+
|
104
|
+
def build_attributes(default, attr)
|
105
|
+
# ADD NEW DATA ATTRIBUTES VALUES TO THE DEFAULT ONES (ONLY VALUES)
|
106
|
+
if attr[:data]
|
107
|
+
default[:data].each do |key, value|
|
108
|
+
default[:data][key] = "#{value} #{attr[:data][key]}".strip
|
109
|
+
end
|
110
|
+
# ADD NEW DATA ATTRIBUTES (KEYS & VALUES)
|
111
|
+
default[:data] = default[:data].merge(attr[:data].reject { |key, _| default[:data][key] })
|
112
|
+
end
|
113
|
+
# MERGE THE DATA ATTRIBUTES TO THE HASH OF HTML ATTRIBUTES
|
114
|
+
default.merge(attr.reject { |key, _| key == :data })
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
Binary file
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# require 'rails/generators'
|
2
|
+
# require 'json'
|
3
|
+
|
4
|
+
# module Abyme
|
5
|
+
# module Generators
|
6
|
+
# class InstallGenerator < Rails::Generators::Base
|
7
|
+
# source_root File.expand_path("templates", __dir__)
|
8
|
+
|
9
|
+
# def setup
|
10
|
+
# # Creating stimulus abyme_controller.js file
|
11
|
+
# # ==========================================
|
12
|
+
# template "abyme_controller.js", "app/javascript/controllers/abyme_controller.js"
|
13
|
+
# add_stimulus
|
14
|
+
# end
|
15
|
+
|
16
|
+
# def add_stimulus
|
17
|
+
# # Checking if stimulus is present in package.json => yarn add if it's not
|
18
|
+
# # =======================================================================
|
19
|
+
# package = JSON.parse(File.open('package.json').read)
|
20
|
+
# exec('yarn add stimulus') if !package['dependencies'].keys.include?('stimulus')
|
21
|
+
# end
|
22
|
+
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
# end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { Controller } from 'stimulus';
|
2
|
+
|
3
|
+
export default class extends Controller {
|
4
|
+
connect() {
|
5
|
+
console.log('Abyme Controller Connected');
|
6
|
+
}
|
7
|
+
|
8
|
+
add_association(event) {
|
9
|
+
event.preventDefault();
|
10
|
+
console.log('Add Association');
|
11
|
+
}
|
12
|
+
|
13
|
+
remove_association(event) {
|
14
|
+
event.preventDefault();
|
15
|
+
console.log('Remove Association');
|
16
|
+
}
|
17
|
+
}
|
data/package.json
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
{
|
2
|
+
"name": "abyme",
|
3
|
+
"version": "0.1.2",
|
4
|
+
"description": "JS companion to abyme gem",
|
5
|
+
"main": "javascript/index.js",
|
6
|
+
"files": [
|
7
|
+
"package.json",
|
8
|
+
"javascript/**/*"
|
9
|
+
],
|
10
|
+
"directories": {
|
11
|
+
"lib": "javascript"
|
12
|
+
},
|
13
|
+
"repository": {
|
14
|
+
"url": "https://github.com/bear-in-mind/abyme",
|
15
|
+
"type": "git"
|
16
|
+
},
|
17
|
+
"author": "Romain Sanson <romain.sanson@hey.com> & Louis Sommer <louis.sommer@hey.com>",
|
18
|
+
"license": "MIT",
|
19
|
+
"private": false,
|
20
|
+
"homepage": "https://github.com/bear-in-mind/abyme",
|
21
|
+
"bugs": "https://github.com/bear-in-mind/abyme/issues",
|
22
|
+
"dependencies": {
|
23
|
+
"stimulus": "^1.1.1"
|
24
|
+
}
|
25
|
+
}
|
data/yarn.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
2
|
+
# yarn lockfile v1
|
3
|
+
|
4
|
+
|
5
|
+
"@stimulus/core@^1.1.1":
|
6
|
+
version "1.1.1"
|
7
|
+
resolved "https://registry.yarnpkg.com/@stimulus/core/-/core-1.1.1.tgz#42b0cfe5b73ca492f41de64b77a03980bae92c82"
|
8
|
+
integrity sha512-PVJv7IpuQx0MVPCBblXc6O2zbCmU8dlxXNH4bC9KK6LsvGaE+PCXXrXQfXUwAsse1/CmRu/iQG7Ov58himjiGg==
|
9
|
+
dependencies:
|
10
|
+
"@stimulus/mutation-observers" "^1.1.1"
|
11
|
+
|
12
|
+
"@stimulus/multimap@^1.1.1":
|
13
|
+
version "1.1.1"
|
14
|
+
resolved "https://registry.yarnpkg.com/@stimulus/multimap/-/multimap-1.1.1.tgz#b95e3fd607345ab36e5d5b55486ee1a12d56b331"
|
15
|
+
integrity sha512-26R1fI3a8uUj0WlMmta4qcfIQGlagegdP4PTz6lz852q/dXlG6r+uPS/bx+H8GtfyS+OOXVr3SkZ0Zg0iRqRfQ==
|
16
|
+
|
17
|
+
"@stimulus/mutation-observers@^1.1.1":
|
18
|
+
version "1.1.1"
|
19
|
+
resolved "https://registry.yarnpkg.com/@stimulus/mutation-observers/-/mutation-observers-1.1.1.tgz#0f6c6f081308427fed2a26360dda0c173b79cfc0"
|
20
|
+
integrity sha512-/zCnnw1KJlWO2mrx0yxYaRFZWMGnDMdOgSnI4hxDLxdWVuL2HMROU8FpHWVBLjKY3T9A+lGkcrmPGDHF3pfS9w==
|
21
|
+
dependencies:
|
22
|
+
"@stimulus/multimap" "^1.1.1"
|
23
|
+
|
24
|
+
"@stimulus/webpack-helpers@^1.1.1":
|
25
|
+
version "1.1.1"
|
26
|
+
resolved "https://registry.yarnpkg.com/@stimulus/webpack-helpers/-/webpack-helpers-1.1.1.tgz#eff60cd4e58b921d1a2764dc5215f5141510f2c2"
|
27
|
+
integrity sha512-XOkqSw53N9072FLHvpLM25PIwy+ndkSSbnTtjKuyzsv8K5yfkFB2rv68jU1pzqYa9FZLcvZWP4yazC0V38dx9A==
|
28
|
+
|
29
|
+
stimulus@^1.1.1:
|
30
|
+
version "1.1.1"
|
31
|
+
resolved "https://registry.yarnpkg.com/stimulus/-/stimulus-1.1.1.tgz#53c2fded6849e7b85eed3ed8dd76e33abd74bec5"
|
32
|
+
integrity sha512-R0mBqKp48YnRDZOxZ8hiOH4Ilph3Yj78CIFTBkCwyHs4iGCpe7xlEdQ7cjIxb+7qVCSxFKgxO+mAQbsNgt/5XQ==
|
33
|
+
dependencies:
|
34
|
+
"@stimulus/core" "^1.1.1"
|
35
|
+
"@stimulus/webpack-helpers" "^1.1.1"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abyme
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Romain Sanson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-10-
|
12
|
+
date: 2020-10-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -40,26 +40,83 @@ dependencies:
|
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '13.0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
|
-
name: rspec
|
43
|
+
name: rspec-rails
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: '
|
48
|
+
version: '0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- - "
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rails-dummy
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: sqlite3
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
54
82
|
- !ruby/object:Gem::Version
|
55
|
-
version: '
|
83
|
+
version: '0'
|
56
84
|
description:
|
57
85
|
email:
|
58
86
|
- louis.sommer@hey.com
|
59
87
|
executables: []
|
60
88
|
extensions: []
|
61
89
|
extra_rdoc_files: []
|
62
|
-
files:
|
90
|
+
files:
|
91
|
+
- ".DS_Store"
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- abyme.gemspec
|
101
|
+
- bin/console
|
102
|
+
- bin/rails
|
103
|
+
- bin/setup
|
104
|
+
- javascript/abyme_controller.js
|
105
|
+
- javascript/index.js
|
106
|
+
- lib/.DS_Store
|
107
|
+
- lib/abyme.rb
|
108
|
+
- lib/abyme/abyme_builder.rb
|
109
|
+
- lib/abyme/engine.rb
|
110
|
+
- lib/abyme/model.rb
|
111
|
+
- lib/abyme/version.rb
|
112
|
+
- lib/abyme/view_helpers.rb
|
113
|
+
- lib/generators/.DS_Store
|
114
|
+
- lib/generators/abyme/install_generator.rb
|
115
|
+
- lib/generators/abyme/templates/abyme_controller.js
|
116
|
+
- node_modules/.yarn-integrity
|
117
|
+
- package.json
|
118
|
+
- yarn-error.log
|
119
|
+
- yarn.lock
|
63
120
|
homepage: https://github.com/bear-in-mind/abyme
|
64
121
|
licenses:
|
65
122
|
- MIT
|