From 453f758f91d57688663e354a54061a65945410e7 Mon Sep 17 00:00:00 2001 From: Shrey Date: Wed, 20 May 2015 20:29:20 +0530 Subject: [PATCH] Controllers + a few tests + new notification added. Work pending on the notification view --- app/assets/javascripts/issues.coffee | 3 + app/assets/stylesheets/issues.scss | 3 + app/controllers/issues_controller.rb | 85 +++++++++++++++++++ app/helpers/issues_helper.rb | 2 + app/models/issue.rb | 1 + app/models/notifier.rb | 8 ++ app/views/issues/index.html.erb | 2 + app/views/issues/new.html.erb | 2 + app/views/issues/show.html.erb | 2 + .../notifier/new_issue_notification.html.erb | 5 ++ config/locales/en-GB.yml | 4 + config/locales/en.yml | 4 + config/routes.rb | 10 +++ test/controllers/issues_controller_test.rb | 70 +++++++++++++++ 14 files changed, 201 insertions(+) create mode 100644 app/assets/javascripts/issues.coffee create mode 100644 app/assets/stylesheets/issues.scss create mode 100644 app/controllers/issues_controller.rb create mode 100644 app/helpers/issues_helper.rb create mode 100644 app/views/issues/index.html.erb create mode 100644 app/views/issues/new.html.erb create mode 100644 app/views/issues/show.html.erb create mode 100644 app/views/notifier/new_issue_notification.html.erb create mode 100644 test/controllers/issues_controller_test.rb diff --git a/app/assets/javascripts/issues.coffee b/app/assets/javascripts/issues.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/issues.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/issues.scss b/app/assets/stylesheets/issues.scss new file mode 100644 index 000000000..391c8ce77 --- /dev/null +++ b/app/assets/stylesheets/issues.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the issues controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb new file mode 100644 index 000000000..0479e6641 --- /dev/null +++ b/app/controllers/issues_controller.rb @@ -0,0 +1,85 @@ +class IssuesController < ApplicationController + layout "site" + + before_action :find_issue, only: [:show, :resolve, :reopen, :ignore] + + def index + @issues = Issue.all + end + + def show + @read_reports = @issue.read_reports + @unread_reports = @issue.unread_reports + end + + def new + unless create_new_issue_params.blank? + @issue = Issue.find_or_initialize_by(create_new_issue_params) + end + end + + def create + @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type]) + 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 + end + end + + @report = @issue.reports.build(report_params) + + if @issue.save + redirect_to @issue, notice: 'Issue was successfully created.' + else + render :new + end + end + + # Status Transistions + def resolve + if @issue.resolve + @issue.save! + redirect_to @issue, notice: "Issue status has been set to: 'Resolved'" + else + render :show + end + end + + def ignore + if @issue.ignore + @issue.save! + redirect_to @issue, notice: "Issue status has been set to: 'Ignored'" + else + render :show + end + end + + def reopen + if @issue.reopen + @issue.save! + redirect_to @issue, notice: "Issue status has been set to: 'Open'" + else + render :show + end + end + + private + + def find_issue + @issue = Issue.find(params[:id]) + end + + def create_new_issue_params + params.permit(:reportable_id, :reportable_type, :user_id) + end + + def issue_params + params[:issue].permit(:reportable_id, :reportable_type,:user_id) + end + + def report_params + params[:report].permit(:details) + end +end diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb new file mode 100644 index 000000000..bfb9d25e5 --- /dev/null +++ b/app/helpers/issues_helper.rb @@ -0,0 +1,2 @@ +module IssuesHelper +end diff --git a/app/models/issue.rb b/app/models/issue.rb index f678e94e1..1726e690f 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -2,6 +2,7 @@ class Issue < ActiveRecord::Base belongs_to :reportable, :polymorphic => true has_many :reports validates :reportable_id, :uniqueness => { :scope => [ :reportable_type ] } + belongs_to :user_id # Check if more statuses are needed enum status: %w( open ignored resolved ) diff --git a/app/models/notifier.rb b/app/models/notifier.rb index 23f7b9907..13751560f 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -172,6 +172,13 @@ class Notifier < ActionMailer::Base end end + def new_issue_notification(recipient) + with_recipient_locale recipient do + subject = I18n.t("notifier.new_issue_notification.subject") + mail :to => recipient.email, :subject => subject + end + end + private def with_recipient_locale(recipient) @@ -187,4 +194,5 @@ class Notifier < ActionMailer::Base EMAIL_FROM end end + end diff --git a/app/views/issues/index.html.erb b/app/views/issues/index.html.erb new file mode 100644 index 000000000..83fe41be9 --- /dev/null +++ b/app/views/issues/index.html.erb @@ -0,0 +1,2 @@ +

