]> git.openstreetmap.org Git - rails.git/commitdiff
Added IssueComments + ForeignKeys + Indexes
authorShrey <shrey14099@iiitd.ac.in>
Thu, 28 May 2015 18:21:54 +0000 (23:51 +0530)
committerMatt Amos <zerebubuth@gmail.com>
Mon, 22 Aug 2016 15:15:12 +0000 (16:15 +0100)
16 files changed:
app/controllers/issues_controller.rb
app/models/issue.rb
app/models/issue_comment.rb [new file with mode: 0644]
app/models/user.rb
app/views/diary_entry/_diary_entry.html.erb
app/views/issues/_comments.html.erb [new file with mode: 0644]
app/views/issues/_reports.html.erb
app/views/issues/new.html.erb
app/views/issues/show.html.erb
config/routes.rb
db/migrate/20150526130032_create_issue_comments.rb [new file with mode: 0644]
db/migrate/20150528113100_add_foreign_keys_for_issues.rb [new file with mode: 0644]
db/migrate/20150528114520_add_indexes_for_issues.rb [new file with mode: 0644]
db/structure.sql
test/fixtures/issue_comments.yml [new file with mode: 0644]
test/models/issue_comment_test.rb [new file with mode: 0644]

index 6ca61b4ce5eea3200d7ac3649c254b1479ef1b90..a00423a51680d8a9c4e0f56402abc009311a686b 100644 (file)
@@ -2,7 +2,8 @@ class IssuesController < ApplicationController
   layout "site"
 
   before_action :authorize_web
-  before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore]
+  before_action :require_user
+  before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
 
   def index
@@ -12,11 +13,13 @@ class IssuesController < ApplicationController
   def show
     @read_reports = @issue.read_reports
     @unread_reports = @issue.unread_reports
+    @comments = @issue.comments
   end
 
   def new
     unless create_new_issue_params.blank?
       @issue = Issue.find_or_initialize_by(create_new_issue_params)
+      puts params[:user_id].to_s + "--------------"
     end
   end
 
@@ -25,20 +28,27 @@ class IssuesController < ApplicationController
     if !@issue 
       @issue = Issue.find_or_initialize_by(issue_params)
       @admins = UserRole.where(role: "administrator")
-      @admins.each do |user|
-        Notifier.new_issue_notification(User.find(user.user_id)).deliver_now
+      @admins.each do |admin|
+        Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
       end
     end
-
     @report = @issue.reports.build(report_params)
-
-    if @issue.save
-      redirect_to @issue, notice: 'Issue was successfully created.'
+    @report.user_id = @user.id
+    if @issue.save!
+      redirect_to root_path, notice: 'Issue was successfully created.'
     else
       render :new
     end
   end
 
+  def comment
+    @issue = Issue.find(params[:id])
+    @issue_comment = @issue.comments.build(issue_comment_params)
+    @issue_comment.user_id = @user.id
+    @issue_comment.save!
+    redirect_to @issue
+  end
+
   # Status Transistions
   def resolve
     if @issue.resolve
@@ -85,10 +95,14 @@ class IssuesController < ApplicationController
     end
 
     def issue_params
-      params[:issue].permit(:reportable_id, :reportable_type,:user_id)
+      params.permit(:reportable_id, :reportable_type,:user_id)
     end
 
     def report_params
-      params[:report].permit(:details, :user_id)
+      params[:report].permit(:details)
+    end
+
+    def issue_comment_params
+      params.require(:issue_comment).permit(:body, :user_id)
     end
 end
index 277ea3569ca94fe003cfa456b6372ce498519648..7a481fe6cc80e0594f51a7ff167075a0a983d22b 100644 (file)
@@ -1,8 +1,10 @@
 class Issue < ActiveRecord::Base
        belongs_to :reportable, :polymorphic => true
        has_many :reports
+       has_many :comments, :class_name => "IssueComment"
        validates :reportable_id, :uniqueness => { :scope => [ :reportable_type ] }
        belongs_to :user
+       validates :user_id, :presence => true
 
        # Check if more statuses are needed
        enum status: %w( open ignored resolved )
diff --git a/app/models/issue_comment.rb b/app/models/issue_comment.rb
new file mode 100644 (file)
index 0000000..455fb04
--- /dev/null
@@ -0,0 +1,6 @@
+class IssueComment < ActiveRecord::Base
+       belongs_to :issue
+       belongs_to :user
+
+       validates :body, :presence => true
+end
index 4a36b3e61e5c2dbca369528bae2356f7dcc87f22..3d262fd254a4c14f782b12c7198b9889a4887e13 100644 (file)
@@ -27,6 +27,8 @@ class User < ActiveRecord::Base
   has_many :roles, :class_name => "UserRole"
 
   has_many :issues
