forme 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fb6883778ebb616402a910c3f46f7bd6ba3734b
4
- data.tar.gz: aed74dd741f94bb368e97390d14a4000f4b73f39
3
+ metadata.gz: 315de851244fa7fd0ccb18fb0d57aa53204fff22
4
+ data.tar.gz: c880954089f9c59174abdf75eed937a2fddde4a0
5
5
  SHA512:
6
- metadata.gz: 297661c5e245fcbb2f7c671d59c79f77b4d07d3bcded836ace5c7a6049f237b3d7711923178dcfcfdc3ac704a654b491c217112d114f033bd9d241f2c3f9b2f4
7
- data.tar.gz: 68d7a1d1adbae977f2c26649a72633ae2c954f8701bca70883153eac44fe86113aedd1a47015ad261f2e91ad119945aab56355d54480dda0782bc11896557b26
6
+ metadata.gz: d598b9cb754eb1973685c6b830451a78b3fbf18e0477373a8021966d0f5b210756fdab2f1209ed5b83cf481f530f8e02995776a829555266233bc008842a163f
7
+ data.tar.gz: a321b164bc8a347573c12cdd1b4bd1e786c45e82c06de27d3219256b8523acdab4e80153dbd4bf7bc82cf6929714987d1002eb5ff27ab043d717ef0c37908814
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ === 1.1.0 (2014-09-07)
2
+
3
+ * Make form without a block in the ERB integration still use hidden tags if method is post (jeremyevans)
4
+
5
+ * Add Forme::RawString class, Forme.raw, and Form#raw for creating raw strings, so you don't need to use extend(Forme::Raw) (jeremyevans)
6
+
7
+ * Allow using nil as explicit transformer value when creating a Form, to disable default transformer (jeremyevans)
8
+
1
9
  === 1.0.0 (2014-07-30)
2
10
 
3
11
  * Add roda plugin for forme (jeremyevans)
@@ -34,17 +34,17 @@ For example, these both compile by default to the same select tag:
34
34
  The processing of high level <tt>Forme::Input</tt>s into raw html
35
35
  data is broken down to the following steps (called transformers):
36
36
 
37
- * +Formatter+: converts a <tt>Forme::Input</tt> instance into a
38
- <tt>Forme::Tag</tt> instance (or array of them).
39
- * +ErrorHandler+: If the <tt>Forme::Input</tt> instance has a error,
40
- takes the formatted tag and marks it as having the error.
41
- * +Labeler+: If the <tt>Forme::Input</tt> instance has a label,
42
- takes the formatted output and labels it.
43
- * +Wrapper+: Takes the output of the formatter, labeler, and
44
- error_handler transformers, and wraps it in another tag (or just
45
- returns it unmodified).
46
- * +Serializer+: converts a <tt>Forme::Tag</tt> instance into an
47
- html string.
37
+ +Formatter+ :: converts a <tt>Forme::Input</tt> instance into a
38
+ <tt>Forme::Tag</tt> instance (or array of them).
39
+ +ErrorHandler+ :: If the <tt>Forme::Input</tt> instance has a error,
40
+ takes the formatted tag and marks it as having the error.
41
+ +Labeler+ :: If the <tt>Forme::Input</tt> instance has a label,
42
+ takes the formatted output and labels it.
43
+ +Wrapper+ :: Takes the output of the formatter, labeler, and
44
+ error_handler transformers, and wraps it in another tag (or just
45
+ returns it unmodified).
46
+ +Serializer+ :: converts a <tt>Forme::Tag</tt> instance into an
47
+ html string.
48
48
 
49
49
  Technically, only the +Serializer+ is necessary. The +input+
50
50
  and +tag+ methods return +Input+ and +Tag+ objects. These objects
@@ -245,7 +245,7 @@ and the second argument if given should be a hash of tag attributes. This allows
245
245
  inputs inside tags:
246
246
 
247
247
  Forme.form do |f|
248
- f.tag(:span, :class="foo") do
248
+ f.tag(:span, :class=>"foo") do
249
249
  f.input(:text)
250
250
  end
251
251
  end
@@ -503,11 +503,12 @@ subform options:
503
503
  an :inputs option or are using a block with additional inputs,
504
504
  you should specify this option.
505
505
 
506
- = Sinatra Support
506
+ = ERB Support
507
507
 
508
- Forme ships with a Sinatra extension that you can get by <tt>require "forme/sinatra"</tt> and using
509
- <tt>helpers Forme::Sinatra::ERB</tt> in your Sinatra::Base subclass. It allows you to use the
510
- following API in your Sinatra ERB forms:
508
+ Forme ships with a ERB extension that you can get by <tt>require "forme/erb"</tt> and using
509
+ <tt>including Forme::ERB::Helper</tt>. This is tested to support ERB templates in both the
510
+ Sinatra and Roda web frameworks. It allows you to use the following API in your erb
511
+ templates:
511
512
 
