padrino-helpers 0.10.6.e → 0.10.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,13 +17,18 @@ module Padrino
17
17
  # @example
18
18
  # flash_tag(:notice, :id => 'flash-notice')
19
19
  # # Generates: <div class="notice">flash-notice</div>
20
+ # flash_tag(:error, :success)
21
+ # # Generates: <div class="error">flash-error</div>
22
+ # # <div class="success">flash-success</div>
20
23
  #
21
24
  # @api public
22
- def flash_tag(kind, options={})
23
- flash_text = flash[kind]
24
- return '' if flash_text.blank?
25
- options.reverse_merge!(:class => kind)
26
- content_tag(:div, flash_text, options)
25
+ def flash_tag(*args)
26
+ options = args.extract_options!
27
+ args.map do |kind|
28
+ flash_text = flash[kind]
29
+ next if flash_text.blank?
30
+ content_tag(:div, flash_text, options.reverse_merge(:class => kind))
31
+ end.compact * "\n"
27
32
  end
28
33
 
29
34
  ##
@@ -65,18 +70,17 @@ module Padrino
65
70
  # @api public
66
71
  def link_to(*args, &block)
67
72
  options = args.extract_options!
68
- options = parse_js_attributes(options) # parses remote, method and confirm options
69
73
  anchor = "##{CGI.escape options.delete(:anchor).to_s}" if options[:anchor]
70
74
 
71
75
  if block_given?
72
- url = args[0] ? args[0] + anchor.to_s : anchor || 'javascript:void(0);'
76
+ url = args[0] ? args[0] + anchor.to_s : anchor || '#'
73
77
  options.reverse_merge!(:href => url)
74
78
  link_content = capture_html(&block)
75
79
  return '' unless parse_conditions(url, options)
76
80
  result_link = content_tag(:a, link_content, options)
77
81
  block_is_template?(block) ? concat_content(result_link) : result_link
78
82
  else
79
- name, url = args[0], (args[1] ? args[1] + anchor.to_s : anchor || 'javascript:void(0);')
83
+ name, url = args[0], (args[1] ? args[1] + anchor.to_s : anchor || '#')
80
84
  return name unless parse_conditions(url, options)
81
85
  options.reverse_merge!(:href => url)
82
86
  content_tag(:a, name, options)
@@ -119,8 +123,8 @@ module Padrino
119
123
  desired_method = options[:method]
120
124
  options.delete(:method) if options[:method].to_s !~ /get|post/i
121
125
  options.reverse_merge!(:method => 'post', :action => url)
122
- options[:enctype] = "multipart/form-data" if options.delete(:multipart)
123
- options["data-remote"] = "true" if options.delete(:remote)
126
+ options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
127
+ options['data-remote'] = 'true' if options.delete(:remote)
124
128
  inner_form_html = hidden_form_method_field(desired_method)
125
129
  inner_form_html += block_given? ? capture_html(&block) : submit_tag(name)
126
130
  content_tag('form', inner_form_html, options)
@@ -349,86 +353,65 @@ module Padrino
349
353
  end
350
354
 
351
355
  private
356
+ ##
357
+ # Returns the uri root of the application with optional paths appended.
358
+ #
359
+ # @example
360
+ # uri_root_path("/some/path") => "/root/some/path"
361
+ # uri_root_path("javascripts", "test.js") => "/uri/root/javascripts/test.js"
362
+ #
363
+ def uri_root_path(*paths)
364
+ root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
365
+ File.join(ENV['RACK_BASE_URI'].to_s, root_uri || '/', *paths)
366
+ end
352
367
 
