sinatra_more 0.1.9 → 0.1.10

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.
data/README.rdoc CHANGED
@@ -114,6 +114,7 @@ methods should be very familiar to anyone who has used rails view helpers.
114
114
 
115
115
  * <tt>form_tag(url, options={}, &block)</tt>
116
116
  * Constructs a form without object based on options
117
+ * Supports form methods 'put' and 'delete' through hidden field
117
118
  * <tt>form_tag('/register', :class => 'example') { ... }</tt>
118
119
  * <tt>field_set_tag(*args, &block)</tt>
119
120
  * Constructs a field_set to group fields with given options
@@ -126,6 +127,9 @@ methods should be very familiar to anyone who has used rails view helpers.
126
127
  * Constructs a label tag from the given options
127
128
  * <tt>label_tag :username, :class => 'long-label'</tt>
128
129
  * <tt>label_tag(:username, :class => 'blocked-label') { ... }</tt>
130
+ * <tt>hidden_field_tag(name, options={})</tt>
131
+ * Constructs a hidden field input from the given options
132
+ * <tt>hidden_field_tag :session_key, :value => 'secret'</tt>
129
133
  * <tt>text_field_tag(name, options={})</tt>
130
134
  * Constructs a text field input from the given options
131
135
  * <tt>text_field_tag :username, :class => 'long'</tt>
@@ -144,7 +148,7 @@ methods should be very familiar to anyone who has used rails view helpers.
144
148
 
145
149
  A form_tag might look like:
146
150
 
147
- - form_tag '/login', :class => 'login-form' do
151
+ - form_tag '/destroy', :class => 'destroy-form', :method => 'delete' do
148
152
  = flash_tag(:notice)
149
153
  - field_set_tag do
150
154
  %p
@@ -154,7 +158,7 @@ A form_tag might look like:
154
158
  = label_tag :password, :class => 'first'
155
159
  = password_field_tag :password, :value => params[:password]
156
160
  - field_set_tag(:class => 'buttons') do
157
- = submit_tag "Login"
161
+ = submit_tag "Remove"
158
162
 
159
163
  ==== FormBuilders
160
164
 
@@ -171,6 +175,8 @@ The following are fields provided by AbstractFormBuilder that can be used within
171
175
  * <tt>f.label :name, :class => 'long'</tt>
172
176
  * <tt>text_field(field, options={})</tt>
173
177
  * <tt>f.text_field :username, :class => 'long'</tt>
178
+ * <tt>hidden_field(field, options={})</tt>
179
+ * <tt>f.hidden_field :session_id, :class => 'hidden'</tt>
174
180
  * <tt>text_area(field, options={})</tt>
175
181
  * <tt>f.text_area :summary, :class => 'long'</tt>
176
182
  * <tt>password_field(field, options={})</tt>
@@ -228,6 +234,8 @@ and would generate this html:
228
234
 
229
235
  ==== Format Helpers
230
236
 
237
+ * <tt>escape_html</tt> (alias <tt>h</tt> and <tt>h!</tt>)
238
+ * (from RackUtils) Escape ampersands, brackets and quotes to their HTML/XML entities.
231
239
  * <tt>relative_time_ago(date)</tt>
232
240
  * Returns relative time in words referencing the given date
233
241
  * <tt>relative_time_ago(2.days.ago)</tt> => "2 days ago"
data/TODO CHANGED
@@ -5,21 +5,17 @@
5
5
  * Take advantage of shared strategies: http://github.com/hassox/warden_strategies/tree/master/lib/
6
6
  * Make warden password strategy support a callback which explains what to do with username, password
7
7
  * WardenPlugin.authenticate_callback { |username, password| User.authenticate(username, password) }
8
- * Remove dependency on activesupport! and enumerate dependencies in rakefile
9
8
  * Add support for missing formhelpers/fields (check_box, radio_button, ...)
