sbfaulkner-sinatra-helpers 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ = sinatra-helpers
2
+
3
+ sinatra-helpers is an extension for sinatra that provides a bunch of useful helpers
4
+ for sinatra applications.
5
+
6
+ == Installation
7
+
8
+ sudo gem install sbfaulkner-sinatra-helpers -s http://gems.github.com
9
+
10
+ == Usage
11
+
12
+ To use the helpers simply:
13
+
14
+ require 'sinatra-helpers'
15
+
16
+ If you don't want to include all of the functionality you can require specific
17
+ functionality in smaller units:
18
+
19
+ require 'sinatra-helpers/haml'
20
+
21
+ or even:
22
+
23
+ require 'sinatra-helpers/haml/partials'
24
+
25
+
26
+ == Legal
27
+
28
+ Author:: S. Brent Faulkner <brentf@unwwwired.net>
29
+ License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 9
4
+ :patch: 1
@@ -0,0 +1,2 @@
1
+ require 'sinatra-helpers/haml'
2
+ require 'sinatra-helpers/html'
@@ -0,0 +1,4 @@
1
+ require File.dirname(__FILE__) + '/haml/flash.rb'
2
+ require File.dirname(__FILE__) + '/haml/forms.rb'
3
+ require File.dirname(__FILE__) + '/haml/links.rb'
4
+ require File.dirname(__FILE__) + '/haml/partials.rb'
@@ -0,0 +1,17 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ module Haml
6
+ module Flash
7
+ def flash_messages
8
+ [ :error, :warning, :notice ].each do |f|
9
+ haml_tag :div, flash[f], :class => f if flash.has?(f)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ helpers Helpers::Haml::Flash
17
+ end
@@ -0,0 +1,149 @@
1
+ require 'sinatra/base'
2
+ require 'active_support/inflector'
3
+
4
+ module Sinatra
5
+ module Helpers
6
+ module Haml
7
+ module Forms
8
+ CHECKBOX_DEFAULTS = {:type => :checkbox, :value => 1}
9
+ FILE_FIELD_DEFAULTS = {:type => :file}
10
+ HIDDEN_FIELD_DEFAULTS = {:type => :hidden}
11
+ PASSWORD_FIELD_DEFAULTS = {:type => :password}
12
+ TEXT_FIELD_DEFAULTS = {:type => :text}
13
+ TEXT_AREA_DEFAULTS = {}
14
+ SELECT_DEFAULTS = {}
15
+
16
+ def errors_for(object)
17
+ unless object.errors.empty?
18
+ errors = object.errors.full_messages
19
+ haml_tag :div, :id => 'errors' do
20
+ haml_tag :p, %Q(Unable to save #{object.class.name.humanize} due to the following #{errors.size == 1 ? 'error' : "#{errors.size} errors"}:)
21
+ haml_tag :ul do
22
+ haml_concat list_of(errors) { |error| haml_concat(error) }
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def parameterize(arg)
29
+ case arg
30
+ when Array
31
+ arg.compact.collect { |a| parameterize(a) }
32
+ when String
33
+ arg
34
+ when Symbol, Numeric
35
+ arg.to_s
36
+ else
37
+ arg.class.name.underscore
38
+ end
39
+ end
40
+
41
+ def object_for(*args)
42
+ args.pop if args.last.is_a?(Hash)
43
+ args.pop
44
+ case object = args.last
45
+ when String, Symbol
46
+ if args.length > 1
47
+ object_for(*args).send(object)
48
+ else
49
+ instance_variable_get "@#{object}"
50
+ end
51
+ else
52
+ object
53
+ end
54
+ end
55
+
56
+ def value_for(*args)
57
+ object = object_for(*args)
58
+ options = args.last.is_a?(Hash) ? args.pop : {}
59
+ object.send args.pop
60
+ end
61
+
62
+ def id_for(*args)
63
+ options = args.last.is_a?(Hash) ? args.pop : {}
64
+ index = options[:index]
65
+ method = args.pop
66
+ args << index << method
67
+ parameterize(args).join('_')
68
+ end
69
+
70
+ def name_for(*args)
71
+ options = args.last.is_a?(Hash) ? args.pop : {}
72
+ index = options[:index]
73
+ method = args.pop
74
+ # pluralize collection name when indexed... explicit parameterization
75
+ args << parameterize(args.pop).pluralize << index if index
76
+ args << method
77
+ parameterize(args).inject { |name,part| "#{name}[#{part}]" }
78
+ end
79
+
80
+ def extract_options(*args)
81
+ args.last.is_a?(Hash) ? args.last.reject { |k,v| k.to_s == 'index' } : {}
82
+ end
83
+
84
+ def label(*args)
85
+ { :for => id_for(*args) }.merge extract_options(*args)
86
+ end
87
+
88
+ def input(*args)
89
+ { :id => id_for(*args), :name => name_for(*args) }.merge extract_options(*args)
90
+ end
91
+
92
+ def checkbox(*args)
93
+ args << {} unless args.last.is_a?(Hash)
94
+ options = args.last
95
+ options[:checked] = value_for(*args) unless options.include?(:checked)
96
+ CHECKBOX_DEFAULTS.merge input(*args)
97
+ end
98
+
99
+ def file_field(*args)
100
+ args << {} unless args.last.is_a?(Hash)
101
+ options = args.last
102
+ options[:value] = value_for(*args) unless options.include?(:value)
103
+ FILE_FIELD_DEFAULTS.merge input(*args)
104
+ end
105
+
106
+ def hidden_field(*args)
107
+ args << {} unless args.last.is_a?(Hash)
108
+ options = args.last
109
+ options[:value] = value_for(*args) unless options.include?(:value)
110
+ HIDDEN_FIELD_DEFAULTS.merge input(*args)
111
+ end
112
+
113
+ def password_field(*args)
114
+ args << {} unless args.last.is_a?(Hash)
115
+ options = args.last
116
+ options[:value] = value_for(*args) unless options.include?(:value)
117
+ PASSWORD_FIELD_DEFAULTS.merge input(*args)
118
+ end
119
+
120
+ def text_field(*args)
121
+ args << {} unless args.last.is_a?(Hash)
122
+ options = args.last
123
+ options[:value] = value_for(*args) unless options.include?(:value)
124
+ TEXT_FIELD_DEFAULTS.merge input(*args)
125
+ end
126
+
127
+ def text_area(*args)
128
+ args << {} unless args.last.is_a?(Hash)
129
+ options = args.last
130
+ TEXT_AREA_DEFAULTS.merge input(*args)
131
+ end
132
+
133
+ def select(*args)
134
+ SELECT_DEFAULTS.merge input(*args)
135
+ end
136
+
137
+ def options_of(collection, options = {}, &block)
138
+ collection.each do |option|
139
+ text, value = option
140
+ selected = block_given? ? block.call(option) : nil
141
+ haml_tag :option, text, options.merge(:selected => selected, :value => value)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ helpers Helpers::Haml::Forms
149
+ end
@@ -0,0 +1,102 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ module Haml
6
+ module Links
7
+ def domain(tld_length = 1)
8
+ request.host.split('.')[-(tld_length+1)..-1].join('.')
9
+ end
10
+
11
+ def subdomain(name, tld_length = 1)
12
+ "#{name}.#{domain(tld_length)}"
13
+ end
14
+
15
+ def subdomain_url(name, tld_length = 1)
16
+ "http://#{subdomain(name, tld_length)}"
17
+ end
18
+
19
+ def asset_url(path, tld_length = 1)
20
+ "#{subdomain_url('assets', tld_length)}#{path}"
21
+ end
22
+
23
+ def css(path, options = {})
24
+ { :href => asset_url(path), :media => "all", :rel => "stylesheet", :type => "text/css" }.merge(options)
25
+ end
26
+
27
+ def icon(path, options = {})
28
+ { :href => asset_url(path), :rel => 'icon', :type => 'image/x-icon' }.merge(options)
29
+ end
30
+
31
+ def javascript(path, options = {})
32
+ { :src => asset_url(path), :type => 'text/javascript' }.merge(options)
33
+ end
34
+
35
+ def image(path, options = {})
36
+ options[:width], options[:height] = options.delete(:size).split('x') if options.include?(:size)
37
+ options[:alt] ||= File.basename(path, '.*')
38
+ { :src => asset_url(path) }.merge(options)
39
+ end
40
+
41
+ def action(path, options = {})
42
+ { :action => path, :method => 'post' }.merge(options)
43
+ end
44
+
45
+ def href(path, options = {})
46
+ script = if options.include?(:success)
47
+ path, original_path = '#', path
48
+ javascript_for_ajax(original_path, options)
49
+ elsif method = options.delete(:method)
50
+ path, original_path = '#', path
51
+ javascript_for_method(original_path, method)
52
+ else
53
+ options.delete(:onclick)
54
+ end
55
+ confirm = options.delete(:confirm)
56
+ script = javascript_with_confirmation(script, confirm) if confirm
57
+ result = { :href => path }
58
+ result[:onclick] = script unless script.nil?
59
+ result.merge(options)
60
+ end
61
+
62
+ protected
63
+ def javascript_options(options)
64
+ options.collect do |key,value|
65
+ value = "'#{value}'" if [:type,:url].include? key
66
+ "#{key}:#{value}"
67
+ end.join(',')
68
+ end
69
+
70
+ def javascript_with_confirmation(javascript, prompt)
71
+ prompt = 'Are you sure?' if prompt == true
72
+ javascript = 'return true;' if javascript.nil?
73
+ "if (confirm('#{prompt}')) { #{javascript} }; return false;"
74
+ end
75
+
76
+ def javascript_for_method(action, method)
77
+ javascript = "$('<form></form>').css('display','none').attr({'method':'post','action':'#{action}'})"
78
+ javascript << ".append($('<input type=\"hidden\">').attr({'name':'_method','value':'#{method}'}))" if method != 'post'
79
+ javascript << ".appendTo($(this).parent()).submit();return false;"
80
+ end
81
+
82
+ def javascript_for_ajax(url, options = {})
83
+ ajax_options = {:url => url}
84
+ success = options.delete(:success)
85
+ if !success.is_a?(Hash)
86
+ function = "$('#{success}').replaceWith(html)"
87
+ elsif success.include?(:update)
88
+ function = success[:position] || 'replaceWith'
89
+ function = "$('#{success[:update]}').#{function}(html);"
90
+ elsif success.include?(:remove)
91
+ function = "$('#{success[:remove]}').remove();"
92
+ end
93
+ ajax_options[:type] = options.delete(:method) if options.include?(:method)
94
+ ajax_options[:success] = "function(html){#{function}}"
95
+ "$.ajax({#{javascript_options(ajax_options)}});return false;"
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ helpers Helpers::Haml::Links
102
+ end
@@ -0,0 +1,26 @@
1
+ require 'sinatra/base'
2
+ require 'enumerator'
3
+
4
+ module Sinatra
5
+ module Helpers
6
+ module Haml
7
+ module Partials
8
+ def haml_partial(name, options = {})
9
+ item_name = name.to_sym
10
+ counter_name = "#{name}_counter".to_sym
11
+ if collection = options.delete(:collection)
12
+ collection.enum_for(:each_with_index).collect do |item,index|
13
+ haml_partial name, options.merge(:locals => {item_name => item, counter_name => index+1})
14
+ end.join
15
+ elsif object = options.delete(:object)
16
+ haml_partial name, options.merge(:locals => {item_name => object, counter_name => nil})
17
+ else
18
+ haml "_#{name}".to_sym, options.merge(:layout => false)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ helpers Helpers::Haml::Partials
26
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/html/escape'
@@ -0,0 +1,20 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ module Html
6
+ module Escape
7
+ def h(text)
8
+ Rack::Utils.escape_html(text)
9
+ end
10
+
11
+ def h!(text, blank_text = '&nbsp;')
12
+ return blank_text if text.nil? || text.empty?
13
+ h text
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ helpers Helpers::Html::Escape
20
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class MockFlash < Hash
4
+ def initialize(flash = {})
5
+ flash.each { |k,v| self[k] = v }
6
+ end
7
+
8
+ alias_method :has?, :include?
9
+ end
10
+
11
+ class FlashTest < Test::Unit::TestCase
12
+ include Haml::Helpers
13
+ include Sinatra::Helpers::Haml::Flash
14
+
15
+ attr_reader :flash
16
+
17
+ def setup
18
+ init_haml_helpers
19
+ super
20
+ end
21
+
22
+ def test_should_not_show_message_for_empty_flash
23
+ @flash = MockFlash.new
24
+ flash_messages
25
+ assert_html ''
26
+ end
27
+
28
+ def test_should_show_message_for_error_flash
29
+ @flash = MockFlash.new :error => 'error message'
30
+ flash_messages
31
+ assert_html 'div', 1
32
+ assert_html 'div.error', :html => 'error message', :count => 1
33
+ end
34
+
35
+ def test_should_show_message_for_warning_flash
36
+ @flash = MockFlash.new :warning => 'warning message'
37
+ flash_messages
38
+ assert_html 'div', 1
39
+ assert_html 'div.warning', :html => 'warning message', :count => 1
40
+ end
41
+
42
+ def test_should_show_message_for_notice_flash
43
+ @flash = MockFlash.new :notice => 'notice message'
44
+ flash_messages
45
+ assert_html 'div', 1
46
+ assert_html 'div.notice', :html => 'notice message', :count => 1
47
+ end
48
+
49
+ def test_should_show_messages_for_multiple_flash
50
+ @flash = MockFlash.new :error => 'error message', :warning => 'warning message', :notice => 'notice message'
51
+ flash_messages
52
+ assert_html 'div', 3
53
+ assert_html 'div.error', :html => 'error message', :count => 1
54
+ assert_html 'div.warning', :html => 'warning message', :count => 1
55
+ assert_html 'div.notice', :html => 'notice message', :count => 1
56
+ end
57
+ end
@@ -0,0 +1,219 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class FormsTest < Test::Unit::TestCase
4
+ include Haml::Helpers
5
+ include Sinatra::Helpers::Haml::Forms
6
+
7
+ def setup
8
+ @invoice = mock
9
+ @invoice.stubs(:id => Time.now.to_i, :class => stub(:name => 'Invoice'))
10
+ init_haml_helpers
11
+ super
12
+ end
13
+
14
+ def test_object_for_should_return_object
15
+ assert_equal @invoice, object_for(:invoice, :id)
16
+ end
17
+
18
+ def test_object_for_should_support_explicit_object
19
+ assert_equal @invoice, object_for(@invoice, :id)
20
+ end
21
+
22
+ def test_value_for_should_return_value
23
+ @invoice.stubs(:client_name => 'Client name')
24
+ assert_equal @invoice.client_name, value_for(:invoice, :client_name)
25
+ end
26
+
27
+ def test_value_for_should_support_explicit_object
28
+ @invoice.stubs(:client_name => 'Client name')
29
+ assert_equal @invoice.client_name, value_for(@invoice, :client_name)
30
+ end
31
+
32
+ def test_id_for_should_build_simple_id
33
+ assert_equal "invoice_id", id_for(:invoice, :id)
34
+ end
35
+
36
+ def test_name_for_should_build_simple_name
37
+ assert_equal "invoice[id]", name_for(:invoice, :id)
38
+ end
39
+
40
+ def test_id_for_should_support_explicit_object
41
+ assert_equal "invoice_id", id_for(@invoice, :id)
42
+ end
43
+
44
+ def test_name_for_should_support_explicit_object
45
+ assert_equal "invoice[id]", name_for(@invoice, :id)
46
+ end
47
+
48
+ def test_id_for_should_index_collection
49
+ assert_equal "line_1_id", id_for(:line, :id, :index => 1)
50
+ end
51
+
52
+ def test_name_for_should_index_collection
53
+ assert_equal "lines[1][id]", name_for(:line, :id, :index => 1)
54
+ end
55
+
56
+ def test_id_for_should_index_collection_by_id
57
+ line = line_stub
58
+ assert_equal "invoice_line_#{line.id}_id", id_for(:invoice, line, :id, :index => line.id)
59
+ end
60
+
61
+ def test_name_for_should_index_collection_by_id
62
+ line = line_stub
63
+ assert_equal "invoice[lines][#{line.id}][id]", name_for(:invoice, line, :id, :index => line.id)
64
+ end
65
+
66
+ def test_id_for_should_index_collection_by_new_id
67
+ line = line_stub
68
+ assert_equal "invoice_line_#{line.id}_id", id_for(:invoice, line, :id, :index => line.id)
69
+ end
70
+
71
+ def test_name_for_should_index_collection_by_new_id
72
+ line = line_stub
73
+ assert_equal "invoice[lines][#{line.id}][id]", name_for(:invoice, line, :id, :index => line.id)
74
+ end
75
+
76
+ def test_name_and_id_should_index_collection_by_same_new_id
77
+ line = line_stub
78
+ id = line.id
79
+ assert_equal "invoice_line_#{id}_id", id_for(:invoice, line, :id, :index => line.id)
80
+ assert_equal "invoice[lines][#{id}][id]", name_for(:invoice, line, :id, :index => line.id)
81
+ end
82
+
83
+ def test_id_for_should_support_parts
84
+ assert_equal "invoice_invoiced_on_year", id_for(@invoice, :invoiced_on, :year)
85
+ end
86
+
87
+ def test_name_for_should_support_parts
88
+ assert_equal "invoice[invoiced_on][year]", name_for(@invoice, :invoiced_on, :year)
89
+ end
90
+
91
+ def test_value_for_should_support_parts
92
+ @invoice.stubs(:invoiced_on => Time.now)
93
+ assert_equal @invoice.invoiced_on.year, value_for(@invoice, :invoiced_on, :year)
94
+ end
95
+
96
+ def test_id_for_should_support_parent
97
+ assert_equal "invoice_line_1_description", id_for(:invoice, :line, :description, :index => 1)
98
+ end
99
+
100
+ def test_name_for_should_support_parent
101
+ assert_equal "invoice[lines][1][description]", name_for(:invoice, :line, :description, :index => 1)
102
+ end
103
+
104
+ def test_id_for_should_support_explicit_object_with_parent
105
+ line = line_stub
106
+ assert_equal "invoice_line_1_description", id_for(@invoice, line, :description, :index => 1)
107
+ end
108
+
109
+ def test_name_for_should_support_explicit_object_with_parent
110
+ line = line_stub
111
+ assert_equal "invoice[lines][1][description]", name_for(@invoice, line, :description, :index => 1)
112
+ end
113
+
114
+ def test_errors_for_returns_nil_when_no_errors
115
+ @invoice.stubs(:errors => stub(:empty? => true))
116
+ errors_for(@invoice)
117
+ assert_html '', nil
118
+ end
119
+
120
+ def test_errors_for_returns_error_messages
121
+ @invoice.stubs(:class => stub(:name => 'Class name'), :errors => stub(:empty? => false, :full_messages => ['Field name']))
122
+ errors_for(@invoice)
123
+ assert_html 'p', :html => /Class name/, :count => 1
124
+ assert_html 'li', :html => /Field name/, :min => 1
125
+ end
126
+
127
+ def test_label
128
+ result = label(@invoice, :client_name)
129
+ assert_equal id_for(@invoice, :client_name), result[:for]
130
+ end
131
+
132
+ def test_checkbox
133
+ line = line_stub(:taxable => true)
134
+ @invoice.stubs(:lines).returns([line])
135
+ result = checkbox(@invoice, line, :taxable, :index => 1)
136
+ assert_equal :checkbox, result[:type]
137
+ assert_equal name_for(@invoice, line, :taxable, :index => 1), result[:name]
138
+ assert_equal id_for(@invoice, line, :taxable, :index => 1), result[:id]
139
+ assert_equal 1, result[:value]
140
+ assert result[:checked]
141
+ end
142
+
143
+ def test_checkbox_not_checked
144
+ line = line_stub(:taxable => false)
145
+ @invoice.stubs(:lines => [line])
146
+ result = checkbox(@invoice, line, :taxable, :index => 1)
147
+ assert_equal :checkbox, result[:type]
148
+ assert_equal name_for(@invoice, line, :taxable, :index => 1), result[:name]
149
+ assert_equal id_for(@invoice, line, :taxable, :index => 1), result[:id]
150
+ assert_equal 1, result[:value]
151
+ assert !result[:checked]
152
+ end
153
+
154
+ def test_hidden_field
155
+ @invoice.stubs(:client_name => 'Client name')
156
+ result = hidden_field(@invoice, :client_name)
157
+ assert_equal :hidden, result[:type]
158
+ assert_equal name_for(@invoice, :client_name), result[:name]
159
+ assert_equal id_for(@invoice, :client_name), result[:id]
160
+ assert_equal @invoice.client_name, result[:value]
161
+ end
162
+
163
+ def test_text_field
164
+ @invoice.stubs(:client_name => 'Client name')
165
+ result = text_field(@invoice, :client_name)
166
+ assert_equal :text, result[:type]
167
+ assert_equal name_for(@invoice, :client_name), result[:name]
168
+ assert_equal id_for(@invoice, :client_name), result[:id]
169
+ assert_equal @invoice.client_name, result[:value]
170
+ end
171
+
172
+ def test_text_area
173
+ result = text_area(@invoice, :client_name)
174
+ assert_equal name_for(@invoice, :client_name), result[:name]
175
+ assert_equal id_for(@invoice, :client_name), result[:id]
176
+ end
177
+
178
+ def test_select
179
+ result = select(@invoice, :client_name)
180
+ assert_equal name_for(@invoice, :client_name), result[:name]
181
+ assert_equal id_for(@invoice, :client_name), result[:id]
182
+ end
183
+
184
+ def test_options_of
185
+ dwarfs = %w(Bashful Doc Dopey Grumpy Happy Sleepy Sneezy)
186
+ options_of dwarfs
187
+ assert_html 'option', :count => 7 do |options|
188
+ assert_equal dwarfs, options.collect { |o| o.inner_html }
189
+ end
190
+ end
191
+
192
+ def test_options_of_with_selection
193
+ dwarfs = %w(Bashful Doc Dopey Grumpy Happy Sleepy Sneezy)
194
+ options_of dwarfs do |dwarf|
195
+ dwarf == 'Sleepy'
196
+ end
197
+ assert_html 'option', :count => 7 do |options|
198
+ assert_equal dwarfs, options.collect { |o| o.inner_html }
199
+ end
200
+ assert_html 'option[@selected="selected"]', :count => 1, :html => 'Sleepy'
201
+ end
202
+
203
+ def test_options_of_with_values
204
+ months = Date::MONTHNAMES.enum_for(:each_with_index).collect[1..12]
205
+ options_of months
206
+ assert_html 'option', :count => 12 do |options|
207
+ options.each_with_index do |option,i|
208
+ assert_html option, "option[@value='#{months[i].last}']", months[i].first
209
+ end
210
+ end
211
+ end
212
+
213
+ protected
214
+ def line_stub(methods = {})
215
+ line = stub({:class => stub(:name => 'Line'), :id => Time.now.to_i}.merge(methods))
216
+ @invoice.stubs(:lines).returns([line])
217
+ line
218
+ end
219
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class LinksTest < Test::Unit::TestCase
4
+ include Haml::Helpers
5
+ include Sinatra::Helpers::Haml::Links
6
+
7
+ attr_reader :request
8
+
9
+ def setup
10
+ @request = mock()
11
+ @request.stubs(:host => 'www.example.com')
12
+ init_haml_helpers
13
+ super
14
+ end
15
+
16
+ def test_domain
17
+ assert_equal 'example.com', domain
18
+ end
19
+
20
+ def test_subdomain
21
+ assert_equal 'test.example.com', subdomain('test')
22
+ end
23
+
24
+ def test_subdomain_url
25
+ assert_equal 'http://another.example.com', subdomain_url('another')
26
+ end
27
+
28
+ def test_asset_url
29
+ assert_equal 'http://assets.example.com/images/logo.gif', asset_url('/images/logo.gif')
30
+ end
31
+
32
+ def test_css
33
+ assert_equal({ :href => 'http://assets.example.com/stylesheets/test.css', :media => 'all', :rel => 'stylesheet', :type => 'text/css' }, css('/stylesheets/test.css'))
34
+ end
35
+
36
+ def test_icon
37
+ assert_equal({ :href => 'http://assets.example.com/icons/save.gif', :rel => 'icon', :type => 'image/x-icon' }, icon('/icons/save.gif'))
38
+ end
39
+
40
+ def test_javascript
41
+ assert_equal({ :src => 'http://assets.example.com/javascripts/jquery.js', :type => 'text/javascript' }, javascript('/javascripts/jquery.js'))
42
+ end
43
+
44
+ def test_image
45
+ assert_equal({ :alt => 'test', :height => '100', :src => 'http://assets.example.com/images/test.jpg', :width => '200' }, image('/images/test.jpg', :size => '200x100'))
46
+ end
47
+
48
+ def test_action
49
+ assert_equal({ :action => '/invoices/123', :method => 'post' }, action('/invoices/123'))
50
+ end
51
+
52
+ def test_action_should_override_method
53
+ assert_equal({ :action => '/invoices/123', :method => 'delete' }, action('/invoices/123', :method => 'delete'))
54
+ end
55
+
56
+ def test_href
57
+ assert_equal({ :href => '/link' }, href('/link'))
58
+ end
59
+
60
+ def test_href_should_support_method_using_hidden_form
61
+ assert_equal({ :href => '#', :onclick => "$('<form></form>').css('display','none').attr({'method':'post','action':'/link'}).append($('<input type=\"hidden\">').attr({'name':'_method','value':'put'})).appendTo($(this).parent()).submit();return false;" }, href('/link', :method => 'put'))
62
+ end
63
+
64
+ def test_href_should_support_confirm
65
+ assert_equal({ :href=>"/link", :onclick => "if (confirm('Really?')) { return true; }; return false;" }, href('/link', :confirm => 'Really?'))
66
+ end
67
+
68
+ def test_href_should_support_ajax
69
+ assert_equal({ :href=> "#", :onclick=> "$.ajax({url:'/link',success:function(html){$('#result').replaceWith(html);}});return false;" }, href('/link', :success => { :update => '#result' }))
70
+ end
71
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class PartialsTest < Test::Unit::TestCase
4
+ include Haml::Helpers
5
+ include Sinatra::Helpers::Haml::Partials
6
+ include Sinatra::Templates
7
+
8
+ @templates = {}
9
+
10
+ class << self
11
+ attr_accessor :templates
12
+ end
13
+
14
+ attr_accessor :options
15
+
16
+ def setup
17
+ @options = mock()
18
+ @options.stubs(:views => File.dirname(__FILE__) + '/views')
19
+ init_haml_helpers
20
+ super
21
+ end
22
+
23
+ def test_partial
24
+ assert_html haml_partial('thing'), nil, "<p>Hello world</p>\n"
25
+ end
26
+
27
+ def test_partial_with_object
28
+ assert_html haml_partial('object', :object => mock(:name => 'object')), nil, "<p>Hello object</p>\n"
29
+ end
30
+
31
+ def test_partial_with_locals
32
+ assert_html haml_partial('object', :locals => { :object => mock(:name => 'local')}), nil, "<p>Hello local</p>\n"
33
+ end
34
+
35
+ def test_partial_with_collection
36
+ assert_html haml_partial('object', :collection => [ mock(:name => 'one'), mock(:name => 'two')]), nil, "<p>Hello one</p>\n<p>Hello two</p>\n"
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ %p== Hello #{object.name}
@@ -0,0 +1 @@
1
+ %p Hello world
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class EscapeTest < Test::Unit::TestCase
4
+ include Sinatra::Helpers::Html::Escape
5
+
6
+ def test_h_should_escape_text
7
+ assert_equal 'this &amp; that', h('this & that')
8
+ end
9
+
10
+ def test_h_should_return_blank_for_empty_string
11
+ assert_equal '', h('')
12
+ end
13
+
14
+ def test_h_should_return_blank_for_nil
15
+ assert_equal '', h(nil)
16
+ end
17
+
18
+ def test_hbang_should_escape_text
19
+ assert_equal 'this &amp; that', h!('this & that')
20
+ end
21
+
22
+ def test_hbang_should_return_nbsp_for_empty_string
23
+ assert_equal '&nbsp;', h!('')
24
+ end
25
+
26
+ def test_hbang_should_return_nbsp_for_nil
27
+ assert_equal '&nbsp;', h!(nil)
28
+ end
29
+
30
+ def test_hbang_should_return_arbitrary_placeholder_for_empty_string
31
+ assert_equal 'N/A', h!('', 'N/A')
32
+ end
33
+
34
+ def test_hbang_should_return_arbitrary_placeholder_for_nil
35
+ assert_equal 'N/A', h!(nil, 'N/A')
36
+ end
37
+ end
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+
3
+ $:.unshift File.dirname(__FILE__) + '../lib'
4
+
5
+ require 'haml'
6
+
7
+ require 'sinatra-helpers'
8
+
9
+ require 'test/unit'
10
+ require 'mocha'
11
+ require 'hpricot'
12
+
13
+ class Test::Unit::TestCase
14
+ def assert_html(*args) # [doc], selector, match)
15
+ match = args.pop
16
+ selector = args.pop
17
+ html = args.pop || haml_buffer.buffer
18
+ html = html.to_html if html.respond_to? :to_html
19
+ selection = Hpricot(html)/selector unless match.nil?
20
+ case match
21
+ when NilClass
22
+ assert_equal selector, html
23
+ when Numeric
24
+ assert_equal match, selection.size, "Expected selection of exactly <#{match}>"
25
+ when String
26
+ assert_equal match, selection.inner_html, "Expected selection to equal <#{match}>"
27
+ when Regexp
28
+ assert_match match, selection.inner_html, "Expected selection to match <#{match}>"
29
+ when Hash
30
+ assert selection.size >= match[:min], "Expected selection of at least <#{match[:min]}>" if match.include?(:min)
31
+ assert selection.size <= match[:max], "Expected selection of at most <#{match[:max]}>" if match.include?(:max)
32
+ assert_html html, selector, match[:count] if match.include?(:count)
33
+ assert_html html, selector, match[:html] if match.include?(:html)
34
+ end
35
+ if block_given? && !selection.empty?
36
+ yield selection
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sbfaulkner-sinatra-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - S. Brent Faulkner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: a bunch of useful helpers for sinatra applications
17
+ email: brentf@unwwwired.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - VERSION.yml
27
+ - lib/sinatra-helpers
28
+ - lib/sinatra-helpers/haml
29
+ - lib/sinatra-helpers/haml/flash.rb
30
+ - lib/sinatra-helpers/haml/forms.rb
31
+ - lib/sinatra-helpers/haml/links.rb
32
+ - lib/sinatra-helpers/haml/partials.rb
33
+ - lib/sinatra-helpers/haml.rb
34
+ - lib/sinatra-helpers/html
35
+ - lib/sinatra-helpers/html/escape.rb
36
+ - lib/sinatra-helpers/html.rb
37
+ - lib/sinatra-helpers.rb
38
+ - test/haml
39
+ - test/haml/flash_test.rb
40
+ - test/haml/forms_test.rb
41
+ - test/haml/links_test.rb
42
+ - test/haml/partials_test.rb
43
+ - test/haml/views
44
+ - test/haml/views/_object.haml
45
+ - test/haml/views/_thing.haml
46
+ - test/html
47
+ - test/html/escape_test.rb
48
+ - test/test_helper.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/sbfaulkner/sinatra-helpers
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --inline-source
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: a bunch of useful helpers for sinatra applications
76
+ test_files: []
77
+