padrino-helpers 0.16.0.pre4 → 0.16.0

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.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +29 -29
  3. data/Rakefile +1 -1
  4. data/lib/padrino/rendering/erb_template.rb +7 -7
  5. data/lib/padrino/rendering/erubi_template.rb +4 -4
  6. data/lib/padrino/rendering/erubis_template.rb +4 -4
  7. data/lib/padrino/rendering/haml_template.rb +1 -1
  8. data/lib/padrino/rendering/hamlit_template.rb +4 -4
  9. data/lib/padrino/rendering/slim_template.rb +5 -5
  10. data/lib/padrino/rendering.rb +54 -45
  11. data/lib/padrino/safe_buffer.rb +31 -36
  12. data/lib/padrino-helpers/asset_tag_helpers.rb +48 -48
  13. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +74 -70
  14. data/lib/padrino-helpers/form_builder/standard_form_builder.rb +20 -20
  15. data/lib/padrino-helpers/form_helpers/errors.rb +14 -14
  16. data/lib/padrino-helpers/form_helpers/options.rb +25 -24
  17. data/lib/padrino-helpers/form_helpers/security.rb +5 -5
  18. data/lib/padrino-helpers/form_helpers.rb +133 -133
  19. data/lib/padrino-helpers/format_helpers.rb +61 -63
  20. data/lib/padrino-helpers/number_helpers.rb +82 -78
  21. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +5 -3
  22. data/lib/padrino-helpers/output_helpers/erb_handler.rb +2 -2
  23. data/lib/padrino-helpers/output_helpers/haml_handler.rb +1 -1
  24. data/lib/padrino-helpers/output_helpers.rb +16 -17
  25. data/lib/padrino-helpers/render_helpers.rb +28 -27
  26. data/lib/padrino-helpers/tag_helpers.rb +62 -58
  27. data/lib/padrino-helpers/translation_helpers.rb +2 -2
  28. data/lib/padrino-helpers.rb +1 -2
  29. data/padrino-helpers.gemspec +17 -17
  30. data/test/fixtures/apps/render.rb +2 -2
  31. data/test/fixtures/markup_app/app.rb +9 -8
  32. data/test/fixtures/markup_app/views/button_to.erb +3 -3
  33. data/test/fixtures/markup_app/views/button_to.haml +2 -2
  34. data/test/fixtures/markup_app/views/button_to.slim +3 -3
  35. data/test/fixtures/markup_app/views/content_for.erb +4 -4
  36. data/test/fixtures/markup_app/views/content_for.haml +4 -4
  37. data/test/fixtures/markup_app/views/content_for.slim +2 -2
  38. data/test/fixtures/markup_app/views/content_tag.erb +3 -3
  39. data/test/fixtures/markup_app/views/content_tag.haml +3 -3
  40. data/test/fixtures/markup_app/views/content_tag.slim +3 -3
  41. data/test/fixtures/markup_app/views/fields_for.erb +2 -2
  42. data/test/fixtures/markup_app/views/fields_for.haml +2 -2
  43. data/test/fixtures/markup_app/views/fields_for.slim +2 -2
  44. data/test/fixtures/markup_app/views/form_for.erb +41 -41
  45. data/test/fixtures/markup_app/views/form_for.haml +41 -41
  46. data/test/fixtures/markup_app/views/form_for.slim +41 -42
  47. data/test/fixtures/markup_app/views/form_tag.erb +47 -47
  48. data/test/fixtures/markup_app/views/form_tag.haml +48 -48
  49. data/test/fixtures/markup_app/views/form_tag.slim +49 -49
  50. data/test/fixtures/markup_app/views/link_to.erb +2 -2
  51. data/test/fixtures/markup_app/views/link_to.haml +2 -2
  52. data/test/fixtures/markup_app/views/link_to.slim +2 -2
  53. data/test/fixtures/markup_app/views/meta_tag.erb +2 -2
  54. data/test/fixtures/markup_app/views/meta_tag.haml +2 -2
  55. data/test/fixtures/markup_app/views/meta_tag.slim +2 -2
  56. data/test/fixtures/markup_app/views/simple_partial.erb +1 -1
  57. data/test/fixtures/markup_app/views/simple_partial.haml +1 -1
  58. data/test/fixtures/markup_app/views/simple_partial.slim +1 -1
  59. data/test/fixtures/render_app/app.rb +13 -12
  60. data/test/fixtures/render_app/views/_unsafe.html.builder +1 -1
  61. data/test/fixtures/render_app/views/_unsafe_object.html.builder +1 -1
  62. data/test/fixtures/render_app/views/explicit_engine.haml +3 -3
  63. data/test/helper.rb +19 -19
  64. data/test/test_asset_tag_helpers.rb +133 -133
  65. data/test/test_form_builder.rb +629 -628
  66. data/test/test_form_helpers.rb +610 -610
  67. data/test/test_format_helpers.rb +66 -63
  68. data/test/test_helpers.rb +2 -2
  69. data/test/test_locale.rb +13 -11
  70. data/test/test_number_helpers.rb +65 -65
  71. data/test/test_output_helpers.rb +76 -76
  72. data/test/test_render_helpers.rb +101 -101
  73. data/test/test_rendering.rb +329 -326
  74. data/test/test_rendering_extensions.rb +4 -4
  75. data/test/test_tag_helpers.rb +55 -55
  76. metadata +6 -6
