From 0a3aba7f891ba55a5500f02907cec15d44554eae Mon Sep 17 00:00:00 2001 From: Kai Krueger Date: Tue, 20 Nov 2012 16:10:07 -0700 Subject: [PATCH 1/1] Remove nearby_place from the note model Instead of storing the auto-generated meta information nearby_place in the database, just look up the information on the fly when needed and cache it for performance. --- app/controllers/notes_controller.rb | 14 -------------- app/views/notes/_note.json.jsonify | 1 - app/views/notes/_note.rss.builder | 11 +++++++---- app/views/notes/_note.xml.builder | 1 - app/views/notes/feed.rss.builder | 9 ++++++--- .../20121119165817_drop_nearby_place_from_notes.rb | 9 +++++++++ 6 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20121119165817_drop_nearby_place_from_notes.rb diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 2089c2289..5a0934247 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -66,20 +66,6 @@ class NotesController < ApplicationController @note = Note.create(:lat => lat, :lon => lon) raise OSM::APIBadUserInput.new("The note is outside this world") unless @note.in_world? - #TODO: move this into a helper function - begin - url = "http://nominatim.openstreetmap.org/reverse?lat=" + lat.to_s + "&lon=" + lon.to_s + "&zoom=16" - response = REXML::Document.new(Net::HTTP.get(URI.parse(url))) - - if result = response.get_text("reversegeocode/result") - @note.nearby_place = result.to_s - else - @note.nearby_place = "unknown" - end - rescue Exception => err - @note.nearby_place = "unknown" - end - # Save the note @note.save! diff --git a/app/views/notes/_note.json.jsonify b/app/views/notes/_note.json.jsonify index 6afb4a555..f252a0dfc 100644 --- a/app/views/notes/_note.json.jsonify +++ b/app/views/notes/_note.json.jsonify @@ -11,7 +11,6 @@ json.properties do json.comment_url comment_note_url(note, :format => params[:format]) json.close_url close_note_url(note, :format => params[:format]) json.date_created note.created_at - json.nearby note.nearby_place json.status note.status json.closed_at note.closed_at if note.status == "closed" diff --git a/app/views/notes/_note.rss.builder b/app/views/notes/_note.rss.builder index bae719f5f..efdae52bb 100644 --- a/app/views/notes/_note.rss.builder +++ b/app/views/notes/_note.rss.builder @@ -1,16 +1,19 @@ xml.item do + location_string = Rails.cache.fetch("location_description_#{note.lat}_#{note.lon}_#{locale}") do + describe_location note.lat, note.lon, 14, locale + end if note.status == "closed" - xml.title t('note.rss.closed', :place => note.nearby_place) + xml.title t('note.rss.closed', :place => location_string) elsif note.comments.length > 1 - xml.title t('note.rss.comment', :place => note.nearby_place) + xml.title t('note.rss.comment', :place => location_string) else - xml.title t('note.rss.new', :place => note.nearby_place) + xml.title t('note.rss.new', :place => location_string) end xml.link browse_note_url(note) xml.guid note_url(note) xml.description render(:partial => "description", :object => note, :formats => [ :html ]) - xml.author note.author_name + xml.author note.comments.first.author_name xml.pubDate note.updated_at.to_s(:rfc822) xml.geo :lat, note.lat xml.geo :long, note.lon diff --git a/app/views/notes/_note.xml.builder b/app/views/notes/_note.xml.builder index c00b49c73..a68946bb7 100644 --- a/app/views/notes/_note.xml.builder +++ b/app/views/notes/_note.xml.builder @@ -4,7 +4,6 @@ xml.note("lon" => note.lon, "lat" => note.lat) do xml.comment_url comment_note_url(note, :format => params[:format]) xml.close_url close_note_url(note, :format => params[:format]) xml.date_created note.created_at - xml.nearby note.nearby_place xml.status note.status if note.status == "closed" diff --git a/app/views/notes/feed.rss.builder b/app/views/notes/feed.rss.builder index dd7d32c5f..ba0e3fbfe 100644 --- a/app/views/notes/feed.rss.builder +++ b/app/views/notes/feed.rss.builder @@ -9,13 +9,16 @@ xml.rss("version" => "2.0", xml.link url_for(:controller => "site", :action => "index", :only_path => false) @comments.each do |comment| + location_string = Rails.cache.fetch("location_description_#{comment.note.lat}_#{comment.note.lon}_#{locale}") do + describe_location comment.note.lat, comment.note.lon, 14, locale + end xml.item do if comment.event == "closed" - xml.title t('note.rss.closed', :place => comment.note.nearby_place) + xml.title t('note.rss.closed', :place => location_string) elsif comment.event == "commented" - xml.title t('note.rss.comment', :place => comment.note.nearby_place) + xml.title t('note.rss.comment', :place => location_string) elsif comment.event == "opened" - xml.title t('note.rss.new', :place => comment.note.nearby_place) + xml.title t('note.rss.new', :place => location_string) else xml.title "unknown event" end diff --git a/db/migrate/20121119165817_drop_nearby_place_from_notes.rb b/db/migrate/20121119165817_drop_nearby_place_from_notes.rb new file mode 100644 index 000000000..bbef25946 --- /dev/null +++ b/db/migrate/20121119165817_drop_nearby_place_from_notes.rb @@ -0,0 +1,9 @@ +class DropNearbyPlaceFromNotes < ActiveRecord::Migration + def up + remove_column :notes, :nearby_place + end + + def down + add_column :notes, :nearby_place, :string + end +end -- 2.43.2