10
- * Add support for forms with method => put, delete using hidden field
11
9
  * Look into creating sinatra generators using rubigen (http://github.com/drnic/rubigen)
12
10
  * http://github.com/quirkey/sinatra-gen
13
11
  * Look into adding any missing helpers from:
14
- * http://github.com/sbfaulkner/sinatra-helpers/tree/master/lib/sinatra-helpers/haml/
15
- * http://github.com/sbfaulkner/sinatra-helpers/blob/master/lib/sinatra-helpers/html/escape.rb
16
- * http://github.com/sbfaulkner/sinatra-helpers/blob/master/lib/sinatra-helpers/haml/links.rb
17
- * http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/mailer/
18
12
  * http://github.com/kelredd/sinatra-helpers/blob/master/lib/sinatra_helpers/erb/links.rb
19
13
  * http://github.com/kelredd/sinatra-helpers/blob/master/lib/sinatra_helpers/erb/forms.rb
20
14
 
21
15
  = COMPLETED
22
16
 
17
+ * Add support for forms with method => put, delete using hidden field
18
+ * Remove dependency on activesupport! and enumerate dependencies in rakefile (as much as possible, need inflectors)
23
19
  * Pull from sinatra-helpers and make erb templates work (and credit keldredd)
24
20
  * http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
25
21
  * fix content_block_tag to eliminate need for concat option
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.1.10
@@ -1,83 +1,89 @@
1
1
  class AbstractFormBuilder
2
2
  attr_accessor :template, :object
3
-
3
+
4
4
  def initialize(template, object)
5
5
  raise "FormBuilder template must be initialized!" unless template
6
6
  raise "FormBuilder object must be initialized!" unless object
7
7
  @template = template
8
8
  @object = object
9
9
  end
10
-
10
+
11
11
  # f.error_messages
12
12
  def error_messages(options={})
13
13
  @template.error_messages_for(@object, options)
14
14
  end
15
-
15
+
16
16
  # f.label :username, :caption => "Nickname"
17
17
  def label(field, options={})
18
18
  options.reverse_merge!(:caption => field.to_s.titleize)
19
19
  @template.label_tag(field_id(field), options)
20
20
  end
21
-
21
+
22
+ # f.hidden_field :session_id, :value => "45"
23
+ def hidden_field(field, options={})
24
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
25
+ @template.hidden_field_tag field_name(field), options
26
+ end
27
+
22
28
  # f.text_field :username, :value => "(blank)", :id => 'username'
23
29
  def text_field(field, options={})
24
30
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
25
31
  @template.text_field_tag field_name(field), options
26
32
  end
27
-
33
+
28
34
  # f.text_area :summary, :value => "(enter summary)", :id => 'summary'
29
35
  def text_area(field, options={})
30
36
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
31
37
  @template.text_area_tag field_name(field), options
32
38
  end
33
-
39
+
34
40
  # f.password_field :password, :id => 'password'
35
41
  def password_field(field, options={})
36
42
  options.reverse_merge!(:value => field_value(field), :id => field_id(field))
37
43
  @template.password_field_tag field_name(field), options
38
44
  end
39
-
45
+
40
46
  # f.file_field(:photo, :class => 'avatar')
41
47
  def file_field(field, options={})
42
48
  options.reverse_merge!(:id => field_id(field))
43
49
  @template.file_field_tag field_name(field), options
44
50
  end
45
-
51
+
46
52
  # f.submit "Update", :class => 'large'
47
53
  def submit(caption="Submit", options={})
48
54
  @template.submit_tag caption, options
49
55
  end
50
-
56
+
51
57
  protected
52
-
58
+
53
59
  # Returns the known field types for a formbuilder
54
60
  def self.field_types
55
- [:text_field, :text_area, :password_field, :file_field]
61
+ [:text_field, :text_area, :password_field, :file_field, :hidden_field]
56
62
  end
57
-
63
+
58
64
  private
59
-
65
+
60
66
  # Returns the object's models name
61
67
  # => user_assignment
62
68
  def object_name
63
69
  object.class.to_s.underscore
64
70
  end
65
-
71
+
66
72
  # Returns the value for the object's field
67
73
  # field_value(:username) => "Joey"
68
74
  def field_value(field)
69
75
  @object && @object.respond_to?(field) ? @object.send(field) : ""
70
76
  end
71
-
77
+
72
78
  # Returns the name for the given field
73
79
  # field_name(:username) => "user[username]"
74
80
  def field_name(field)
75
81
  "#{object_name}[#{field}]"
76
82
  end
77
-
83
+
78
84
  # Returns the id for the given field
79
85
  # field_id(:username) => "user_username"
80
86
  def field_id(field)
81
87
  "#{object_name}_#{field}"
82
88
  end
83
- end
89
+ end
@@ -4,7 +4,7 @@ class StandardFormBuilder < AbstractFormBuilder
4
4
  # text_area_block(:username, { :class => 'long' }, { :class => 'wide-label' })
5
5
  # password_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
6
6
  # file_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
7
- self.field_types.each do |field_type|
7
+ (self.field_types - [:hidden_field]).each do |field_type|
8
8
  class_eval <<-EOF
9
9
  def #{field_type}_block(field, options={}, label_options={})
10
10
  label_options.reverse_merge!(:caption => options.delete(:caption)) if options[:caption]
@@ -3,22 +3,20 @@ module SinatraMore
3
3
  # Constructs a form for object using given or default form_builder
4
4
  # form_for @user, '/register', :id => 'register' do |f| ... end
5
5
  def form_for(object, url, settings={}, &block)
6
- default_builder = self.respond_to?(:options) && self.options.default_builder
7
- configured_builder = settings[:builder] || default_builder || 'StandardFormBuilder'
8
- configured_builder = configured_builder.constantize if configured_builder.is_a?(String)
9
- settings.reverse_merge!(:method => 'post', :action => url)
10
- settings[:enctype] = "multipart/form-data" if settings.delete(:multipart)
11
- form_html = capture_html(configured_builder.new(self, object), &block)
12
- concat_content content_tag('form', form_html, settings)
6
+ builder_class = configured_form_builder_class(settings[:builder])
7
+ form_html = capture_html(builder_class.new(self, object), &block)
8
+ form_tag(url, settings) { form_html }
13
9
  end
14
10
 
15
11
  # Constructs a form without object based on options
16
12
  # form_tag '/register' do ... end
17
13
  def form_tag(url, options={}, &block)
18
14
  options.reverse_merge!(:method => 'post', :action => url)
19
- concat_content content_tag('form', capture_html(&block), options)
15
+ options[:enctype] = "multipart/form-data" if options.delete(:multipart)
16
+ inner_form_html = hidden_form_method_field(options[:method]) + capture_html(&block)
17
+ concat_content content_tag('form', inner_form_html, options)
20
18
  end
21
-
19
+
22
20
  # Constructs a field_set to group fields with given options
23
21
  # field_set_tag("Office", :class => 'office-set')
24
22
  # parameters: legend_text=nil, options={}
@@ -56,6 +54,13 @@ module SinatraMore
56
54
  end
57
55
  end
58
56
 
57
+ # Constructs a hidden field input from the given options
58
+ # hidden_field_tag :session_key, :value => "__secret__"
59
+ def hidden_field_tag(name, options={})
60
+ options.reverse_merge!(:name => name)
61
+ input_tag(:hidden, options)
62
+ end
63
+
59
64
  # Constructs a text field input from the given options
60
65
  # text_field_tag :username, :class => 'long'
61
66
  def text_field_tag(name, options={})
@@ -90,5 +95,28 @@ module SinatraMore
90
95
  options.reverse_merge!(:value => caption)
91
96
  input_tag(:submit, options)
92
97
  end
98
+
99
+ protected
100
+
101
+ # returns the hidden method field for 'put' and 'delete' forms
102
+ # Only 'get' and 'post' are allowed within browsers;
103
+ # 'put' and 'delete' are just specified using hidden fields with form action still 'put'.
104
+ # hidden_form_method_field('delete') => <input name="_method" value="delete" />
105
+ def hidden_form_method_field(desired_method)
106
+ return '' if (desired_method =~ /get|post/)
107
+ original_method = desired_method.dup
108
+ desired_method.replace('post')
109
+ hidden_field_tag(:_method, :value => original_method)
110
+ end
111
+
112
+ # Returns the FormBuilder class to use based on all available setting sources
113
+ # If explicitly defined, returns that, otherwise returns defaults
114
+ # configured_form_builder_class(nil) => StandardFormBuilder
115
+ def configured_form_builder_class(explicit_builder=nil)
116
+ default_builder = self.respond_to?(:options) && self.options.default_builder
117
+ configured_builder = explicit_builder || default_builder || 'StandardFormBuilder'
118
+ configured_builder = configured_builder.constantize if configured_builder.is_a?(String)
119
+ configured_builder
120
+ end
93
121
  end
94
122
  end
@@ -1,6 +1,19 @@
1
1
  module SinatraMore
2
2
  module FormatHelpers
3
-
3
+
4
+ # Returns escaped text to protect against malicious content
5
+ def h(text)
6
+ Rack::Utils.escape_html(text)
7
+ end
8
+ alias escape_html h
9
+
10
+ # Returns escaped text to protect against malicious content
11
+ # Returns blank if the text is empty
12
+ def h!(text, blank_text = '&nbsp;')
13
+ return blank_text if text.nil? || text.empty?
14
+ h text
15
+ end
16
+
4
17
  # Returns relative time in words referencing the given date
5
18
  # relative_time_ago(Time.now)
6
19
  def relative_time_ago(date)
data/sinatra_more.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_more}
8
- s.version = "0.1.9"
8
+ s.version = "0.1.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Esquenazi"]
@@ -43,6 +43,7 @@ end
43
43
 
