abyme 0.2.4 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.babelrc +6 -0
  4. data/.github/workflows/build.yml +1 -1
  5. data/.gitignore +1 -0
  6. data/CHANGELOG.md +114 -0
  7. data/Gemfile.lock +126 -99
  8. data/README.md +148 -181
  9. data/abyme.gemspec +7 -1
  10. data/dist/abyme.js +2 -0
  11. data/dist/abyme.js.map +1 -0
  12. data/dist/abyme.modern.js +2 -0
  13. data/dist/abyme.modern.js.map +1 -0
  14. data/dist/abyme.module.js +2 -0
  15. data/dist/abyme.module.js.map +1 -0
  16. data/dist/abyme.umd.js +2 -0
  17. data/dist/abyme.umd.js.map +1 -0
  18. data/jest/jest-setup.js +2 -0
  19. data/lib/abyme.rb +2 -0
  20. data/lib/abyme/abyme_builder.rb +11 -18
  21. data/lib/abyme/action_view_extensions/builder.rb +17 -0
  22. data/lib/abyme/controller.rb +15 -0
  23. data/lib/abyme/engine.rb +3 -1
  24. data/lib/abyme/model.rb +82 -6
  25. data/lib/abyme/version.rb +2 -2
  26. data/lib/abyme/view_helpers.rb +75 -57
  27. data/lib/generators/abyme/controller/USAGE +16 -0
  28. data/lib/generators/abyme/controller/controller_generator.rb +25 -0
  29. data/lib/generators/abyme/model/USAGE +21 -0
  30. data/lib/generators/abyme/model/model_generator.rb +70 -0
  31. data/lib/generators/abyme/resource/USAGE +10 -0
  32. data/lib/generators/abyme/resource/resource_generator.rb +18 -0
  33. data/lib/generators/abyme/stimulus/USAGE +5 -0
  34. data/lib/generators/abyme/stimulus/stimulus_generator.rb +21 -0
  35. data/lib/generators/abyme/view/USAGE +11 -0
  36. data/lib/generators/abyme/view/view_generator.rb +65 -0
  37. data/package-lock.json +31992 -0
  38. data/package.json +35 -5
  39. data/specs/index.test.js +26 -0
  40. data/{javascript → src}/abyme_controller.js +5 -1
  41. data/{javascript → src}/index.js +0 -0
  42. data/yarn.lock +7678 -22
  43. metadata +99 -4
data/README.md CHANGED
@@ -1,16 +1,28 @@
1
- # Abyme 🕳
2
-
3
- abyme is a modern take on handling dynamic nested forms in Rails 6+ using StimulusJS.
1
+ # abyme 🕳
4
2
 
