From 683722ed5c1b3224a04c23b4d175a0638964f713 Mon Sep 17 00:00:00 2001 From: Shrey Date: Thu, 28 May 2015 23:51:54 +0530 Subject: [PATCH] Added IssueComments + ForeignKeys + Indexes --- app/controllers/issues_controller.rb | 32 +++-- app/models/issue.rb | 2 + app/models/issue_comment.rb | 6 + app/models/user.rb | 2 + app/views/diary_entry/_diary_entry.html.erb | 2 +- app/views/issues/_comments.html.erb | 18 +++ app/views/issues/_reports.html.erb | 12 +- app/views/issues/new.html.erb | 23 ++- app/views/issues/show.html.erb | 6 +- config/routes.rb | 2 + .../20150526130032_create_issue_comments.rb | 12 ++ ...50528113100_add_foreign_keys_for_issues.rb | 11 ++ .../20150528114520_add_indexes_for_issues.rb | 19 +++ db/structure.sql | 136 ++++++++++++++++++ test/fixtures/issue_comments.yml | 13 ++ test/models/issue_comment_test.rb | 7 + 16 files changed, 285 insertions(+), 18 deletions(-) create mode 100644 app/models/issue_comment.rb create mode 100644 app/views/issues/_comments.html.erb create mode 100644 db/migrate/20150526130032_create_issue_comments.rb create mode 100644 db/migrate/20150528113100_add_foreign_keys_for_issues.rb create mode 100644 db/migrate/20150528114520_add_indexes_for_issues.rb create mode 100644 test/fixtures/issue_comments.yml create mode 100644 test/models/issue_comment_test.rb diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 6ca61b4ce..a00423a51 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -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 diff --git a/app/models/issue.rb b/app/models/issue.rb index 277ea3569..7a481fe6c 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -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 index 000000000..455fb040b --- /dev/null +++ b/app/models/issue_comment.rb @@ -0,0 +1,6 @@ +class IssueComment < ActiveRecord::Base + belongs_to :issue + belongs_to :user + + validates :body, :presence => true +end diff --git a/app/models/user.rb b/app/models/user.rb index 4a36b3e61..3d262fd25 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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)) } diff --git a/app/views/diary_entry/_diary_entry.html.erb b/app/views/diary_entry/_diary_entry.html.erb index efcd2ec47..8278e80b0 100644 --- a/app/views/diary_entry/_diary_entry.html.erb +++ b/app/views/diary_entry/_diary_entry.html.erb @@ -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 %> -
  • <%= link_to 'Report', new_issue_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name, user: diary_entry.user.id) %>
  • +
  • <%= link_to 'Report', new_issue_url(reportable_id: diary_entry.id, reportable_type: diary_entry.class.name, user_id: diary_entry.user.id) %>
  • <%= 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 index 000000000..3e236fb8e --- /dev/null +++ b/app/views/issues/_comments.html.erb @@ -0,0 +1,18 @@ +<% comments.each do |comment| %> +
    +
    + <%= link_to user_thumbnail(comment.user), :controller => :user,:action =>:view, :display_name => comment.user.display_name %> +
    + <%= link_to comment.user.display_name, :controller => :user,:action =>:view, :display_name => comment.user.display_name %>
    + <%= comment.body %> +
    + + On <%= l comment.created_at.to_datetime, :format => :long %> +
    +<% end %> +
    + <%= 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 %> +
    \ No newline at end of file diff --git a/app/views/issues/_reports.html.erb b/app/views/issues/_reports.html.erb index 0a55a59ec..29b7e94d1 100644 --- a/app/views/issues/_reports.html.erb +++ b/app/views/issues/_reports.html.erb @@ -1,11 +1,13 @@ <% reports.each do |report| %>
    -
    - <%= user_thumbnail report.user %> - <%= report.details %> +
    + <%= link_to user_thumbnail(report.user), :controller => :user,:action =>:view, :display_name => report.user.display_name %>
    - <%= 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 %> + <%= link_to report.user.display_name, :controller => :user,:action =>:view, :display_name => report.user.display_name %>
    + <%= report.details %> +
    + + On <%= l report.created_at.to_datetime, :format => :long %>

    <% end %> diff --git a/app/views/issues/new.html.erb b/app/views/issues/new.html.erb index 83c5d0196..2a6cd66f9 100644 --- a/app/views/issues/new.html.erb +++ b/app/views/issues/new.html.erb @@ -1,2 +1,21 @@ -

    Issues#new

    -

    Find me in app/views/issues/new.html.erb

    +<% content_for :heading do %> +

    Report a new Issue for <%= reportable_url(@issue.reportable) %>

    +<% end %> + +<%= form_for(@issue) do |f| %> + <%= f.error_messages %> +
    +
    + <%= 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] %> +
    +
    + + <%= 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." %> +
    +
    + <%= submit_tag %> +
    +
    +<% end %> \ No newline at end of file diff --git a/app/views/issues/show.html.erb b/app/views/issues/show.html.erb index d756595cf..abbe0fa98 100644 --- a/app/views/issues/show.html.erb +++ b/app/views/issues/show.html.erb @@ -11,7 +11,6 @@

    <%= link_to "Ignore", ignore_issue_url(@issue), :method => :post if @issue.may_ignore? %>

    <%= link_to "Reopen", reopen_issue_url(@issue), :method => :post if @issue.may_reopen? %>

    <% end %> -

    Reports under this issue:

    <% if @read_reports.present? %> @@ -27,3 +26,8 @@ <%= render 'reports',reports: @unread_reports %>
    <% end %> +
    +

    Comments on this issue:

    +
    + <%= render 'comments', comments: @comments %> +
    diff --git a/config/routes.rb b/config/routes.rb index a1d73985d..468b99327 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 index 000000000..92b328f9a --- /dev/null +++ b/db/migrate/20150526130032_create_issue_comments.rb @@ -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 index 000000000..4d0d28e8d --- /dev/null +++ b/db/migrate/20150528113100_add_foreign_keys_for_issues.rb @@ -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 index 000000000..071bf4f4f --- /dev/null +++ b/db/migrate/20150528114520_add_indexes_for_issues.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index ce03a757a..f0a07adf5 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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 index 000000000..435e4becd --- /dev/null +++ b/test/fixtures/issue_comments.yml @@ -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 index 000000000..e7f5f3442 --- /dev/null +++ b/test/models/issue_comment_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class IssueCommentTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end -- 2.43.2