From 93ad499daea04427bdac9bfc190a60ec352a3ecc Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Fri, 20 Feb 2009 17:31:31 -0300 Subject: [PATCH] ActionItem941: looking up country names --- app/helpers/countries_helper.rb | 11 +++++++++++ app/helpers/profile_editor_helper.rb | 2 +- app/models/profile.rb | 6 +++++- app/views/blocks/profile_info.rhtml | 2 +- db/schema.rb | 10 +++++----- test/unit/countries_helper_test.rb | 12 +++++++++++- test/unit/profile_test.rb | 11 ++++++++++- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/app/helpers/countries_helper.rb b/app/helpers/countries_helper.rb index ea13bb2..56466a2 100644 --- a/app/helpers/countries_helper.rb +++ b/app/helpers/countries_helper.rb @@ -1,5 +1,7 @@ class CountriesHelper + include Singleton + include GetText bindtextdomain 'iso_3166' @@ -253,6 +255,11 @@ class CountriesHelper ["Zimbabwe", "ZW"] ] + COUNTRIES_HASH = COUNTRIES.inject({}) do |hash,entry| + hash[entry[1]] = entry[0] + hash + end + def self.countries COUNTRIES end @@ -261,4 +268,8 @@ class CountriesHelper self.class.countries.map {|item| [gettext(item[0]), item[1] ]}.sort_by { |entry| entry.first.transliterate } end + def lookup(code) + gettext(COUNTRIES_HASH[code]) + end + end diff --git a/app/helpers/profile_editor_helper.rb b/app/helpers/profile_editor_helper.rb index 2537da3..a2c0109 100644 --- a/app/helpers/profile_editor_helper.rb +++ b/app/helpers/profile_editor_helper.rb @@ -102,7 +102,7 @@ module ProfileEditorHelper end def country_helper - @country_helper ||= CountriesHelper.new + @country_helper ||= CountriesHelper.instance end def select_country(title, object, method, options) diff --git a/app/models/profile.rb b/app/models/profile.rb index dbc1141..ff5f2e8 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -140,10 +140,14 @@ class Profile < ActiveRecord::Base if myregion myregion.hierarchy.reverse.first(2).map(&:name).join(' - ') else - [ :city, :state, :country ].map {|item| self.respond_to?(item) ? self.send(item) : nil }.compact.join(' - ') + [ :city, :state, :country_name ].map {|item| self.respond_to?(item) ? self.send(item) : nil }.compact.join(' - ') end end + def country_name + CountriesHelper.instance.lookup(country) if respond_to?(:country) + end + def pending_categorizations @pending_categorizations ||= [] end diff --git a/app/views/blocks/profile_info.rhtml b/app/views/blocks/profile_info.rhtml index 8e4ac21..d2114d6 100644 --- a/app/views/blocks/profile_info.rhtml +++ b/app/views/blocks/profile_info.rhtml @@ -31,7 +31,7 @@ <%= [ [ profile.city, 'locality' ], [ profile.state, 'region' ], - [ profile.country, 'country-name' ] + [ profile.country_name, 'country-name' ] ].map{ |s,c| s =~ /^\s*$/ ? nil : content_tag( 'span', s, :class => c ) }.compact.join ' - ' %> diff --git a/db/schema.rb b/db/schema.rb index 5adbd04..e90ad0c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -83,8 +83,8 @@ ActiveRecord::Schema.define(:version => 62) do t.boolean "virtual", :default => false end - add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id" add_index "articles_categories", ["article_id"], :name => "index_articles_categories_on_article_id" + add_index "articles_categories", ["category_id"], :name => "index_articles_categories_on_category_id" create_table "blocks", :force => true do |t| t.string "title" @@ -124,8 +124,8 @@ ActiveRecord::Schema.define(:version => 62) do t.boolean "virtual", :default => false end - add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" + add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" create_table "comments", :force => true do |t| t.string "title" @@ -192,8 +192,8 @@ ActiveRecord::Schema.define(:version => 62) do t.datetime "updated_at" end - add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" + add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" create_table "products", :force => true do |t| t.integer "enterprise_id" @@ -260,7 +260,7 @@ ActiveRecord::Schema.define(:version => 62) do t.string "name" t.text "permissions" t.string "key" - t.boolean "system", :default => false + t.boolean "system", :default => false end create_table "taggings", :force => true do |t| @@ -270,8 +270,8 @@ ActiveRecord::Schema.define(:version => 62) do t.datetime "created_at" end - add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type" add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" + add_index "taggings", ["taggable_id", "taggable_type"], :name => "index_taggings_on_taggable_id_and_taggable_type" create_table "tags", :force => true do |t| t.string "name" diff --git a/test/unit/countries_helper_test.rb b/test/unit/countries_helper_test.rb index 6697f7b..63f723b 100644 --- a/test/unit/countries_helper_test.rb +++ b/test/unit/countries_helper_test.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper' class CountriesHelperTest < Test::Unit::TestCase def setup - @helper = CountriesHelper.new + @helper = CountriesHelper.instance end attr_reader :helper @@ -33,4 +33,14 @@ class CountriesHelperTest < Test::Unit::TestCase assert_equal [["Ă…land Islands", "AX"], ["Brazil", "BR"]], helper.countries end + should 'lookup country names by code' do + assert_equal 'France', helper.lookup('FR') + assert_equal 'Germany', helper.lookup('DE') + end + + should 'translate lookups' do + helper.expects(:gettext).with('Germany').returns('Alemanha') + assert_equal 'Alemanha', helper.lookup('DE') + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 2666c20..5250f90 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -715,10 +715,19 @@ class ProfileTest < Test::Unit::TestCase p.expects(:region).returns(nil) p.expects(:city).returns("Salvador") p.expects(:state).returns("Bahia") - p.expects(:country).returns("Brasil") + p.expects(:country_name).returns("Brasil") assert_equal 'Salvador - Bahia - Brasil', p.location end + should 'lookup country name' do + p = Profile.new + # two sample countries; trust the rest works + p.stubs(:country).returns('BR') + assert_equal 'Brazil', p.country_name + p.stubs(:country).returns('AR') + assert_equal 'Argentina', p.country_name + end + should 'give empty location if nothing is available' do p = Profile.new p.expects(:region).returns(nil) -- libgit2 0.21.2