Split comment field out of map bugs table

Rather than have all comments in a single text field, have each comment in its own entry
and only combine them back on output
This commit is contained in:
Kai Krueger 2010-03-01 21:05:40 +00:00
parent 42822a8b89
commit eac7348ad2
4 changed files with 85 additions and 8 deletions

View file

@ -1,6 +1,7 @@
class MapBugsController < ApplicationController
before_filter :check_api_readable
before_filter :authorize_web, :only => [:add_bug, :close_bug, :edit_bug]
before_filter :check_api_writable, :only => [:add_bug, :close_bug, :edit_bug]
after_filter :compress_output
around_filter :api_call_handle_error, :api_call_timeout
@ -34,7 +35,17 @@ class MapBugsController < ApplicationController
resp = ""
bugs.each do |bug|
resp += "putAJAXMarker(" + bug.id.to_s + ", " + bug.lon.to_s + ", " + bug.lat.to_s + " , '" + bug.text + "'," + (bug.status=="open"?"0":"1") + ");\n"
resp += "putAJAXMarker(" + bug.id.to_s + ", " + bug.lon.to_s + ", " + bug.lat.to_s;
comment_no = 1
bug.map_bug_comment.each do |comment|
resp += (comment_no == 1 ? ", '" : "<hr />")
resp += comment.comment if comment.comment
resp += " [ "
resp += comment.commenter_name if comment.commenter_name
resp += " " + comment.date_created.to_s + " ]"
comment_no += 1
end
resp += (comment_no == 1 ? "," : "', ") + (bug.status=="open"?"0":"1") + ");\n"
end
render :text => resp, :content_type => "text/javascript"
@ -49,7 +60,9 @@ class MapBugsController < ApplicationController
lat = params['lat'].to_f
comment = params['text']
bug = MapBug.create_bug(lat, lon, comment)
bug = MapBug.create_bug(lat, lon)
bug.save;
add_comment(bug, comment);
render_ok
end
@ -61,9 +74,8 @@ class MapBugsController < ApplicationController
id = params['id'].to_i
bug = MapBug.find_by_id(id);
bug.text += "<hr /> " + params['text']
bug.last_changed = Time.now.getutc;
bug.save;
bug_comment = add_comment(bug, params['text']);
render_ok
end
@ -99,4 +111,19 @@ class MapBugsController < ApplicationController
##TODO: needs to be implemented
end
def add_comment(bug, comment)
t = Time.now.getutc
bug_comment = bug.map_bug_comment.create(:date_created => t, :visible => true, :comment => comment);
if @user
bug_comment.commenter_id = @user.id
bug_comment.commenter_name = @user.display_name
else
bug_comment.commenter_ip = request.remote_ip
bug_comment.commenter_name = "anonymous (a)"
end
bug_comment.save;
bug.last_changed = t
bug.save
end
end

View file

@ -11,15 +11,15 @@ class MapBug < ActiveRecord::Base
validates_presence_of :last_changed
validates_inclusion_of :status, :in => [ "open", "closed", "hidden" ]
has_many :map_bug_comment, :foreign_key => :bug_id, :order => :date_created, :conditions => { :visible => true }
def self.create_bug(lat, lon, comment)
def self.create_bug(lat, lon)
bug = MapBug.new(:lat => lat, :lon => lon);
raise OSM::APIBadUserInput.new("The node is outside this world") unless bug.in_world?
bug.text = comment
bug.date_created = Time.now.getutc
bug.last_changed = Time.now.getutc
bug.status = "open";
bug.save;
return bug;
end

View file

@ -0,0 +1,14 @@
class MapBugComment < ActiveRecord::Base
set_table_name 'map_bug_comment'
belongs_to :map_bug, :foreign_key => 'bug_id'
validates_presence_of :id, :on => :update
validates_uniqueness_of :id
validates_presence_of :visible
validates_presence_of :date_created
end

View file

@ -0,0 +1,36 @@
require 'lib/migrate'
class RefactorMapBugTables < ActiveRecord::Migration
def self.up
create_table :map_bug_comment do |t|
t.column :id, :bigint, :null => false
t.column :bug_id, :bigint, :null => false
t.boolean :visible, :null => false
t.datetime :date_created, :null => false
t.string :commenter_name
t.string :commenter_ip
t.column :commenter_id, :bigint
t.string :comment
end
remove_column :map_bugs, :text
add_index :map_bug_comment, [:bug_id], :name => "map_bug_comment_id_idx"
add_foreign_key :map_bug_comment, [:bug_id], :map_bugs, [:id]
add_foreign_key :map_bug_comment, [:commenter_id], :users, [:id]
end
def self.down
add_column :map_bugs, :text, :string
remove_index :map_bugs, :name => "map_bug_comment_id_idx"
remove_foreign_key :map_bug_comment, [:bug_id]
remove_foreign_key :map_bug_comment, [:commenter_id]
drop_table :map_bugs_comment
end
end