Issues#index

+

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

diff --git a/app/views/issues/new.html.erb b/app/views/issues/new.html.erb new file mode 100644 index 000000000..83c5d0196 --- /dev/null +++ b/app/views/issues/new.html.erb @@ -0,0 +1,2 @@ +

Issues#new

+

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

diff --git a/app/views/issues/show.html.erb b/app/views/issues/show.html.erb new file mode 100644 index 000000000..1c3b8bb65 --- /dev/null +++ b/app/views/issues/show.html.erb @@ -0,0 +1,2 @@ +

Issues#show

+

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

diff --git a/app/views/notifier/new_issue_notification.html.erb b/app/views/notifier/new_issue_notification.html.erb new file mode 100644 index 000000000..f57b7c6fc --- /dev/null +++ b/app/views/notifier/new_issue_notification.html.erb @@ -0,0 +1,5 @@ +

<%= t("notifier.new_issue_notification.greeting") %>

+ +

<%= t("notifier.new_issue_notification.new_issue") %>

+ + diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index c4ea03585..101b71e2e 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -1349,6 +1349,10 @@ en-GB: partial_changeset_with_comment: with comment '%{changeset_comment}' partial_changeset_without_comment: without comment details: More details about the changeset can be found at %{url}. + new_issue_notification: + subject: "[OpenStreetMap] New Issue" + greeting: "Hi," + new_issue: "A new issue has been created:" message: inbox: title: Inbox diff --git a/config/locales/en.yml b/config/locales/en.yml index 96a35284f..3c9ec7131 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1317,6 +1317,10 @@ en: partial_changeset_with_comment: "with comment '%{changeset_comment}'" partial_changeset_without_comment: "without comment" details: "More details about the changeset can be found at %{url}." + new_issue_notification: + subject: "[OpenStreetMap] New Issue" + greeting: "Hi," + new_issue: "A new issue has been created:" message: inbox: title: "Inbox" diff --git a/config/routes.rb b/config/routes.rb index 085d67417..a1d73985d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -288,6 +288,16 @@ OpenStreetMap::Application.routes.draw do resources :user_blocks match "/blocks/:id/revoke" => "user_blocks#revoke", :via => [:get, :post], :as => "revoke_user_block" + # issues and reports + resources :issues do + member do + post "resolve" + post "assign" + post "ignore" + post "reopen" + end + end + # redactions resources :redactions end diff --git a/test/controllers/issues_controller_test.rb b/test/controllers/issues_controller_test.rb new file mode 100644 index 000000000..325e0c297 --- /dev/null +++ b/test/controllers/issues_controller_test.rb @@ -0,0 +1,70 @@ +require 'test_helper' + +class IssuesControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + + def test_new_issue + # Test creation of a new issue and a new report + get :new, {reportable_id: 1, reportable_type: "IssueOne", user: 1} + assert_response :success + assert_difference "Issue.count",1 do + details = "Details of a report" + post :create, { :report => { :deatils => details}, + :issue => { reportable_id: 1, reportable_type: "IssueOne", user: 1} } + end + assert_response :redirect + end + + def test_new_report + # Test creation of a new report for an existing issue + get :new, {reportable_id: 1, reportable_type: "IssueOne", user: 1} + assert_response :success + assert_difference "Issue.count",1 do + details = "Details of a report" + post :create, { :report => { :details => details}, + :issue => { reportable_id: 1, reportable_type: "IssueOne", user: 1} } + end + assert_response :redirect + + get :new, {reportable_id: 1, reportable_type: "IssueOne", user: 1} + assert_response :success + assert_no_difference "Issue.count" do + details = "Details of another report under the same issue" + post :create, { :report => { :details => details}, + :issue => { reportable_id: 1, reportable_type: "IssueOne", user: 1} } + end + assert_response :redirect + assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").reports.count,2 + end + + def test_change_status + # Create Issue + get :new, {reportable_id: 1, reportable_type: "IssueOne", user: 1} + assert_response :success + assert_difference "Issue.count",1 do + details = "Details of a report" + post :create, { :report => { :deatils => details}, + :issue => { reportable_id: 1, reportable_type: "IssueOne", user: 1} } + end + assert_response :redirect + + # Test 'Resolved' + get :resolve, id: Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").id + assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").resolved?, true + assert_response :redirect + + # Test 'Reopen' + get :reopen, id: Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").id + assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").open?, true + assert_response :redirect + + # Test 'Ignored' + get :ignore, id: Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").id + assert_equal Issue.find_by_reportable_id_and_reportable_type(1,"IssueOne").ignored?, true + assert_response :redirect + end + +end -- 2.43.2