Added sortable headers + search + reportable Notes
authorShrey <shrey14099@iiitd.ac.in>
Mon, 20 Jul 2015 16:52:41 +0000 (22:22 +0530)
committerMatt Amos <zerebubuth@gmail.com>
Mon, 22 Aug 2016 15:18:15 +0000 (16:18 +0100)
app/controllers/issues_controller.rb
app/helpers/issues_helper.rb
app/models/issue.rb
app/models/user.rb
app/views/browse/note.html.erb
app/views/issues/index.html.erb
app/views/issues/show.html.erb
config/locales/en-GB.yml
config/locales/en.yml
db/migrate/20150719160821_add_report_count_to_issues.rb [new file with mode: 0644]
db/structure.sql

index beb1425cd3775744b3bd42554c3c34138034c219..ecac5bd1677bc324a1c70dd6a4cff119ea727a54 100644 (file)
@@ -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
index ac5bd59cdb28a4f0b7e917a7fe89d9e61e4f7492..bcae2e224a9d62e3e2cc06c3fd8935a6718aafa9 100644 (file)
@@ -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
index 0abc27063de4c547eb212a3642b8d5e696b124ce..690a024772038c1b2aab89243fe155e1257a61c2 100644 (file)
@@ -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
index 866074766e462c176c2c18a4b5814c1a9aba81b3..1c38ea1ccdac3708f336381c77c0ce4414179f04 100644 (file)
@@ -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
index 1bacd27d6b2dc3db27b476e2dc06d860a1cf78ef..c6a1f86cafa9292c2c34036d56c5dd0c9f744f42 100644 (file)
@@ -2,6 +2,13 @@
 
 <h2>
   <a class="geolink" href="<%= root_path %>"><span class="icon close"></span></a>
+  <% if @user and @user.id!=@note.author.id %>
+    <div class="report-button">
+      <%= 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 %>
+    </div>
+  <% end %>
   <%= t "browse.note.#{@note.status}_title", :note_name => @note.id %>
 </h2>
 
index 98fbbaa2d345525ffc4e266fcc0c6fda4bd75cc5..be0a448edfc26ce3db95e1cb5406ae8da4c53aae 100644 (file)
@@ -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 %>
 <br/>
        <thead>
                <tr>
                        <tr>
-                               <td><b> Status </b></td>
-                               <td><b> Number of Reports</b></td>
-                               <td><b> Last updated at</b></td>
-                               <td><b> Link to instance </b></td>
-                               <td><b> Reported User </b></td>
-                               <td></td>
+                               <td><b> <%= sortable("status") %></b></td>
+                               <td><b> <%= sortable("report_count", "Number of Reports") %></b></td>
+                               <td><b> <%= sortable("updated_at","Last updated at") %></b></td>
+                               <td><b> <%= sortable("updated_by","Last updated by") %></b></td>
+                               <td><b> Link to reports </b></td>
+                               <td><b> <%= sortable("reported_user_id","Reported User") %> </b></td>
+                               <td><b> Link to reported instance</b></td>
                        </tr>
                </tr>
        </thead>
                <% @issues.each do |issue| %>
                        <tr>
                                <td><span class="count-number"> <strong><%= issue.status %></strong></span> </td>
-                               <td><%= issue.reports.count %></td>
-                               <td><%= issue.updated_at.strftime('%H:%M, %m/%d/%y') %></td>
-                               <td> <%= reportable_url(issue.reportable) %></td>
+                               <td><%= issue.report_count %></td>
+                               <td><%= issue.updated_at.strftime('%H:%M %m/%d/%y') %></td>
+                               <td><% if issue.user_updated %> <%= issue.user_updated.display_name %> <% else %> - <% end %></td>
+                               <td> <%= reports_url(issue) %></td>
                                <td><%= link_to issue.user.display_name , :controller => :user, :action => :view,:display_name => issue.user.display_name %></td>
-                               <td><b><%= link_to "Show Reports", issue %></b></td>
+                               <td><%= instance_url(issue.reportable) %></td>
                        </tr>
                <% end %>
        </tbody>
index 4de7a77c6efb795bf0975c6915030e67828c75e3..e64eb0e718464bd9aa30cb5229b9858d09396c71 100644 (file)
@@ -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 %> <br/>
+                                                       <%= reports_url(issue) %> <br/>
                                                <% end %>
                                        <% end %>
                                <% else %>
index 2fc05a3654a799827bb523a4b1d34fec8398dafa..9fb92714b1e21a4d99f1ca030bc79543d46a3ac0 100644 (file)
@@ -227,6 +227,7 @@ en-GB:
       reopened_by_anonymous: Reactivated by anonymous <abbr title='%{exact_time}'>%{when}
         ago</abbr>
       hidden_by: Hidden by %{user} <abbr title='%{exact_time}'>%{when} ago</abbr>
+      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
index ac4b97b8229cdf638a276c9650a04ba6b05415f9..1d6aacba23692e6a6221d622d6ae52f27352c1be 100644 (file)
@@ -207,6 +207,7 @@ en:
       reopened_by: "Reactivated by %{user} <abbr title='%{exact_time}'>%{when} ago</abbr>"
       reopened_by_anonymous: "Reactivated by anonymous <abbr title='%{exact_time}'>%{when} ago</abbr>"
       hidden_by: "Hidden by %{user} <abbr title='%{exact_time}'>%{when} ago</abbr>"
+      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 (file)
index 0000000..f7ee556
--- /dev/null
@@ -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
index 5919574f45f9ba170583149b57882e547265eda9..3ee7b9ac6cd8d7734f9c03d8f71438e64b161380 100644 (file)
@@ -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');