From cbfac6a339202bbe648cc20031fb7ff3fd29d832 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Sat, 7 Jun 2008 21:51:29 +0000 Subject: [PATCH] ActionItem265: starting with google maps --- app/helpers/search_helper.rb | 11 +++++++++++ app/models/google_maps.rb | 33 +++++++++++++++++++++++++++++++++ app/views/search/_display_results.rhtml | 4 ++++ app/views/search/_google_maps.rhtml | 23 +++++++++++++++++++++++ app/views/search/category_index.rhtml | 2 +- app/views/search/index.rhtml | 2 +- app/views/search/people.rhtml | 2 +- test/unit/google_maps_test.rb | 33 +++++++++++++++++++++++++++++++++ 8 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 app/models/google_maps.rb create mode 100644 app/views/search/_google_maps.rhtml create mode 100644 test/unit/google_maps_test.rb diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index 81182fb..8ee415b 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -14,4 +14,15 @@ module SearchHelper def remove_stop_words(query) (query.downcase.scan(/"[^"]*"?|'[^']*'?|[^'"\s]+/) - (STOP_WORDS[locale] || [])).join(' ') end + + def display_results + partial = + if params[:display] == 'map' + 'google_maps' + else + 'display_results' + end + render :partial => partial + end + end diff --git a/app/models/google_maps.rb b/app/models/google_maps.rb new file mode 100644 index 0000000..60e2a0a --- /dev/null +++ b/app/models/google_maps.rb @@ -0,0 +1,33 @@ +class GoogleMaps + + extend ActionView::Helpers::TagHelper + + class << self + + def config_file + File.join(RAILS_ROOT, 'config', 'web2.0.yml') + end + + def enabled? + File.exists?(config_file) + end + + def key + if enabled? + config = YAML.load_file(config_file) + if config.has_key?(:googlemaps) + config[:googlemaps][:key] + else + nil + end + else + nil + end + end + + def api_url + "http://maps.google.com/maps?file=api&v=2&key=#{key}" + end + + end +end diff --git a/app/views/search/_display_results.rhtml b/app/views/search/_display_results.rhtml index a9f8ea6..c75dbd8 100644 --- a/app/views/search/_display_results.rhtml +++ b/app/views/search/_display_results.rhtml @@ -1,3 +1,7 @@ +
+ <%= link_to _('Display in map'), :display => 'map' %> +
+
<% diff --git a/app/views/search/_google_maps.rhtml b/app/views/search/_google_maps.rhtml new file mode 100644 index 0000000..5ed2686 --- /dev/null +++ b/app/views/search/_google_maps.rhtml @@ -0,0 +1,23 @@ +
+ <%= link_to _('Display in list'), :display => 'list' %> +
+ +<%= content_tag('script', '', :src => GoogleMaps.api_url, :type => 'text/javascript') %> + + +
+
+
+ + diff --git a/app/views/search/category_index.rhtml b/app/views/search/category_index.rhtml index 8a201d8..8b688be 100644 --- a/app/views/search/category_index.rhtml +++ b/app/views/search/category_index.rhtml @@ -3,7 +3,7 @@
<%= image_tag(@category.image.public_filename(:thumb), :id => 'category-image') if @category.image %>

<%= _('Category: %s') % @category.name %>

- <%= render :partial => 'display_results' %> + <%= display_results %>

<%= _('Sub-categories') %>

diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml index f1f21fd..39689b5 100644 --- a/app/views/search/index.rhtml +++ b/app/views/search/index.rhtml @@ -4,7 +4,7 @@ <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> -<%= render :partial => 'display_results' %> +<%= display_results %>

diff --git a/app/views/search/people.rhtml b/app/views/search/people.rhtml index aaa97ff..7952fb5 100644 --- a/app/views/search/people.rhtml +++ b/app/views/search/people.rhtml @@ -11,6 +11,6 @@ <%= render :partial => 'search_form', :locals => { :form_title => @query.blank? ? _('Search') : _("Refine your search"), :simple_search => true } %> -<%= render :partial => 'display_results' %> +<%= display_results %>
diff --git a/test/unit/google_maps_test.rb b/test/unit/google_maps_test.rb new file mode 100644 index 0000000..3c68dbf --- /dev/null +++ b/test/unit/google_maps_test.rb @@ -0,0 +1,33 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class GoogleMapsTest < Test::Unit::TestCase + + CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'web2.0.yml') + + should 'retrieve key from "web2.0" config file' do + YAML.expects(:load_file).with(CONFIG_FILE).returns({:googlemaps => { :key => 'MYKEY' }}) + assert_equal 'MYKEY', GoogleMaps.key + end + + should 'disable if config file not present' do + File.expects(:exists?).with(CONFIG_FILE).returns(false) + assert !GoogleMaps.enabled? + end + + should 'not crash if config not informed' do + File.expects(:exists?).with(CONFIG_FILE).returns(true) + YAML.expects(:load_file).with(CONFIG_FILE).returns({}) + assert_nil GoogleMaps.key + end + + should 'not crash if config file not found' do + GoogleMaps.expects(:config_file).returns('/not/present.yml') + assert_nil GoogleMaps.key + end + + should 'point correctly to google maps' do + GoogleMaps.expects(:key).returns('MY_FUCKING_KEY') + assert_equal 'http://maps.google.com/maps?file=api&v=2&key=MY_FUCKING_KEY', GoogleMaps.api_url + end + +end -- libgit2 0.21.2