512
513
  <% form(@obj, :action=>'/foo') do |f| %>
513
514
  <%= f.input(:field) %>
@@ -516,8 +517,8 @@ following API in your Sinatra ERB forms:
516
517
  <% end %>
517
518
  <% end %>
518
519
 
519
- This example is for ERB/Erubis. Other Sinatra template libraries work differently and
520
- do not support this integration.
520
+ In order to this to work transparently, the ERB outvar needs to be <tt>@_out_buf</tt>. If that
521
+ is not the case, use the :output option to +form+ to specify the outvar.
521
522
 
522
523
  = Rails Support
523
524
 
@@ -77,6 +77,12 @@ module Forme
77
77
  classes.compact.join(' ')
78
78
  end
79
79
 
80
+ # Create a RawString using the given string, which will disable automatic
81
+ # escaping for that string.
82
+ def self.raw(s)
83
+ RawString.new(s)
84
+ end
85
+
80
86
  # If there is a related transformer, call it with the given +args+ and +block+.
81
87
  # Otherwise, attempt to return the initial input without modifying it.
82
88
  def self.transform(type, trans_name, default_opts, *args, &block)
@@ -217,7 +223,9 @@ module Forme
217
223
  when Symbol
218
224
  @opts[t] = Forme.transformer(t, @opts[t], @opts)
219
225
  when nil
220
- @opts[t] = Forme.transformer(t, config, @opts)
226
+ unless @opts.has_key?(t)
227
+ @opts[t] = Forme.transformer(t, config, @opts)
228
+ end
221
229
  end
222
230
  end
223
231
 
@@ -291,8 +299,8 @@ module Forme
291
299
  # You can use array arguments if you want inputs to be created with specific
292
300
  # options:
293
301
  #
294
- # inputs([:field1, :field2])
295
- # inputs([[:field1, {:name=>'foo'}], :field2])
302
+ # f.inputs([:field1, :field2])
303
+ # f.inputs([[:field1, {:name=>'foo'}], :field2])
296
304
  #
297
305
  # The given +opts+ are passed to the +inputs_wrapper+, and the default
298
306
  # +inputs_wrapper+ supports a <tt>:legend</tt> option that is used to
@@ -306,11 +314,15 @@ module Forme
306
314
  #
307
315
  # This can also be called with a single hash argument to just use an options hash:
308
316
  #
309
- # inputs(:legend=>'Foo'){...}
317
+ # f.inputs(:legend=>'Foo') do
318
+ # # ...
319
+ # end
310
320
  #
311
321
  # or even without any arguments:
312
322
  #
313
- # inputs{...}
323
+ # f.inputs do
324
+ # # ...
325
+ # end
314
326
  def inputs(inputs=[], opts={}, &block)
315
327
  _inputs(inputs, opts, &block)
316
328
  end
@@ -413,6 +425,11 @@ module Forme
413
425
  end
414
426
  end
415
427
 
428
+ # Return a new string that will not be html escaped by the default serializer.
429
+ def raw(s)
430
+ Forme.raw(s)
431
+ end
432
+
416
433
  # Marks the string as containing already escaped output. Returns string given
417
434
  # by default, but subclasses for specific web frameworks can handle automatic
418
435
  # html escaping by overriding this.
@@ -611,6 +628,12 @@ module Forme
611
628
  module Raw
612
629
  end
613
630
 
631
+ # A String subclass that includes Raw, which will cause the default
632
+ # serializer to no longer html escape the string.
633
+ class RawString < ::String
634
+ include Raw
635
+ end
636
+
614
637
  # The default formatter used by the library. Any custom formatters should
615
638
  # probably inherit from this formatter unless they have very special needs.
616
639
  #
@@ -91,13 +91,10 @@ module Forme
91
91
  # Create a +Form+ object tied to the current output buffer,
92
92
  # using the standard ERB hidden tags.
93
93
  def form(obj=nil, attr={}, opts={}, &block)
94
- if block
95
- h = {:output=>@_out_buf, :hidden_tags=>Forme::ERB::HIDDEN_TAGS, :env=>env}
96
- (obj.is_a?(Hash) ? attr = attr.merge(h) : opts = opts.merge(h))
97
- Form.form(obj, attr, opts, &block)
98
- else
99
- Form.form(obj, attr, opts)
100
- end
94
+ h = {:hidden_tags=>Forme::ERB::HIDDEN_TAGS, :env=>env}
95
+ h[:output] = @_out_buf if block
96
+ (obj.is_a?(Hash) ? attr = attr.merge(h) : opts = opts.merge(h))
97
+ Form.form(obj, attr, opts, &block)
101
98
  end