44
44
  class MarkupUser
45
45
  def errors; Errors.new; end
46
+ def session_id; 45; end
46
47
  end
47
48
 
48
49
  class Errors < Array
@@ -1,5 +1,6 @@
1
1
  <% form_for MarkupUser.new, '/demo', :id => 'demo' do |f| %>
2
2
  <%= f.error_messages(:header_message => "custom MarkupUser cannot be saved!") %>
3
+ <%= f.hidden_field :session_id %>
3
4
  <p>
4
5
  <%= f.label :username, :caption => "Login", :class => 'user-label' %>
5
6
  <%= f.text_field :username, :class => 'user-text', :value => "John" %>
@@ -25,6 +26,7 @@
25
26
 
26
27
  <% form_for MarkupUser.new, '/another_demo', :id => 'demo2', :method => 'get' do |f| %>
27
28
  <%= f.error_messages :header_message => "custom MarkupUser cannot be saved!" %>
29
+ <%= f.hidden_field :session_id %>
28
30
  <%= f.text_field_block :username, { :class => 'input' }, { :caption => 'Nickname', :class => 'label' } %>
29
31
  <%= f.password_field_block :code, { :class => 'input' } %>
30
32
  <%= f.text_area_block :about, { :class => 'textarea' } %>
@@ -1,5 +1,6 @@
1
1
  - form_for MarkupUser.new, '/demo', :id => 'demo' do |f|
