Commit 93ad499daea04427bdac9bfc190a60ec352a3ecc

Authored by Antonio Terceiro
1 parent d2b6e21e

ActionItem941: looking up country names

app/helpers/countries_helper.rb
1 class CountriesHelper 1 class CountriesHelper
2 2
  3 + include Singleton
  4 +
3 include GetText 5 include GetText
4 bindtextdomain 'iso_3166' 6 bindtextdomain 'iso_3166'
5 7
@@ -253,6 +255,11 @@ class CountriesHelper @@ -253,6 +255,11 @@ class CountriesHelper
253 ["Zimbabwe", "ZW"] 255 ["Zimbabwe", "ZW"]
254 ] 256 ]
255 257
  258 + COUNTRIES_HASH = COUNTRIES.inject({}) do |hash,entry|
  259 + hash[entry[1]] = entry[0]
  260 + hash
  261 + end
  262 +
256 def self.countries 263 def self.countries
257 COUNTRIES 264 COUNTRIES
258 end 265 end
@@ -261,4 +268,8 @@ class CountriesHelper @@ -261,4 +268,8 @@ class CountriesHelper
261 self.class.countries.map {|item| [gettext(item[0]), item[1] ]}.sort_by { |entry| entry.first.transliterate } 268 self.class.countries.map {|item| [gettext(item[0]), item[1] ]}.sort_by { |entry| entry.first.transliterate }
262 end 269 end
263 270
  271 + def lookup(code)
  272 + gettext(COUNTRIES_HASH[code])
  273 + end
  274 +
264 end 275 end
app/helpers/profile_editor_helper.rb
@@ -102,7 +102,7 @@ module ProfileEditorHelper @@ -102,7 +102,7 @@ module ProfileEditorHelper
102 end 102 end
103 103
104 def country_helper 104 def country_helper
105 - @country_helper ||= CountriesHelper.new 105 + @country_helper ||= CountriesHelper.instance
106 end 106 end
107 107
108 def select_country(title, object, method, options) 108 def select_country(title, object, method, options)
app/models/profile.rb
@@ -140,10 +140,14 @@ class Profile < ActiveRecord::Base @@ -140,10 +140,14 @@ class Profile < ActiveRecord::Base
140 if myregion 140 if myregion
141 myregion.hierarchy.reverse.first(2).map(&:name).join(' - ') 141 myregion.hierarchy.reverse.first(2).map(&:name).join(' - ')
142 else 142 else
143 - [ :city, :state, :country ].map {|item| self.respond_to?(item) ? self.send(item) : nil }.compact.join(' - ') 143 + [ :city, :state, :country_name ].map {|item| self.respond_to?(item) ? self.send(item) : nil }.compact.join(' - ')
144 end 144 end
145 end 145 end
146 146
  147 + def country_name
  148 + CountriesHelper.instance.lookup(country) if respond_to?(:country)
  149 + end
  150 +
147 def pending_categorizations 151 def pending_categorizations
148 @pending_categorizations ||= [] 152 @pending_categorizations ||= []
149 end 153 end
app/views/blocks/profile_info.rhtml
@@ -31,7 +31,7 @@ @@ -31,7 +31,7 @@
31 <%= 31 <%=
32 [ [ profile.city, 'locality' ], 32 [ [ profile.city, 'locality' ],
33 [ profile.state, 'region' ], 33 [ profile.state, 'region' ],
34 - [ profile.country, 'country-name' ] 34 + [ profile.country_name, 'country-name' ]
35 ].map{ |s,c| s =~ /^\s*$/ ? nil : content_tag( 'span', s, :class => c ) }.compact.join ' - ' 35 ].map{ |s,c| s =~ /^\s*$/ ? nil : content_tag( 'span', s, :class => c ) }.compact.join ' - '
36 %> 36 %>
37 </div> 37 </div>
@@ -83,8 +83,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do @@ -83,8 +83,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do
83 t.boolean "virtual", :default => false 83 t.boolean "virtual", :default => false
84 end 84 end
85 85
86 - add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id"  
87 add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" 86 add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id"
  87 + add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id"
88 88
89 create_table "blocks", :force => true do |t| 89 create_table "blocks", :force => true do |t|
90 t.string "title" 90 t.string "title"
@@ -124,8 +124,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do @@ -124,8 +124,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do
124 t.boolean "virtual", :default => false 124 t.boolean "virtual", :default => false
125 end 125 end
126 126
127 - add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id"  
128 add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" 127 add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id"
  128 + add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id"
