]> git.openstreetmap.org Git - rails.git/commitdiff
Split comment field out of map bugs table
authorKai Krueger <kakrueger@gmail.com>
Mon, 1 Mar 2010 21:05:40 +0000 (21:05 +0000)
committerKai Krueger <kakrueger@gmail.com>
Mon, 1 Mar 2010 21:05:40 +0000 (21:05 +0000)
Rather than have all comments in a single text field, have each comment in its own entry
and only combine them back on output

app/controllers/map_bugs_controller.rb
app/models/map_bug.rb
app/models/map_bug_comment.rb [new file with mode: 0644]
db/migrate/051_refactor_map_bug_tables.rb [new file with mode: 0644]

index d3a62f553d3706db9f8927993e692fa0b6f25535..ef965ea3c9f4ed037638ec101028a50b4db7d767 100644 (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
index 61c9f145f5aff0b2c37c73b009cf90d873cc7009..0a00398723856bd95e79fe9b3ab721ee82beb3f6 100644 (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
 
diff --git a/app/models/map_bug_comment.rb b/app/models/map_bug_comment.rb
new file mode 100644 (file)
index 0000000..9839cae
--- /dev/null
@@ -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 (file)
index 0000000..b45f935
--- /dev/null
@@ -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