+  has_many :issue_comments
+  
   has_many :reports
 
   scope :visible, -> { where(:status => %w(pending active confirmed)) }
index efcd2ec4744f53675e6a6472342c7096d896e7a7..8278e80b07d668817cad5848dee9a61231f830be 100644 (file)
@@ -31,7 +31,7 @@
       <%= link_to t('diary_entry.diary_entry.edit_link'), :action => 'edit', :display_name => diary_entry.user.display_name, :id => diary_entry.id %>
     <% end %>
 
-      <li><%= link_to 'Report', new_issue_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name, user: diary_entry.user.id) %></li>
+      <li><%= link_to 'Report', new_issue_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name, user_id: diary_entry.user.id) %></li>
 
     <%= if_administrator(:li) do %>
       <%= link_to t('diary_entry.diary_entry.hide_link'), hide_diary_entry_path(:display_name => diary_entry.user.display_name, :id => diary_entry.id), :method => :post, :data => { :confirm => t('diary_entry.diary_entry.confirm') } %>
diff --git a/app/views/issues/_comments.html.erb b/app/views/issues/_comments.html.erb
new file mode 100644 (file)
index 0000000..3e236fb
--- /dev/null
@@ -0,0 +1,18 @@
+<% comments.each do |comment| %>
+       <div class="comment">
+               <div style="float:left">
+                       <%= link_to user_thumbnail(comment.user), :controller => :user,:action =>:view, :display_name => comment.user.display_name %>
+               </div>
+               <b> <%= link_to comment.user.display_name, :controller => :user,:action =>:view, :display_name => comment.user.display_name %> </b> <br/>
+               <%= comment.body %>
+       </div>
+       <span class="deemphasize">
+       On <%= l comment.created_at.to_datetime, :format => :long %> </span>
+       <hr>
+<% end %>
+<div class="comment">
+       <%= form_for :issue_comment, :url => { :action => 'comment', :id => @issue.id, :user_id => @user.id } do |f| %>
+           <%= richtext_area :issue_comment, :body, :cols => 80, :rows => 8 %>
+       <%= submit_tag 'Submit' %>
+       <% end %>
+</div>
\ No newline at end of file
index 0a55a59ec2717391712445725955faba12066f6e..29b7e94d1e80138b3de22669e55815f27fa06baa 100644 (file)
@@ -1,11 +1,13 @@
 <% reports.each do |report| %>
        <div class="reports">
-               <div class="display:inline">
-                       <%= user_thumbnail report.user %>
-                       <%= report.details %>
+               <div style="float:left">
+                       <%= link_to user_thumbnail(report.user), :controller => :user,:action =>:view, :display_name => report.user.display_name %>
                </div>
-               <span class="deemphasize"><%= raw(t('Reported by:',:link_user => (link_to h(report.user.display_name), :controller => :user, :action => :view, :display_name => report.user.display_name), :comment_created_at => link_to(l(report.created_at,:format => :friendly)))) %>
-               on <%= l report.created_at.to_datetime, :format => :long %> </span>
+               <b><%= link_to report.user.display_name, :controller => :user,:action =>:view, :display_name => report.user.display_name %></b> <br/>
+               <%= report.details %>
+               <br/>
+               <span class="deemphasize">
+               On <%= l report.created_at.to_datetime, :format => :long %> </span>
        </div>
        <hr>
 <% end %>
index 83c5d0196235956da852ce9864aa46993f7e9c40..2a6cd66f943bb8826e33e85a0871234784e3034b 100644 (file)
@@ -1,2 +1,21 @@
-<h1>Issues#new</h1>
-<p>Find me in app/views/issues/new.html.erb</p>
+<% content_for :heading do %>
+    <h1>Report a new Issue for <%= reportable_url(@issue.reportable) %></h1>
+<% end %>
+
+<%= form_for(@issue) do |f| %>
+    <%= f.error_messages %>
+        <fieldset>
+            <div class='form-row'>
+                <%= f.hidden_field :reportable_id, :value => params[:reportable_id] %>
+                <%= f.hidden_field :reportable_type, :value => params[:reportable_type] %>
+                <%= f.hidden_field :user_id, :value => params[:user_id] %>
+            </div>
+            <div class='form-row'>
+                <label class="standard-label"><%= t 'issue.new.message' -%></label>
+                <%= text_area :report, :details, :cols => 80, :rows => 20, placeholder: "Tell us what's wrong! Any information you can give will go on to help moderators a long way." %>
+            </div>
+            <div class='buttons'>
+                <%= submit_tag %>
+            </div>
+        </fieldset>
+<% end %>
\ No newline at end of file
index d756595cf9335f6123c908cf6a660a2055923336..abbe0fa985fb3626aa1a9d3f99dacdbfeffaf2ee 100644 (file)
@@ -11,7 +11,6 @@
        <p><%= link_to "Ignore", ignore_issue_url(@issue), :method => :post if @issue.may_ignore? %></p>
        <p><%= link_to "Reopen", reopen_issue_url(@issue), :method => :post if @issue.may_reopen? %></p>
 <% end %>