102
99
  end
103
100
  end
@@ -1,6 +1,6 @@
1
1
  module Forme
2
2
  # Version constant, use <tt>Forme.version</tt> instead.
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
 
5
5
  # Returns the version as a frozen string (e.g. '0.1.0')
6
6
  def self.version
@@ -119,6 +119,10 @@ END
119
119
  erb "<%= form([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'123') %>"
120
120
  end
121
121
 
122
+ r.get 'noblock_post' do
123
+ erb "<%= form({:method=>:post}, :button=>'xyz') %>"
124
+ end
125
+
122
126
  r.get 'noblock_empty' do
123
127
  erb "<%= form(:action=>'/baz') %>"
124
128
  end
@@ -171,6 +175,10 @@ shared_examples_for "erb integration" do
171
175
  sin_get('/noblock').should == '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
172
176
  end
173
177
 
178
+ specify "#form should work without a block and still have hidden tags emitted" do
179
+ sin_get('/noblock_post').sub(%r{<input name=\"_csrf\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"_csrf\" type=\"hidden\" value=\"csrf\"/>").should == '<form method="post"><input name="_csrf" type="hidden" value="csrf"/><input type="submit" value="xyz"/></form>'
180
+ end
181
+
174
182
  specify "#form with an empty form should work" do
175
183
  sin_get('/noblock_empty').should == '<form action="/baz"></form>'
176
184
  end
@@ -515,10 +515,18 @@ describe "Forme plain forms" do
515
515
  @f.tag(:div, {}, ['<p></p>']).to_s.should == '<div>&lt;p&gt;&lt;/p&gt;</div>'
516
516
  end
517
517
 
518
- specify "should not escape raw tag content" do
518
+ specify "should not escape raw tag content using Forme::Raw" do
519
519
  @f.tag(:div, {}, ['<p></p>'.extend(Forme::Raw)]).to_s.should == '<div><p></p></div>'
520
520
  end
521
521
 
522
+ specify "should not escape raw tag content using Forme.raw" do
523
+ @f.tag(:div, {}, [Forme.raw('<p></p>')]).to_s.should == '<div><p></p></div>'
524
+ end
525
+
526
+ specify "should not escape raw tag content using Form#raw" do
527
+ @f.tag(:div, {}, [@f.raw('<p></p>')]).to_s.should == '<div><p></p></div>'
528
+ end
529
+
522
530
  specify "should escape tag content in attribute values" do
523
531
  @f.tag(:div, :foo=>'<p></p>').to_s.should == '<div foo="&lt;p&gt;&lt;/p&gt;"></div>'
524
532
  end
@@ -671,6 +679,10 @@ describe "Forme custom" do
671
679
  specify "inputs_wrappers can be specified as a proc" do
672
680
  Forme::Form.new(:inputs_wrapper=>proc{|f, opts, &block| f.tag(:div, &block)}).inputs([:textarea]).to_s.should == '<div><textarea></textarea></div>'
673
681
  end
682
+
683
+ specify "can use nil as value to disable default transformer" do
684
+ Forme::Form.new(:labeler=>nil).input(:textarea, :label=>'foo').to_s.should == '<textarea></textarea>'
685
+ end
674
686
  end
675
687
 
676
688
  describe "Forme built-in custom" do
@@ -172,6 +172,10 @@ END
172
172
  render :inline => "<%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'123') %>"
173
173
  end
174
174
 
175
+ def noblock_post
176
+ render :inline => "<%= forme({:method=>'post'}, :button=>'xyz') %>"
177
+ end
178
+
175
179
  def safe_buffer
176
180
  render :inline => "<%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'<b>foo</b>'.html_safe) %>"
177
181
  end
@@ -233,6 +237,10 @@ describe "Forme Rails integration" do
233
237
  sin_get('/noblock').should == '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
234
238
  end
235
239
 
240
+ specify "#form should work without a block with hidden tags" do
241
+ sin_get('/noblock_post').sub(%r{<input name=\"authenticity_token\" type=\"hidden\" value=\"([^\"]+)\"/>}, "<input name=\"authenticity_token\" type=\"hidden\" value=\"csrf\"/>").should == '<form method="post"><input name="authenticity_token" type="hidden" value="csrf"/><input type="submit" value="xyz"/></form>'
242
+ end
243
+
236
244
  specify "#form should handle Rails SafeBuffers" do
237
245
  sin_get('/safe_buffer').should == '<form action="/baz"><fieldset class="inputs"><legend><b>foo</b></legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
238
246
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forme
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-30 00:00:00.000000000 Z
11
+ date: 2014-09-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Forme is a forms library with the following goals: