From 0b913efd0583570c5a9cd900189fc88085218d61 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sat, 27 Feb 2010 12:41:56 +0000 Subject: [PATCH] Use Nominatim to reverse geocode diary location information and display a friendly human readable location instead of some coordinates. --- app/helpers/application_helper.rb | 11 +++++++++++ app/models/diary_sweeper.rb | 4 ++++ app/views/diary_entry/_diary_entry.html.erb | 3 ++- app/views/diary_entry/_location.html.erb | 11 +++++++++++ config/locales/en.yml | 8 ++++---- public/stylesheets/common.css | 4 ++++ test/functional/diary_entry_controller_test.rb | 8 ++++---- 7 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 app/views/diary_entry/_location.html.erb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d805e27ad..e466e8349 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,6 @@ module ApplicationHelper + require 'rexml/document' + def htmlize(text) return linkify(sanitize(simple_format(text))) end @@ -34,6 +36,15 @@ module ApplicationHelper return js end + def describe_location(lat, lon, zoom, language) + zoom = zoom || 14 + language = language || request.user_preferred_languages.join(',') + url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}" + response = REXML::Document.new(Net::HTTP.get(URI.parse(url))) + + return response.get_text("reversegeocode/result").to_s + end + private def javascript_strings_for_key(key) diff --git a/app/models/diary_sweeper.rb b/app/models/diary_sweeper.rb index c003cc0f5..59d578f7e 100644 --- a/app/models/diary_sweeper.rb +++ b/app/models/diary_sweeper.rb @@ -30,5 +30,9 @@ private expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => nil) expire_action(:controller => 'diary_entry', :action => 'rss', :language => entry.language_code, :display_name => nil) expire_action(:controller => 'diary_entry', :action => 'rss', :language => nil, :display_name => entry.user.display_name) + + if record.is_a?(DiaryEntry) + expire_fragment(:controller => 'diary_entry', :action => 'view', :display_name => entry.user.display_name, :id => entry.id, :part => "location") + end end end diff --git a/app/views/diary_entry/_diary_entry.html.erb b/app/views/diary_entry/_diary_entry.html.erb index 0f4f6c206..c9a6baec0 100644 --- a/app/views/diary_entry/_diary_entry.html.erb +++ b/app/views/diary_entry/_diary_entry.html.erb @@ -1,7 +1,8 @@ <%= link_to h(diary_entry.title), :action => 'view', :display_name => diary_entry.user.display_name, :id => diary_entry.id %>
<%= htmlize(diary_entry.body) %> <% if diary_entry.latitude and diary_entry.longitude %> -<%= t 'map.coordinates' %>
<%= diary_entry.latitude %>; <%= diary_entry.longitude %>
(<%=link_to (t 'map.view'), :controller => 'site', :action => 'index', :lat => diary_entry.latitude, :lon => diary_entry.longitude, :zoom => 14 %> / <%=link_to (t 'map.edit'), :controller => 'site', :action => 'edit', :lat => diary_entry.latitude, :lon => diary_entry.longitude, :zoom => 14 %>)
+<%= render :partial => "location", :object => diary_entry %> +
<% end %> <%= t 'diary_entry.diary_entry.posted_by', :link_user => (link_to h(diary_entry.user.display_name), :controller => 'user', :action => 'view', :display_name => diary_entry.user.display_name), :created => l(diary_entry.created_at), :language_link => (link_to h(diary_entry.language.name), :controller => 'diary_entry', :action => 'list', :language => diary_entry.language_code) %> <% if params[:action] == 'list' %> diff --git a/app/views/diary_entry/_location.html.erb b/app/views/diary_entry/_location.html.erb new file mode 100644 index 000000000..ec8ea462a --- /dev/null +++ b/app/views/diary_entry/_location.html.erb @@ -0,0 +1,11 @@ +<% cache(:controller => 'diary_entry', :action => 'view', :display_name => location.user.display_name, :id => location.id, :part => "location") do %> +<%= t 'diary_entry.location.location' %> + + +<%= describe_location location.latitude, location.longitude, 14, location.language_code %> + + +(<%=link_to t('diary_entry.location.view'), :controller => 'site', :action => 'index', :lat => location.latitude, :lon => location.longitude, :zoom => 14 %> +/ +<%=link_to t('diary_entry.location.edit'), :controller => 'site', :action => 'edit', :lat => location.latitude, :lon => location.longitude, :zoom => 14 %>) +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index e75cc2206..47847680f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -76,10 +76,6 @@ en: with_id: "{{id}}" with_version: "{{id}}, v{{version}}" with_name: "{{name}} ({{id}})" - map: - view: View - edit: Edit - coordinates: "Coordinates:" browse: changeset: title: "Changeset" @@ -341,6 +337,10 @@ en: comment_from: "Comment from {{link_user}} at {{comment_created_at}}" hide_link: Hide this comment confirm: Confirm + location: + location: "Location:" + view: "View" + edit: "Edit" feed: user: title: "OpenStreetMap diary entries for {{user}}" diff --git a/public/stylesheets/common.css b/public/stylesheets/common.css index aa40f689a..738a16e62 100644 --- a/public/stylesheets/common.css +++ b/public/stylesheets/common.css @@ -700,3 +700,7 @@ input[type="submit"] { top: 15px; left: 15px } + +abbr.geo { + border-bottom: none; +} diff --git a/test/functional/diary_entry_controller_test.rb b/test/functional/diary_entry_controller_test.rb index 53d9716b3..bd65088e4 100644 --- a/test/functional/diary_entry_controller_test.rb +++ b/test/functional/diary_entry_controller_test.rb @@ -3,6 +3,8 @@ require File.dirname(__FILE__) + '/../test_helper' class DiaryEntryControllerTest < ActionController::TestCase fixtures :users, :diary_entries, :diary_comments + include ActionView::Helpers::NumberHelper + def test_showing_new_diary_entry get :new assert_response :redirect @@ -110,8 +112,7 @@ class DiaryEntryControllerTest < ActionController::TestCase # This next line won't work if the text has been run through the htmlize function # due to formatting that could be introduced assert_select "p", :text => /#{new_body}/, :count => 1 - assert_select "span.latitude", :text => new_latitude, :count => 1 - assert_select "span.longitude", :text => new_longitude, :count => 1 + assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1 # As we're not logged in, check that you cannot edit #print @response.body assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 1 @@ -134,8 +135,7 @@ class DiaryEntryControllerTest < ActionController::TestCase # This next line won't work if the text has been run through the htmlize function # due to formatting that could be introduced assert_select "p", :text => /#{new_body}/, :count => 1 - assert_select "span.latitude", :text => new_latitude, :count => 1 - assert_select "span.longitude", :text => new_longitude, :count => 1 + assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1 # As we're not logged in, check that you cannot edit assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 0 end -- 2.39.5