Rather than have all comments in a single text field, have each comment in its own entry
and only combine them back on output
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
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"
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
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
##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
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
--- /dev/null
+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
--- /dev/null
+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