353
- ##
354
- # Returns the uri root of the application with optional paths appended.
355
- #
356
- # @example
357
- # uri_root_path("/some/path") => "/root/some/path"
358
- # uri_root_path("javascripts", "test.js") => "/uri/root/javascripts/test.js"
359
- #
360
- def uri_root_path(*paths)
361
- root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
362
- File.join(ENV['RACK_BASE_URI'].to_s, root_uri || '/', *paths)
363
- end
364
-
365
- ##
366
- # Returns the timestamp mtime for an asset
367
- #
368
- # @example
369
- # asset_timestamp("some/path/to/file.png") => "?154543678"
370
- # asset_timestamp("/some/absolute/path.png", true) => nil
371
- #
372
- def asset_timestamp(file_path, absolute=false)
373
- return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
374
- public_file_path = Padrino.root("public", file_path) if Padrino.respond_to?(:root)
375
- stamp = File.mtime(public_file_path).to_i if public_file_path && File.exist?(public_file_path)
376
- stamp ||= Time.now.to_i unless absolute
377
- "?#{stamp}" if stamp
378
- end
379
-
380
- ###
381
- # Returns the asset folder given a kind.
382
- #
383
- # @example
384
- # asset_folder_name(:css) => 'stylesheets'
385
- # asset_folder_name(:js) => 'javascripts'
386
- # asset_folder_name(:images) => 'images'
387
- #
388
- def asset_folder_name(kind)
389
- case kind
390
- when :css then 'stylesheets'
391
- when :js then 'javascripts'
392
- else kind.to_s
393
- end
394
- end
368
+ ##
369
+ # Returns the timestamp mtime for an asset
370
+ #
371
+ # @example
372
+ # asset_timestamp("some/path/to/file.png") => "?154543678"
373
+ # asset_timestamp("/some/absolute/path.png", true) => nil
374
+ #
375
+ def asset_timestamp(file_path, absolute=false)
376
+ return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
377
+ public_file_path = Padrino.root("public", file_path) if Padrino.respond_to?(:root)
378
+ stamp = File.mtime(public_file_path).to_i if public_file_path && File.exist?(public_file_path)
379
+ stamp ||= Time.now.to_i unless absolute
380
+ "?#{stamp}" if stamp
381
+ end
395
382
 
396
- ##
397
- # Parses link_to options for given correct conditions
398
- #
399
- # @example
400
- # parse_conditions("/some/url", :if => false) => true
401
- #
402
- def parse_conditions(url, options)
403
- if options.has_key?(:if)
404
- condition = options.delete(:if)
405
- condition == :current ? url == request.path_info : condition
406
- elsif condition = options.delete(:unless)
407
- condition == :current ? url != request.path_info : !condition
408
- else
409
- true
410
- end
383
+ ###
384
+ # Returns the asset folder given a kind.
385
+ #
386
+ # @example
387
+ # asset_folder_name(:css) => 'stylesheets'
388
+ # asset_folder_name(:js) => 'javascripts'
389
+ # asset_folder_name(:images) => 'images'
390
+ #
391
+ def asset_folder_name(kind)
392
+ case kind
393
+ when :css then 'stylesheets'
394
+ when :js then 'javascripts'
395
+ else kind.to_s
411
396
  end
397
+ end
412
398
 
413
- ##
414
- # Parses link_to options for given js declarations (remote, method, confirm)
415
- # Not destructive on options; returns updated options
416
- #
417
- # parse_js_attributes(:remote => true, :confirm => "test", :method => :delete)
418
- # => { "data-remote" => true, "data-method" => "delete", "data-confirm" => "test" }
419
- #
420
- def parse_js_attributes(options)
421
- options = options.dup
422
- options["data-remote"] = "true" if options.delete(:remote)
423
- if link_confirm = options.delete(:confirm)
424
- options["data-confirm"] = link_confirm
425
- end
426
- if link_method = options.delete(:method)
427
- options["data-method"] = link_method
428
- options["rel"] = "nofollow"
429
- end
430
- options
399
+ ##
400
+ # Parses link_to options for given correct conditions
401
+ #
402
+ # @example
403
+ # parse_conditions("/some/url", :if => false) => true
404
+ #
405
+ def parse_conditions(url, options)
406
+ if options.has_key?(:if)
407
+ condition = options.delete(:if)
408
+ condition == :current ? url == request.path_info : condition
409
+ elsif condition = options.delete(:unless)
410
+ condition == :current ? url != request.path_info : !condition
411
+ else
412
+ true
431
413
  end
414
+ end
432
415
  end # AssetTagHelpers
433
416
  end # Helpers
434
417
  end # Padrino
@@ -74,13 +74,11 @@ module Padrino
74
74
  #
75
75
  # @api public
76
76
  def form_tag(url, options={}, &block)