129 129
130 create_table "comments", :force => true do |t| 130 create_table "comments", :force => true do |t|
131 t.string "title" 131 t.string "title"
@@ -192,8 +192,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do @@ -192,8 +192,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do
192 t.datetime "updated_at" 192 t.datetime "updated_at"
193 end 193 end
194 194
195 - add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id"  
196 add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" 195 add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id"
  196 + add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id"
197 197
198 create_table "products", :force => true do |t| 198 create_table "products", :force => true do |t|
199 t.integer "enterprise_id" 199 t.integer "enterprise_id"
@@ -260,7 +260,7 @@ ActiveRecord::Schema.define(:version =&gt; 62) do @@ -260,7 +260,7 @@ ActiveRecord::Schema.define(:version =&gt; 62) do
260 t.string "name" 260 t.string "name"
261 t.text "permissions" 261 t.text "permissions"
262 t.string "key" 262 t.string "key"
263 - t.boolean "system", :default => false 263 + t.boolean "system", :default => false
264 end 264 end
265 265
266 create_table "taggings", :force => true do |t| 266 create_table "taggings", :force => true do |t|
@@ -270,8 +270,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do @@ -270,8 +270,8 @@ ActiveRecord::Schema.define(:version =&gt; 62) do
270 t.datetime "created_at" 270 t.datetime "created_at"
271 end 271 end
272 272
273 - add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"  
274 add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" 273 add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
  274 + add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type"
275 275
276 create_table "tags", :force => true do |t| 276 create_table "tags", :force => true do |t|
277 t.string "name" 277 t.string "name"
test/unit/countries_helper_test.rb
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39; @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39;
3 class CountriesHelperTest < Test::Unit::TestCase 3 class CountriesHelperTest < Test::Unit::TestCase
4 4
5 def setup 5 def setup
6 - @helper = CountriesHelper.new 6 + @helper = CountriesHelper.instance
7 end 7 end
8 attr_reader :helper 8 attr_reader :helper
9 9
@@ -33,4 +33,14 @@ class CountriesHelperTest &lt; Test::Unit::TestCase @@ -33,4 +33,14 @@ class CountriesHelperTest &lt; Test::Unit::TestCase
33 assert_equal [["Åland Islands", "AX"], ["Brazil", "BR"]], helper.countries 33 assert_equal [["Åland Islands", "AX"], ["Brazil", "BR"]], helper.countries
34 end 34 end
35 35
  36 + should 'lookup country names by code' do
  37 + assert_equal 'France', helper.lookup('FR')
  38 + assert_equal 'Germany', helper.lookup('DE')
  39 + end
  40 +
  41 + should 'translate lookups' do
  42 + helper.expects(:gettext).with('Germany').returns('Alemanha')
  43 + assert_equal 'Alemanha', helper.lookup('DE')
  44 + end
  45 +
36 end 46 end
test/unit/profile_test.rb
@@ -715,10 +715,19 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -715,10 +715,19 @@ class ProfileTest &lt; Test::Unit::TestCase
715 p.expects(:region).returns(nil) 715 p.expects(:region).returns(nil)
716 p.expects(:city).returns("Salvador") 716 p.expects(:city).returns("Salvador")
717 p.expects(:state).returns("Bahia") 717 p.expects(:state).returns("Bahia")
718 - p.expects(:country).returns("Brasil") 718 + p.expects(:country_name).returns("Brasil")
719 assert_equal 'Salvador - Bahia - Brasil', p.location 719 assert_equal 'Salvador - Bahia - Brasil', p.location
720 end 720 end
721 721
  722 + should 'lookup country name' do
  723 + p = Profile.new
  724 + # two sample countries; trust the rest works
  725 + p.stubs(:country).returns('BR')
  726 + assert_equal 'Brazil', p.country_name
  727 + p.stubs(:country).returns('AR')
  728 + assert_equal 'Argentina', p.country_name
  729 + end
  730 +
722 should 'give empty location if nothing is available' do 731 should 'give empty location if nothing is available' do
723 p = Profile.new 732 p = Profile.new
724 p.expects(:region).returns(nil) 733 p.expects(:region).returns(nil)