rondo_form 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ab7c415b1a97a538742b84ffd41f05acb28e60dc6bb5721d059670c4b69e5e91
4
+ data.tar.gz: e953952e1955dd4a8194b96ea28aff09aa6e0990ce5113648f4d94f846f55335
5
+ SHA512:
6
+ metadata.gz: 680cb74e9d615f9e1d3444363796a2cc8ec8d25dbc3caf2504bcee3188cb88c4001b911259cb256bb236d6bbaf6e0238edc71570fee7894996fd68ffc08cfe6b
7
+ data.tar.gz: 1331fec9838d555ba6742f28aa74259644acbe7a019716a99118413eecb069fd69926f31a21db89e78a9d710e080caa2a0a62f56cd570344ff1e0194c4dbc96c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rondo_form.gemspec
6
+ gemspec
7
+ group :development, :test do
8
+ gem "rails", "~> 7.0"
9
+ gem "rake", "~> 13.0"
10
+ gem "rspec", "~> 3.0"
11
+ end
12
+
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rondo_form (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (13.0.6)
11
+ rspec (3.12.0)
12
+ rspec-core (~> 3.12.0)
13
+ rspec-expectations (~> 3.12.0)
14
+ rspec-mocks (~> 3.12.0)
15
+ rspec-core (3.12.2)
16
+ rspec-support (~> 3.12.0)
17
+ rspec-expectations (3.12.3)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.12.0)
20
+ rspec-mocks (3.12.5)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.12.0)
23
+ rspec-support (3.12.0)
24
+
25
+ PLATFORMS
26
+ x86_64-linux
27
+
28
+ DEPENDENCIES
29
+ rake (~> 13.0)
30
+ rondo_form!
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.3.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 hungle00
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,35 @@
1
+ # RondoForm
2
+
3
+ Handle nested forms, same as Cocoon, but using StimulusJS
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add rondo_form
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install rondo_form
14
+
15
+ Run the installation task:
16
+
17
+ $ rails g rondo_form:install
18
+
19
+ ## Usage
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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hungle00/rondo_form.
32
+
33
+ ## License
34
+
35
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,18 @@
1
+ module RondoForm
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path('templates', __dir__)
5
+ desc "This generator installs the javascript needed for rondo_form"
6
+
7
+ def copy_the_javascript
8
+ copy_file "cocoon_controller.js", "app/javascript/controllers/cocoon_controller.js", force: true
9
+ if (Rails.root.join("app/javascript/controllers/index.js")).exist?
10
+ append_to_file "app/javascript/controllers/index.js",
11
+ %(import CocoonController from "./cocoon_controller"\napplication.register("cocoon", CocoonController)\n)
12
+ else
13
+ say %(Couldn't find "app/javascript/controllers/index.js".), :red
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+ static targets = ["template"]
5
+
6
+ addField(e) {
7
+ e.preventDefault()
8
+ let assoc = e.target.dataset.association
9
+ let newField = this.buildNewAssociation(assoc)
10
+ let insertionNode = this.templateTarget.parentElement
11
+ insertionNode.insertAdjacentHTML("beforebegin", newField)
12
+ }
13
+
14
+ removeField(e) {
15
+ e.preventDefault()
16
+ let closestField = e.target.closest(".nested-fields")
17
+ if(e.target.matches('.dynamic')) {
18
+ closestField.remove()
19
+ } else {
20
+ closestField.style.display = "none"
21
+ }
22
+ }
23
+
24
+ buildNewAssociation(assoc) {
25
+ let content = this.templateTarget.innerHTML;
26
+ let regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g');
27
+ let new_id = new Date().getTime();
28
+ let new_content = content.replace(regexp_braced, '[' + new_id + ']');
29
+ if (new_content == content) {
30
+ regexp_braced = new RegExp('\\[new_' + assoc + 's\\]', 'g');
31
+ new_content = content.replace(regexp_braced, '[' + new_id + ']');
32
+ }
33
+ return new_content
34
+ }
35
+ }
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RondoForm
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,72 @@
1
+ module RondoForm
2
+ module ViewHelpers
3
+ # this will show a link to remove the current association. This should be placed inside the partial.
4
+ # either you give
5
+ # - *name* : the text of the link
6
+ # - *f* : the form this link should be placed in
7
+ # - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
8
+ #
9
+ # or you use the form without *name* with a *&block*
10
+ # - *f* : the form this link should be placed in
11
+ # - *html_options*: html options to be passed to link_to (see <tt>link_to</tt>)
12
+ # - *&block*: the output of the block will be show in the link, see <tt>link_to</tt>
13
+
14
+ def link_to_remove_association(*args, &block)
15
+ if block_given?
16
+ f = args.first
17
+ html_options = args.second || {}
18
+ name = capture(&block)
19
+ link_to_remove_association(name, f, html_options)
20
+ else
21
+ name = args[0]
22
+ f = args[1]
23
+ html_options = args[2] || {}
24
+
25
+ is_dynamic = f.object.new_record?
26
+ html_options[:class] = [html_options[:class], "remove_fields #{is_dynamic ? 'dynamic' : 'existing'}"].compact.join(' ')
27
+ html_options[:'data-action'] = "click->cocoon#removeField"
28
+ f.hidden_field(:_destroy) + link_to(name, '', html_options)
29
+ end
30
+ end
31
+
32
+ # :nodoc:
33
+ def render_association(association, f, new_object)
34
+ f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
35
+ render(association.to_s.singularize + "_fields", :f => builder, :dynamic => true)
36
+ end
37
+ end
38
+
39
+ # shows a link that will allow to dynamically add a new associated object.
40
+ #
41
+ # - *name* : the text to show in the link
42
+ # - *f* : the form this should come in (the formtastic form)
43
+ # - *association* : the associated objects, e.g. :tasks, this should be the name of the <tt>has_many</tt> relation.
44
+ # - *html_options*: html options to be passed to <tt>link_to</tt> (see <tt>link_to</tt>)
45
+ # - *&block*: see <tt>link_to</tt>
46
+
47
+ def link_to_add_association(*args, &block)
48
+ if block_given?
49
+ f = args[0]
50
+ association = args[1]
51
+ html_options = args[2] || {}
52
+ link_to_add_association(capture(&block), f, association, html_options)
53
+ else
54
+ name = args[0]
55
+ f = args[1]
56
+ association = args[2]
57
+ html_options = args[3] || {}
58
+
59
+ html_options[:class] = [html_options[:class], "add_fields"].compact.join(' ')
60
+ html_options[:'data-association'] = association.to_s.singularize
61
+ html_options[:'data-action'] = "click->cocoon#addField"
62
+
63
+ new_object = f.object.class.reflect_on_association(association).klass.new
64
+ model_name = new_object.class.name.underscore
65
+ hidden_div = content_tag("template", id: "#{model_name}_fields_template", data: {cocoon_target: 'template'}) do
66
+ render_association(association, f, new_object)
67
+ end
68
+ end
69
+ hidden_div.html_safe + link_to(name, '', html_options )
70
+ end
71
+ end
72
+ end
data/lib/rondo_form.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ require_relative "rondo_form/version"
3
+ require_relative "rondo_form/view_helpers"
4
+
5
+ module RondoForm
6
+ class Engine < ::Rails::Engine
7
+ # configure our plugin on boot
8
+ initializer "rondo_form.initialize" do |app|
9
+ ActiveSupport.on_load :action_view do
10
+ ActionView::Base.send :include, RondoForm::ViewHelpers
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module RondoForm
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rondo_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hungle00
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ - - "<="
35
+ - !ruby/object:Gem::Version
36
+ version: '3'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '1.17'
44
+ - - "<="
45
+ - !ruby/object:Gem::Version
46
+ version: '3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '7.0'
54
+ - - "<="
55
+ - !ruby/object:Gem::Version
56
+ version: '20.0'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '7.0'
64
+ - - "<="
65
+ - !ruby/object:Gem::Version
66
+ version: '20.0'
67
+ description: Handle nested forms using Stimulus JS
68
+ email:
69
+ - hungle2048@gmail.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - ".rspec"
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - lib/generators/rondo_form/install_generator.rb
81
+ - lib/generators/rondo_form/templates/cocoon_controller.js
82
+ - lib/rondo_form.rb
83
+ - lib/rondo_form/version.rb
84
+ - lib/rondo_form/view_helpers.rb
85
+ - sig/rondo_form.rbs
86
+ homepage: https://github.com/hungle00/rondo_form
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://github.com/hungle00/rondo_form
91
+ source_code_uri: https://github.com/hungle00/rondo_form
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 2.6.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.2.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Cocoon, but with Stimulus JS
111
+ test_files: []