Skip to content

Commit 3508f7e

Browse files
Initial commit
0 parents  commit 3508f7e

23 files changed

+9133
-0
lines changed

.babelrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"@babel/plugin-proposal-class-properties"
4+
]
5+
}

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg

.eslintrc.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://eslint.org/docs/user-guide/configuring
2+
3+
module.exports = {
4+
env: {
5+
browser: true,
6+
node: true,
7+
es6: true
8+
},
9+
10+
globals: {
11+
page: 'readonly'
12+
}
13+
}

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patreon: guillaumebriday

.github/workflows/lint.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Lint
2+
3+
on: [push]
4+
5+
jobs:
6+
eslint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: actions/setup-node@v1
11+
with:
12+
node-version: '14.x'
13+
- run: |
14+
yarn install
15+
yarn lint

.github/workflows/test.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
name: Test
3+
4+
on: [push]
5+
6+
jobs:
7+
jest:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v1
11+
- uses: actions/setup-node@v1
12+
with:
13+
node-version: '14.x'
14+
- name: test
15+
run: |
16+
yarn install
17+
yarn test
18+
env:
19+
CI: true
20+
NOVE_ENV: test

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
pkg
3+
build

.node-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
14.13.0

.npmignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.github
2+
jest
3+
.babelrc
4+
jest.config.js
5+
index.html
6+
snowpack.config.json
7+
netlify.toml
8+
.node-version
9+
.eslintrc.js
10+
src

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 120
3+
}

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.0.0] - 2020-10-15
10+
11+
### Added
12+
13+
- Adding controller

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Guillaume Briday
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 all
13+
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 THE
21+
SOFTWARE.

README.md

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Stimulus Rails Nested Form
2+
3+
[![](https://img.shields.io/npm/dt/stimulus-rails-nested-form.svg)](https://www.npmjs.com/package/stimulus-rails-nested-form)
4+
[![](https://img.shields.io/npm/v/stimulus-rails-nested-form.svg)](https://www.npmjs.com/package/stimulus-rails-nested-form)
5+
[![](https://github.com/stimulus-components/stimulus-rails-nested-form/workflows/Lint/badge.svg)](https://github.com/stimulus-components/stimulus-rails-nested-form)
6+
[![](https://github.com/stimulus-components/stimulus-rails-nested-form/workflows/Test/badge.svg)](https://github.com/stimulus-components/stimulus-rails-nested-form)
7+
[![](https://img.shields.io/github/license/stimulus-components/stimulus-rails-nested-form.svg)](https://github.com/stimulus-components/stimulus-rails-nested-form)
8+
[![Netlify Status](https://api.netlify.com/api/v1/badges/c21b3ca7-40fa-4de3-aad5-56dbc343ace6/deploy-status)](https://stimulus-rails-nested-form.netlify.com)
9+
10+
## Getting started
11+
12+
A Stimulus controller to create new fields on the fly to populate your Rails relationship with `accepts_nested_attributes_for`.
13+
14+
[Nested attributes](https://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods) allow you to save attributes on associated records through the parent.
15+
16+
## Installation
17+
18+
```bash
19+
$ yarn add stimulus-rails-nested-form
20+
```
21+
22+
And use it in your JS file:
23+
```js
24+
import { Application } from "stimulus"
25+
import NestedForm from "stimulus-rails-nested-form"
26+
27+
const application = Application.start()
28+
application.register("nested-form", NestedForm)
29+
```
30+
31+
## Usage
32+
33+
In your models:
34+
```ruby
35+
class User < ApplicationRecord
36+
has_many :todos
37+
accepts_nested_attributes_for :todos, reject_if: :all_blank, allow_destroy: true
38+
end
39+
40+
class Todo < ApplicationRecord
41+
belongs_to :user
42+
end
43+
```
44+
45+
In your controller:
46+
```ruby
47+
class UsersController < ApplicationController
48+
def update
49+
if user.update(user_params)
50+
redirect_to users_path
51+
else
52+
render :edit
53+
end
54+
end
55+
56+
private
57+
58+
def user_params
59+
params
60+
.require(:user)
61+
.permit(
62+
todos_attributes: [:id, :_destroy, :description]
63+
)
64+
end
65+
end
66+
```
67+
68+
To DRY up the code, we extract the fields in a partial called `todo_form` to use it in the [template](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template) with a new `Todo` and in the default `fields_for`.
69+
70+
In your view:
71+
```html
72+
<%= form_with model: @user, data: { controller: 'nested-form' } do |f| %>
73+
<template data-target="nested-form.template">
74+
<%= f.fields_for :todos, Todo.new, child_index: 'NEW_RECORD' do |todo_fields| %>
75+
<%= render "todo_form", f: todo_fields %>
76+
<% end %>
77+
</template>
78+
79+
<%= f.fields_for :todos do |todo_fields| %>
80+
<%= render "todo_form", f: todo_fields %>
81+
<% end %>
82+
83+
<!-- Inserted elements will be injected before that target. -->
84+
<div data-target="nested-form.target"></div>
85+
86+
<button type="button" data-action="click->nested-form#add">
87+
Add todo
88+
</button>
89+
90+
<%= f.submit 'Save todos' %>
91+
<% end %>
92+
```
93+
94+
In the `todo_form.html.erb` partial:
95+
```html
96+
<%= f.label :description %>
97+
<%= f.text_field :description %>
98+
```
99+
100+
As explained in the [documentation](https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for), we need to
101+
specify the `child_index` and replace its value in JavaScript because the index needs to be unique for each fields.
102+
103+
## Extending Controller
104+
105+
You can use inheritance to extend the functionality of any Stimulus components.
106+
107+
```js
108+
import NestedForm from "stimulus-rails-nested-form"
109+
110+
export default class extends NestedForm {
111+
connect() {
112+
super.connect()
113+
console.log("Do what you cant here.")
114+
}
115+
}
116+
```
117+
118+
These controllers will automatically have access to targets defined in the parent class.
119+
120+
If you override the connect, disconnect or any other methods from the parent, you'll want to call `super.method()` to make sure the parent functionality is executed.
121+
122+
## Development
123+
124+
### Project setup
125+
```bash
126+
$ yarn install
127+
$ yarn dev
128+
```
129+
130+
### Tests
131+
132+
[Jest](https://jestjs.io/) and [Puppeteer](https://github.com/puppeteer/puppeteer) are responsible to test this component:
133+
```bash
134+
$ yarn test
135+
```
136+
137+
### Linter
138+
[Prettier](https://prettier.io/) and [ESLint](https://eslint.org/) are responsible to lint and format this component:
139+
```bash
140+
$ yarn lint
141+
$ yarn format
142+
```
143+
144+
## Contributing
145+
146+
Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.
147+
148+
## License
149+
150+
This project is released under the [MIT](http://opensource.org/licenses/MIT) license.

0 commit comments

Comments
 (0)