5
3
  [![Gem Version](https://badge.fury.io/rb/abyme.svg)](https://badge.fury.io/rb/abyme)
6
4
  ![build](https://github.com/bear-in-mind/abyme/workflows/build/badge.svg)
7
5
  [![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)
6
+ [![Coverage Status](https://coveralls.io/repos/github/bear-in-mind/abyme/badge.svg?branch=master)](https://coveralls.io/github/bear-in-mind/abyme?branch=master)
9
7
 
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.
8
+ abyme is an easy and form-agnostic way to handle nested attributes in Rails, using [stimulus](https://stimulusjs.org/handbook/introduction) under the hood. Here's an example :
9
+ ```ruby
10
+ # views/projects/_form.html.erb
11
+ <%= form_for @project do |f| %>
12
+ <%= f.text_field :title %>
13
+ <%= f.text_area :description %>
14
+
15
+ <%= f.abyme_for(:tasks) %>
16
+
17
+ <%= f.submit 'Save' %>
18
+ <% end %>
19
+ ```
20
+ Supposing you have a `Project` that `has_many :tasks` and a partial located in `views/abyme/_task_fields` containing your form fields for `tasks`, the `abyme_for` command will generate and display 3 elements in this order :
21
+ - A `div` containing all task fields for `@project.tasks` (either persisted or already built instances of `tasks`)
22
+ - A `div` which will contain all additional tasks about to be created (added through the `Add task` button below)
23
+ - A `button` to generate fields for new instances of tasks
12
24
 
13
- Any enhancement proposition or bug report welcome !
25
+ Have a look below to learn more about configuration and all its different options.
14
26
 
15
27
  ## Demo app
16
28
 
@@ -34,7 +46,12 @@ And then execute:
34
46
  $ yarn add abyme
35
47
 
36
48
 
37
- Assuming you [already installed Stimulus](https://stimulusjs.org/handbook/introduction), add this in `app/javascript/controllers/index.js` :
49
+ Assuming you [already installed Stimulus](https://stimulusjs.org/handbook/introduction), you need to register the `stimulus` controller that takes care of the JavaScript behaviour. You can launch this generator :
50
+ ```bash
51
+ rails generate abyme:stimulus
52
+ ```
53
+ Or you can register it yourself :
54
+
38
55
  ```javascript
39
56
  // app/javascript/controllers/index.js
40
57
  import { Application } from "stimulus"
@@ -49,271 +66,221 @@ application.load(definitionsFromContext(context))
49
66
  application.register('abyme', AbymeController)
50
67
  ```
51
68
 
52
- ## What are nested forms and why a new gem ?
53
-
54
- 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.
69
+ ## Getting started
70
+
71
+ To learn more about the *why* of this gem, check out our [wiki](https://github.com/bear-in-mind/abyme/wiki/What-are-nested-forms-and-why-a-new-gem-%3F)
72
+
73
+ You may also check out our [step by step tutorial](https://github.com/bear-in-mind/abyme/wiki/Step-by-step-Tutorial) and our [advanced configuration guide](https://github.com/bear-in-mind/abyme/wiki/Step-by-step-:-Advanced-configuration) (currently in construction).
74
+ ## Documentation
75
+ As in our [our tutorial](https://github.com/bear-in-mind/abyme/wiki/Step-by-step-Tutorial), we'll assume we have a `Project` model, that `has_many :tasks` for the rest of the documentation.
76
+ ### Generators
77
+ To be up and running in no time, we built a few generators. Feel free to skip these if you prefer a manual implementation.
78
+ #### Resource
79
+ To generate everything you need with one command, use this generator :
80
+ ```bash
81
+ rails generate abyme:resource project tasks
82
+ # Includes configuration in Project model
83
+ # Adds abyme_attributes in ProjectsController permitted params
84
+ # Creates a partial and minimum boilerplate in app/views/abyme/_task_fields.html.erb
85
+ ```
86
+ Now, head to your parent form and [keep reading](https://github.com/eki-177/abyme#abyme_forassociation-options---block) !
87
+
88
+ You can also specify [attributes to be permitted](https://github.com/eki-177/abyme#model), or permit all of them (see below). This will populate the partial with input fields for the specified attributes
89
+ ```bash
90
+ rails generate abyme:resource project tasks description title
91
+ # Includes configuration in Project model, including permitted attributes
92
+ # Adds abyme_attributes in ProjectsController permitted params
93
+ # Creates a partial with input fields for the specified attributes in app/views/abyme/_task_fields.html.erb
94
+ ```
55
95
 
56
- 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).
96
+ #### Individual generators
97
+ All the generators launched by the main `resource` generator are available individually :
98
+ ```bash
99
+ # Controller
100
+ rails generate abyme:controller Projects
101
+
102
+ # Model
103
+ # Permit only a few attributes
104
+ rails generate abyme:model project tasks description title
105
+ # Permit all attributes
106
+ rails generate abyme:model project participants all_attributes
107
+
108
+ # Views
109
+ # Without attributes (use this if you're not using SimpleForm or don't care about generating input fields)
110
+ rails generate abyme:view tasks
111
+ # With a few attributes
112
+ rails generate abyme:view tasks name description
113
+ # With all attributes
114
+ rails generate abyme:view tasks all_attributes
115
+ ```
57
116
 
58
- 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.
117
+ ### Model
59
118
 
60
- ## Basic Configuration
119
+ 💡 Don't forget to `include Abyme::Model` in your parent model
61
120
 
62
- ### Models
63
- Let's consider a to-do application with Projects having many Taks, themselves having many Comments.
121
+ #### #abymize(:association, permit: nil, reject: nil, options = {})
122
+ In models, the `abyme_for :association` acts as an alias for this command :
64
123
  ```ruby
65
- # models/project.rb
66
- class Project < ApplicationRecord
67
- has_many :tasks
68
- validates :title, :description, presence: true
69
- end
70
-
71
- # models/task.rb
72
- class Task < ApplicationRecord
73
- belongs_to :project
74
- has_many :comments
75
- validates :title, :description, presence: true
76
- end
77
-
78
- # models/comment.rb
79
- class Comment < ApplicationRecord
80
- belongs_to :task
81
- validates :content, presence: true
82
- end
124
+ accepts_nested_attributes_for :association, reject_if: :all_blank, :allow_destroy: true
83
125
  ```
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 :
126
+
127
+ * `permit: []` : allows you to generate a hash of attributes that can be easily called on the controller side through the `::abyme_attributes` class method (see details below).
86
128
  ```ruby
87
- # models/project.rb
88
- class Project < ApplicationRecord
89
- include Abyme::Model
90
- has_many :tasks, inverse_of: :project
91
- # ...
92
- abyme_for :tasks
93
- end
94
-
95
- # models/task.rb
96
- class Task < ApplicationRecord
97
- include Abyme::Model
98
- has_many :comments, inverse_of: :task
99
- # ...
100
- abyme_for :comments
101
- end
129
+ abymize :association, permit: [:name, :description]
130
+
131
+ # You may also permit all attributes like so :
132
+ abymize :association, permit: :all_attributes
102
133
  ```
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.
104
-
105
- ### Controller
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`.
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 :
108
134
 
135
+ * `reject: []` : allows you to add all attributes to `::abyme_attributes`, excepted the ones specified.
109
136
  ```ruby
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
115
- ]
116
- ]
117
- )
118
- end
137
+ abymize :association, reject: [:password]
119
138
  ```
120
- A few explanations here.
121
-
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.
123
-
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
139
 
140
+ * `options: {}` : [the same options] you may pass to the `accepts_nested_attributes` method (see [this link](https://api.rubyonrails.org/v6.1.0/classes/ActiveRecord/NestedAttributes/ClassMethods.html) for details)
138
141
  ```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 %>
142
+ abyme_for :association, limit: 3, allow_destroy: false
151
143
  ```
152
144
 
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:
145
+ #### ::abyme_attributes
146
+ Returns a hash to the right format to be included in the `strong params` on the controller side. For a `Project` model with nested `:tasks` :
158
147
  ```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 %>
148
+ Project.abyme_attributes
149
+ # => {tasks_attributes: [:title, :description, :id, :_destroy]}
167
150
  ```
168
151
 
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.
152
+ ### Controller
153
+ #### #abyme_attributes
154
+ Infers the name of the resource from the controller name, and calls the `::abyme_attributes` method on it. Hence, in your `ProjectsController` :
155
+ ```ruby
156
+ def project_params
157
+ params.require(:project).permit(:title, :description, abyme_attributes)
158
+ end
159
+ ```
170
160
 
171
- ### What about the controller ?
161
+ ### Views
172
162
 
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 👨‍👧‍👧
163
+ #### #abyme_for(:association, options = {}, &block)
164
+ This is the container for all your nested fields. It takes the symbolized association as a parameter, along with options, and an optional block to specify any layout you may wish for the different parts of the `abyme` builder.
175
165
 
176
- ### Auto mode
166
+ 💡 Please note an id is automatically added to this element, which value is : `abyme--association_name`.
177
167
 
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:
168
+ 💡 If you don't pass a block, `records`, `new_records` and `add_association` will be called and will appear in this order in your layout.
169
+ * `partial: ` : allows you to indicate a custom partial path for both `records` and `new_records`
179
170
  ```ruby
180
- # views/abyme/_task_fields.html.erb
181
- # ... rest of the partial above
182
- <%= abymize(:comments, f) %>
171
+ <%= f.abyme_for(:tasks, partial: 'projects/task_fields') do |abyme| %>
172
+ <%= abyme.records %>
173
+ <%= abyme.new_records %>
174
+ <%= add_associated_record %>
175
+ <% end %>
183
176
  ```
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
-
177
+ * `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).
190
178
  ```ruby
191
- accepts_nested_attributes_for :association, reject_if: :all_blank, :allow_destroy: true
179
+ <%= f.abyme_for(:tasks, limit: 5) do |abyme| %>
180
+ # 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
181
+ <%= abyme.records %>
182
+ <%= abyme.new_records %>
183
+ <%= add_associated_record %>
184
+ <% end %>
192
185
  ```
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 :
186
+ * `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.
195
187
  ```ruby
196
- abyme_for :association, limit: 3, allow_destroy: false
188
+ <%= f.abyme_for(:tasks, min_count: 1) do |abyme| %>
189
+ # 1 blank task will automatically be added to the form.
190
+ <%= abyme.records %>
191
+ <%= abyme.new_records %>
192
+ <%= add_associated_record %>
193
+ <% end %>
197
194
  ```
198
195
 
199
- ### Views
196
+ *If you're not passing a block*, the `abyme_for` method can take a few additional options:
197
+ * `button_text: ` this will set the `add_association` button text to the string of your choice.
198
+
199
+ 💡 All options that should be passed to either `records` or `new_records` below can be passed here and will be passed down.
200
200
 
201
201
  #### #records
202
202
  A few options can be passed to `abyme.records`:
203
203
  * `collection:` : allows you to pass a collection of your choice to only display specific objects.
204
204
  ```ruby
205
- <%= abymize(:tasks, f) do |abyme| %>
205
+ <%= f.abyme_for(:tasks) do |abyme| %>
206
206
  <%= abyme.records(collection: @project.tasks.where(done: false)) %>
207
207
  <%= abyme.new_records %>
208
- <%= add_association %>
208
+ <%= add_associated_record %>
209
209
  <% end %>
210
210
  ```
211
211
  * `order:` : allows you to pass an ActiveRecord `order` method to sort your instances the way you want.
212
212
  ```ruby
213
- <%= abymize(:tasks, f) do |abyme| %>
213
+ <%= f.abyme_for(:tasks) do |abyme| %>
214
214
  <%= abyme.records(order: { created_at: :asc }) %>
215
215
  <%= abyme.new_records %>
216
- <%= add_association %>
216
+ <%= add_associated_record %>
217
217
  <% end %>
218
218
  ```
219
- * `partial:` : allows you to indicate a custom partial, if one has not already been passed to `abymize`.
219
+ * `partial:` : allows you to indicate a custom partial, if one has not already been passed to `abyme_for`.
220
220
  ```ruby
221
- <%= abymize(:tasks, f) do |abyme| %>
221
+ <%= f.abyme_for(:tasks) do |abyme| %>
222
222
  <%= abyme.records %>
223
223
  <%= abyme.new_records(partial: 'projects/task_fields') %>
224
- <%= add_association %>
224
+ <%= add_associated_record %>
225
225
  <% end %>
226
226
  ```
227
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
228
  ```ruby
229
- <%= abymize(:tasks, f) do |abyme| %>
229
+ <%= f.abyme_for(:tasks) do |abyme| %>
230
230
  <%= abyme.records(fields_html: { class: "some-class" }) %>
231
231
  # Every set of persisted fields will have these 3 classes : 'abyme--fields', 'task-fields', and 'some-class'
232
232
  <%= abyme.new_records %>
233
- <%= add_association %>
233
+ <%= add_associated_record %>
234
234
  <% end %>
235
235
  ```
236
- * `wrapper_html:` : gives you the possibility to add any HTML attribute you may want to the wrapper containing all fields.
236
+ * `wrapper_html:` : gives you the possibility to add any HTML attribute you may want to the wrapper containing all persisted fields.
237
237
  ```ruby
238
- <%= abymize(:tasks, f) do |abyme| %>
238
+ <%= f.abyme_for(:tasks) do |abyme| %>
239
239
  <%= abyme.records(wrapper_html: { class: "persisted-records" }) %>
240
240
  # The wrapper containing all persisted task fields will have an id "abyme-tasks-wrapper" and a class "persisted-records"
241
241
  <%= abyme.new_records %>
242
- <%= add_association %>
242
+ <%= add_associated_record %>
243
243
  <% end %>
244
244
  ```
245
245
  #### #new_records
246
246
  Here are the options that can be passed to `abyme.new_records`:
247
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
248
  ```ruby
249
- <%= abymize(:tasks, f) do |abyme| %>
249
+ <%= f.abyme_for(:tasks) do |abyme| %>
250
250
  <%= abyme.records %>
251
251
  <%= abyme.new_records(position: :start) %>
252
- <%= add_association %>
252
+ <%= add_associated_record %>
253
253
  <% end %>
254
254
  ```
255
255
  * `partial:` : same as `#records`
256
256
  * `fields_html:` : same as `#records`
257
257
  * `wrapper_html:` : same as `#records`
258
258
 
259
- #### #add_association, #remove_association
259
+ #### #add_associated_record, #remove_associated_record
260
260
  These 2 methods behave the same. Here are their options :
261
261
  * `tag:` : allows you to specify a tag of your choosing, like `:a`, or `:div`. Default is `:button`.
262
262
  * `content:` : the text to display inside the element. Default is `Add association_name`
263
263
  * `html:` : gives you the possibility to add any HTML attribute you may want to the element.
264
264
  ```ruby
265
- <%= abymize(:tasks, f) do |abyme| %>
265
+ <%= f.abyme_for(:tasks) do |abyme| %>
266
266
  # ...
267
- <%= add_association(tag: :a, content: "Add a super task", html: {id: "add-super-task"}) %>
267
+ <%= add_associated_record(tag: :a, content: "Add a super task", html: {id: "add-super-task"}) %>
268
268
  <% end %>
269
269
  ```
270
270
 
271
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
272
  ```ruby
273
- <%= abymize(:tasks, f) do |abyme| %>
273
+ <%= f.abyme_for(:tasks) do |abyme| %>
274
274
  # ...
275
- <%= add_association(tag: :div, html: {id: "add-super-task", class: "flex"}) do %>
275
+ <%= add_associated_record(tag: :div, html: {id: "add-super-task", class: "flex"}) do %>
276
276
  <i class="fas fa-plus"></i>
277
277
  <h2>Add a super task</h2>
278
278
  <% end %>
279
279
  <% end %>
280
280
  ```
281
281
 
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
- ```
310
-
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.
314
-
315
282
  ## 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).
283
+ 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 `abyme_for` method).
317
284
 
318
285
  We're currently thinking about a way to attach to these via Stimulus. Coming soon !
319
286
 
@@ -341,7 +308,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
341
308
 
342
309
  ## Contributing
343
310
 
344
- Bug reports and pull requests are welcome on GitHub at https://github.com/bear-in-mind/abyme.
311
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eki-177/abyme.
345
312
 
346
313
  ## License
347
314
 
data/abyme.gemspec CHANGED
@@ -34,10 +34,16 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency 'database_cleaner-active_record'
35
35
  spec.add_development_dependency 'capybara'
36
36
  spec.add_development_dependency 'webdrivers'
37
+ spec.add_development_dependency "generator_spec"
37
38
 
38
39
  # Dummy app
39
40
  spec.add_development_dependency "sqlite3"
40
- spec.add_development_dependency 'rails'
41
+ spec.add_development_dependency 'rails', "~> 6.0.3.6"
42
+ spec.add_development_dependency 'pry-byebug'
43
+ spec.add_development_dependency 'byebug'
44
+ spec.add_development_dependency 'web-console'
45
+ spec.add_development_dependency 'simple_form'
46
+
41
47
  spec.add_development_dependency 'puma'
42
48
  spec.add_development_dependency 'simplecov'
43
49
  spec.add_development_dependency 'simplecov-lcov'