padrino-helpers 0.10.6.b → 0.10.6.c

Sign up to get free protection for your applications and to get access to all the features.
@@ -338,27 +338,24 @@ module Padrino
338
338
  # @api semipublic
339
339
  def asset_path(kind, source)
340
340
  return source if source =~ /^http/
341
- asset_folder = case kind
342
- when :css then 'stylesheets'
343
- when :js then 'javascripts'
344
- else kind.to_s
345
- end
341
+ is_absolute = source =~ %r{^/}
342
+ asset_folder = asset_folder_name(kind)
346
343
  source = source.to_s.gsub(/\s/, '%20')
347
- ignore_extension = (asset_folder.to_s == kind.to_s) # don't append extension
344
+ ignore_extension = (asset_folder.to_s == kind.to_s) # don't append an extension
348
345
  source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
349
- result_path = source if source =~ %r{^/} # absolute path
350
- result_path ||= uri_root_path(asset_folder, source)
351
- timestamp = asset_timestamp(result_path)
346
+ result_path = is_absolute ? source : uri_root_path(asset_folder, source)
347
+ timestamp = asset_timestamp(result_path, is_absolute)
352
348
  "#{result_path}#{timestamp}"
353
349
  end
354
350
 
355
351
  private
356
352
 
357
353
  ##
358
- # Returns the uri root of the application.
354
+ # Returns the uri root of the application with optional paths appended.
359
355
  #
360
356
  # @example
361
- # uri_root_path("/some/path") => "/base/some/path"
357
+ # uri_root_path("/some/path") => "/root/some/path"
358
+ # uri_root_path("javascripts", "test.js") => "/uri/root/javascripts/test.js"
362
359
  #
363
360
  def uri_root_path(*paths)
364
361
  root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
@@ -370,13 +367,30 @@ module Padrino
370
367
  #
371
368
  # @example
372
369
  # asset_timestamp("some/path/to/file.png") => "?154543678"
370
+ # asset_timestamp("/some/absolute/path.png", true) => nil
373
371
  #
374
- def asset_timestamp(file_path)
372
+ def asset_timestamp(file_path, absolute=false)
375
373
  return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
376
- public_path = Padrino.root("public", file_path) if Padrino.respond_to?(:root)
377
- stamp = Time.now.to_i unless public_path && File.exist?(public_path)
378
- stamp ||= File.mtime(public_path).to_i
379
- "?#{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
380
394
  end
381
395
 
382
396
  ##
@@ -375,7 +375,7 @@ module Padrino
375
375
  javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
376
376
  html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
377
377
  end
378
-
378
+ alias :escape_javascript :js_escape_html
379
379
  end # FormatHelpers
380
380
  end # Helpers
381
381
  end # Padrino
@@ -146,10 +146,10 @@ describe "AssetTagHelpers" do
146
146
  context 'for #image_tag method' do
147
147
  should "display image tag absolute link with no options" do
148
148
  time = stop_time_for_test
149
- assert_has_tag('img', :src => "/absolute/pic.gif?#{time.to_i}") { image_tag('/absolute/pic.gif') }
149
+ assert_has_tag('img', :src => "/absolute/pic.gif") { image_tag('/absolute/pic.gif') }
150
150
  end
151
151
 
152
- should "display image tag absolute link with specified uri root" do
152
+ should "display image tag relative link with specified uri root" do
153
153
  time = stop_time_for_test
154
154
  self.class.stubs(:uri_root).returns("/blog")
155
155
  assert_has_tag('img', :src => "/blog/images/relative/pic.gif?#{time.to_i}") { image_tag('relative/pic.gif') }
@@ -196,7 +196,7 @@ describe "AssetTagHelpers" do
196
196
  time = stop_time_for_test
197
197
  expected_options = { :media => "screen", :rel => "stylesheet", :type => "text/css" }
198
198
  actual_html = stylesheet_link_tag('/css/style')
199
- assert_has_tag('link', expected_options.merge(:href => "/css/style.css?#{time.to_i}")) { actual_html }
199
+ assert_has_tag('link', expected_options.merge(:href => "/css/style.css")) { actual_html }
200
200
  end
201
201
 
202
202
  should "display stylesheet link item with uri root" do
@@ -252,7 +252,7 @@ describe "AssetTagHelpers" do
252
252
  should "display javascript item with absolute path" do
253
253
  time = stop_time_for_test
254
254
  actual_html = javascript_include_tag('/js/application')
255
- assert_has_tag('script', :src => "/js/application.js?#{time.to_i}", :type => "text/javascript") { actual_html }
255
+ assert_has_tag('script', :src => "/js/application.js", :type => "text/javascript") { actual_html }
256
256
  end
257
257
 
258
258
  should "display javascript item with uri root" do
@@ -738,7 +738,7 @@ describe "FormBuilder" do
738
738
 
739
739
  should "display correct image submit button html" do
740
740
  actual_html = standard_builder.image_submit('/system/ok.png', :class => 'large')
741
- assert_has_tag('input.large[type=image]', :src => "/system/ok.png?#{@stamp}") { actual_html }
741
+ assert_has_tag('input.large[type=image]', :src => "/system/ok.png") { actual_html }
742
742
  end
743
743
 
744
744
  should "display correct image submit button in haml" do
@@ -749,7 +749,7 @@ describe "FormHelpers" do
749
749
 
750
750
  should "display image submit tag in ruby with absolute path" do
751
751
  actual_html = image_submit_tag('/system/ok.png', :class => 'success')
752
- assert_has_tag(:input, :type => 'image', :class => "success", :src => "/system/ok.png?#{@stamp}") { actual_html }
752
+ assert_has_tag(:input, :type => 'image', :class => "success", :src => "/system/ok.png") { actual_html }
753
753
  end
754
754
 
755
755
  should "display image submit tag in erb" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 99
4
+ hash: 96
5
5
  prerelease: 7
6
6
  segments:
7
7
  - 0
8
8
  - 10
9
9
  - 6
10
- - b
11
- version: 0.10.6.b
10
+ - c
11
+ version: 0.10.6.c
12
12
  platform: ruby
13
13
  authors:
14
14
  - Padrino Team
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2012-01-23 00:00:00 -08:00
22
+ date: 2012-01-25 00:00:00 -08:00
23
23
  default_executable:
24
24
  dependencies:
25
25
  - !ruby/object:Gem::Dependency
@@ -30,13 +30,13 @@ dependencies:
30
30
  requirements:
31
31
  - - "="
32
32
  - !ruby/object:Gem::Version
33
- hash: 99
33
+ hash: 96
34
34
  segments:
35
35
  - 0
36
36
  - 10
37
37
  - 6
38
- - b
39
- version: 0.10.6.b
38
+ - c
39
+ version: 0.10.6.c
40
40
  type: :runtime
41
41
  version_requirements: *id001
42
42
  - !ruby/object:Gem::Dependency