Commit 4984c77639c4cdcc0bb23a39a2f6ee37d2912028
1 parent
799399d0
Exists in
master
and in
28 other branches
ActionItem265: polishing google maps configuration
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1929 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
54 additions
and
17 deletions
Show diff stats
app/helpers/search_helper.rb
@@ -16,20 +16,27 @@ module SearchHelper | @@ -16,20 +16,27 @@ module SearchHelper | ||
16 | end | 16 | end |
17 | 17 | ||
18 | def display_results | 18 | def display_results |
19 | + | ||
20 | + unless GoogleMaps.enabled? | ||
21 | + return render(:partial => 'display_results') | ||
22 | + end | ||
23 | + | ||
19 | data = | 24 | data = |
20 | if params[:display] == 'map' | 25 | if params[:display] == 'map' |
21 | { | 26 | { |
22 | :partial => 'google_maps', | 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 | else | 31 | else |
26 | { | 32 | { |
27 | :partial => 'display_results', | 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 | end | 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 | end | 40 | end |
34 | 41 | ||
35 | end | 42 | end |
app/models/google_maps.rb
@@ -4,25 +4,31 @@ class GoogleMaps | @@ -4,25 +4,31 @@ class GoogleMaps | ||
4 | 4 | ||
5 | class << self | 5 | class << self |
6 | 6 | ||
7 | + def erase_config | ||
8 | + @config = nil | ||
9 | + end | ||
10 | + | ||
7 | def config_file | 11 | def config_file |
8 | File.join(RAILS_ROOT, 'config', 'web2.0.yml') | 12 | File.join(RAILS_ROOT, 'config', 'web2.0.yml') |
9 | end | 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 | def enabled? | 26 | def enabled? |
12 | - File.exists?(config_file) | 27 | + !config['key'].nil? |
13 | end | 28 | end |
14 | 29 | ||
15 | def key | 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 | end | 32 | end |
27 | 33 | ||
28 | def api_url | 34 | def api_url |
app/views/search/_google_maps.rhtml
1 | <%= content_tag('script', '', :src => GoogleMaps.api_url, :type => 'text/javascript') %> | 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 | </div> | 6 | </div> |
7 | 7 | ||
8 | <script type='text/javascript'> | 8 | <script type='text/javascript'> |
public/stylesheets/search.css
test/unit/google_maps_test.rb
@@ -4,15 +4,31 @@ class GoogleMapsTest < Test::Unit::TestCase | @@ -4,15 +4,31 @@ class GoogleMapsTest < Test::Unit::TestCase | ||
4 | 4 | ||
5 | CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'web2.0.yml') | 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 | should 'retrieve key from "web2.0" config file' do | 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 | assert_equal 'MYKEY', GoogleMaps.key | 14 | assert_equal 'MYKEY', GoogleMaps.key |
10 | end | 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 | should 'disable if config file not present' do | 22 | should 'disable if config file not present' do |
13 | File.expects(:exists?).with(CONFIG_FILE).returns(false) | 23 | File.expects(:exists?).with(CONFIG_FILE).returns(false) |
14 | assert !GoogleMaps.enabled? | 24 | assert !GoogleMaps.enabled? |
15 | end | 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 | should 'not crash if config not informed' do | 33 | should 'not crash if config not informed' do |
18 | File.expects(:exists?).with(CONFIG_FILE).returns(true) | 34 | File.expects(:exists?).with(CONFIG_FILE).returns(true) |