abyme 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +6 -0
- data/abyme-0.1.2.gem +0 -0
- data/abyme.gemspec +32 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/javascript/abyme_controller.js +71 -0
- data/javascript/index.js +2 -0
- data/lib/abyme.rb +8 -0
- data/lib/abyme/abyme_builder.rb +61 -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 +104 -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 +29 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54da5a9a0118b560b3e9006d85f175d3a84ccd067a8654a61c4a46dfeb5ba15b
|
4
|
+
data.tar.gz: 3d836ed9cf5564e2b311a8e100466bc589307c0c5bf77f527fae70c94bc05fb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7095310324451424bccce250acd5cddd059108861581169990ec1be1abe8e5a9453aaf7710f1cb62d63bebbb5af4220f6257897119d43477e2a923b01067d3e
|
7
|
+
data.tar.gz: 20bb396ba92d7b739fdf137d37627163f71f3dbc4dbbe84ea346d24ccbe7317af2b253a1b451e52a650da0e01b078a153e8565e268475765769347e2c1e56be5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
abyme (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.4.4)
|
10
|
+
rake (13.0.1)
|
11
|
+
rspec (3.9.0)
|
12
|
+
rspec-core (~> 3.9.0)
|
13
|
+
rspec-expectations (~> 3.9.0)
|
14
|
+
rspec-mocks (~> 3.9.0)
|
15
|
+
rspec-core (3.9.2)
|
16
|
+
rspec-support (~> 3.9.3)
|
17
|
+
rspec-expectations (3.9.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.9.0)
|
20
|
+
rspec-mocks (3.9.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.9.0)
|
23
|
+
rspec-support (3.9.3)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
abyme!
|
30
|
+
bundler (~> 2.0)
|
31
|
+
rake (~> 13.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
2.0.2
|
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,123 @@
|
|
1
|
+
# Abyme 🕳
|
2
|
+
|
3
|
+
abyme is a modern take on handling dynamic nested forms in Rails 6+ using StimulusJS.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'abyme'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
$ yarn add 'abyme'
|
17
|
+
|
18
|
+
|
19
|
+
Assuming you [already installed Stimulus](https://stimulusjs.org/handbook/introduction), add this in `app/javascript/controllers/index.js` :
|
20
|
+
```javascript
|
21
|
+
// app/javascript/controllers/index.js
|
22
|
+
import { Application } from "stimulus"
|
23
|
+
import { definitionsFromContext } from "stimulus/webpack-helpers"
|
24
|
+
// Add this line below
|
25
|
+
import { AbymeController } from 'abyme'
|
26
|
+
|
27
|
+
const application = Application.start()
|
28
|
+
const context = require.context("controllers", true, /_controller\.js$/)
|
29
|
+
application.load(definitionsFromContext(context))
|
30
|
+
// And this one
|
31
|
+
application.register('abyme', AbymeController)
|
32
|
+
```
|
33
|
+
|
34
|
+
## What are nested forms and why a new gem ?
|
35
|
+
|
36
|
+
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.
|
37
|
+
|
38
|
+
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).
|
39
|
+
|
40
|
+
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.
|
41
|
+
|
42
|
+
## Basic Configuration
|
43
|
+
|
44
|
+
### Models
|
45
|
+
Let's consider a to-do application with Projects having many Taks, themselves having many Comments.
|
46
|
+
```ruby
|
47
|
+
# models/project.rb
|
48
|
+
class Project < ApplicationRecord
|
49
|
+
has_many :tasks, inverse_of: :project, dependent: :destroy
|
50
|
+
validates :title, :description, presence: true
|
51
|
+
end
|
52
|
+
|
53
|
+
# models/task.rb
|
54
|
+
class Task < ApplicationRecord
|
55
|
+
belongs_to :project
|
56
|
+
has_many :comments, inverse_of: :project, dependent: :destroy
|
57
|
+
validates :title, :description, presence: true
|
58
|
+
end
|
59
|
+
|
60
|
+
# models/comment.rb
|
61
|
+
class Comment < ApplicationRecord
|
62
|
+
belongs_to :task
|
63
|
+
validates :content, presence: true
|
64
|
+
end
|
65
|
+
```
|
66
|
+
The end-goal is to be able to create a project along with different tasks, and immediately add comments to some of these tasks ; all in a single form.
|
67
|
+
What we'll have is a 2-level nested form. Thus, we'll need to add these lines to both `Project` and `Task` :
|
68
|
+
```ruby
|
69
|
+
# models/project.rb
|
70
|
+
class Project < ApplicationRecord
|
71
|
+
include Abyme::Model
|
72
|
+
#...
|
73
|
+
abyme_for :tasks
|
74
|
+
end
|
75
|
+
|
76
|
+
# models/task.rb
|
77
|
+
class Task < ApplicationRecord
|
78
|
+
include Abyme::Model
|
79
|
+
#...
|
80
|
+
abyme_for :comments
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
### Controller
|
85
|
+
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`.
|
86
|
+
The only configuration needed here will be our strong_params. Nested attributes require a very specific syntax to white-list the permitted attributes. It looks like this :
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
def project_params
|
90
|
+
params.require(:project).permit(
|
91
|
+
:title, :description, tasks_attributes: [
|
92
|
+
:id, :title, :description, :_destroy, comments_attributes: [
|
93
|
+
:id, :content, :_destroy
|
94
|
+
]
|
95
|
+
]
|
96
|
+
)
|
97
|
+
end
|
98
|
+
```
|
99
|
+
A few explanations here.
|
100
|
+
|
101
|
+
* 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.
|
102
|
+
|
103
|
+
**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: [...]`)
|
104
|
+
|
105
|
+
* 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.
|
106
|
+
|
107
|
+
### View
|
108
|
+
|
109
|
+
TODO...
|
110
|
+
|
111
|
+
## Development
|
112
|
+
|
113
|
+
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.
|
114
|
+
|
115
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/abyme.
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/abyme-0.1.2.gem
ADDED
Binary file
|
data/abyme.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
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|node_modules)/}) }
|
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", "~> 3.0"
|
32
|
+
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/setup
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
import { Controller } from 'stimulus';
|
2
|
+
|
3
|
+
export default class extends Controller {
|
4
|
+
static targets = ['template', 'associations'];
|
5
|
+
|
6
|
+
connect() {
|
7
|
+
console.log('Abyme Connect');
|
8
|
+
}
|
9
|
+
|
10
|
+
get position() {
|
11
|
+
return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';
|
12
|
+
}
|
13
|
+
|
14
|
+
add_association(event) {
|
15
|
+
event.preventDefault();
|
16
|
+
|
17
|
+
let html = this.templateTarget.innerHTML.replace(
|
18
|
+
/NEW_RECORD/g,
|
19
|
+
new Date().getTime()
|
20
|
+
);
|
21
|
+
|
22
|
+
if (html.match(/<template[\s\S]+<\/template>/)) {
|
23
|
+
const template = html
|
24
|
+
.match(/<template[\s\S]+<\/template>/)[0]
|
25
|
+
.replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g, `[NEW_RECORD]$2`);
|
26
|
+
|
27
|
+
html = html.replace(/<template[\s\S]+<\/template>/g, template);
|
28
|
+
}
|
29
|
+
|
30
|
+
this.create_event('before-add', html)
|
31
|
+
this.associationsTarget.insertAdjacentHTML(this.position, html);
|
32
|
+
this.create_event('after-add', html)
|
33
|
+
}
|
34
|
+
|
35
|
+
remove_association(event) {
|
36
|
+
event.preventDefault();
|
37
|
+
|
38
|
+
this.create_event('before-remove')
|
39
|
+
let wrapper = event.target.closest('.abyme--fields');
|
40
|
+
wrapper.querySelector("input[name*='_destroy']").value = 1;
|
41
|
+
wrapper.style.display = 'none';
|
42
|
+
this.create_event('after-remove')
|
43
|
+
}
|
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
|
+
}
|
data/javascript/index.js
ADDED
data/lib/abyme.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Abyme
|
2
|
+
class AbymeBuilder < ActionView::Base
|
3
|
+
include ActionView
|
4
|
+
|
5
|
+
def initialize(association:, form:, lookup_context:, &block)
|
6
|
+
@association = association
|
7
|
+
@form = form
|
8
|
+
@lookup_context = lookup_context
|
9
|
+
yield(self) if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def records(options = {})
|
13
|
+
persisted_records_for(@association, @form, options) do |form|
|
14
|
+
render_association_partial(form, options)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def new_records(options = {}, &block)
|
19
|
+
new_records_for(@association, @form, options) do |form|
|
20
|
+
render_association_partial(form, options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# def add_association(options = {}, &block)
|
25
|
+
# action = 'click->abyme#add_association'
|
26
|
+
# create_button(action, options, &block)
|
27
|
+
# end
|
28
|
+
|
29
|
+
# def remove_association(options = {}, &block)
|
30
|
+
# action = 'click->abyme#remove_association'
|
31
|
+
# create_button(action, options, &block)
|
32
|
+
# end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def render_association_partial(form, options)
|
37
|
+
partial = options[:partial] || "shared/#{@association.to_s.singularize}_fields"
|
38
|
+
# ActionController::Base.render(partial: "shared/#{@association.to_s.singularize}_fields", locals: { f: form })
|
39
|
+
ActionController::Base.render(partial: partial, locals: { f: form })
|
40
|
+
end
|
41
|
+
|
42
|
+
# def create_button(action, options, &block)
|
43
|
+
# options[:attributes] = {} if options[:attributes].nil?
|
44
|
+
# options[:tag] = :button if options[:tag].nil?
|
45
|
+
|
46
|
+
# if block_given?
|
47
|
+
# concat content_tag(options[:tag], { data: { action: action }}.merge(options[:attributes])) do
|
48
|
+
# # capture(&block)
|
49
|
+
# yield
|
50
|
+
# end
|
51
|
+
# else
|
52
|
+
# render content_tag(options[:tag], options[:content], {data: { action: action }}.merge(options[:attributes]))
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
|
56
|
+
# def formatize(association)
|
57
|
+
# association.class.name.tableize
|
58
|
+
# end
|
59
|
+
|
60
|
+
end
|
61
|
+
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,104 @@
|
|
1
|
+
require 'abyme/abyme_builder'
|
2
|
+
|
3
|
+
module Abyme
|
4
|
+
module ViewHelpers
|
5
|
+
|
6
|
+
def abymize(association, form, options = {}, &block)
|
7
|
+
content_tag(:div, data: { controller: 'abyme' }, id: "abyme--#{association}") do
|
8
|
+
if block_given?
|
9
|
+
yield(Abyme::AbymeBuilder.new(association: association, form: form, lookup_context: self.lookup_context))
|
10
|
+
else
|
11
|
+
model = association.to_s.singularize.classify.constantize
|
12
|
+
concat(persisted_records_for(association, form, options))
|
13
|
+
concat(new_records_for(association, form, options))
|
14
|
+
concat(add_association(content: options[:add] || "Add #{model}"))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def new_records_for(association, form, options = {}, &block)
|
20
|
+
content_tag(:div, data: { target: 'abyme.associations', association: association, abyme_position: options[:position] || :end }) do
|
21
|
+
content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: { target: 'abyme.template' }) do
|
22
|
+
form.fields_for association, association.to_s.classify.constantize.new, child_index: 'NEW_RECORD' do |f|
|
23
|
+
content_tag(:div, basic_markup(options[:html])) do
|
24
|
+
if block_given?
|
25
|
+
# Here, f is the fields_for ; f.object becomes association.new rather than the original form.object
|
26
|
+
yield(f)
|
27
|
+
else
|
28
|
+
render "shared/#{association.to_s.singularize}_fields", f: f
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def persisted_records_for(association, form, options = {})
|
37
|
+
if options[:collection]
|
38
|
+
records = options[:collection]
|
39
|
+
else
|
40
|
+
records = form.object.send(association)
|
41
|
+
end
|
42
|
+
|
43
|
+
if options[:order].present?
|
44
|
+
records = records.order(options[:order])
|
45
|
+
|
46
|
+
# GET INVALID RECORDS
|
47
|
+
invalids = form.object.send(association).reject(&:persisted?)
|
48
|
+
|
49
|
+
if invalids.any?
|
50
|
+
records = records.to_a.concat(invalids)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
form.fields_for(association, records) do |f|
|
55
|
+
content_tag(:div, basic_markup(options[:html])) do
|
56
|
+
if block_given?
|
57
|
+
yield(f)
|
58
|
+
else
|
59
|
+
render "shared/#{association.to_s.singularize}_fields", f: f
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def add_association(options = {}, &block)
|
66
|
+
action = 'click->abyme#add_association'
|
67
|
+
create_button(action, options, &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
def remove_association(options = {}, &block)
|
71
|
+
action = 'click->abyme#remove_association'
|
72
|
+
create_button(action, options, &block)
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def create_button(action, options, &block)
|
78
|
+
options[:html] ||= {}
|
79
|
+
options[:tag] ||= :button
|
80
|
+
options[:content] ||= 'Add Association'
|
81
|
+
|
82
|
+
if block_given?
|
83
|
+
content_tag(options[:tag], { data: { action: action } }.merge(options[:html])) do
|
84
|
+
capture(&block)
|
85
|
+
end
|
86
|
+
else
|
87
|
+
content_tag(options[:tag], options[:content], { data: { action: action } }.merge(options[:html]))
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def basic_markup(html)
|
92
|
+
|
93
|
+
if html && html[:class]
|
94
|
+
html[:class] = 'abyme--fields ' + html[:class]
|
95
|
+
else
|
96
|
+
html ||= {}
|
97
|
+
html[:class] = 'abyme--fields'
|
98
|
+
end
|
99
|
+
|
100
|
+
return html
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
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.1.
|
4
|
+
version: 0.1.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-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -59,7 +59,33 @@ email:
|
|
59
59
|
executables: []
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
|
-
files:
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- abyme-0.1.2.gem
|
72
|
+
- abyme.gemspec
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- javascript/abyme_controller.js
|
76
|
+
- javascript/index.js
|
77
|
+
- lib/abyme.rb
|
78
|
+
- lib/abyme/abyme_builder.rb
|
79
|
+
- lib/abyme/engine.rb
|
80
|
+
- lib/abyme/model.rb
|
81
|
+
- lib/abyme/version.rb
|
82
|
+
- lib/abyme/view_helpers.rb
|
83
|
+
- lib/generators/.DS_Store
|
84
|
+
- lib/generators/abyme/install_generator.rb
|
85
|
+
- lib/generators/abyme/templates/abyme_controller.js
|
86
|
+
- package.json
|
87
|
+
- yarn-error.log
|
88
|
+
- yarn.lock
|
63
89
|
homepage: https://github.com/bear-in-mind/abyme
|
64
90
|
licenses:
|
65
91
|
- MIT
|