vue-form-for 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +109 -0
- data/lib/vue-form-for/form_builder.rb +30 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '019def4fa71dae16fcdac1cc36ac206726a99ab3'
|
4
|
+
data.tar.gz: 9733ade5ba2ed0b3d15daefa1c112250c03d00ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce25f0da67e1aac93b87aaa26a80b6cfa057ade399f5b2c6b019069ccc4f9da8447fe4cdca3b091920a9d9c44e229a2881716c367de96f817566d48807e1fd5a
|
7
|
+
data.tar.gz: 18f9ad7bd787e45a3ea71c377d3437d1d1c2ac3361322c538b0a5eb694fd684518545043eaa47f33ab08bdf039cd2e715bb2f78ebce9f1a9d2d4c461c2e1c97c
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,3 +2,112 @@ vue-form-for
|
|
2
2
|
============
|
3
3
|
|
4
4
|
A custom Rails form builder for Vue.js
|
5
|
+
|
6
|
+
Synopsis
|
7
|
+
--------
|
8
|
+
|
9
|
+
```
|
10
|
+
<%= vue_form_for User.new do |f| %>
|
11
|
+
<%= f.text_field :name %>
|
12
|
+
<% end %>
|
13
|
+
```
|
14
|
+
|
15
|
+
```html
|
16
|
+
<form ...>
|
17
|
+
...
|
18
|
+
<input v-model="user.name" type="text" name="user[name]" ... />
|
19
|
+
</form>
|
20
|
+
```
|
21
|
+
|
22
|
+
Installation
|
23
|
+
------------
|
24
|
+
|
25
|
+
Add the following line to `Gemfile`:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem "vue-form-for"
|
29
|
+
```
|
30
|
+
|
31
|
+
Run `bundle install` on the terminal.
|
32
|
+
|
33
|
+
Usage
|
34
|
+
-----
|
35
|
+
|
36
|
+
```
|
37
|
+
<%= vue_form_for User.new do |f| %>
|
38
|
+
<%= f.text_field :name %>
|
39
|
+
<%= f.submit "Create" %>
|
40
|
+
<% end %>
|
41
|
+
```
|
42
|
+
|
43
|
+
The above ERB template will be rendered into the following HTML fragment:
|
44
|
+
|
45
|
+
```html
|
46
|
+
<form class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
|
47
|
+
<input name="utf8" type="hidden" value="✓" />
|
48
|
+
<input type="hidden" name="authenticity_token" value="..." />
|
49
|
+
<input v-model="user.name" type="text" name="user[name]" id="user_name" />
|
50
|
+
<input type="submit" name="commit" value="Create" />
|
51
|
+
</form>
|
52
|
+
```
|
53
|
+
|
54
|
+
Note that the third `<input>` element has a `v-model` attriubte, which can be
|
55
|
+
interpreted by Vue.js as the _directive_ to create two-way data bindings between
|
56
|
+
form fields and component's data.
|
57
|
+
|
58
|
+
If you are using the [Webpacker](https://github.com/rails/webpacker),
|
59
|
+
create `app/javascript/packs/new_user_form.js` with following code:
|
60
|
+
|
61
|
+
```javascript
|
62
|
+
import Vue from 'vue/dist/vue.esm'
|
63
|
+
|
64
|
+
document.addEventListener("DOMContentLoaded", () => {
|
65
|
+
const NewUserForm = new Vue({
|
66
|
+
el: "#new_user",
|
67
|
+
data: {
|
68
|
+
user: {
|
69
|
+
name: ""
|
70
|
+
}
|
71
|
+
}
|
72
|
+
})
|
73
|
+
})
|
74
|
+
```
|
75
|
+
|
76
|
+
Add this line to the ERB template:
|
77
|
+
|
78
|
+
```text
|
79
|
+
<%= javascript_pack_tag "new_user_form" %>
|
80
|
+
```
|
81
|
+
|
82
|
+
Then, you can get the value of `user[name]` field by the `user.name`.
|
83
|
+
|
84
|
+
Other Functionalities
|
85
|
+
---------------------
|
86
|
+
|
87
|
+
You can provide a hash to the `:v` option:
|
88
|
+
|
89
|
+
```
|
90
|
+
<%= vue_form_for User.new do |f| %>
|
91
|
+
<%= f.text_field :name, v: { model: "customer.name" } %>
|
92
|
+
<%= f.submit "Create", v: { if: "submittable" } %>
|
93
|
+
<% end %>
|
94
|
+
```
|
95
|
+
|
96
|
+
The above ERB template is identical with the following one:
|
97
|
+
|
98
|
+
```
|
99
|
+
<%= vue_form_for User.new do |f| %>
|
100
|
+
<%= f.text_field :name, "v-model" => "customer.name" %>
|
101
|
+
<%= f.submit "Create", "v-if" => "submittable" %>
|
102
|
+
<% end %>
|
103
|
+
```
|
104
|
+
|
105
|
+
License
|
106
|
+
-------
|
107
|
+
|
108
|
+
The `initial-test-data` is distributed under the MIT license. ([MIT-LICENSE](https://github.com/oiax/initial-test-data/blob/master/MIT-LICENSE))
|
109
|
+
|
110
|
+
Author
|
111
|
+
------
|
112
|
+
|
113
|
+
Tsutomu Kuroda (t-kuroda@oiax.jp)
|
@@ -3,10 +3,40 @@ module VueFormFor
|
|
3
3
|
(field_helpers - [:label, :fields_for]).each do |selector|
|
4
4
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
5
5
|
def #{selector}(method, options = {})
|
6
|
+
resolve_vue_options(options)
|
6
7
|
options[:"v-model"] ||= "\#{@object_name}.\#{method}"
|
7
8
|
super(method, options)
|
8
9
|
end
|
9
10
|
RUBY_EVAL
|
10
11
|
end
|
12
|
+
|
13
|
+
def label(method, text = nil, options = {}, &block)
|
14
|
+
resolve_vue_options(options)
|
15
|
+
super(method, text, options, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def submit(value = nil, options = {})
|
19
|
+
resolve_vue_options(options)
|
20
|
+
super(value, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def button(value = nil, options = {}, &block)
|
24
|
+
resolve_vue_options(options)
|
25
|
+
super(value, options, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
private def resolve_vue_options(options)
|
29
|
+
if options[:v].kind_of?(Hash)
|
30
|
+
h = options.delete(:v)
|
31
|
+
h.each do |key, value|
|
32
|
+
case key
|
33
|
+
when /\A[:@]/
|
34
|
+
options[key] = value
|
35
|
+
else
|
36
|
+
options[:"v-#{key}"] = value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
11
41
|
end
|
12
42
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vue-form-for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tsutomu KURODA
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A custom Rails form builder for Vue.js
|
14
14
|
email: t-kuroda@oiax.jp
|