vue-rails-form-builder 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eef8da7255dabca2715386145fcc26924d6c57d2
4
+ data.tar.gz: cc4b535b836f76ebcc0da4e868f9db6be0e4456e
5
+ SHA512:
6
+ metadata.gz: 6ae0a9d420c239f59a6e50df6e89d5be1a540317a56db421ee5adc59d42e2b28c18fd68f27fc7bdc95406e1926f30cc2a428bbaaeec19427f182f63ee9b3a161
7
+ data.tar.gz: 52a4b0bc39f1d19aeb18080a66220b85d687de7ee134f95eff16878897ad485c4bd9adace00b38a0662b1f92d594b6c5b0e3d6ba779f3c8fe8a8fdc9aba676be
@@ -0,0 +1,38 @@
1
+ # CHANGELOG - vue-rails-form-builder
2
+
3
+ ## 0.5.0 (2017-05-12)
4
+
5
+ * Change the name of this gem to `vue-rails-form-builder` from `vue-form-for`
6
+
7
+ ## 0.4.0 (2017-05-11)
8
+
9
+ * Support SELECT tag.
10
+
11
+ ## 0.3.3 (2017-05-09)
12
+
13
+ * Generate v-model attr for check boxes and radio buttons.
14
+
15
+ ## 0.3.2 (2017-05-09)
16
+
17
+ * Generate correct value for v-model on nested form.
18
+
19
+ ## 0.3.1 (2017-04-11)
20
+
21
+ * Fix `check_box` and `radio_button` helper methods.
22
+
23
+ ## 0.3.0 (2017-04-09)
24
+
25
+ * Add `vue_tag` and `vue_content_tag` helper methods
26
+ that can handle `:bind`, `:on`, `:checked`, `:disabled`, `:multiple`,
27
+ `:readonly`, `:selected`, `:text`, `:html`, `:show`, `:if`, `:else`,
28
+ `:else_if`, `:for`, `:model`, `:pre`, `:cloak`, `:once` options.
29
+ * Add similar functionalities to the form building helpers.
30
+ * Remove `:v` option handling.
31
+
32
+ ## 0.2.0 (2017-04-07)
33
+
34
+ * Allow the form builder to resolve `:v` option.
35
+
36
+ ## 0.1.0 (2017-04-05)
37
+
38
+ * The first release with minimum functionalities.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Tsutomu Kuroda
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.
@@ -0,0 +1,223 @@
1
+ vue-rails-form-builder
2
+ ======================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/vue-rails-form-builder.svg)](https://badge.fury.io/rb/vue-rails-form-builder)
5
+
6
+ A custom Rails form builder for Vue.js
7
+
8
+ Synopsis
9
+ --------
10
+
11
+ ```erb
12
+ <%= vue_form_for User.new do |f| %>
13
+ <%= f.text_field :name %>
14
+ <% end %>
15
+ ```
16
+
17
+ ```html
18
+ <form ...>
19
+ ...
20
+ <input v-model="user.name" type="text" name="user[name]" ... />
21
+ </form>
22
+ ```
23
+
24
+ Installation
25
+ ------------
26
+
27
+ Add the following line to `Gemfile`:
28
+
29
+ ```ruby
30
+ gem "vue-rails-form-builder"
31
+ ```
32
+
33
+ Run `bundle install` on the terminal.
34
+
35
+ Usage
36
+ -----
37
+
38
+ ```erb
39
+ <%= vue_form_for User.new do |f| %>
40
+ <%= f.text_field :name %>
41
+ <%= f.submit "Create" %>
42
+ <% end %>
43
+ ```
44
+
45
+ The above ERB template will be rendered into the following HTML fragment:
46
+
47
+ ```html
48
+ <form class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
49
+ <input name="utf8" type="hidden" value="&#x2713;" />
50
+ <input type="hidden" name="authenticity_token" value="..." />
51
+ <input v-model="user.name" type="text" name="user[name]" id="user_name" />
52
+ <input type="submit" name="commit" value="Create" />
53
+ </form>
54
+ ```
55
+
56
+ Note that the third `<input>` element has a `v-model` attriubte, which can be
57
+ interpreted by Vue.js as the _directive_ to create two-way data bindings between
58
+ form fields and component's data.
59
+
60
+ If you are using the [Webpacker](https://github.com/rails/webpacker),
61
+ create `app/javascript/packs/new_user_form.js` with following code:
62
+
63
+ ```javascript
64
+ import Vue from 'vue/dist/vue.esm'
65
+
66
+ document.addEventListener("DOMContentLoaded", () => {
67
+ const NewUserForm = new Vue({
68
+ el: "#new_user",
69
+ data: {
70
+ user: {
71
+ name: ""
72
+ }
73
+ }
74
+ })
75
+ })
76
+ ```
77
+
78
+ Add this line to the ERB template:
79
+
80
+ ```erb
81
+ <%= javascript_pack_tag "new_user_form" %>
82
+ ```
83
+
84
+ Then, you can get the value of `user[name]` field by the `user.name`.
85
+
86
+ Tag Helper
87
+ ----------
88
+
89
+ This gem provides two additional helper methods: `vue_tag` and `vue_content_tag`.
90
+
91
+ Basically, they behave like `tag` and `content_tag` helpers of Action Views.
92
+ But, they interpret the *HTML options* in a different way as explained below.
93
+
94
+ ### The `:bind` option
95
+
96
+ If the *HTML options* have a `:bind` key and its value is a hash,
97
+ they get transformed into the Vue.js `v-bind` directives.
98
+
99
+ In the example below, these two lines have the same result:
100
+
101
+ ```erb
102
+ <%= vue_content_tag(:span, "Hello", bind: { style: "{ color: textColor }" }) %>
103
+ <%= vue_content_tag(:span, "Hello", "v-bind:style" => "{ color: textColor }" }) %>
104
+ ```
105
+
106
+ Note that you should use the latter style if you want to specify *modifiers*
107
+ to the `v-bind` directives. For example:
108
+
109
+ ```erb
110
+ <%= vue_content_tag(:span, "Hello", "v-bind:text-content.prop" => "message" }) %>
111
+ ```
112
+
113
+ ### The `:on` option
114
+
115
+ If the *HTML options* have a `:on` key and its value is a hash,
116
+ they get transformed into the Vue.js `v-on` directives.
117
+
118
+ In the example below, these two lines have the same result:
119
+
120
+ ```erb
121
+ <%= vue_content_tag(:span, "Hello", on: { click: "doThis" }) %>
122
+ <%= vue_content_tag(:span, "Hello", "v-on:click" => "doThis" }) %>
123
+ ```
124
+
125
+ Note that you should use the latter style if you want to specify *modifiers*
126
+ to the `v-on` directives. For example:
127
+
128
+ ```erb
129
+ <%= vue_content_tag(:span, "Hello", "v-on:click.once" => "doThis" }) %>
130
+ <%= vue_content_tag(:button, "Hello", "v-on:click.prevent" => "doThis" }) %>
131
+ ```
132
+
133
+ ### Boolean attributes
134
+
135
+ If the *HTML options* have a string value (not a boolean value)
136
+ for `checked`, `disabled`, `multiple`, `readonly` or `selected` key,
137
+ the key gets transformed by adding `v-bind:` to its head.
138
+
139
+ In the example below, these two lines have the same result:
140
+
141
+ ```erb
142
+ <%= vue_content_tag(:button, "Click me!", disabled: "!clickable") %>
143
+ <%= vue_content_tag(:button, "Click me!", "v-bind:disabled" => "!clickable") %>
144
+ ```
145
+
146
+ If you want to add a normal attribute without `v-bind:` prefix,
147
+ specify `true` (boolean) to these keys:
148
+
149
+ ```erb
150
+ <%= vue_content_tag(:button, "Click me!", disabled: true) %>
151
+ ```
152
+
153
+ This line produces the following HTML fragment:
154
+
155
+ ```html
156
+ <button disabled="disabled">Click me!</button>
157
+ ```
158
+
159
+
160
+ ### Vue.js directives
161
+
162
+ If the *HTML options* have one or more of the following keys
163
+
164
+ > `text`, `html`, `show`, `if`, `else`, `else_if`, `for`, `model`, `pre`, `cloak`, `once`
165
+
166
+ then, these keys get transformed by adding `v-` to their head.
167
+
168
+ In the example below, these two lines have the same result:
169
+
170
+ ```erb
171
+ <%= vue_tag(:hr, if: "itemsPresent") %>
172
+ <%= vue_tag(:hr, "v-if" => "itemsPresent") %>
173
+ ```
174
+
175
+ Note that the `:else_if` key is transformed into the `v-else-if` directive:
176
+
177
+ ```erb
178
+ <%= vue_tag(:hr, else_if: "itemsPresent") %>
179
+ <%= vue_tag(:hr, "v-else-if" => "itemsPresent") %>
180
+ ```
181
+
182
+ ### Extensions to the form building helpers
183
+
184
+ When you build HTML forms using `vue_form_for`,
185
+ the form building helpers, such as `text_field`, `check_box`, etc.,
186
+ have these additional behavior.
187
+
188
+ Example:
189
+
190
+ ```erb
191
+ <%= vue_form_for User.new do |f| %>
192
+ <%= f.text_field :name, model: "userName" %>
193
+ <label>
194
+ <%= f.check_box :administrator, on: { click: "doThis" } %> Administrator
195
+ </label>
196
+ <%= f.submit "Create", disabled: "!submittable" %>
197
+ <% end %>
198
+ ```
199
+
200
+ Data Initialization
201
+ -------------------
202
+
203
+ As the official Vue.js document says:
204
+
205
+ > `v-model` will ignore the initial `value`, `checked` or `selected` attributes
206
+ > found on any form elements.
207
+ > (https://vuejs.org/v2/guide/forms.html)
208
+
209
+ Because of this, all form controls get reset after the Vue component is mounted.
210
+
211
+ However, you can use
212
+ [vue-data-scooper](https://github.com/kuroda/vue-data-scooper) plugin
213
+ in order to keep the original state of the form.
214
+
215
+ License
216
+ -------
217
+
218
+ The `initial-test-data` is distributed under the MIT license. ([MIT-LICENSE](https://github.com/oiax/initial-test-data/blob/master/MIT-LICENSE))
219
+
220
+ Author
221
+ ------
222
+
223
+ Tsutomu Kuroda (t-kuroda@oiax.jp)
@@ -0,0 +1,2 @@
1
+ require "vue-rails-form-builder/form_builder"
2
+ require "vue-rails-form-builder/railtie"
@@ -0,0 +1,51 @@
1
+ require_relative "./vue_options_resolver"
2
+
3
+ module VueRailsFormBuilder
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+ include VueRailsFormBuilder::VueOptionsResolver
6
+
7
+ (field_helpers - [:label, :check_box, :radio_button, :fields_for])
8
+ .each do |selector|
9
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
10
+ def #{selector}(method, options = {})
11
+ resolve_vue_options(options)
12
+ add_v_model_attribute(method, options)
13
+ super(method, options)
14
+ end
15
+ RUBY_EVAL
16
+ end
17
+
18
+ def label(method, text = nil, options = {}, &block)
19
+ resolve_vue_options(options)
20
+ super(method, text, options, &block)
21
+ end
22
+
23
+ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
24
+ resolve_vue_options(options)
25
+ add_v_model_attribute(method, options)
26
+ super(method, options, checked_value, unchecked_value)
27
+ end
28
+
29
+ def radio_button(method, tag_value, options = {})
30
+ resolve_vue_options(options)
31
+ add_v_model_attribute(method, options)
32
+ super(method, tag_value, options)
33
+ end
34
+
35
+ def select(method, choices = nil, options = {}, html_options = {}, &block)
36
+ resolve_vue_options(html_options)
37
+ add_v_model_attribute(method, html_options)
38
+ super(method, choices, options, html_options, &block)
39
+ end
40
+
41
+ def submit(value = nil, options = {})
42
+ resolve_vue_options(options)
43
+ super(value, options)
44
+ end
45
+
46
+ def button(value = nil, options = {}, &block)
47
+ resolve_vue_options(options)
48
+ super(value, options, &block)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ module VueRailsFormBuilder
2
+ module FormHelper
3
+ def vue_form_for(record, options = {}, &block)
4
+ options[:builder] ||= VueRailsFormBuilder::FormBuilder
5
+ form_for(record, options, &block)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ require "vue-rails-form-builder/form_helper"
2
+ require "vue-rails-form-builder/tag_helper"
3
+
4
+ module VueRailsFormBuilder
5
+ class Railtie < Rails::Railtie
6
+ initializer "vue-rails-form-builder.view_form_helper" do
7
+ ActiveSupport.on_load :action_view do
8
+ include VueRailsFormBuilder::FormHelper
9
+ include VueRailsFormBuilder::TagHelper
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "./vue_options_resolver"
2
+
3
+ module VueRailsFormBuilder
4
+ module TagHelper
5
+ include VueRailsFormBuilder::VueOptionsResolver
6
+
7
+ def vue_tag(name, options = nil, open = false, escape = true)
8
+ resolve_vue_options(options) if options
9
+ tag(name, options, open, escape)
10
+ end
11
+
12
+ def vue_content_tag(name, content_or_options_with_block = nil, options = nil,
13
+ escape = true, &block)
14
+
15
+ if block_given?
16
+ if content_or_options_with_block.is_a?(Hash)
17
+ resolve_vue_options(content_or_options_with_block)
18
+ end
19
+ else
20
+ resolve_vue_options(options) if options
21
+ end
22
+
23
+ content_tag(name, content_or_options_with_block, options, escape, &block)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,47 @@
1
+ module VueRailsFormBuilder
2
+ module VueOptionsResolver
3
+ private def resolve_vue_options(options)
4
+ if options[:bind].kind_of?(Hash)
5
+ h = options.delete(:bind)
6
+ h.each do |key, value|
7
+ if value.kind_of?(String)
8
+ options[:"v-bind:#{key}"] = value
9
+ end
10
+ end
11
+ end
12
+
13
+ if options[:on].kind_of?(Hash)
14
+ h = options.delete(:on)
15
+ h.each do |key, value|
16
+ if value.kind_of?(String)
17
+ options[:"v-on:#{key}"] = value
18
+ end
19
+ end
20
+ end
21
+
22
+ %i(checked disabled multiple readonly selected).each do |attr_name|
23
+ if options[attr_name].kind_of?(String)
24
+ options[:"v-bind:#{attr_name}"] = options.delete(attr_name)
25
+ end
26
+ end
27
+
28
+ %i(text html show if else else_if for model).each do |directive|
29
+ if options[directive].kind_of?(String)
30
+ options[:"v-#{directive.to_s.dasherize}"] = options.delete(directive)
31
+ end
32
+ end
33
+
34
+ %i(pre cloak once).each do |directive|
35
+ if options[directive]
36
+ options.delete(directive)
37
+ options[:"v-#{directive}"] = "v-#{directive}"
38
+ end
39
+ end
40
+ end
41
+
42
+ private def add_v_model_attribute(method, options)
43
+ namespace = @object_name.gsub(/\[/, ".").gsub(/\]/, "")
44
+ options[:"v-model"] ||= "#{namespace}.#{method}"
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vue-rails-form-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Tsutomu KURODA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: railties
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '4.2'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '4.2'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6'
53
+ description: 'This gem provides three view helpers for Rails app: vue_form_for, vue_tag,
54
+ vue_content_tag.'
55
+ email: t-kuroda@oiax.jp
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - CHANGELOG.md
61
+ - MIT-LICENSE
62
+ - README.md
63
+ - lib/vue-rails-form-builder.rb
64
+ - lib/vue-rails-form-builder/form_builder.rb
65
+ - lib/vue-rails-form-builder/form_helper.rb
66
+ - lib/vue-rails-form-builder/railtie.rb
67
+ - lib/vue-rails-form-builder/tag_helper.rb
68
+ - lib/vue-rails-form-builder/vue_options_resolver.rb
69
+ homepage: https://github.com/kuroda/vue-rails-form-builder
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.2.2
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.11
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A custom Rails form builder for Vue.js
93
+ test_files: []