@@ -3,35 +3,29 @@ module Padrino
3
3
  # Padrino::SafeBuffer is based on ActiveSupport::SafeBuffer
4
4
  #
5
5
  class SafeBuffer < String
6
- UNSAFE_STRING_METHODS = %w(
6
+ UNSAFE_STRING_METHODS = %w[
7
7
  capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
8
8
  slice squeeze strip sub succ swapcase tr tr_s upcase
9
- )
9
+ ]
10
10
 
11
- alias_method :original_concat, :concat
11
+ alias original_concat concat
12
12
  private :original_concat
13
13
 
14
14
  class SafeConcatError < StandardError
15
15
  def initialize
16
- super "Could not concatenate to the buffer because it is not html safe."
16
+ super 'Could not concatenate to the buffer because it is not html safe.'
17
17
  end
18
18
  end
19
19
 
20
20
  def [](*args)
21
21
  if args.size < 2
22
22
  super
23
+ elsif html_safe?
24
+ new_safe_buffer = super
25
+ new_safe_buffer&.instance_variable_set :@html_safe, true
26
+ new_safe_buffer
23
27
  else
24
- if html_safe?
25
- new_safe_buffer = super
26
-
27
- if new_safe_buffer
28
- new_safe_buffer.instance_variable_set :@html_safe, true
29
- end
30
-
31
- new_safe_buffer
32
- else
33
- to_str[*args]
34
- end
28
+ to_str[*args]
35
29
  end
36
30
  end
37
31
 
@@ -40,7 +34,7 @@ module Padrino
40
34
  original_concat(value)
41
35
  end
42
36
 
43
- def initialize(str = "")
37
+ def initialize(str = '')
44
38
  @html_safe = true
45
39
  super
46
40
  end
@@ -67,13 +61,14 @@ module Padrino
67
61
  dup.concat(other)
68
62
  end
69
63
 
70
- def %(args)
71
- case args
72
- when Hash
73
- escaped_args = Hash[args.map { |k,arg| [k, html_escape_interpolated_argument(arg)] }]
74
- else
75
- escaped_args = Array(args).map { |arg| html_escape_interpolated_argument(arg) }
76
- end
64
+ def %(other)
65
+ escaped_args =
66
+ case other
67
+ when Hash
68
+ other.transform_values { |arg| html_escape_interpolated_argument(arg) }
69
+ else
70
+ Array(other).map { |arg| html_escape_interpolated_argument(arg) }
71
+ end
77
72
 