2
2
  = f.error_messages(:header_message => "custom MarkupUser cannot be saved!")
3
+ = f.hidden_field :session_id
3
4
  %p
4
5
  = f.label :username, :caption => "Login", :class => 'user-label'
5
6
  = f.text_field :username, :class => 'user-text', :value => "John"
@@ -20,6 +21,7 @@
20
21
 
21
22
  - form_for MarkupUser.new, '/another_demo', :id => 'demo2', :method => 'get' do |f|
22
23
  = f.error_messages :header_message => "custom MarkupUser cannot be saved!"
24
+ = f.hidden_field :session_id
23
25
  = f.text_field_block :username, { :class => 'input' }, { :caption => 'Nickname', :class => 'label' }
24
26
  = f.password_field_block :code, { :class => 'input' }
25
27
  = f.text_area_block :about, { :class => 'textarea' }
@@ -1,5 +1,6 @@
1
1
  <% form_tag '/simple', :class => 'simple-form' do %>
2
2
  <%= error_messages_for(nil) %>
3
+ <%= hidden_field_tag :session_id, :value => "__secret__" %>
3
4
  <% field_set_tag do %>
4
5
  <%= label_tag :username %>
5
6
  <%= text_field_tag :username %>
@@ -11,6 +12,7 @@
11
12
 
12
13
  <% form_tag '/advanced', :id => 'advanced', :class => 'advanced-form', :method => 'get' do %>
13
14
  <%= error_messages_for MarkupUser.new, :header_message => "There are problems with saving user!" %>
15
+ <%= hidden_field_tag :session_id, :value => "__secret__" %>
14
16
  <% field_set_tag "Advanced", :class => 'advanced-field-set' do %>
15
17
  <p>
16
18
  <%= label_tag :username, :class => 'first', :caption => "Nickname" %>
@@ -1,6 +1,7 @@
1
1
  - form_tag '/simple', :class => 'simple-form' do
2
2
  = error_messages_for nil
3
3
  - field_set_tag do
4
+ = hidden_field_tag :session_id, :value => "__secret__"
4
5
  = label_tag :username
5
6
  = text_field_tag :username
6
7
  = label_tag :password
@@ -9,6 +10,7 @@
9
10
 
10
11
  - form_tag '/advanced', :id => 'advanced', :class => 'advanced-form', :method => 'get' do
11
12
  = error_messages_for MarkupUser.new, :header_message => "There are problems with saving user!"
13
+ = hidden_field_tag :session_id, :value => "__secret__"
12
14
  - field_set_tag "Advanced", :class => 'advanced-field-set' do