77
- desired_method = options[:method]
78
- data_method = options.delete(:method) if options[:method].to_s !~ /get|post/i
79
- options.reverse_merge!(:method => "post", :action => url)
80
- options[:enctype] = "multipart/form-data" if options.delete(:multipart)
81
- options["data-remote"] = "true" if options.delete(:remote)
82
- options["data-method"] = data_method if data_method
83
- options["accept-charset"] ||= "UTF-8"
77
+ desired_method = options[:method].to_s
78
+ options.delete(:method) unless desired_method =~ /get|post/i
79
+ options.reverse_merge!(:method => 'post', :action => url)
80
+ options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
81
+ options['accept-charset'] ||= 'UTF-8'
84
82
  inner_form_html = hidden_form_method_field(desired_method)
85
83
  inner_form_html += capture_html(&block)
86
84
  concat_content content_tag(:form, inner_form_html, options)
@@ -2,11 +2,11 @@ module Padrino
2
2
  module Helpers
3
3
  ##
4
4
  # Helpers related to producing html tags within templates.
5
- ##
5
+ #
6
6
  module TagHelpers
7
7
  ##
8
8
  # Tag values escaped to html entities
9
- ##
9
+ #
10
10
  ESCAPE_VALUES = {
11
11
  "<" => "&lt;",
12
12
  ">" => "&gt;",
@@ -28,6 +28,20 @@ module Padrino
28
28
  :selected
29
29
  ]
30
30
 
31
+ ##
32
+ # Custom data attributes,
33
+ # feel free to update with yours:
34
+ #
35
+ # Padrino::Helpers::TagHelpers::DATA_ATTRIBUTES.push(:dialog)
36
+ # text_field :foo, :dialog => true
37
+ # # Generates: <input type="text" data-dialog="true" name="foo" />
38
+ #
39
+ DATA_ATTRIBUTES = [
40
+ :method,
41
+ :remote,
42
+ :confirm
43
+ ]
44
+
31
45
  ##
32
46
  # Creates an HTML tag with given name, content, and options
33
47
  #
@@ -92,7 +106,9 @@ module Padrino
92
106
 
93
107
  content = content.join("\n") if content.respond_to?(:join)
94
108
 
95
- output = "<#{name}#{tag_options(options) if options}>#{content}</#{name}>"
109
+ options = parse_data_options(name, options)
110
+ attributes = tag_attributes(options)
111
+ output = "<#{name}#{attributes}>#{content}</#{name}>"
96
112
  block_is_template?(block) ? concat_content(output) : output
97
113
  end
98
114
 
@@ -182,35 +198,63 @@ module Padrino
182
198
  #
183
199
  # @api public
184
200
  def tag(name, options = nil, open = false)
185
- "<#{name}#{tag_options(options) if options}#{open ? '>' : ' />'}"
201
+ options = parse_data_options(name, options)
202
+ attributes = tag_attributes(options)
203
+ "<#{name}#{attributes}#{open ? '>' : ' />'}"
186
204
  end
187
205
 
188
206
  private
