vue-form-for 0.1.0 → 0.2.0

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
  SHA1:
3
- metadata.gz: 141a5e932e4ee1bbe1e69e52640428b0204c51bd
4
- data.tar.gz: 39695da0d14cd099694f0806e91c266347d64862
3
+ metadata.gz: '019def4fa71dae16fcdac1cc36ac206726a99ab3'
4
+ data.tar.gz: 9733ade5ba2ed0b3d15daefa1c112250c03d00ae
5
5
  SHA512:
6
- metadata.gz: 042d7b803638e3d24b06ee0c1dc3457f12f1903caaaa522468846d5b3a7a163711a1daf0b97e7132423f69b432dc09804b1fff4ba3fc48dd6aded3a6bbbaae63
7
- data.tar.gz: dda89dbfc4aed523577d97afe2034b64828d6c0b792188283f47f6fc0ec4b90b7fdfd144c759aef0f6c367b6d77e20729f0d11c5789f0879c7c0628d03a9b8fc
6
+ metadata.gz: ce25f0da67e1aac93b87aaa26a80b6cfa057ade399f5b2c6b019069ccc4f9da8447fe4cdca3b091920a9d9c44e229a2881716c367de96f817566d48807e1fd5a
7
+ data.tar.gz: 18f9ad7bd787e45a3ea71c377d3437d1d1c2ac3361322c538b0a5eb694fd684518545043eaa47f33ab08bdf039cd2e715bb2f78ebce9f1a9d2d4c461c2e1c97c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG - vue-form-for
2
2
 
3
+ ## 0.2.0 (2017-04-07)
4
+
5
+ * Allow the form builder to resolve `:v` option.
6
+
3
7
  ## 0.1.0 (2017-04-05)
4
8
 
5
9
  * The first release with minimum functionalities.
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="&#x2713;" />
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.1.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-05 00:00:00.000000000 Z
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