-
 <h3>Reports under this issue:</h3>
 
 <% if @read_reports.present? %>
@@ -27,3 +26,8 @@
                <%= render 'reports',reports: @unread_reports %>
        </div>
 <% end %>      
+<br/>
+<h3>Comments on this issue:</h3>
+       <div class="unread-reports">
+               <%= render 'comments', comments: @comments %>
+       </div>
index a1d73985dd1d0bbb5fc4a30f41753db80d8c3d10..468b9932789b2f827b4e5eac8b9b5b7623c6767b 100644 (file)
@@ -297,6 +297,8 @@ OpenStreetMap::Application.routes.draw do
       post "reopen"
     end
   end
+  
+  post '/comment' => 'issues#comment'
 
   # redactions
   resources :redactions
diff --git a/db/migrate/20150526130032_create_issue_comments.rb b/db/migrate/20150526130032_create_issue_comments.rb
new file mode 100644 (file)
index 0000000..92b328f
--- /dev/null
@@ -0,0 +1,12 @@
+class CreateIssueComments < ActiveRecord::Migration
+  def change
+    create_table :issue_comments do |t|
+      t.integer :issue_id
+      t.integer :user_id
+      t.text :body
+      t.datetime :created_at
+
+      t.timestamps null: false
+    end
+  end
+end
diff --git a/db/migrate/20150528113100_add_foreign_keys_for_issues.rb b/db/migrate/20150528113100_add_foreign_keys_for_issues.rb
new file mode 100644 (file)
index 0000000..4d0d28e
--- /dev/null
@@ -0,0 +1,11 @@
+require "migrate"
+
+class AddForeignKeysForIssues < ActiveRecord::Migration
+  def change
+       add_foreign_key :issues, :users, :name => "issues_user_id_fkey"
+       add_foreign_key :reports, :issues, :name => "reports_issue_id_fkey"
+       add_foreign_key :reports, :users, :name => "reports_user_id_fkey"
+       add_foreign_key :issue_comments, :issues, :name => "issue_comments_issue_id_fkey"
+       add_foreign_key :issue_comments, :users, :name => "issue_comments_user_id"
+  end
+end
diff --git a/db/migrate/20150528114520_add_indexes_for_issues.rb b/db/migrate/20150528114520_add_indexes_for_issues.rb
new file mode 100644 (file)
index 0000000..071bf4f
--- /dev/null
@@ -0,0 +1,19 @@
+class AddIndexesForIssues < ActiveRecord::Migration
+       def self.up
+    add_index :issues, :user_id
+    add_index :issues, [:reportable_id, :reportable_type]
+    add_index :reports, :issue_id
+    add_index :reports, :user_id
+       add_index :issue_comments, :user_id
+       add_index :issue_comments, :issue_id
+  end
+
+  def self.down
+    remove_index :issues, :user_id
+    remove_index :issues, [:reportable_id, :reportable_type]
+    remove_index :reports, :issue_id
+    remove_index :reports, :user_id
+       remove_index :issue_comments, :user_id
+       remove_index :issue_comments, :issue_id
+  end
+end
index ce03a757af81f4b5fde340e627204939bce0e2e6..f0a07adf5df13a0f5d7bf9091c9f5f98b42e8184 100644 (file)
@@ -663,6 +663,39 @@ CREATE SEQUENCE gpx_files_id_seq
 ALTER SEQUENCE gpx_files_id_seq OWNED BY gpx_files.id;
 
 
+--
+-- Name: issue_comments; Type: TABLE; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE TABLE issue_comments (
+    id integer NOT NULL,
+    issue_id integer,
+    user_id integer,
+    body text,
+    created_at timestamp without time zone NOT NULL,
+    updated_at timestamp without time zone NOT NULL
+);
+
+
+--
+-- Name: issue_comments_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE issue_comments_id_seq
+    START WITH 1
+    INCREMENT BY 1
+    NO MINVALUE
+    NO MAXVALUE
+    CACHE 1;
+
+
+--
+-- Name: issue_comments_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE issue_comments_id_seq OWNED BY issue_comments.id;
+
+
 --
 -- Name: issues; Type: TABLE; Schema: public; Owner: -; Tablespace: 
 --
