form-reflex 1.0.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +79 -0
- data/Rakefile +19 -0
- data/app/reflexes/form_reflex.rb +95 -0
- data/lib/form/reflex.rb +7 -0
- data/lib/form/reflex/engine.rb +7 -0
- data/lib/form/reflex/version.rb +5 -0
- data/lib/tasks/form/reflex_tasks.rake +4 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a799e9ab14541fc9ed4319b6706b7b540c015cd4c20fbb269340659139107180
|
4
|
+
data.tar.gz: 8c864e62015c5b29399f69073588e036529c632eed8411f2b9a1b438eb538831
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5bc0b823f32bff55448da116ef8381b55f1c47517727e6da0f977496c23d1b10df0e720965bd3ccef44a47102410969cb27aa20e59198417e47d0541b773aae0
|
7
|
+
data.tar.gz: 94da0a0371716462b506eb18f340f90c926a5ad0f317b04309a79deebffab006d6e2f4ca5d110c1c5613611453c495d8ea07e0db548204df5787e79e9d064227
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2020 Joshua LeBlanc
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# FormReflex
|
2
|
+
FormReflex is a light extension of [optimism](https://optimism.leastbad.com/), leveraging [stimulus_reflex](https://docs.stimulusreflex.com/).
|
3
|
+
|
4
|
+
FormReflex provides real time form validation on change, using the validations already provided on your model.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Please follow the setup instructions for optimism [here](https://optimism.leastbad.com/quick-start)
|
9
|
+
|
10
|
+
Usage for FormReflex is *identical* to the optimism usage, with 2 exceptions:
|
11
|
+
|
12
|
+
1) You must specify data-reflex="change->FormReflex#handle_change" on your form control
|
13
|
+
|
14
|
+
```erb
|
15
|
+
data-reflex="change->FormReflex#handle_change"
|
16
|
+
```
|
17
|
+
|
18
|
+
2) The model you want to validate must be initialized in session[:model]
|
19
|
+
|
20
|
+
For example:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
def new
|
24
|
+
@post = Post.new
|
25
|
+
session[:model] = @post unless @stimulus_reflex
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def set_post
|
30
|
+
@post = Post.find(params[:id])
|
31
|
+
session[:model] = @post unless @stimulus_reflex
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
All in all, pulling in [Optimism's](https://optimism.leastbad.com/) own example, it would look like this:
|
36
|
+
|
37
|
+
```erb
|
38
|
+
<%= form_with(model: session[:model]) do |form| %>
|
39
|
+
<div class="field">
|
40
|
+
<%= form.label :name %>
|
41
|
+
<%= form.text_field :name, data: { reflex: "change->FormControl#handle_change" } %>
|
42
|
+
<%= form.error_for :name %>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<div class="field">
|
46
|
+
<%= form.label :body %>
|
47
|
+
<%= form.text_area :body, data: { reflex: "change->FormControl#handle_change" } %>
|
48
|
+
<%= form.error_for :body %>
|
49
|
+
</div>
|
50
|
+
|
51
|
+
<div class="actions">
|
52
|
+
<%= form.submit %>
|
53
|
+
</div>
|
54
|
+
<% end %>
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
## Installation
|
59
|
+
Add this line to your application's Gemfile:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
gem 'form-reflex'
|
63
|
+
```
|
64
|
+
|
65
|
+
And then execute:
|
66
|
+
```bash
|
67
|
+
$ bundle
|
68
|
+
```
|
69
|
+
|
70
|
+
Or install it yourself as:
|
71
|
+
```bash
|
72
|
+
$ gem install form-reflex
|
73
|
+
```
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
Contribution directions go here.
|
77
|
+
|
78
|
+
## License
|
79
|
+
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,19 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Form::Reflex'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
load 'rails/tasks/statistics.rake'
|
18
|
+
|
19
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class FormReflex < ApplicationReflex
|
4
|
+
include Optimism
|
5
|
+
|
6
|
+
before_reflex do |reflex|
|
7
|
+
# Handle change keeps the session[:model] object up to date with what's going on in the form.
|
8
|
+
# We need to track changes in order to reconcile what fields_for fields we're deleting, as
|
9
|
+
# well as performing validation on change
|
10
|
+
if reflex.method_name == "handle_change"
|
11
|
+
model = session[:model]
|
12
|
+
|
13
|
+
# validation_path will be passed to broadcast_errors
|
14
|
+
# It's a hash built to mimic safe params you would pass in the controller
|
15
|
+
# so given a name foo[bar][0][id], we will build
|
16
|
+
# {
|
17
|
+
# "foo": {
|
18
|
+
# "bar": {
|
19
|
+
# "0": {
|
20
|
+
# id: nil
|
21
|
+
# }
|
22
|
+
# }
|
23
|
+
# }
|
24
|
+
# }
|
25
|
+
validation_path = {}
|
26
|
+
validation_ptr = validation_path
|
27
|
+
|
28
|
+
# Similar to the validation path, we need to locate the child model we want to modify
|
29
|
+
# when working with fields_for. So following the example above, we start with the `Foo` model,
|
30
|
+
# but we want to end up with the first `Bar` child.
|
31
|
+
name_parts = element[:name].scan(/\[(.+?)\]/).flatten
|
32
|
+
name_parts[0...name_parts.length - 1].each do |part|
|
33
|
+
validation_ptr[part] ||= {}
|
34
|
+
validation_ptr = validation_ptr[part]
|
35
|
+
part.chomp! '_attributes'
|
36
|
+
if safe?(model, part) && model.respond_to?(part)
|
37
|
+
model = model.send part
|
38
|
+
elsif model.respond_to? '[]'
|
39
|
+
model = model.send '[]', part.to_i
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
validation_ptr[name_parts.last] = nil
|
44
|
+
|
45
|
+
if element[:type] == "checkbox"
|
46
|
+
model.send "#{name_parts.last}=", element[:checked]
|
47
|
+
else
|
48
|
+
model.send "#{name_parts.last}=", element[:value]
|
49
|
+
end
|
50
|
+
|
51
|
+
model.validate
|
52
|
+
broadcast_errors(session[:model], validation_path)
|
53
|
+
throw :abort
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def current_user
|
58
|
+
controller.current_user
|
59
|
+
end
|
60
|
+
|
61
|
+
# The add button declares what association to add with data-association
|
62
|
+
def association
|
63
|
+
a = element.dataset[:association]
|
64
|
+
# send is spoopy, make sure the message you're sending is actually an association
|
65
|
+
return unless safe?(session[:model], a)
|
66
|
+
|
67
|
+
session[:model].send(a)
|
68
|
+
end
|
69
|
+
|
70
|
+
# We need to determine if we're `send`ing a safe message to the model
|
71
|
+
# We don't want someone to modify the name of an html element and send `destroy!` for example
|
72
|
+
def safe?(model, thing)
|
73
|
+
safe = false
|
74
|
+
safe ||= !model.association(thing).nil? if model.respond_to?('association')
|
75
|
+
if model.respond_to? 'attributes'
|
76
|
+
safe ||= model.attributes.keys.include?(thing)
|
77
|
+
end
|
78
|
+
safe
|
79
|
+
end
|
80
|
+
|
81
|
+
# Creates a new, empty association.
|
82
|
+
def add
|
83
|
+
association.build
|
84
|
+
end
|
85
|
+
|
86
|
+
# Removes an unsaved association. This for for the use case where the user has added a new
|
87
|
+
# association, hasn't saved the form yet, and wants to remove the un-saved association.
|
88
|
+
def delete
|
89
|
+
association.delete association[element.dataset[:index].to_i]
|
90
|
+
end
|
91
|
+
|
92
|
+
def handle_change
|
93
|
+
# stub
|
94
|
+
end
|
95
|
+
end
|
data/lib/form/reflex.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: form-reflex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua LeBlanc
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.3
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 6.0.3.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 6.0.3
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 6.0.3.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: optimism
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: stimulus_reflex
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: sqlite3
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
description: A library reflex to provide realtime form validation using the validations
|
76
|
+
you already have
|
77
|
+
email:
|
78
|
+
- jleblanc@hey.com
|
79
|
+
executables: []
|
80
|
+
extensions: []
|
81
|
+
extra_rdoc_files: []
|
82
|
+
files:
|
83
|
+
- MIT-LICENSE
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- app/reflexes/form_reflex.rb
|
87
|
+
- lib/form/reflex.rb
|
88
|
+
- lib/form/reflex/engine.rb
|
89
|
+
- lib/form/reflex/version.rb
|
90
|
+
- lib/tasks/form/reflex_tasks.rake
|
91
|
+
homepage: https://github.com/joshleblanc/form-reflex
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.0.3
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A library reflex to provide realtime form validation using the validations
|
115
|
+
you already have
|
116
|
+
test_files: []
|