Skip to content

Commit 960fc80

Browse files
author
Luca Guidi
committed
Extracted some code into proper helpers
1 parent 89018d8 commit 960fc80

8 files changed

+84
-65
lines changed

Diff for: app/helpers/click_to_globalize_helper.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module ClickToGlobalizeHelper
2+
@@click_partial = 'shared/_click_to_globalize'
3+
4+
# Render +app/views/shared/_click_to_globalize.html.erb+.
5+
def click_to_globalize
6+
render @@click_partial if controller.globalize?
7+
end
8+
9+
# Get form_authenticity_token if the application is protected from forgery.
10+
# See ActionController::RequestForgeryProtection for details.
11+
def authenticity_token
12+
protect_against_forgery? ? form_authenticity_token : ''
13+
end
14+
end

Diff for: app/helpers/locales_helper.rb

+27
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
module LocalesHelper
2+
# Return all available locales from I18n, except <tt>:root</tt>.
3+
def locales
4+
I18n.available_locales - [ :root ]
5+
end
6+
7+
# Creates the HTML markup for the languages picker menu.
8+
#
9+
# Example:
10+
# I18n.available_locales # => [:root, :en, :it]
11+
# I18n.locale # => :en
12+
#
13+
# <ul>
14+
# <li><a href="/locales/en/change" title="* en">* en</a></li> |
15+
# <li><a href="/locales/it/change" title="it">it</a></li>
16+
# </ul>
17+
def locales_menu
18+
returning html = "" do
19+
html << content_tag(:ul) do
20+
locales.map do |locale|
21+
title = locale == I18n.locale ? "* #{h(locale)}" : h(locale)
22+
content_tag(:li) do
23+
link_to title, change_locale_path(locale), :title => title, :method => :put
24+
end
25+
end * " | "
26+
end
27+
end
28+
end
229
end

Diff for: init.rb

-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77

88
ActionView::Base.class_eval do
99
include Click::Observer::View
10-
include Click::Helper
1110
end

Diff for: lib/click_to_globalize.rb

-39
Original file line numberDiff line numberDiff line change
@@ -93,43 +93,4 @@ def globalize?
9393
end
9494
end
9595
end
96-
97-
module Helper
98-
@@click_partial = 'shared/_click_to_globalize'
99-
100-
# Render +app/views/shared/_click_to_globalize.html.erb+.
101-
def click_to_globalize
102-
render @@click_partial if controller.globalize?
103-
end
104-
105-
# Get form_authenticity_token if the application is protected from forgery.
106-
# See ActionController::RequestForgeryProtection for details.
107-
def authenticity_token
108-
protect_against_forgery? ? form_authenticity_token : ''
109-
end
110-
111-
def locales
112-
I18n.available_locales
113-
end
114-
115-
# Creates the HTML markup for the languages picker menu.
116-
#
117-
# Example:
118-
# I18n.available_locales # => [:en, :it]
119-
# I18n.locale # => :en
120-
#
121-
# <ul>
122-
# <li><a href="/locales/set/en" title="* en">* en</a></li> |
123-
# <li><a href="/locales/set/it" title="it">it</a></li>
124-
# </ul>
125-
def languages_menu
126-
returning result = '<ul>' do
127-
result << locales.map do |locale|
128-
locale = "* #{locale}" if locale == I18n.locale
129-
"<li>#{link_to locale, {:controller => 'locales', :action => 'set', :id => locale}, {:title => "#{language} [#{locale}]"}}</li>"
130-
end * ' | '
131-
end
132-
result << '</ul>'
133-
end
134-
end
13596
end

Diff for: test/test_helper.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,17 @@ def load_translations
1212
def locale_observer
1313
@locale_observer ||= Click::Observer::LocaleObserver.new
1414
end
15-
end
15+
end
16+
17+
class ActionView::TestCase
18+
private
19+
# HACK this is a placehoder, dunno why it isn't included by default
20+
def protect_against_forgery?
21+
false
22+
end
23+
24+
# HACK this is a placehoder, dunno why it isn't included by default
25+
def form_authenticity_token
26+
"hack"
27+
end
28+
end

Diff for: test/unit/helpers/click_to_globalize_helper_test.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2+
3+
class ClickToGlobalizeHelperTest < ActionView::TestCase
4+
test "authenticity_token with protection from forgery active" do
5+
stubs(:protect_against_forgery?).returns true
6+
assert_equal form_authenticity_token, authenticity_token
7+
end
8+
9+
test "authenticity_token with protection from forgery inactive" do
10+
assert_equal "", authenticity_token
11+
end
12+
end

Diff for: test/unit/helpers/helper_test.rb

-24
This file was deleted.

Diff for: test/unit/helpers/locales_helper_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2+
3+
class LocalesHelperTest < ActionView::TestCase
4+
test "locales" do
5+
assert_equal expected_locales, locales
6+
end
7+
8+
test "locales_menu" do
9+
expected = %(<ul><li><a href=\"/locales/en/change\" title=\"en\" onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'put'); f.appendChild(m);f.submit();return false;\">en</a></li></ul>)
10+
assert_dom_equal expected, locales_menu
11+
end
12+
13+
private
14+
def expected_locales
15+
I18n.available_locales - [ :root ]
16+
end
17+
end

0 commit comments

Comments
 (0)