@@ -1335,6 +1368,13 @@ ALTER TABLE ONLY gpx_file_tags ALTER COLUMN id SET DEFAULT nextval('gpx_file_tag
 ALTER TABLE ONLY gpx_files ALTER COLUMN id SET DEFAULT nextval('gpx_files_id_seq'::regclass);
 
 
+--
+-- Name: id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY issue_comments ALTER COLUMN id SET DEFAULT nextval('issue_comments_id_seq'::regclass);
+
+
 --
 -- Name: id; Type: DEFAULT; Schema: public; Owner: -
 --
@@ -1555,6 +1595,14 @@ ALTER TABLE ONLY gpx_files
     ADD CONSTRAINT gpx_files_pkey PRIMARY KEY (id);
 
 
+--
+-- Name: issue_comments_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
+--
+
+ALTER TABLE ONLY issue_comments
+    ADD CONSTRAINT issue_comments_pkey PRIMARY KEY (id);
+
+
 --
 -- Name: issues_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
 --
@@ -1927,6 +1975,34 @@ CREATE UNIQUE INDEX index_changesets_subscribers_on_subscriber_id_and_changeset_
 CREATE UNIQUE INDEX index_client_applications_on_key ON client_applications USING btree (key);
 
 
+--
+-- Name: index_issue_comments_on_issue_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE INDEX index_issue_comments_on_issue_id ON issue_comments USING btree (issue_id);
+
+
+--
+-- Name: index_issue_comments_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE INDEX index_issue_comments_on_user_id ON issue_comments USING btree (user_id);
+
+
+--
+-- Name: index_issues_on_reportable_id_and_reportable_type; Type: INDEX; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE INDEX index_issues_on_reportable_id_and_reportable_type ON issues USING btree (reportable_id, reportable_type);
+
+
+--
+-- Name: index_issues_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE INDEX index_issues_on_user_id ON issues USING btree (user_id);
+
+
 --
 -- Name: index_note_comments_on_body; Type: INDEX; Schema: public; Owner: -
 --
@@ -1955,6 +2031,20 @@ CREATE UNIQUE INDEX index_oauth_nonces_on_nonce_and_timestamp ON oauth_nonces US
 CREATE UNIQUE INDEX index_oauth_tokens_on_token ON oauth_tokens USING btree (token);
 
 
+--
+-- Name: index_reports_on_issue_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE INDEX index_reports_on_issue_id ON reports USING btree (issue_id);
+
+
+--
+-- Name: index_reports_on_user_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
+--
+
+CREATE INDEX index_reports_on_user_id ON reports USING btree (user_id);
+
+
 --
 -- Name: index_user_blocks_on_user_id; Type: INDEX; Schema: public; Owner: -
 --
@@ -2351,6 +2441,30 @@ ALTER TABLE ONLY gpx_files
     ADD CONSTRAINT gpx_files_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);
 
 
+--
+-- Name: issue_comments_issue_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY issue_comments
+    ADD CONSTRAINT issue_comments_issue_id_fkey FOREIGN KEY (issue_id) REFERENCES issues(id);
+
+
+--
+-- Name: issue_comments_user_id; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY issue_comments
+    ADD CONSTRAINT issue_comments_user_id FOREIGN KEY (user_id) REFERENCES users(id);
+
+
+--
+-- Name: issues_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY issues
+    ADD CONSTRAINT issues_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);
+
+
 --
 -- Name: messages_from_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
@@ -2463,6 +2577,22 @@ ALTER TABLE ONLY relations
     ADD CONSTRAINT relations_redaction_id_fkey FOREIGN KEY (redaction_id) REFERENCES redactions(id);
 
 
+--
+-- Name: reports_issue_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY reports
+    ADD CONSTRAINT reports_issue_id_fkey FOREIGN KEY (issue_id) REFERENCES issues(id);
+
+
+--
+-- Name: reports_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY reports
+    ADD CONSTRAINT reports_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);
+
+
 --
 -- Name: user_blocks_moderator_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
@@ -2657,6 +2787,12 @@ INSERT INTO schema_migrations (version) VALUES ('20150516073616');
 
 INSERT INTO schema_migrations (version) VALUES ('20150516075620');
 
+INSERT INTO schema_migrations (version) VALUES ('20150526130032');
+
+INSERT INTO schema_migrations (version) VALUES ('20150528113100');
+
+INSERT INTO schema_migrations (version) VALUES ('20150528114520');
+
 INSERT INTO schema_migrations (version) VALUES ('21');
 
 INSERT INTO schema_migrations (version) VALUES ('22');
diff --git a/test/fixtures/issue_comments.yml b/test/fixtures/issue_comments.yml
new file mode 100644 (file)
index 0000000..435e4be
--- /dev/null
@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  issue_id: 1
+  user_id: 1
+  body: MyText
+  created_at: 2015-05-26 18:30:32
+
+two:
+  issue_id: 1
+  user_id: 1
+  body: MyText
+  created_at: 2015-05-26 18:30:32
diff --git a/test/models/issue_comment_test.rb b/test/models/issue_comment_test.rb
new file mode 100644 (file)
index 0000000..e7f5f34
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class IssueCommentTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end