Commit 4984c77639c4cdcc0bb23a39a2f6ee37d2912028

Authored by AntonioTerceiro
1 parent 799399d0

ActionItem265: polishing google maps configuration


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1929 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/helpers/search_helper.rb
... ... @@ -16,20 +16,27 @@ module SearchHelper
16 16 end
17 17  
18 18 def display_results
  19 +
  20 + unless GoogleMaps.enabled?
  21 + return render(:partial => 'display_results')
  22 + end
  23 +
19 24 data =
20 25 if params[:display] == 'map'
21 26 {
22 27 :partial => 'google_maps',
23   - :toggle => link_to(_('Display in list'), params.merge(:display => 'list'))
  28 + :toggle => button(:search, _('Display in list'), params.merge(:display => 'list'), :class => "map-toggle-button" ),
  29 + :class => 'map' ,
24 30 }
25 31 else
26 32 {
27 33 :partial => 'display_results',
28   - :toggle => link_to(_('Display in map'), params.merge(:display => 'map'))
  34 + :toggle => button(:search, _('Display in map'), params.merge(:display => 'map'), :class => "map-toggle-button" ),
  35 + :class => 'list' ,
29 36 }
30 37 end
31 38  
32   - data[:toggle] + (render :partial => data[:partial])
  39 + content_tag('div', data[:toggle] + (render :partial => data[:partial]), :class => "map-or-list-search-results #{data[:class]}")
33 40 end
34 41  
35 42 end
... ...
app/models/google_maps.rb
... ... @@ -4,25 +4,31 @@ class GoogleMaps
4 4  
5 5 class << self
6 6  
  7 + def erase_config
  8 + @config = nil
  9 + end
  10 +
7 11 def config_file
8 12 File.join(RAILS_ROOT, 'config', 'web2.0.yml')
9 13 end
10 14  
  15 + def config
  16 + if @config.nil?
  17 + if File.exists?(config_file)
  18 + yaml = YAML.load_file(config_file)
  19 + @config = yaml['googlemaps']
  20 + end
  21 + end
  22 +
  23 + @config ||= {}
  24 + end
  25 +
11 26 def enabled?
12   - File.exists?(config_file)
  27 + !config['key'].nil?
13 28 end
14 29  
15 30 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
  31 + config['key']
26 32 end
27 33  
28 34 def api_url
... ...
app/views/search/_google_maps.rhtml
1 1 <%= content_tag('script', '', :src => GoogleMaps.api_url, :type => 'text/javascript') %>
2 2  
3 3  
4   -<div style='text-align: center; margin: 2em;'>
5   - <div id="map" style="margin: auto; width: 100%; height: 500px"></div>
  4 +<div style='text-align: center;'>
  5 + <div id="map"></div>
6 6 </div>
7 7  
8 8 <script type='text/javascript'>
... ...
public/stylesheets/search.css
... ... @@ -76,3 +76,11 @@
76 76 margin-bottom: -20px;
77 77 }
78 78  
  79 +.map-or-list-search-results {
  80 + margin-left: 2em;
  81 + margin-right: 2em;
  82 +}
  83 +
  84 +#map {
  85 + height: 500px;
  86 +}
... ...
test/unit/google_maps_test.rb
... ... @@ -4,15 +4,31 @@ class GoogleMapsTest &lt; Test::Unit::TestCase
4 4  
5 5 CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'web2.0.yml')
6 6  
  7 + def setup
  8 + # force loading of config at every test
  9 + GoogleMaps.erase_config
  10 + end
  11 +
7 12 should 'retrieve key from "web2.0" config file' do
8   - YAML.expects(:load_file).with(CONFIG_FILE).returns({:googlemaps => { :key => 'MYKEY' }})
  13 + YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'key' => 'MYKEY' }})
9 14 assert_equal 'MYKEY', GoogleMaps.key
10 15 end
11 16  
  17 + should 'enable when key is defined' do
  18 + YAML.expects(:load_file).with(CONFIG_FILE).returns({'googlemaps' => { 'key' => 'MYKEY' }})
  19 + assert GoogleMaps.enabled?
  20 + end
  21 +
12 22 should 'disable if config file not present' do
13 23 File.expects(:exists?).with(CONFIG_FILE).returns(false)
14 24 assert !GoogleMaps.enabled?
15 25 end
  26 +
  27 + should 'disable if key not defined' do
  28 + File.expects(:exists?).with(CONFIG_FILE).returns(true)
  29 + YAML.expects(:load_file).with(CONFIG_FILE).returns({})
  30 + assert !GoogleMaps.enabled?
  31 + end
16 32  
17 33 should 'not crash if config not informed' do
18 34 File.expects(:exists?).with(CONFIG_FILE).returns(true)
... ...