189
- ##
190
- # Returns a compiled list of HTML attributes
191
- ##
192
- def tag_options(options)
193
- return if options.blank?
194
- attributes = []
195
- options.each do |attribute, value|
196
- next if value.nil? || value == false
197
- if attribute == :data && value.is_a?(Hash)
198
- value.each { |k, v| attributes << %[data-#{k.to_s.dasherize}="#{escape_value(v)}"] }
199
- elsif BOOLEAN_ATTRIBUTES.include?(attribute)
200
- attributes << attribute.to_s
201
- else
202
- attributes << %[#{attribute}="#{escape_value(value)}"]
203
- end
207
+ ##
208
+ # Returns a compiled list of HTML attributes
209
+ ##
210
+ def tag_attributes(options)
211
+ return '' if options.nil?
212
+ attributes = options.map do |k, v|
213
+ next if v.nil? || v == false
214
+ if v.is_a?(Hash)
215
+ nested_values(k, v)
216
+ elsif BOOLEAN_ATTRIBUTES.include?(k)
217
+ k.to_s
218
+ else
219
+ %(#{k}="#{escape_value(v)}")
204
220
  end
205
- " #{attributes.join(' ')}"
206
- end
221
+ end.compact
222
+ attributes.empty? ? '' : " #{attributes * ' '}"
223
+ end
207
224
 
208
- ##
209
- # Escape tag values to their HTML/XML entities.
210
- ##
211
- def escape_value(string)
212
- string.to_s.gsub(Regexp.union(*ESCAPE_VALUES.keys)){|c| ESCAPE_VALUES[c] }
225
+ ##
226
+ # Escape tag values to their HTML/XML entities.
227
+ ##
228
+ def escape_value(string)
229
+ string.to_s.gsub(Regexp.union(*ESCAPE_VALUES.keys)) { |c| ESCAPE_VALUES[c] }
230
+ end
231
+
232
+ ##
233
+ # Iterate through nested values
234
+ #
235
+ def nested_values(attribute, hash)
236
+ hash.map do |k, v|
237
+ if v.is_a?(Hash)
238
+ nested_values("#{attribute}-#{k.to_s.dasherize}", v)
239
+ else
240
+ %(#{attribute}-#{k.to_s.dasherize}="#{escape_value(v)}")
241
+ end
242
+ end * ' '
243
+ end
244
+
245
+ ##
246
+ # Parses custom data attributes
247
+ #
248
+ def parse_data_options(tag, options)
249
+ return if options.nil?
250
+ parsed_options = options.dup
251
+ options.each do |k, v|
252
+ next if !DATA_ATTRIBUTES.include?(k) || (tag.to_s == 'form' && k == :method)
253
+ parsed_options["data-#{k}"] = parsed_options.delete(k)
254
+ parsed_options[:rel] = 'nofollow' if k == :method
213
255
  end
256
+ parsed_options
257
+ end
214
258
  end # TagHelpers
215
259
  end # Helpers
216
260
  end # Padrino
@@ -9,7 +9,7 @@ describe "AssetTagHelpers" do
9
9
  end
10
10
 
11
11
  def flash
12
- { :notice => "Demo notice" }
12
+ @_flash ||= { :notice => "Demo notice" }
13
13
  end
14
14
 
15
15
  context 'for #flash_tag method' do
@@ -20,6 +20,14 @@ describe "AssetTagHelpers" do
20
20
  actual_html = flash_tag(:notice, :class => 'notice', :id => 'notice-area')
21
21
  assert_has_tag('div.notice#notice-area', :content => "Demo notice") { actual_html }
22
22
  end
23
+ should "display multiple flash tags with given attributes" do
24
+ flash[:error] = 'wrong'
25
+ flash[:success] = 'okey'
26
+ actual_html = flash_tag(:success, :error, :id => 'area')
27
+ assert_has_tag('div.success#area', :content => flash[:success]) { actual_html }
28
+ assert_has_tag('div.error#area', :content => flash[:error]) { actual_html }
29
+ assert_has_no_tag('div.notice') { actual_html }
30
+ end
23
31
  end
24
32
 
25
33
  context 'for #link_to method' do
@@ -39,7 +47,7 @@ describe "AssetTagHelpers" do
39
47
 
40
48
  should "display link element with void url and options" do
41
49
  actual_link = link_to('Sign up', :class => "test")
42
- assert_has_tag('a', :content => "Sign up", :href => 'javascript:void(0);', :class => 'test') { actual_link }
50
+ assert_has_tag('a', :content => "Sign up", :href => '#', :class => 'test') { actual_link }
43
51
  end
44
52
 
45
53
  should "display link element with remote option" do
@@ -63,7 +63,7 @@ describe "FormBuilder" do
63
63
 
64
64
  should "display correct form html with remote option and method put" do
65
65
  actual_html = form_for(@user, '/update', :"accept-charset" => "UTF-8", :remote => true, :method => 'put') { "Demo" }
66
- assert_has_tag('form', :"accept-charset" => "UTF-8", :method => 'post', "data-method" => 'put', "data-remote" => 'true') { actual_html }
66
+ assert_has_tag('form', :"accept-charset" => "UTF-8", :method => 'post', "data-remote" => 'true') { actual_html }
67
67
  assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
68
68
  end
69
69
 
@@ -28,7 +28,7 @@ describe "FormHelpers" do
28
28
 
29
29
  should "display correct form with remote and method is put" do
30
30
  actual_html = form_tag('/update', :"accept-charset" => "UTF-8", :method => 'put', :remote => true) { "Demo" }
31
- assert_has_tag(:form, "data-remote" => 'true', :"accept-charset" => "UTF-8", :"data-method" => 'put') { actual_html }
31
+ assert_has_tag(:form, "data-remote" => 'true', :"accept-charset" => "UTF-8") { actual_html }
32
32
  assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'put') { actual_html }
33
33
  end
34
34
 
@@ -1,3 +1,6 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/markup_app/app')
3
+
1
4
  describe "NumberHelpers" do
2
5
  include Padrino::Helpers::NumberHelpers
3
6
 
@@ -26,6 +26,11 @@ describe "TagHelpers" do
26
26
  assert_has_tag(:a, 'data-remote' => 'true', 'data-method' => 'post') { actual_html }
27
27
  end
28
28
 
29
+ should "support nested attributes" do
30
+ actual_html = tag(:div, :data => {:dojo => {:type => 'dijit.form.TextBox', :props => 'readOnly: true'}})
31
+ assert_has_tag(:div, 'data-dojo-type' => 'dijit.form.TextBox', 'data-dojo-props' => 'readOnly: true') { actual_html }
32
+ end
33
+
29
34
  should "support open tags" do
30
35
  actual_html = tag(:p, { :class => 'demo' }, true)
31
36
  assert_equal "<p class=\"demo\">", actual_html
metadata CHANGED
@@ -1,14 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: -1410374394
5
- prerelease: 7
6
- segments:
7
- - 0
8
- - 10
9
- - 6
10
- - e
11
- version: 0.10.6.e
4
+ prerelease:
5
+ version: 0.10.6
12
6
  platform: ruby
13
7
  authors:
14
8
  - Padrino Team
@@ -19,7 +13,7 @@ autorequire:
19
13
  bindir: bin
20
14
  cert_chain: []
21
15
 
22
- date: 2012-02-23 00:00:00 Z
16
+ date: 2012-03-15 00:00:00 Z
23
17
  dependencies:
24
18
  - !ruby/object:Gem::Dependency
25
19
  name: padrino-core
@@ -29,13 +23,7 @@ dependencies:
29
23
  requirements:
30
24
  - - "="
31
25
  - !ruby/object:Gem::Version
32
- hash: -1410374394
33
- segments:
34
- - 0
35
- - 10
36
- - 6
37
- - e
38
- version: 0.10.6.e
26
+ version: 0.10.6
39
27
  type: :runtime
40
28
  version_requirements: *id001
41
29
  - !ruby/object:Gem::Dependency
@@ -46,10 +34,6 @@ dependencies:
46
34
  requirements:
47
35
  - - ~>
48
36
  - !ruby/object:Gem::Version
49
- hash: 7
50
- segments:
51
- - 0
52
- - 6
53
37
  version: "0.6"
54
38
  type: :runtime
55
39
  version_requirements: *id002
@@ -174,25 +158,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
158
  requirements:
175
159
  - - ">="
176
160
  - !ruby/object:Gem::Version
177
- hash: 3
178
- segments:
179
- - 0
180
161
  version: "0"
181
162
  required_rubygems_version: !ruby/object:Gem::Requirement
182
163
  none: false
183
164
  requirements:
184
- - - ">"
165
+ - - ">="
185
166
  - !ruby/object:Gem::Version
186
- hash: 25
187
- segments:
188
- - 1
189
- - 3
190
- - 1
191
- version: 1.3.1
167
+ version: 1.3.6
192
168
  requirements: []
193
169
 
194
170
  rubyforge_project: padrino-helpers
195
- rubygems_version: 1.8.15
171
+ rubygems_version: 1.8.19
196
172
  signing_key:
197
173
  specification_version: 3
198
174
  summary: Helpers for padrino
@@ -255,4 +231,3 @@ test_files:
255
231
  - test/test_output_helpers.rb
256
232
  - test/test_render_helpers.rb
257
233
  - test/test_tag_helpers.rb
258
- has_rdoc: