rondo_form 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ee9d8f4d7737a47f8e756a02d3dab153a2f8e162b8b5141755976c381470114
4
- data.tar.gz: 2d1b339d1a473c7336ecf5fe3182b23591ab409f5cffe580fabd62b1f0c3ed1b
3
+ metadata.gz: ad2d01fb9c206a3f7942241492eee9ba969f10483a9a3a2f30d53d8fad04186c
4
+ data.tar.gz: 81567ac3ffe29883945dcf5b34855796570d16be438cf21a39893a2882c4af55
5
5
  SHA512:
6
- metadata.gz: 10b686a87cb7e4de4cd13616d9b85d875d36db630209989c5bb40ae19d23fa66ee0b6153b4220a42c6e4479289b0515f427917d0385ccea5a7f5c44741504d71
7
- data.tar.gz: 76fcdce9be21925c0f1aba373fd19f996e7c9fbac7d4e3e952df969ebf4b3a6ed3eb232d47a8326161ad0f627f61267afbcc146c465f1e1dbbc2df6906e72e96
6
+ metadata.gz: 45b7754068730e619cba9d2d4433e3ab876e2c468d8176c324ad744ef1148efb76a723719819660cd0eebe8f03f9db575aa80785dd539805d8e94b590bab6f32
7
+ data.tar.gz: c2cbb54fa7d4a4d33ab66b78096df1e8b72dc10bea32374471b49abc71adf15b00720d5f04b8500558f5cbc6b49939f161b75e4c7962c1b5abe29af1ad777d50
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RondoForm
2
2
 
3
- Handle nested forms, same as Cocoon, but using StimulusJS
3
+ Handle dynamic nested forms, same as Cocoon, but using StimulusJS
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,23 +8,64 @@ Install the gem and add to the application's Gemfile by executing:
8
8
 
9
9
  $ bundle add rondo_form
10
10
 
11
- If bundler is not being used to manage dependencies, install the gem by executing:
11
+ Or inside the Gemfile add the following
12
12
 
13
- $ gem install rondo_form
13
+ $ gem 'rondo_form', '~> 0.2.2'
14
14
 
15
15
  Run the installation task:
16
16
 
17
- $ rails g rondo_form:install
17
+ $ rails g rondo_form:install
18
18
 
19
19
  ## Usage
20
20
 
21
-
22
-
23
- ## Development
24
-
25
- 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.
26
-
27
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
21
+ For example, we have `Project` model, which has `has_many` relationship with `Task` model:
22
+ ```
23
+ rails g scaffold Project name:string description:string
24
+ rails g model Task description:string done:boolean project:belongs_to
25
+ ```
26
+
27
+ ### Sample with SimpleForm
28
+ In your `projects/_form` partial:
29
+ ``` erb
30
+ <%= simple_form_for(@project) do |f| %>
31
+
32
+ <div class="form-inputs">
33
+ <%= f.input :name %>
34
+ <%= f.input :description %>
35
+ </div>
36
+
37
+ <h3 class="text-xl mt-4">Tasks</h3>
38
+ <div class="my-2" data-controller="nested-rondo">
39
+ <div data-nested-rondo-target="fieldContain">
40
+ <%= f.simple_fields_for :tasks do |task| %>
41
+ <%= render "task_fields", f: task %>
42
+ <% end %>
43
+ </div>
44
+ <div class="links">
45
+ <%= link_to_add_association "Add Task", f, :tasks %>
46
+ </div>
47
+ </div>
48
+
49
+ <div class="form-actions mt-4">
50
+ <%= f.button :submit %>
51
+ </div>
52
+ <% end %>
53
+
54
+ ```
55
+
56
+ In your `_task_fields` partial:
57
+ ``` erb
58
+ <div class="nested-fields">
59
+ <%= f.input :description %>
60
+ <%= f.input :done, as: :boolean %>
61
+ <%= link_to_remove_association "Remove Task", f %>
62
+ </div>
63
+
64
+ ```
65
+
66
+ _Note_:
67
+ - Stimulus controller is `nested-rondo`, so you need to declare the element with `data-controller="nested-rondo"`.
68
+ - You must add `data-nested-rondo-target="fieldContain"` to an element, that wraps all nested fields, the new field will be prepended to this element.
28
69
 
29
70
  ## Contributing
30
71
 
@@ -5,10 +5,10 @@ module RondoForm
5
5
  desc "This generator installs the javascript needed for rondo_form"
6
6
 
7
7
  def copy_the_javascript
8
- copy_file "cocoon_controller.js", "app/javascript/controllers/cocoon_controller.js", force: true
8
+ copy_file "nested_rondo_controller.js", "app/javascript/controllers/nested_rondo_controller.js", force: true
9
9
  if (Rails.root.join("app/javascript/controllers/index.js")).exist?
10
10
  append_to_file "app/javascript/controllers/index.js",
11
- %(import CocoonController from "./cocoon_controller"\napplication.register("cocoon", CocoonController)\n)
11
+ %(import NestedRondoController from "./nested_rondo_controller"\napplication.register("nested-rondo", NestedRondoController)\n)
12
12
  else
13
13
  say %(Couldn't find "app/javascript/controllers/index.js".), :red
14
14
  end
@@ -1,15 +1,14 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
2
 
3
3
  export default class extends Controller {
4
- static targets = ["template"]
4
+ static targets = ["template", "fieldContain"]
5
5
 
6
6
  addField(e) {
7
7
  e.preventDefault();
8
8
 
9
9
  let assoc = e.target.dataset.association;
10
10
  let newField = this.buildNewAssociation(assoc);
11
- let insertionNode = this.templateTarget.parentElement;
12
- insertionNode.insertAdjacentHTML("beforebegin", newField);
11
+ this.fieldContainTarget.insertAdjacentHTML("beforeend", newField);
13
12
  }
14
13
 
15
14
  removeField(e) {
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RondoForm
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -24,7 +24,7 @@ module RondoForm
24
24
 
25
25
  is_dynamic = f.object.new_record?
26
26
  html_options[:class] = [html_options[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ')
27
- html_options[:'data-action'] = "click->cocoon#removeField"
27
+ html_options[:'data-action'] = "click->nested-rondo#removeField"
28
28
  f.hidden_field(:_destroy) + link_to(name, '', html_options)
29
29
  end
30
30
  end
@@ -48,11 +48,11 @@ module RondoForm
48
48
 
49
49
  html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
50
50
  html_options[:'data-association'] = association.to_s.singularize
51
- html_options[:'data-action'] = "click->cocoon#addField"
51
+ html_options[:'data-action'] = "click->nested-rondo#addField"
52
52
 
53
53
  new_object = f.object.class.reflect_on_association(association).klass.new
54
54
  model_name = new_object.class.name.underscore
55
- hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {cocoon_target: 'template'}) do
55
+ hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {'nested-rondo_target': 'template'}) do
56
56
  render_association(association, f, new_object)
57
57
  end
58
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rondo_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hungle00
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-14 00:00:00.000000000 Z
11
+ date: 2023-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -78,7 +78,7 @@ files:
78
78
  - README.md
79
79
  - Rakefile
80
80
  - lib/generators/rondo_form/install_generator.rb
81
- - lib/generators/rondo_form/templates/cocoon_controller.js
81
+ - lib/generators/rondo_form/templates/nested_rondo_controller.js
82
82
  - lib/rondo_form.rb
83
83
  - lib/rondo_form/version.rb
84
84
  - lib/rondo_form/view_helpers.rb