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.
This commit is contained in:
Kai Krueger 2012-11-20 16:10:07 -07:00 committed by Tom Hughes
parent fd6abf41bf
commit 0a3aba7f89
6 changed files with 22 additions and 23 deletions

View file

@ -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!

View file

@ -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"

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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