13
15
  %p
14
16
  = label_tag :username, :class => 'first', :caption => "Nickname"
@@ -9,7 +9,7 @@ class TestFormBuilder < Test::Unit::TestCase
9
9
  end
10
10
 
11
11
  def setup
12
- @user = stub(:errors => stub(:full_messages => ["1", "2"], :none? => false), :class => 'User', :first_name => "Joe")
12
+ @user = stub(:errors => stub(:full_messages => ["1", "2"], :none? => false), :class => 'User', :first_name => "Joe", :session_id => 54)
13
13
  @user_none = stub(:errors => stub(:none? => true), :class => 'User')
14
14
  end
15
15
 
@@ -19,8 +19,21 @@ class TestFormBuilder < Test::Unit::TestCase
19
19
 
20
20
  context 'for #form_for method' do
21
21
  should "display correct form html" do
22
- actual_html = form_for(@user, '/register', :id => 'register') { "Demo" }
23
- assert_has_tag('form', :action => '/register', :id => 'register', :content => "Demo") { actual_html }
22
+ actual_html = form_for(@user, '/register', :id => 'register', :method => 'post') { "Demo" }
23
+ assert_has_tag('form', :action => '/register', :id => 'register', :method => 'post', :content => "Demo") { actual_html }
24
+ assert_has_tag('form input[type=hidden]', :name => '_method', :count => 0) { actual_html } # no method action field
25
+ end
26
+
27
+ should "display correct form html with method :post" do
28
+ actual_html = form_for(@user, '/update', :method => 'put') { "Demo" }
29
+ assert_has_tag('form', :action => '/update', :method => 'post') { actual_html }
30
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
31
+ end
32
+
33
+ should "display correct form html with method :delete" do
34
+ actual_html = form_for(@user, '/destroy', :method => 'delete') { "Demo" }
35
+ assert_has_tag('form', :action => '/destroy', :method => 'post') { actual_html }
36
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
24
37
  end
25
38
 
26
39
  should "display correct form html with multipart" do
@@ -106,6 +119,25 @@ class TestFormBuilder < Test::Unit::TestCase
106
119
  end
107
120
  end
108
121
 
122
+ context 'for #hidden_field method' do
123
+ should "display correct hidden field html" do
124
+ actual_html = standard_builder.hidden_field(:session_id, :class => 'hidden')
125
+ assert_has_tag('input.hidden[type=hidden]', :value => "54", :id => 'user_session_id', :name => 'user[session_id]') { actual_html }
126
+ end
127
+
128
+ should "display correct hidden field in haml" do
129
+ visit '/haml/form_for'
130
+ assert_have_selector '#demo input[type=hidden]', :id => 'markup_user_session_id', :value => "45"
131
+ assert_have_selector '#demo2 input', :type => 'hidden', :name => 'markup_user[session_id]'
132
+ end
133
+
134
+ should "display correct hidden field in erb" do
135
+ visit '/erb/form_for'
136
+ assert_have_selector '#demo input[type=hidden]', :id => 'markup_user_session_id', :value => "45"
137
+ assert_have_selector '#demo2 input', :type => 'hidden', :name => 'markup_user[session_id]'
138
+ end
139
+ end
140
+
109
141
  context 'for #text_field method' do
110
142
  should "display correct text field html" do
111
143
  actual_html = standard_builder.text_field(:first_name, :class => 'large')
@@ -115,13 +147,13 @@ class TestFormBuilder < Test::Unit::TestCase
115
147
  should "display correct text field in haml" do
116
148
  visit '/haml/form_for'
117
149
  assert_have_selector '#demo input.user-text[type=text]', :id => 'markup_user_username', :value => "John"
118
- assert_have_selector '#demo2 input', :class => 'input', :name => 'markup_user[username]'
150
+ assert_have_selector '#demo2 input', :type => 'text', :class => 'input', :name => 'markup_user[username]'
119
151
  end
120
152
 
121
153
  should "display correct text field in erb" do
122
154
  visit '/erb/form_for'
123
155
  assert_have_selector '#demo input.user-text[type=text]', :id => 'markup_user_username', :value => "John"
124
- assert_have_selector '#demo2 input', :class => 'input', :name => 'markup_user[username]'
156
+ assert_have_selector '#demo2 input', :type => 'text', :class => 'input', :name => 'markup_user[username]'
125
157
  end
126
158
  end
127
159
 
