Commit cbfac6a339202bbe648cc20031fb7ff3fd29d832

Authored by AntonioTerceiro
1 parent dc6ef1ff

ActionItem265: starting with google maps


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1926 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/helpers/search_helper.rb
... ... @@ -14,4 +14,15 @@ module SearchHelper
14 14 def remove_stop_words(query)
15 15 (query.downcase.scan(/"[^"]*"?|'[^']*'?|[^'"\s]+/) - (STOP_WORDS[locale] || [])).join(' ')
16 16 end
  17 +
  18 + def display_results
  19 + partial =
  20 + if params[:display] == 'map'
  21 + 'google_maps'
  22 + else
  23 + 'display_results'
  24 + end
  25 + render :partial => partial
  26 + end
  27 +
17 28 end
... ...
app/models/google_maps.rb 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +class GoogleMaps
  2 +
  3 + extend ActionView::Helpers::TagHelper
  4 +
  5 + class << self
  6 +
  7 + def config_file
  8 + File.join(RAILS_ROOT, 'config', 'web2.0.yml')
  9 + end
  10 +
  11 + def enabled?
  12 + File.exists?(config_file)
  13 + end
  14 +
  15 + def key
  16 + if enabled?
  17 + config = YAML.load_file(config_file)
  18 + if config.has_key?(:googlemaps)
  19 + config[:googlemaps][:key]
  20 + else
  21 + nil
  22 + end
  23 + else
  24 + nil
  25 + end
  26 + end
  27 +
  28 + def api_url
  29 + "http://maps.google.com/maps?file=api&amp;v=2&amp;key=#{key}"
  30 + end
  31 +
  32 + end
  33 +end
... ...
app/views/search/_display_results.rhtml
  1 +<div style='float: left;'>
  2 + <%= link_to _('Display in map'), :display => 'map' %>
  3 +</div>
  4 +
1 5 <div id="search-results" class="<%= 'only-one-result-box' if @results.size == 1 %>">
2 6  
3 7 <%
... ...
app/views/search/_google_maps.rhtml 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +<div style='float: left;'>
  2 + <%= link_to _('Display in list'), :display => 'list' %>
  3 +</div>
  4 +
  5 +<%= content_tag('script', '', :src => GoogleMaps.api_url, :type => 'text/javascript') %>
  6 +
  7 +
  8 +<div style='text-align: center; margin: 2em;'>
  9 + <div id="map" style="margin: auto; width: 100%; height: 500px"></div>
  10 +</div>
  11 +
  12 +<script type='text/javascript'>
  13 +if (GBrowserIsCompatible()) {
  14 + var map = new GMap2(document.getElementById("map"));
  15 + map.setCenter(new GLatLng(-15.0, -50.1419), 4);
  16 +
  17 + point = new GLatLng(-15, -40);
  18 + marker = new GMarker(point);
  19 + map.addOverlay(marker);
  20 + GEvent.addListener(marker, 'click', function() { marker.openInfoWindow("teste") });
  21 +
  22 +}
  23 +</script>
... ...
app/views/search/category_index.rhtml
... ... @@ -3,7 +3,7 @@
3 3 <div id="category-image"><%= image_tag(@category.image.public_filename(:thumb), :id => 'category-image') if @category.image %></div>
4 4 <h1 id="category-name"><%= _('Category: %s') % @category.name %></h1>
5 5  
6   - <%= render :partial => 'display_results' %>
  6 + <%= display_results %>
7 7  
8 8 <div id="category-childs">
9 9 <h2> <%= _('Sub-categories') %> </h2>
... ...
app/views/search/index.rhtml
... ... @@ -4,7 +4,7 @@
4 4  
5 5 <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %>
6 6  
7   -<%= render :partial => 'display_results' %>
  7 +<%= display_results %>
8 8  
9 9 </div><!-- end id="search-page" -->
10 10 <br style="clear:both" />
... ...
app/views/search/people.rhtml
... ... @@ -11,6 +11,6 @@
11 11  
12 12 <%= render :partial => 'search_form', :locals => { :form_title => @query.blank? ? _('Search') : _("Refine your search"), :simple_search => true } %>
13 13  
14   -<%= render :partial => 'display_results' %>
  14 +<%= display_results %>
15 15  
16 16 <br style="clear:both" />
... ...
test/unit/google_maps_test.rb 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class GoogleMapsTest < Test::Unit::TestCase
  4 +
  5 + CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'web2.0.yml')
  6 +
  7 + should 'retrieve key from "web2.0" config file' do
  8 + YAML.expects(:load_file).with(CONFIG_FILE).returns({:googlemaps => { :key => 'MYKEY' }})
  9 + assert_equal 'MYKEY', GoogleMaps.key
  10 + end
  11 +
  12 + should 'disable if config file not present' do
  13 + File.expects(:exists?).with(CONFIG_FILE).returns(false)
  14 + assert !GoogleMaps.enabled?
  15 + end
  16 +
  17 + should 'not crash if config not informed' do
  18 + File.expects(:exists?).with(CONFIG_FILE).returns(true)
  19 + YAML.expects(:load_file).with(CONFIG_FILE).returns({})
  20 + assert_nil GoogleMaps.key
  21 + end
  22 +
  23 + should 'not crash if config file not found' do
  24 + GoogleMaps.expects(:config_file).returns('/not/present.yml')
  25 + assert_nil GoogleMaps.key
  26 + end
  27 +
  28 + should 'point correctly to google maps' do
  29 + GoogleMaps.expects(:key).returns('MY_FUCKING_KEY')
  30 + assert_equal 'http://maps.google.com/maps?file=api&amp;v=2&amp;key=MY_FUCKING_KEY', GoogleMaps.api_url
  31 + end
  32 +
  33 +end
... ...