From e86200b94219322e9d3c6e5fc136bb42e2f133d7 Mon Sep 17 00:00:00 2001 From: Taylor Griffin Date: Sun, 1 Feb 2015 17:32:49 +1100 Subject: [PATCH] requester receives a message when crop request is rejected --- app/controllers/crops_controller.rb | 3 ++- app/mailers/notifier.rb | 5 ++++ app/views/crops/_form.html.haml | 5 ++++ .../notifier/crop_request_rejected.html.haml | 7 +++++ ...62506_add_reason_for_rejection_to_crops.rb | 5 ++++ db/schema.rb | 13 +++++----- spec/factories/crop.rb | 5 ++++ spec/mailers/notifier_spec.rb | 26 +++++++++++++++++++ 8 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 app/views/notifier/crop_request_rejected.html.haml create mode 100644 db/migrate/20150201062506_add_reason_for_rejection_to_crops.rb diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index a4528ca19..5f859b4de 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -141,6 +141,7 @@ class CropsController < ApplicationController requester = @crop.requester new_status = @crop.approval_status Notifier.crop_request_approved(requester, @crop).deliver! if new_status == "approved" + Notifier.crop_request_rejected(requester, @crop).deliver! if new_status == "rejected" end format.html { redirect_to @crop, notice: 'Crop was successfully updated.' } format.json { head :no_content } @@ -166,6 +167,6 @@ class CropsController < ApplicationController private def crop_params - params.require(:crop).permit(:en_wikipedia_url, :name, :parent_id, :creator_id, :approval_status, :scientific_names_attributes => [:scientific_name]) + params.require(:crop).permit(:en_wikipedia_url, :name, :parent_id, :creator_id, :approval_status, :reason_for_rejection, :scientific_names_attributes => [:scientific_name]) end end diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index 81c7352ed..88c5f50f5 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -32,4 +32,9 @@ class Notifier < ActionMailer::Base mail(:to => @member.email, :subject => "#{crop.name.capitalize} has been approved") end + def crop_request_rejected(member, crop) + @member, @crop = member, crop + mail(:to => @member.email, :subject => "#{crop.name.capitalize} has been rejected") + end + end diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index 17473f13d..284faa0eb 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -46,6 +46,11 @@ .col-md-8 = f.select(:approval_status, ['rejected', 'pending', 'approved'], {}, {:class => 'form-control'}) + .form-group + = f.label :reason_for_rejection, 'Reason for rejection', :class => 'control-label col-md-2' + .col-md-8= f.text_area :reason_for_rejection, :rows => 6, :class => 'form-control' + + .form-group .form-actions.col-md-offset-2.col-md-8 = f.submit 'Save', :class => 'btn btn-primary' diff --git a/app/views/notifier/crop_request_rejected.html.haml b/app/views/notifier/crop_request_rejected.html.haml new file mode 100644 index 000000000..315f7036d --- /dev/null +++ b/app/views/notifier/crop_request_rejected.html.haml @@ -0,0 +1,7 @@ +- site_name = ENV['GROWSTUFF_SITE_NAME'] +%p Hello #{@member.login_name}, + +%p + Your request for the new crop: #{link_to @crop.name, crop_url(@crop)} has been rejected for the following reason. + + = @crop.reason_for_rejection \ No newline at end of file diff --git a/db/migrate/20150201062506_add_reason_for_rejection_to_crops.rb b/db/migrate/20150201062506_add_reason_for_rejection_to_crops.rb new file mode 100644 index 000000000..0fd283a67 --- /dev/null +++ b/db/migrate/20150201062506_add_reason_for_rejection_to_crops.rb @@ -0,0 +1,5 @@ +class AddReasonForRejectionToCrops < ActiveRecord::Migration + def change + add_column :crops, :reason_for_rejection, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 951353dc9..554190279 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150201053200) do +ActiveRecord::Schema.define(version: 20150201062506) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -62,16 +62,17 @@ ActiveRecord::Schema.define(version: 20150201053200) do end create_table "crops", force: true do |t| - t.string "name", null: false + t.string "name", null: false t.string "en_wikipedia_url" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "slug" t.integer "parent_id" - t.integer "plantings_count", default: 0 + t.integer "plantings_count", default: 0 t.integer "creator_id" t.integer "requester_id" - t.string "approval_status", default: "approved" + t.string "approval_status", default: "approved" + t.text "reason_for_rejection" end add_index "crops", ["name"], name: "index_crops_on_name", using: :btree diff --git a/spec/factories/crop.rb b/spec/factories/crop.rb index ec519fa1e..ed4aefe54 100644 --- a/spec/factories/crop.rb +++ b/spec/factories/crop.rb @@ -59,6 +59,11 @@ FactoryGirl.define do name "Ultra berry" end + factory :rejected_crop do + name "Fail bean" + reason_for_rejection "Totally fake" + end + end end diff --git a/spec/mailers/notifier_spec.rb b/spec/mailers/notifier_spec.rb index 0a813bdc5..dc7a4a63e 100644 --- a/spec/mailers/notifier_spec.rb +++ b/spec/mailers/notifier_spec.rb @@ -97,5 +97,31 @@ describe Notifier do end + describe "crop rejected" do + let(:member) { FactoryGirl.create(:member) } + let(:crop) { FactoryGirl.create(:rejected_crop) } + let(:mail) { Notifier.crop_request_rejected(member, crop) } + + it 'sets the subject correctly' do + expect(mail.subject).to eq "Fail bean has been rejected" + end + + it 'comes from noreply@growstuff.org' do + expect(mail.from).to eq ['noreply@growstuff.org'] + end + + it 'sends the mail to the recipient of the notification' do + expect(mail.to).to eq [member.email] + end + + it 'includes the rejected crop URL' do + expect(mail.body.encoded).to match crop_url(crop) + end + + it 'includes the reason for rejection' do + expect(mail.body.encoded).to match "Totally fake" + end + end + end