Commit 93ad499daea04427bdac9bfc190a60ec352a3ecc

Authored by Antonio Terceiro
1 parent d2b6e21e

ActionItem941: looking up country names

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