express_templates 0.3.2 → 0.3.4
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 +4 -4
- data/lib/express_templates/components/form_for.rb +93 -28
- data/lib/express_templates/version.rb +1 -1
- data/test/components/form_for_test.rb +54 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5d21da31290cc55f88dced62806bcda510084f7
|
4
|
+
data.tar.gz: 9dd96eb7a6c1cf233d0bf11c65511f1515c7fb30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b091217942e9c4ccc1e92e250f7dab0623e23bd204e3c965301eee2da6b0ff6aff9457362c087f661d2f66bbf9abd25603df29dc6d2c8f67ea6896b765fd3d50
|
7
|
+
data.tar.gz: 97561ff9e8624760dc595d96a873ef56d0893669460cc16f2a59517ac9672934f4bb1e89f6a056fc4331da95eb8426bdc9632746081e55e16d43d3508b27836d
|
@@ -5,7 +5,7 @@ module ExpressTemplates
|
|
5
5
|
#
|
6
6
|
# Example:
|
7
7
|
#
|
8
|
-
# ````
|
8
|
+
# ````
|
9
9
|
# form_for(:people) do |f|
|
10
10
|
# f.text_field :name
|
11
11
|
# f.email_field :email
|
@@ -53,7 +53,7 @@ module ExpressTemplates
|
|
53
53
|
#
|
54
54
|
# Example:
|
55
55
|
#
|
56
|
-
# ````
|
56
|
+
# ````
|
57
57
|
# form_for(:posts) do
|
58
58
|
# f.text_field :title, wrapped_class: 'string optional'
|
59
59
|
# end
|
@@ -71,7 +71,7 @@ module ExpressTemplates
|
|
71
71
|
#
|
72
72
|
# In addition to this, label text can also be customized using the `label` option:
|
73
73
|
#
|
74
|
-
# ````
|
74
|
+
# ````
|
75
75
|
# f.email_field :email_address, label: 'Your Email'
|
76
76
|
# ````
|
77
77
|
#
|
@@ -86,7 +86,10 @@ module ExpressTemplates
|
|
86
86
|
|
87
87
|
def initialize(*args)
|
88
88
|
# TODO: need a better way to select the form options
|
89
|
-
@form_options = args.
|
89
|
+
if @form_options = args.find { |x| x.is_a?(Hash) }
|
90
|
+
@_method = @form_options.delete :method
|
91
|
+
end
|
92
|
+
|
90
93
|
super(*args)
|
91
94
|
_process_args!(args) # from Configurable
|
92
95
|
yield(self) if block_given?
|
@@ -98,7 +101,7 @@ module ExpressTemplates
|
|
98
101
|
#
|
99
102
|
# Example:
|
100
103
|
#
|
101
|
-
# ````
|
104
|
+
# ````
|
102
105
|
# form_for(:people) do |f|
|
103
106
|
# f.phone_field :phone
|
104
107
|
# end
|
@@ -106,17 +109,27 @@ module ExpressTemplates
|
|
106
109
|
#
|
107
110
|
# This will precompile as
|
108
111
|
#
|
109
|
-
# ````
|
112
|
+
# ````
|
110
113
|
# ...
|
111
114
|
# phone_field_tag :phone, @people.phone, {}
|
112
115
|
# ...
|
113
116
|
# ````
|
114
117
|
#
|
118
|
+
# You can also add html options to the form (add classes, id, etc)
|
119
|
+
#
|
120
|
+
# ````
|
121
|
+
# form_for(:people, html_options: {class: 'edit_form', id: 'people_form'}) do |f|
|
122
|
+
# f.phone_field :phone
|
123
|
+
# end
|
124
|
+
# ````
|
125
|
+
#
|
126
|
+
# # <form action='/people' method='post' id='people_form' class='edit_form'>
|
127
|
+
#
|
115
128
|
# Fields can also have custom classes via the `class` option:
|
116
129
|
#
|
117
130
|
# Example:
|
118
131
|
#
|
119
|
-
# ````
|
132
|
+
# ````
|
120
133
|
# f.url_field :website_url, class: 'url-string'
|
121
134
|
# ````
|
122
135
|
#
|
@@ -134,7 +147,7 @@ module ExpressTemplates
|
|
134
147
|
#
|
135
148
|
#
|
136
149
|
# = Fields
|
137
|
-
# ````
|
150
|
+
# ````
|
138
151
|
# f.text_field :name
|
139
152
|
# # <div>
|
140
153
|
# # <label for="person_name">Name</label>
|
@@ -257,26 +270,19 @@ module ExpressTemplates
|
|
257
270
|
@fields << Field.new(name, options, :submit)
|
258
271
|
end
|
259
272
|
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
form_action = if form_method == :patch
|
272
|
-
%Q(/#{resource_name.pluralize}/{{@#{resource_name}.id}})
|
273
|
-
else
|
274
|
-
%Q(/#{resource_name.pluralize})
|
275
|
-
end
|
276
|
-
|
277
|
-
form_args = {action: form_action, method: :post}
|
278
|
-
form_args.merge!(form_options) unless form_options.nil?
|
273
|
+
# You can add extra actions to a form instead of just submit
|
274
|
+
# ==== Examples
|
275
|
+
# f.actions({submit: ['Save', {class: 'submit primary'}], cancel: ['Cancel it', class: 'cancel secondary']})
|
276
|
+
# # <div>
|
277
|
+
# # <input type="submit" name="submit primary" value: "Save" />
|
278
|
+
# # <a href="#" onclick="return false;" class="cancel secondary">Cancel it</a>
|
279
|
+
# # </div>
|
280
|
+
def actions(extra_actions)
|
281
|
+
@fields ||= []
|
282
|
+
@fields << Actions.new(extra_actions)
|
283
|
+
end
|
279
284
|
|
285
|
+
emits -> {
|
280
286
|
form(form_args) {
|
281
287
|
form_rails_support form_method
|
282
288
|
fields.each do |field|
|
@@ -284,7 +290,7 @@ module ExpressTemplates
|
|
284
290
|
field_type = field.type.to_s
|
285
291
|
resource_field_name = "#{resource_name.singularize}[#{field_name}]"
|
286
292
|
label_name = "#{resource_name.singularize}_#{field_name}"
|
287
|
-
field_label = field.label || field_name.to_s.
|
293
|
+
field_label = field.label || field_name.to_s.titleize
|
288
294
|
|
289
295
|
div(class: field.wrapper_class) {
|
290
296
|
if field_type == 'select'
|
@@ -302,6 +308,18 @@ module ExpressTemplates
|
|
302
308
|
end
|
303
309
|
elsif field_type == 'submit'
|
304
310
|
submit_tag(field_name, field.options)
|
311
|
+
elsif field_type == 'actions'
|
312
|
+
field.extra_actions.each do |action|
|
313
|
+
action_type = action.first
|
314
|
+
action_options = action.last
|
315
|
+
|
316
|
+
if action_type == :submit
|
317
|
+
submit_tag(action_options.first, action_options.last)
|
318
|
+
elsif action_type == :cancel
|
319
|
+
default_opts = {href: '#', onclick: 'return false;'}.merge!(action_options.last)
|
320
|
+
a(default_opts) { action_options.first }
|
321
|
+
end
|
322
|
+
end
|
305
323
|
else
|
306
324
|
label_tag(label_name, field_label) unless field_type == 'hidden'
|
307
325
|
args = [resource_field_name, "{{@#{resource_name.singularize}.#{field_name}}}", field.options]
|
@@ -326,6 +344,42 @@ module ExpressTemplates
|
|
326
344
|
wrap_for_stack_trace(lookup(:markup))
|
327
345
|
end
|
328
346
|
|
347
|
+
private
|
348
|
+
|
349
|
+
def form_method
|
350
|
+
if @_method == :put
|
351
|
+
:patch
|
352
|
+
else
|
353
|
+
@form_options.present? ? @form_options[:method] : :post
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
def _action(resource_name)
|
358
|
+
base_url = %Q(/#{resource_name.pluralize})
|
359
|
+
if form_method == :patch
|
360
|
+
%Q(#{base_url}/{{@#{resource_name}.id}})
|
361
|
+
else
|
362
|
+
base_url
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
def form_args
|
367
|
+
default_args = {action: _action(resource_name), method: :post}
|
368
|
+
|
369
|
+
if @form_options.nil?
|
370
|
+
default_args
|
371
|
+
else
|
372
|
+
if html_options = @form_options.delete(:html_options)
|
373
|
+
@form_options.merge!(html_options)
|
374
|
+
end
|
375
|
+
default_args.merge!(@form_options)
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
def resource_name
|
380
|
+
my[:id].to_s
|
381
|
+
end
|
382
|
+
|
329
383
|
class Field
|
330
384
|
attr :name, :options, :label, :type, :wrapper_class
|
331
385
|
def initialize(name, options = {}, type=:text)
|
@@ -381,6 +435,17 @@ module ExpressTemplates
|
|
381
435
|
end
|
382
436
|
end
|
383
437
|
end
|
438
|
+
|
439
|
+
# need to fix this some day (actions doesn't need to inherit from field)
|
440
|
+
class Actions < Field
|
441
|
+
attr :extra_actions
|
442
|
+
def initialize(extra_actions)
|
443
|
+
@type = :actions
|
444
|
+
@name = ''
|
445
|
+
@label = ''
|
446
|
+
@extra_actions = extra_actions
|
447
|
+
end
|
448
|
+
end
|
384
449
|
end
|
385
450
|
end
|
386
451
|
end
|
@@ -25,33 +25,33 @@ class FormForTest < ActiveSupport::TestCase
|
|
25
25
|
def setup
|
26
26
|
@example_compiled = -> {
|
27
27
|
ExpressTemplates::Components::FormFor.render_in(self) {
|
28
|
-
"<form action=\"/resources\" method=\"
|
28
|
+
"<form action=\"/resources/#{@resource.id}\" method=\"post\" url=\"/posts\" id=\"post_form\">
|
29
29
|
<div style=\"display:none\">
|
30
|
-
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:
|
30
|
+
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:patch)})+%Q(#{token_tag})+"
|
31
31
|
</div>
|
32
32
|
|
33
33
|
<div class=\"\">
|
34
|
-
"+%Q(#{label_tag(
|
34
|
+
"+%Q(#{label_tag("resource_name", "post title")})+%Q(#{text_field_tag("resource[name]", @resource.name, label: "post title")})+"
|
35
35
|
</div>
|
36
36
|
|
37
37
|
<div class=\"\">
|
38
|
-
"+%Q(#{label_tag(
|
38
|
+
"+%Q(#{label_tag("resource_body", "Body")})+%Q(#{text_field_tag("resource[body]", @resource.body, class: "string")})+"
|
39
39
|
</div>
|
40
40
|
|
41
41
|
<div class=\"field input\">
|
42
|
-
"+%Q(#{label_tag(
|
42
|
+
"+%Q(#{label_tag("resource_email", "Email")})+%Q(#{email_field_tag("resource[email]", @resource.email, {})})+"
|
43
43
|
</div>
|
44
44
|
|
45
45
|
<div class=\"\">
|
46
|
-
"+%Q(#{label_tag(
|
46
|
+
"+%Q(#{label_tag("resource_mobile_phone", "Mobile Phone")})+%Q(#{phone_field_tag("resource[mobile_phone]", @resource.mobile_phone, {})})+"
|
47
47
|
</div>
|
48
48
|
|
49
49
|
<div class=\"\">
|
50
|
-
"+%Q(#{label_tag(
|
50
|
+
"+%Q(#{label_tag("resource_url", "Url")})+%Q(#{url_field_tag("resource[url]", @resource.url, {})})+"
|
51
51
|
</div>
|
52
52
|
|
53
53
|
<div class=\"\">
|
54
|
-
"+%Q(#{label_tag(
|
54
|
+
"+%Q(#{label_tag("resource_number", "Number")})+%Q(#{number_field_tag("resource[number]", @resource.number, {})})+"
|
55
55
|
</div>
|
56
56
|
|
57
57
|
<div class=\"\">"+%Q(#{submit_tag("Save it!", {})})+"</div>
|
@@ -70,11 +70,11 @@ class FormForTest < ActiveSupport::TestCase
|
|
70
70
|
def simple_form(resource)
|
71
71
|
ctx = Context.new(resource)
|
72
72
|
fragment = -> {
|
73
|
-
form_for(:resource, method: :put, url: '/
|
73
|
+
form_for(:resource, method: :put, url: '/posts', html_options: {id: 'post_form'}) do |f|
|
74
74
|
f.text_field :name, label: 'post title'
|
75
75
|
f.text_field :body, class: 'string'
|
76
76
|
f.email_field :email, wrapper_class: 'field input'
|
77
|
-
f.phone_field :
|
77
|
+
f.phone_field :mobile_phone
|
78
78
|
f.url_field :url
|
79
79
|
f.number_field :number
|
80
80
|
f.submit 'Save it!'
|
@@ -114,10 +114,52 @@ class FormForTest < ActiveSupport::TestCase
|
|
114
114
|
return ctx, fragment
|
115
115
|
end
|
116
116
|
|
117
|
+
def advanced_form(resource)
|
118
|
+
ctx = Context.new(resource)
|
119
|
+
fragment = -> {
|
120
|
+
form_for(:resource, method: :put, url: '/posts') do |f|
|
121
|
+
f.text_field :name, label: 'post title'
|
122
|
+
f.text_field :body, class: 'string'
|
123
|
+
f.actions({submit: ['Save', {class: 'submit primary'}], cancel: ['Cancel it', class: 'cancel secondary']})
|
124
|
+
end
|
125
|
+
}
|
126
|
+
return ctx, fragment
|
127
|
+
end
|
128
|
+
|
117
129
|
test "fields compiled source is legible and transparent" do
|
118
130
|
ExpressTemplates::Markup::Tag.formatted do
|
119
|
-
|
120
|
-
|
131
|
+
ctx, fragment = simple_form(resource)
|
132
|
+
assert_equal example_compiled_src, ExpressTemplates.compile(&fragment)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
test 'advanced form can have additional actions' do
|
137
|
+
@example_compiled = -> {
|
138
|
+
ExpressTemplates::Components::FormFor.render_in(self) {
|
139
|
+
"<form action=\"/resources/#{@resource.id}\" method=\"post\" url=\"/posts\">
|
140
|
+
<div style=\"display:none\">
|
141
|
+
"+%Q(#{utf8_enforcer_tag})+%Q(#{method_tag(:patch)})+%Q(#{token_tag})+"
|
142
|
+
</div>
|
143
|
+
|
144
|
+
<div class=\"\">
|
145
|
+
"+%Q(#{label_tag("resource_name", "post title")})+%Q(#{text_field_tag("resource[name]", @resource.name, label: "post title")})+"
|
146
|
+
</div>
|
147
|
+
|
148
|
+
<div class=\"\">
|
149
|
+
"+%Q(#{label_tag("resource_body", "Body")})+%Q(#{text_field_tag("resource[body]", @resource.body, class: "string")})+"
|
150
|
+
</div>
|
151
|
+
|
152
|
+
<div class=\"\">
|
153
|
+
"+%Q(#{submit_tag("Save", class: "submit primary")})+"
|
154
|
+
<a href=\"#\" onclick=\"return false;\" class=\"cancel secondary\">Cancel it</a>
|
155
|
+
</div>
|
156
|
+
</form>
|
157
|
+
"
|
158
|
+
}
|
159
|
+
}
|
160
|
+
ExpressTemplates::Markup::Tag.formatted do
|
161
|
+
ctx, fragment = advanced_form(resource)
|
162
|
+
assert_equal example_compiled_src, ExpressTemplates.compile(&fragment)
|
121
163
|
end
|
122
164
|
end
|
123
165
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: express_templates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steven Talcott Smith
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-03-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -286,7 +286,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
286
286
|
version: '0'
|
287
287
|
requirements: []
|
288
288
|
rubyforge_project:
|
289
|
-
rubygems_version: 2.4.
|
289
|
+
rubygems_version: 2.4.6
|
290
290
|
signing_key:
|
291
291
|
specification_version: 4
|
292
292
|
summary: Write HTML templates in declarative Ruby. Create reusable view components.
|