78
73
  self.class.new(super(escaped_args))
79
74
  end
@@ -95,24 +90,24 @@ module Padrino
95
90
  end
96
91
 
97
92
  UNSAFE_STRING_METHODS.each do |unsafe_method|
98
- if unsafe_method.respond_to?(unsafe_method)
99
- class_eval <<-EOT, __FILE__, __LINE__ + 1
100
- def #{unsafe_method}(*args, &block) # def capitalize(*args, &block)
101
- to_str.#{unsafe_method}(*args, &block) # to_str.capitalize(*args, &block)
102
- end # end
103
-
104
- def #{unsafe_method}!(*args) # def capitalize!(*args)
105
- @html_safe = false # @html_safe = false
106
- super # super
107
- end # end
108
- EOT
109
- end
93
+ next unless unsafe_method.respond_to?(unsafe_method)
94
+
95
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
96
+ def #{unsafe_method}(*args, &block) # def capitalize(*args, &block)
97
+ to_str.#{unsafe_method}(*args, &block) # to_str.capitalize(*args, &block)
98
+ end # end
99
+
100
+ def #{unsafe_method}!(*args) # def capitalize!(*args)
101
+ @html_safe = false # @html_safe = false
102
+ super # super
103
+ end # end
104
+ RUBY
110
105
  end
111
106
 
112
107
  private
113
108
 
114
109
  def html_escape_interpolated_argument(arg)
115
- (!html_safe? || arg.html_safe?) ? arg : CGI.escapeHTML(arg.to_s)
110
+ !html_safe? || arg.html_safe? ? arg : CGI.escapeHTML(arg.to_s)
116
111
  end
117
112
  end
118
113
  end
@@ -4,11 +4,11 @@ module Padrino
4
4
  # Helpers related to producing assets (images, stylesheets, js, etc) within templates.
5
5
  #
6
6
  module AssetTagHelpers
