Guest User

Untitled

a guest
Jul 22nd, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. From fbe5ddcd44555eadc0ce69e0822515db14601c8b Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@gmail.com>
  3. Date: Tue, 26 Jan 2010 18:37:25 +0100
  4. Subject: [PATCH] Make t('.helper') work again.
  5.  
  6. ---
  7. actionpack/lib/action_view/base.rb | 2 +-
  8. .../lib/action_view/helpers/translation_helper.rb | 2 +-
  9. actionpack/lib/action_view/template.rb | 5 +++++
  10. actionpack/lib/action_view/template/resolver.rb | 12 +++++++-----
  11. actionpack/test/controller/helper_test.rb | 20 ++++++++++----------
  12. actionpack/test/fixtures/test/translation.erb | 1 +
  13. .../test/template/translation_helper_test.rb | 10 +++++-----
  14. 7 files changed, 30 insertions(+), 22 deletions(-)
  15. create mode 100644 actionpack/test/fixtures/test/translation.erb
  16.  
  17. diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
  18. index c4b0455..3e38b70 100644
  19. --- a/actionpack/lib/action_view/base.rb
  20. +++ b/actionpack/lib/action_view/base.rb
  21. @@ -220,7 +220,7 @@ module ActionView #:nodoc:
  22. ActionController::Base.allow_concurrency || (cache_template_loading.nil? ? !ActiveSupport::Dependencies.load? : cache_template_loading)
  23. end
  24.  
  25. - attr_internal :request, :layout
  26. + attr_internal :request, :layout, :_template
  27.  
  28. def controller_path
  29. @controller_path ||= controller && controller.controller_path
  30. diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb
  31. index 35c431d..4ca8a11 100644
  32. --- a/actionpack/lib/action_view/helpers/translation_helper.rb
  33. +++ b/actionpack/lib/action_view/helpers/translation_helper.rb
  34. @@ -29,7 +29,7 @@ module ActionView
  35. private
  36. def scope_key_by_partial(key)
  37. if key.to_s.first == "."
  38. - template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s
  39. + _template.path_without_details.gsub(%r{/_?}, ".") + key.to_s
  40. else
  41. key
  42. end
  43. diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb
  44. index adaf654..0eef8a3 100644
  45. --- a/actionpack/lib/action_view/template.rb
  46. +++ b/actionpack/lib/action_view/template.rb
  47. @@ -37,6 +37,7 @@ module ActionView
  48.  
  49. def render(view, locals, &block)
  50. method_name = compile(locals, view)
  51. + view._template = self
  52. view.send(method_name, locals, &block)
  53. rescue Exception => e
  54. if e.is_a?(Template::Error)
  55. @@ -47,6 +48,10 @@ module ActionView
  56. end
  57. end
  58.  
  59. + def path_without_details
  60. + @details[:path]
  61. + end
  62. +
  63. # TODO: Figure out how to abstract this
  64. def variable_name
  65. @variable_name ||= identifier[%r'_?(\w+)(\.\w+)*$', 1].to_sym
  66. diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb
  67. index c6a1790..323d99e 100644
  68. --- a/actionpack/lib/action_view/template/resolver.rb
  69. +++ b/actionpack/lib/action_view/template/resolver.rb
  70. @@ -117,15 +117,17 @@ module ActionView
  71. # # :api: plugin
  72. def path_to_details(path)
  73. # [:erb, :format => :html, :locale => :en, :partial => true/false]
  74. - if m = path.match(%r'(?:^|/)(_)?[\w-]+((?:\.[\w-]+)*)\.(\w+)$')
  75. - partial = m[1] == '_'
  76. - details = (m[2]||"").split('.').reject { |e| e.empty? }
  77. - handler = Template.handler_class_for_extension(m[3])
  78. + if m = path.match(%r'^(.*/(_)?[\w-]+)((?:\.[\w-]+)*)\.(\w+)$')
  79. + partial = m[2] == '_'
  80. + details = (m[3]||"").split('.').reject { |e| e.empty? }
  81. + handler = Template.handler_class_for_extension(m[4])
  82.  
  83. format = Mime[details.last] && details.pop.to_sym
  84. locale = details.last && details.pop.to_sym
  85.  
  86. - return handler, :format => format, :locale => locale, :partial => partial
  87. + path = (m[1].gsub("#{@path}/", "") << details.join("."))
  88. +
  89. + return handler, :format => format, :locale => locale, :partial => partial, :path => path
  90. end
  91. end
  92. end
  93. diff --git a/actionpack/test/fixtures/test/translation.erb b/actionpack/test/fixtures/test/translation.erb
  94. new file mode 100644
  95. index 0000000..81a837d
  96. --- /dev/null
  97. +++ b/actionpack/test/fixtures/test/translation.erb
  98. @@ -0,0 +1 @@
  99. +<%= t('.helper') %>
  100. \ No newline at end of file
  101. diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb
  102. index d67d2c7..4b73c44 100644
  103. --- a/actionpack/test/template/translation_helper_test.rb
  104. +++ b/actionpack/test/template/translation_helper_test.rb
  105. @@ -1,9 +1,9 @@
  106. require 'abstract_unit'
  107.  
  108. -class TranslationHelperTest < Test::Unit::TestCase
  109. +class TranslationHelperTest < ActiveSupport::TestCase
  110. include ActionView::Helpers::TagHelper
  111. include ActionView::Helpers::TranslationHelper
  112. -
  113. +
  114. attr_reader :request
  115. def setup
  116. end
  117. @@ -25,8 +25,8 @@ class TranslationHelperTest < Test::Unit::TestCase
  118. end
  119.  
  120. def test_scoping_by_partial
  121. - expects(:template).returns(stub(:path_without_format_and_extension => "people/index"))
  122. - I18n.expects(:translate).with("people.index.foo", :locale => 'en', :raise => true).returns("")
  123. - translate ".foo", :locale => 'en'
  124. + I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper")
  125. + @view = ActionView::Base.new(ActionController::Base.view_paths, {})
  126. + assert_equal "helper", @view.render(:file => "test/translation")
  127. end
  128. end
  129. --
  130. 1.6.5
Add Comment
Please, Sign In to add comment