Skip to content

Commit

Permalink
Fix t('.helper').
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 26, 2010
1 parent 9f63c4b commit dc57d54
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
6 changes: 4 additions & 2 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -276,9 +276,11 @@ def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil,
@config = nil
@formats = formats
@assigns = assigns_for_first_render.each { |key, value| instance_variable_set("@#{key}", value) }
@_controller = controller
@helpers = self.class.helpers || Module.new
@_content_for = Hash.new {|h,k| h[k] = ActionView::SafeBuffer.new }

@_controller = controller
@_content_for = Hash.new {|h,k| h[k] = ActionView::SafeBuffer.new }
@_virtual_path = nil
self.view_paths = view_paths
end

Expand Down
8 changes: 6 additions & 2 deletions actionpack/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -25,11 +25,15 @@ def localize(*args)
end
alias :l :localize


private

def scope_key_by_partial(key)
if key.to_s.first == "."
template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s
if @_virtual_path
@_virtual_path.gsub(%r{/_?}, ".") + key.to_s
else
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
end
else
key
end
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/template.rb
Expand Up @@ -87,9 +87,9 @@ def compile(locals, view)

source = <<-end_src
def #{method_name}(local_assigns)
old_output_buffer = output_buffer;#{locals_code};#{code}
_old_virtual_path, @_virtual_path = @_virtual_path, #{@details[:virtual_path].inspect};_old_output_buffer = output_buffer;#{locals_code};#{code}
ensure
self.output_buffer = old_output_buffer
@_virtual_path, self.output_buffer = _old_virtual_path, _old_output_buffer
end
end_src

Expand Down
13 changes: 8 additions & 5 deletions actionpack/lib/action_view/template/resolver.rb
Expand Up @@ -117,15 +117,18 @@ def query(path, exts)
# # :api: plugin
def path_to_details(path)
# [:erb, :format => :html, :locale => :en, :partial => true/false]
if m = path.match(%r'(?:^|/)(_)?[\w-]+((?:\.[\w-]+)*)\.(\w+)$')
partial = m[1] == '_'
details = (m[2]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[3])
if m = path.match(%r'((^|.*/)(_)?[\w-]+)((?:\.[\w-]+)*)\.(\w+)$')
partial = m[3] == '_'
details = (m[4]||"").split('.').reject { |e| e.empty? }
handler = Template.handler_class_for_extension(m[5])

format = Mime[details.last] && details.pop.to_sym
locale = details.last && details.pop.to_sym

return handler, :format => format, :locale => locale, :partial => partial
virtual_path = (m[1].gsub("#{@path}/", "") << details.join("."))

return handler, :format => format, :locale => locale, :partial => partial,
:virtual_path => virtual_path
end
end
end
Expand Down
21 changes: 11 additions & 10 deletions actionpack/test/controller/helper_test.rb
Expand Up @@ -135,16 +135,17 @@ def test_helper_proxy
assert methods.include?('foobar')
end

def test_deprecation
assert_deprecated do
ActionController::Base.helpers_dir = "some/foo/bar"
end
assert_deprecated do
assert_equal ["some/foo/bar"], ActionController::Base.helpers_dir
end
ensure
ActionController::Base.helpers_path = [File.dirname(__FILE__) + '/../fixtures/helpers']
end
# TODO Add this deprecation back before Rails 3.0 final release
# def test_deprecation
# assert_deprecated do
# ActionController::Base.helpers_dir = "some/foo/bar"
# end
# assert_deprecated do
# assert_equal ["some/foo/bar"], ActionController::Base.helpers_dir
# end
# ensure
# ActionController::Base.helpers_path = [File.dirname(__FILE__) + '/../fixtures/helpers']
# end

private
def expected_helper_methods
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/fixtures/test/translation.erb
@@ -0,0 +1 @@
<%= t('.helper') %>
10 changes: 5 additions & 5 deletions actionpack/test/template/translation_helper_test.rb
@@ -1,9 +1,9 @@
require 'abstract_unit'

class TranslationHelperTest < Test::Unit::TestCase
class TranslationHelperTest < ActiveSupport::TestCase
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TranslationHelper

attr_reader :request
def setup
end
Expand All @@ -25,8 +25,8 @@ def test_delegates_localize_to_i18n
end

def test_scoping_by_partial
expects(:template).returns(stub(:path_without_format_and_extension => "people/index"))
I18n.expects(:translate).with("people.index.foo", :locale => 'en', :raise => true).returns("")
translate ".foo", :locale => 'en'
I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper")
@view = ActionView::Base.new(ActionController::Base.view_paths, {})
assert_equal "helper", @view.render(:file => "test/translation")
end
end

0 comments on commit dc57d54

Please sign in to comment.