7
- APPEND_ASSET_EXTENSIONS = ["js", "css"]
7
+ APPEND_ASSET_EXTENSIONS = %w[js css]
8
8
  ABSOLUTE_URL_PATTERN = %r{^(https?://)}
9
9
  ASSET_FOLDERS = {
10
- :js => 'javascripts',
11
- :css => 'stylesheets',
10
+ js: 'javascripts',
11
+ css: 'stylesheets'
12
12
  }
13
13
 
14
14
  ##
@@ -18,12 +18,12 @@ module Padrino
18
18
  # The type of flash to display in the tag.
19
19
  # @param [Hash] options
20
20
  # The html options for this section.
21
- # use :bootstrap => true to support Twitter's bootstrap dismiss alert button.
21
+ # use +bootstrap: true+ to support Twitter's bootstrap dismiss alert button.
22
22
  #
23
23
  # @return [String] Flash tag html with specified +options+.
24
24
  #
25
25
  # @example
26
- # flash_tag(:notice, :id => 'flash-notice')
26
+ # flash_tag(:notice, id: 'flash-notice')
27
27
  # # Generates: <div class="notice" id="flash-notice">flash-notice</div>
28
28
  # flash_tag(:error, :success)
29
29
  # # Generates: <div class="error">flash-error</div>
@@ -32,11 +32,11 @@ module Padrino
32
32
  def flash_tag(*args)
33
33
  options = args.last.is_a?(Hash) ? args.pop : {}
34
34
  bootstrap = options.delete(:bootstrap) if options[:bootstrap]
35
- args.inject(SafeBuffer.new) do |html,kind|
35
+ args.inject(SafeBuffer.new) do |html, kind|
36
36
  next html unless flash[kind]
37
37
  flash_text = SafeBuffer.new << flash[kind]
38
- flash_text << content_tag(:button, '&times;'.html_safe, {:type => :button, :class => :close, :'data-dismiss' => :alert}) if bootstrap
39
- html << content_tag(:div, flash_text, { :class => kind }.update(options))
38
+ flash_text << content_tag(:button, '&times;'.html_safe, { type: :button, class: :close, 'data-dismiss': :alert }) if bootstrap
39
+ html << content_tag(:div, flash_text, { class: kind }.update(options))
40
40
  end
41
41
  end
42
42
 
@@ -66,16 +66,16 @@ module Padrino
66
66
  # @return [String] Link tag html with specified +options+.
67
67
  #
68
68
  # @example
69
- # link_to('click me', '/dashboard', :class => 'linky')
69
+ # link_to('click me', '/dashboard', class: 'linky')
70
70
  # # Generates <a class="linky" href="/dashboard">click me</a>
71
71
  #
72
- # link_to('click me', '/dashboard', :remote => true)
72
+ # link_to('click me', '/dashboard', remote: true)
73
73
  # # Generates <a href="/dashboard" data-remote="true">click me</a>
74
74
  #
75
- # link_to('click me', '/dashboard', :method => :delete)
75
+ # link_to('click me', '/dashboard', method: :delete)
76
76
  # # Generates <a href="/dashboard" data-method="delete" rel="nofollow">click me</a>
77
77
  #
78
- # link_to('/dashboard', :class => 'blocky') { 'click me' }
78
+ # link_to('/dashboard', class: 'blocky') { 'click me' }
79
79
  # # Generates <a class="blocky" href="/dashboard">click me</a>
80
80
  #
81
81
  # Note that you can pass :+if+ or :+unless+ conditions, but if you provide :current as
@@ -85,7 +85,7 @@ module Padrino
85
85
  options = args.last.is_a?(Hash) ? args.pop : {}
86
86
  name = block_given? ? '' : args.shift
87
87
  href = args.first
88
- options = { :href => href ? escape_link(href) : '#' }.update(options)
88
+ options = { href: href ? escape_link(href) : '#' }.update(options)
89
89
  return name unless parse_conditions(href, options)
90
90
  block_given? ? content_tag(:a, options, &block) : content_tag(:a, name, options)
91
91
  end
@@ -109,14 +109,14 @@ module Padrino
109
109
  # @return [String] Feed link html tag with specified +options+.
110
110
  #
111
111
  # @example
112
- # feed_tag :atom, url(:blog, :posts, :format => :atom), :title => "ATOM"
112
+ # feed_tag :atom, url(:blog, :posts, format: :atom), title: "ATOM"
113
113
  # # Generates: <link type="application/atom+xml" rel="alternate" href="/blog/posts.atom" title="ATOM" />
114
- # feed_tag :rss, url(:blog, :posts, :format => :rss)
114
+ # feed_tag :rss, url(:blog, :posts, format: :rss)
115
115
  # # Generates: <link type="application/rss+xml" rel="alternate" href="/blog/posts.rss" title="rss" />
116
116
  #
117
- def feed_tag(mime, url, options={})
118
- full_mime = (mime == :atom) ? 'application/atom+xml' : 'application/rss+xml'
119
- tag(:link, { :rel => 'alternate', :type => full_mime, :title => mime, :href => url }.update(options))
117
+ def feed_tag(mime, url, options = {})
118
+ full_mime = mime == :atom ? 'application/atom+xml' : 'application/rss+xml'
119
+ tag(:link, { rel: 'alternate', type: full_mime, title: mime, href: url }.update(options))
120
120
  end
121
121
 
122
122
  ##
@@ -142,12 +142,12 @@ module Padrino
142
142
  # mail_to "me@demo.com", "My Email"
143
143
  # # Generates: <a href="mailto:me@demo.com">My Email</a>
144
144
  #
145
- def mail_to(email, caption=nil, mail_options={})
146
- mail_options, html_options = mail_options.partition{ |key,_| [:cc, :bcc, :subject, :body].include?(key) }
145
+ def mail_to(email, caption = nil, mail_options = {})
146
+ mail_options, html_options = mail_options.partition { |key, _| %i[cc bcc subject body].include?(key) }
147
147
  mail_query = Rack::Utils.build_query(Hash[mail_options]).gsub(/\+/, '%20').gsub('%40', '@')
148
148
  mail_href = "mailto:#{email}"
149
149
  mail_href << "?#{mail_query}" unless mail_query.empty?
150
- link_to((caption || email), mail_href, Hash[html_options])
150
+ link_to(caption || email, mail_href, Hash[html_options])
151
151
  end
152
152
 
153
153
  ##
@@ -161,14 +161,14 @@ module Padrino
161
161
  # @return [String] Meta html tag with specified +options+.
162
162
  #
163
163
  # @example
164
- # meta_tag "weblog,news", :name => "keywords"
164
+ # meta_tag 'weblog,news', name: 'keywords'
165
165
  # # Generates: <meta name="keywords" content="weblog,news" />
166
166
  #
167
- # meta_tag "text/html; charset=UTF-8", 'http-equiv' => "Content-Type"
167
+ # meta_tag 'text/html; charset=UTF-8', 'http-equiv' => 'Content-Type'
168
168
  # # Generates: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
169
169
  #
170
- def meta_tag(content, options={})
171
- options = { "content" => content }.update(options)
170
+ def meta_tag(content, options = {})
171
+ options = { 'content' => content }.update(options)
172
172
  tag(:meta, options)
173
173
  end
174
174
 
@@ -186,11 +186,11 @@ module Padrino
186
186
  # favicon_tag 'favicon.png'
187
187
  # favicon_tag 'icons/favicon.png'
188
188
  # # or override some options
189
- # favicon_tag 'favicon.png', :type => 'image/ico'
189
+ # favicon_tag 'favicon.png', type: 'image/ico'
190
190
  #
191
- def favicon_tag(source, options={})
192
- type = File.extname(source).sub('.','')
193
- options = { :href => image_path(source), :rel => 'icon', :type => "image/#{type}" }.update(options)
191
+ def favicon_tag(source, options = {})
192
+ type = File.extname(source).sub('.', '')
193
+ options = { href: image_path(source), rel: 'icon', type: "image/#{type}" }.update(options)
194
194
  tag(:link, options)
195
195
  end
196
196
 
@@ -207,8 +207,8 @@ module Padrino
207
207
  # @example
208
208
  # image_tag('icons/avatar.png')
209
209
  #
210
- def image_tag(url, options={})
211
- options = { :src => image_path(url) }.update(options)
210
+ def image_tag(url, options = {})
211
+ options = { src: image_path(url) }.update(options)
212
212
  options[:alt] ||= image_alt(url) unless url.to_s =~ /\A(?:cid|data):|\A\Z/
213
213
  tag(:img, options)
214
214
  end
@@ -241,11 +241,11 @@ module Padrino
241
241
  # @api public.
242
242
  def stylesheet_link_tag(*sources)
243
243
  options = {
244
- :rel => 'stylesheet',
245
- :type => 'text/css'
244
+ rel: 'stylesheet',
245
+ type: 'text/css'
246
246
  }.update(sources.last.is_a?(Hash) ? Utils.symbolize_keys(sources.pop) : {})
247
- sources.flatten.inject(SafeBuffer.new) do |all,source|
248
- all << tag(:link, { :href => asset_path(:css, source) }.update(options))
247
+ sources.flatten.inject(SafeBuffer.new) do |all, source|
248
+ all << tag(:link, { href: asset_path(:css, source) }.update(options))
249
249
  end
250
250
  end
251
251
 
@@ -265,10 +265,10 @@ module Padrino
265
265
  #
266
266
  def javascript_include_tag(*sources)
267
267
  options = {
268
- :type => 'text/javascript'
268
+ type: 'text/javascript'
269
269
  }.update(sources.last.is_a?(Hash) ? Utils.symbolize_keys(sources.pop) : {})
270
- sources.flatten.inject(SafeBuffer.new) do |all,source|
271
- all << content_tag(:script, nil, { :src => asset_path(:js, source) }.update(options))
270
+ sources.flatten.inject(SafeBuffer.new) do |all, source|
271
+ all << content_tag(:script, nil, { src: asset_path(:js, source) }.update(options))
272
272
  end
273
273
  end
274
274
 
@@ -316,7 +316,7 @@ module Padrino
316
316
  def asset_path(kind, source = nil)
317
317
  kind, source = source, kind if source.nil?
318
318
  source = asset_normalize_extension(kind, escape_link(source.to_s))
319
- return source if source =~ ABSOLUTE_URL_PATTERN || source =~ /^\//
319
+ return source if source =~ ABSOLUTE_URL_PATTERN || source =~ %r{^/}
320
320
  source = File.join(asset_folder_name(kind), source)
321
321
  timestamp = asset_timestamp(source)
322
322
  result_path = uri_root_path(source)
@@ -329,8 +329,8 @@ module Padrino
329
329
  # Returns the URI root of the application with optional paths appended.
330
330
  #
331
331
  # @example
332
- # uri_root_path("/some/path") => "/root/some/path"
333
- # uri_root_path("javascripts", "test.js") => "/uri/root/javascripts/test.js"
332
+ # uri_root_path('/some/path') => "/root/some/path"
333
+ # uri_root_path('javascripts', 'test.js') => "/uri/root/javascripts/test.js"
334
334
  #
335
335
  def uri_root_path(*paths)
336
336
  root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
@@ -341,12 +341,12 @@ module Padrino
341
341
  # Returns the timestamp mtime for an asset.
342
342
  #
343
343
  # @example
344
- # asset_timestamp("some/path/to/file.png") => "?154543678"
344
+ # asset_timestamp('some/path/to/file.png') => "?154543678"
345
345
  #
346
346
  def asset_timestamp(file_path)
347
347
  return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
348
348
  public_path = self.class.public_folder if self.class.respond_to?(:public_folder)
349
- public_path ||= Padrino.root("public") if Padrino.respond_to?(:root)
349
+ public_path ||= Padrino.root('public') if Padrino.respond_to?(:root)
350
350
  public_file_path = File.join(public_path, file_path) if public_path
351
351
  stamp = File.mtime(public_file_path).to_i if public_file_path && File.exist?(public_file_path)
352
352
  stamp ||= Time.now.to_i
@@ -377,8 +377,8 @@ module Padrino
377
377
  #
378
378
  # @example
379
379
  #
380
- # asset_normalize_extension(:images, "/foo/bar/baz.png") => "/foo/bar/baz.png"
381
- # asset_normalize_extension(:js, "/foo/bar/baz") => "/foo/bar/baz.js"
380
+ # asset_normalize_extension(:images, '/foo/bar/baz.png') => "/foo/bar/baz.png"
381
+ # asset_normalize_extension(:js, '/foo/bar/baz') => "/foo/bar/baz.js"
382
382
  #
383
383
  def asset_normalize_extension(kind, source)
384
384
  ignore_extension = !APPEND_ASSET_EXTENSIONS.include?(kind.to_s)
@@ -390,13 +390,13 @@ module Padrino
390
390
  # Parses link_to options for given correct conditions.
391
391
  #
392
392
  # @example
393
- # parse_conditions("/some/url", :if => false) => true
393
+ # parse_conditions("/some/url", if: false) => true
394
394
  #
395
395
  def parse_conditions(url, options)
396
- if options.has_key?(:if)
396
+ if options.key?(:if)
397
397
  condition = options.delete(:if)
398
398
  condition == :current ? url == request.path_info : condition
399
- elsif condition = options.delete(:unless)
399
+ elsif (condition = options.delete(:unless))
400
400
  condition == :current ? url != request.path_info : !condition
401
401
  else
402
402
  true