@@ -10,15 +10,33 @@ class TestFormHelpers < Test::Unit::TestCase
10
10
 
11
11
  context 'for #form_tag method' do
12
12
  should "display correct forms in ruby" do
13
- actual_html = form_tag('/register', :class => 'test', :action => "hello") { "Demo" }
13
+ actual_html = form_tag('/register', :class => 'test', :method => "post") { "Demo" }
14
14
  assert_has_tag(:form, :class => "test") { actual_html }
15
+ assert_has_tag('form input', :type => 'hidden', :name => '_method', :count => 0) { actual_html }
15
16
  end
16
-
17
- should "display correct inputs in ruby for form_tag" do
18
- actual_html = form_tag('/register', :class => 'test', :action => "hello") { text_field_tag(:username) }
17
+
18
+ should "display correct text inputs within form_tag" do
19
+ actual_html = form_tag('/register', :class => 'test') { text_field_tag(:username) }
19
20
  assert_has_tag('form input', :type => 'text', :name => "username") { actual_html }
20
21
  end
21
22
 
23
+ should "display correct form with method :put" do
24
+ actual_html = form_tag('/update', :class => 'put-form', :method => "put") { "Demo" }
25
+ assert_has_tag(:form, :class => "put-form", :method => 'post') { actual_html }
26
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
27
+ end
28
+
29
+ should "display correct form with method :delete" do
30
+ actual_html = form_tag('/remove', :class => 'delete-form', :method => "delete") { "Demo" }
31
+ assert_has_tag(:form, :class => "delete-form", :method => 'post') { actual_html }
32
+ assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
33
+ end
34
+
35
+ should "display correct form with multipart encoding" do
36
+ actual_html = form_tag('/remove', :multipart => true) { "Demo" }
37
+ assert_has_tag(:form, :enctype => "multipart/form-data") { actual_html }
38
+ end
39
+
22
40
  should "display correct forms in erb" do
23
41
  visit '/erb/form_tag'
24
42
  assert_have_selector 'form.simple-form', :action => '/simple'
@@ -124,6 +142,25 @@ class TestFormHelpers < Test::Unit::TestCase
124
142
  end
125
143
  end
126
144
 
145
+ context 'for #hidden_field_tag method' do
146
+ should "display hidden field in ruby" do
147
+ actual_html = hidden_field_tag(:session_key, :id => 'session_id', :value => '56768')
148
+ assert_has_tag(:input, :type => 'hidden', :id => "session_id", :name => 'session_key', :value => '56768') { actual_html }
149
+ end
150
+
151
+ should "display hidden field in erb" do
152
+ visit '/erb/form_tag'
153
+ assert_have_selector 'form.simple-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"
154
+ assert_have_selector 'form.advanced-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"
155
+ end
156
+
157
+ should "display hidden field in haml" do
158
+ visit '/haml/form_tag'
159
+ assert_have_selector 'form.simple-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"
160
+ assert_have_selector 'form.advanced-form input[type=hidden]', :count => 1, :name => 'session_id', :value => "__secret__"
161
+ end
162
+ end
163
+
127
164
  context 'for #text_field_tag method' do
128
165
  should "display text field in ruby" do
129
166
  actual_html = text_field_tag(:username, :class => 'long')
@@ -8,6 +8,23 @@ class TestFormatHelpers < Test::Unit::TestCase
8
8
 
9
9
  include SinatraMore::FormatHelpers
10
10
 
11
+ context 'for #h and #h! method' do
12
+ should "escape the simple html" do
13
+ assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', h('<h1>hello</h1>')
14
+ assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', escape_html('<h1>hello</h1>')
15
+ end
16
+ should "escape all brackets, quotes and ampersands" do
17
+ assert_equal '&lt;h1&gt;&lt;&gt;&quot;&amp;demo&amp;&quot;&lt;&gt;&lt;/h1&gt;', h('<h1><>"&demo&"<></h1>')
18
+ end
19
+ should "return default text if text is empty" do
20
+ assert_equal 'default', h!("", "default")
21
+ assert_equal '&nbsp;', h!("")
22
+ end
23
+ should "return text escaped if not empty" do
24
+ assert_equal '&lt;h1&gt;hello&lt;/h1&gt;', h!('<h1>hello</h1>')
25
+ end
26
+ end
27
+
11
28
  context 'for #relative_time_ago method' do
12
29
  should "display today" do
13
30
  assert_equal 'today', relative_time_ago(Time.now)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_more
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi