From: Shrey Date: Mon, 20 Jul 2015 16:52:41 +0000 (+0530) Subject: Added sortable headers + search + reportable Notes X-Git-Tag: live~2957^2~134 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/69c1f6d1862aad00f80a0d2e32dbbe70aba65649 Added sortable headers + search + reportable Notes --- diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index beb1425cd..ecac5bd16 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -3,33 +3,48 @@ class IssuesController < ApplicationController before_action :authorize_web before_action :require_user + before_action :set_issues before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment] before_action :find_issue, only: [:show, :resolve, :reopen, :ignore] before_action :get_user_role, only: [:show, :index] + helper_method :sort_column, :sort_direction + def index + if @user.moderator? + @issue_types = @moderator_issues + else + @issue_types = @admin_issues + end + + @issues = Issue.where(issue_type: @user_role).order(sort_column + " " + sort_direction) + # If search - if params[:search_by_user] + if params[:search_by_user] and !params[:search_by_user].blank? @find_user = User.find_by_display_name(params[:search_by_user]) if @find_user - @issues = Issue.where(reported_user_id: @find_user.id, issue_type: @user_role).order(:status) + @issues = @issues.where(reported_user_id: @find_user.id) else - @issues = Issue.where(issue_type: @user_role).order(:status) notice = t('issues.index.search.user_not_found') end + end - if @find_user !=nil and @issues.first == nil - @issues = Issue.where(issue_type: @user_role).order(:status) - notice = t('issues.index.search.issues_not_found') - end - if notice - redirect_to issues_path, notice: notice - end - - else - @issues = Issue.where(issue_type: @user_role).order(:status) + if params[:status] and !params[:status][0].blank? + @issues = @issues.where(status: params[:status][0].to_i) + end + + if params[:issue_type] and !params[:issue_type][0].blank? + @issues = @issues.where(reportable_type: params[:issue_type][0]) + end + + if @issues.first == nil + notice = t('issues.index.search.issues_not_found') end + + if notice + redirect_to issues_path, notice: notice + end end def show @@ -51,21 +66,15 @@ class IssuesController < ApplicationController end def create - - # TODO: Find better place to add these - admin_issues = [ 'DiaryEntry', 'DiaryComment', 'User'] - moderator_issues = [ 'Changeset' ] - - @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type]) - # Check if Issue alrwady exists + # Check if Issue already exists if !@issue @issue = Issue.find_or_initialize_by(issue_params) @issue.updated_by = nil # Reassign to moderators if it is a moderator issue @issue.issue_type = "administrator" - if moderator_issues.include? @issue.reportable.class.name + if @moderator_issues.include? @issue.reportable.class.name reassign_issue end @@ -82,7 +91,6 @@ class IssuesController < ApplicationController details = get_report_details @report.reporter_user_id = @user.id @report.details = details - # Checking if instance has been updated since last report @last_report = @issue.reports.order(updated_at: :desc).last if check_if_updated @@ -92,6 +100,8 @@ class IssuesController < ApplicationController end if @issue.save! + @issue.report_count = @issue.reports.count + @issue.save! redirect_to root_path, notice: t('issues.create.successful_report') end else @@ -125,7 +135,10 @@ class IssuesController < ApplicationController notice = t('issues.update.successful_update') end + if @report.save! + @issue.report_count = @issue.reports.count + @issue.save! redirect_to root_path, notice: notice end else @@ -194,6 +207,11 @@ class IssuesController < ApplicationController private + def set_issues + @admin_issues = [ 'DiaryEntry', 'DiaryComment', 'User'] + @moderator_issues = [ 'Changeset', 'Note' ] + end + def get_user_role # Get user role if @user.administrator? @@ -204,7 +222,7 @@ class IssuesController < ApplicationController end def check_if_updated - if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at + if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.has_attribute?(:updated_by) and @issue.reportable.updated_at > @last_report.updated_at return true else return false @@ -251,4 +269,11 @@ class IssuesController < ApplicationController params.require(:issue_comment).permit(:body) end + def sort_column + Issue.column_names.include?(params[:sort]) ? params[:sort] : "status" + end + + def sort_direction + %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" + end end diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index ac5bd59cd..bcae2e224 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -5,13 +5,13 @@ module IssuesHelper case class_name when "DiaryEntry" link_to reportable.title, :controller => reportable.class.name.underscore, - :action => :view, - :display_name => reportable.user.display_name, - :id => reportable.id + :action => :view, + :display_name => reportable.user.display_name, + :id => reportable.id when "User" link_to reportable.display_name.to_s, :controller => reportable.class.name.underscore, - :action => :view, - :display_name => reportable.display_name + :action => :view, + :display_name => reportable.display_name when "DiaryComment" link_to "#{reportable.diary_entry.title}, Comment id ##{reportable.id}", :controller => reportable.diary_entry.class.name.underscore, :action => :view, @@ -20,10 +20,73 @@ module IssuesHelper :comment_id => reportable.id when "Changeset" link_to "Changeset ##{reportable.id}", :controller => :browse, - :action => :changeset, - :id => reportable.id + :action => :changeset, + :id => reportable.id + when "Note" + link_to "Note ##{reportable.id}", :controller => :browse, + :action => :note, + :id => reportable.id else nil end end + + def reports_url(issue) + class_name = issue.reportable.class.name + case class_name + when "DiaryEntry" + link_to issue.reportable.title, issue + when "User" + link_to issue.reportable.display_name.to_s, issue + when "DiaryComment" + link_to "#{issue.reportable.diary_entry.title}, Comment id ##{issue.reportable.id}", issue + when "Changeset" + link_to "Changeset ##{issue.reportable.id}",issue + when "Note" + link_to "Note ##{issue.reportable.id}", issue + else + nil + end + end + + def instance_url(reportable) + class_name = reportable.class.name + case class_name + when "DiaryEntry" + link_to "Show Instance", :controller => reportable.class.name.underscore, + :action => :view, + :display_name => reportable.user.display_name, + :id => reportable.id + when "User" + link_to "Show Instance", :controller => reportable.class.name.underscore, + :action => :view, + :display_name => reportable.display_name + when "DiaryComment" + link_to "Show Instance", :controller => reportable.diary_entry.class.name.underscore, + :action => :view, + :display_name => reportable.diary_entry.user.display_name, + :id => reportable.diary_entry.id, + :comment_id => reportable.id + when "Changeset" + link_to "Show Instance", :controller => :browse, + :action => :changeset, + :id => reportable.id + when "Note" + link_to "Show Instance", :controller => :browse, + :action => :note, + :id => reportable.id + else + nil + end + end + + def sortable(column,title=nil) + title ||= column.titleize + direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc" + if column == sort_column + arrow = direction == "desc" ? ["25B2".hex].pack("U") : ["25BC".hex].pack("U") + title = title + arrow + end + link_to title, params.merge(:sort => column, :direction => direction) + end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 0abc27063..690a02477 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1,6 +1,7 @@ class Issue < ActiveRecord::Base belongs_to :reportable, :polymorphic => true belongs_to :user, :class_name => "User", :foreign_key => :reported_user_id + belongs_to :user_updated, :class_name => "User", :foreign_key => :updated_by has_many :reports, dependent: :destroy has_many :comments, :class_name => "IssueComment", dependent: :destroy diff --git a/app/models/user.rb b/app/models/user.rb index 866074766..1c38ea1cc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,6 +27,7 @@ class User < ActiveRecord::Base has_many :roles, :class_name => "UserRole" has_many :issues, :class_name => "Issue", :foreign_key => :reported_user_id + has_one :issue, :class_name => "Issue", :foreign_key => :updated_by has_many :issue_comments has_many :reports diff --git a/app/views/browse/note.html.erb b/app/views/browse/note.html.erb index 1bacd27d6..c6a1f86ca 100644 --- a/app/views/browse/note.html.erb +++ b/app/views/browse/note.html.erb @@ -2,6 +2,13 @@

+ <% if @user and @user.id!=@note.author.id %> +
+ <%= link_to new_issue_url(reportable_id: @note.id, reportable_type: @note.class.name, reported_user_id: @note.author.id), :title => t('browse.note.report') do %> + <%= image_tag('notice.png', size: '10x10') %> + <% end %> +
+ <% end %> <%= t "browse.note.#{@note.status}_title", :note_name => @note.id %>

diff --git a/app/views/issues/index.html.erb b/app/views/issues/index.html.erb index 98fbbaa2d..be0a448ed 100644 --- a/app/views/issues/index.html.erb +++ b/app/views/issues/index.html.erb @@ -5,7 +5,10 @@ <% end %> <%= form_tag(issues_path, :method => :get) do %> - <%= text_field_tag :search_by_user, params[:search_by_user], placeholder: "Search by Reported User" %> + Search for a particular issue(s): + <%= select :status, nil, [['open', 0],['resolved',2],['ignored',1]],{:include_blank => "Select status"},data: { behavior: 'category_dropdown' } %> + <%= select :issue_type, nil, @issue_types,{:include_blank => "Select type"},data: { behavior: 'category_dropdown' } %> + <%= text_field_tag :search_by_user, params[:search_by_user], placeholder: "Reported User" %> <%= submit_tag "Search" %> <% end %>
@@ -15,12 +18,13 @@ - Status - Number of Reports - Last updated at - Link to instance - Reported User - + <%= sortable("status") %> + <%= sortable("report_count", "Number of Reports") %> + <%= sortable("updated_at","Last updated at") %> + <%= sortable("updated_by","Last updated by") %> + Link to reports + <%= sortable("reported_user_id","Reported User") %> + Link to reported instance @@ -28,11 +32,12 @@ <% @issues.each do |issue| %> <%= issue.status %> - <%= issue.reports.count %> - <%= issue.updated_at.strftime('%H:%M, %m/%d/%y') %> - <%= reportable_url(issue.reportable) %> + <%= issue.report_count %> + <%= issue.updated_at.strftime('%H:%M %m/%d/%y') %> + <% if issue.user_updated %> <%= issue.user_updated.display_name %> <% else %> - <% end %> + <%= reports_url(issue) %> <%= link_to issue.user.display_name , :controller => :user, :action => :view,:display_name => issue.user.display_name %> - <%= link_to "Show Reports", issue %> + <%= instance_url(issue.reportable) %> <% end %> diff --git a/app/views/issues/show.html.erb b/app/views/issues/show.html.erb index 4de7a77c6..e64eb0e71 100644 --- a/app/views/issues/show.html.erb +++ b/app/views/issues/show.html.erb @@ -39,7 +39,7 @@ <% if @related_issues.count > 1 %> <% @related_issues.each do |issue| %> <% if issue.id != @issue.id %> - <%= link_to "#{issue.reportable_type} ##{issue.reportable_id}", issue %>
+ <%= reports_url(issue) %>
<% end %> <% end %> <% else %> diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index 2fc05a365..9fb92714b 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -227,6 +227,7 @@ en-GB: reopened_by_anonymous: Reactivated by anonymous %{when} ago hidden_by: Hidden by %{user} %{when} ago + report: Report this note? query: title: Query Features introduction: Click on the map to find nearby features. @@ -943,10 +944,10 @@ en-GB: resolve: Resolve ignore: Ignore reopen: Reopen - index: + index: search: user_not_found: User does not exist - issues_not_found: No Issues against the user + issues_not_found: No such issues found create: successful_report: Your report has been registered sucessfully provide_details: Please provide the required details diff --git a/config/locales/en.yml b/config/locales/en.yml index ac4b97b82..1d6aacba2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -207,6 +207,7 @@ en: reopened_by: "Reactivated by %{user} %{when} ago" reopened_by_anonymous: "Reactivated by anonymous %{when} ago" hidden_by: "Hidden by %{user} %{when} ago" + report: Report this note? query: title: "Query Features" introduction: "Click on the map to find nearby features." @@ -916,7 +917,7 @@ en: index: search: user_not_found: User does not exist - issues_not_found: No Issues against the user + issues_not_found: No such issues found create: successful_report: Your report has been registered sucessfully provide_details: Please provide the required details diff --git a/db/migrate/20150719160821_add_report_count_to_issues.rb b/db/migrate/20150719160821_add_report_count_to_issues.rb new file mode 100644 index 000000000..f7ee556cf --- /dev/null +++ b/db/migrate/20150719160821_add_report_count_to_issues.rb @@ -0,0 +1,7 @@ +class AddReportCountToIssues < ActiveRecord::Migration + def change + add_column :issues, :report_count, :integer, :default => 0 + add_foreign_key :issues, :users, :column => :updated_by, :name => "issues_updated_by_fkey", on_delete: :cascade + add_index :issues, :updated_by + end +end diff --git a/db/structure.sql b/db/structure.sql index 5919574f4..3ee7b9ac6 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -712,7 +712,8 @@ CREATE TABLE issues ( resolved_by integer, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, - updated_by integer + updated_by integer, + report_count integer DEFAULT 0 ); @@ -2006,6 +2007,13 @@ CREATE INDEX index_issues_on_reportable_id_and_reportable_type ON issues USING b CREATE INDEX index_issues_on_reported_user_id ON issues USING btree (reported_user_id); +-- +-- Name: index_issues_on_updated_by; Type: INDEX; Schema: public; Owner: -; Tablespace: +-- + +CREATE INDEX index_issues_on_updated_by ON issues USING btree (updated_by); + + -- -- Name: index_note_comments_on_body; Type: INDEX; Schema: public; Owner: - -- @@ -2468,6 +2476,14 @@ ALTER TABLE ONLY issues ADD CONSTRAINT issues_reported_user_id_fkey FOREIGN KEY (reported_user_id) REFERENCES users(id) ON DELETE CASCADE; +-- +-- Name: issues_updated_by_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY issues + ADD CONSTRAINT issues_updated_by_fkey FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE CASCADE; + + -- -- Name: messages_from_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -2790,6 +2806,8 @@ INSERT INTO schema_migrations (version) VALUES ('20150516073616'); INSERT INTO schema_migrations (version) VALUES ('20150526130032'); +INSERT INTO schema_migrations (version) VALUES ('20150719160821'); + INSERT INTO schema_migrations (version) VALUES ('21'); INSERT INTO schema_migrations (version) VALUES ('22');