From: Kai Krueger Date: Mon, 1 Mar 2010 21:05:40 +0000 (+0000) Subject: Split comment field out of map bugs table X-Git-Tag: live~5064^2~236 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/eac7348ad2793cb3ad72c68bef84ff863af92d5f 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 --- diff --git a/app/controllers/map_bugs_controller.rb b/app/controllers/map_bugs_controller.rb index d3a62f553..ef965ea3c 100644 --- a/app/controllers/map_bugs_controller.rb +++ b/app/controllers/map_bugs_controller.rb @@ -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 ? ", '" : "
") + 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 += "
" + 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 diff --git a/app/models/map_bug.rb b/app/models/map_bug.rb index 61c9f145f..0a0039872 100644 --- a/app/models/map_bug.rb +++ b/app/models/map_bug.rb @@ -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 diff --git a/app/models/map_bug_comment.rb b/app/models/map_bug_comment.rb new file mode 100644 index 000000000..9839cae45 --- /dev/null +++ b/app/models/map_bug_comment.rb @@ -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 diff --git a/db/migrate/051_refactor_map_bug_tables.rb b/db/migrate/051_refactor_map_bug_tables.rb new file mode 100644 index 000000000..b45f935cc --- /dev/null +++ b/db/migrate/051_refactor_map_bug_tables.rb @@ -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