Skip to content

Commit

Permalink
Extracted formatting logic in #format_translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Guidi committed Jul 17, 2008
1 parent 049a810 commit 5bd9bc3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
26 changes: 18 additions & 8 deletions lib/click_to_globalize.rb
Expand Up @@ -107,7 +107,7 @@ def formatting
# * textilize_without_paragraph (textile)
# * markdown (markdown)
def formatting_method
case @@formatting
@@formatting_method ||= case @@formatting
when :textile then :textilize_without_paragraph
when :markdown then :markdown
end
Expand Down Expand Up @@ -205,14 +205,24 @@ def globalize?
def observe_locales
return unless globalize?
locale_observer = LocaleObserver.new
Globalize::Locale.add_observer(locale_observer)
Locale.add_observer(locale_observer)
yield
Globalize::Locale.remove_observer(locale_observer)
session[:__globalize_translations] = if Locale.formatting
locale_observer.translations.each{|key, translation| locale_observer.translations[key] = strip_tags(self.send(Locale.formatting_method, translation)) }
else
locale_observer.translations
end
Locale.remove_observer(locale_observer)
session[:__globalize_translations] = format_translations(locale_observer)
end

# Fetch the translations from the given LocaleObserver.
# If the formatting feature is turned on it also provide to textilize or markdown.
def format_translations(locale_observer)
if Locale.formatting
formatting_method = Locale.formatting_method
locale_observer.translations.inject({}) do |result, translation|
result[translation.first] = self.send(formatting_method, strip_tags(translation.last))
result
end
else
locale_observer.translations
end
end
end
end
Expand Down
41 changes: 37 additions & 4 deletions test/click_to_globalize_test.rb
Expand Up @@ -6,8 +6,8 @@ class ClickToGlobalizeController < ApplicationController
around_filter :observe_locales
def index
Locale.set(params[:locale])
hello_world = Translation.find_by_tr_key_and_language_id(params[:key], params[:language_id])
@greet = hello_world.tr_key.t
translation = Translation.find_by_tr_key_and_language_id(params[:key], params[:language_id])
@greet = translation.tr_key.t
render :nothing => true, :status => 200
end
end
Expand Down Expand Up @@ -222,6 +222,31 @@ def test_controller_observe_locales
assert_equal expected, @request.session[:__globalize_translations]
end

def test_should_return_formatted_translations
create_translation('hello_mars', '*Hello Mars!*')
with_formatting :textile do
get :index, params(:key => 'hello_mars')
assert_response :success

expected = { 'hello_mars' => %(<strong>Hello Mars!</strong>) }
assert_equal expected, @request.session[:__globalize_translations]
end
end

uses_mocha 'ClickToGlobalizeFormattingTest' do
def test_should_return_plain_translations
Locale.stubs(:formatting).returns nil
create_translation('hello_moon', '*Hello Moon!*')
with_formatting :unexistent do
get :index, params(:key => 'hello_moon')
assert_response :success

expected = { 'hello_moon' => '*Hello Moon!*' }
assert_equal expected, @request.session[:__globalize_translations]
end
end
end

### LOCALE_CONTROLLER

def test_check_globalize
Expand Down Expand Up @@ -252,7 +277,15 @@ def locale_observer
@locale_observer ||= LocaleObserver.new
end

def params
{ :key => @hello_world.tr_key, :language_id => 1, :locale => @default_locale.code }
def params(options = {})
{ :key => @hello_world.tr_key, :language_id => 1, :locale => @default_locale.code }.merge!(options)
end

def create_translation(key, text)
translation = Translation.new(:tr_key => key, :text => text,
:language_id => 1, :pluralization_index => 1) do |t|
t.type = 'ViewTranslation'
end
translation.save
end
end
9 changes: 9 additions & 0 deletions test/test_helper.rb
Expand Up @@ -52,6 +52,15 @@ def assert_any(collection, message = nil)
end
end

# Thanks to Rails Core Team
def uses_mocha(description)
require 'rubygems'
require 'mocha'
yield
rescue LoadError
$stderr.puts "Skipping #{description} tests. `gem install mocha` and try again."
end

LocalesController.class_eval do #:nodoc:
public :clear_cache, :inline
end
Expand Down

0 comments on commit 5bd9bc3

Please sign in to comment.