Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit 7841d5f

Browse files
authored
Merge pull request #26 from TechforgoodCAST/dev
v0.2.9
2 parents 84c4c92 + 5c262b9 commit 7841d5f

File tree

14 files changed

+64
-7
lines changed

14 files changed

+64
-7
lines changed

app/controllers/comments_controller.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ def create
99
@comment = @commentable.comments.new(comment_params)
1010

1111
if @comment.save
12-
CommentsMailer.new_comment(@comment, @commentable.playback).deliver_now
12+
if user_signed_in?
13+
CommentsMailer.new_comment(@comment, @commentable.playback).deliver_now
14+
end
15+
1316
redirect_to(
1417
playback_path(@commentable.playback, anchor: @commentable.anchor),
1518
notice: 'Comment was successfully created.'
@@ -19,6 +22,18 @@ def create
1922
end
2023
end
2124

25+
def set_helpful
26+
@playback = Playback.find(params[:commentable_id])
27+
28+
@comment = Comment.find(params[:comment])
29+
@comment.update(helpful: params[:help])
30+
31+
redirect_to(
32+
playback_path(@playback),
33+
notice: 'Thanks for your feedback!'
34+
)
35+
end
36+
2237
private
2338

2439
def set_commentable
@@ -28,6 +43,6 @@ def set_commentable
2843
end
2944

3045
def comment_params
31-
params.require(:comment).permit(:author, :body, :done, :step_id)
46+
params.require(:comment).permit(:author, :body, :done, :step_id, :helpful)
3247
end
3348
end

app/controllers/playbacks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class PlaybacksController < ApplicationController
2-
before_action :authenticate_user!, except: %i[show pending]
2+
before_action :authenticate_user!, except: %i[show]
33
before_action :set_playback, only: %i[show edit update destroy]
44

55
def pending

app/models/comment.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
class Comment < ApplicationRecord
2+
HELPFUL = {
3+
'No' => -1,
4+
'Kind of' => 0,
5+
'Yes' => 1
6+
}.freeze
7+
28
belongs_to :commentable, polymorphic: true, touch: true
39

410
validates :author, :body, presence: true
11+
validates :helpful, inclusion: { in: HELPFUL }, on: :update
512

613
before_validation :mark_step_as_done!
714
after_create :update_comments_count!

app/views/comments/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</div>
3232

3333
<div class="notice mb30">
34-
<strong>Please note:</strong> Subject to availability, advisors aim to respond to support requests within 1-2 working days, and that messages posted here are only visible to CAST and anyone who has been given it's unique link. If you have additional questions please contact <%= mail_to 'hello@wearecast.org.uk', 'hello@wearecast.org.uk'%>.
34+
<strong>Please note:</strong> Subject to availability, advisors aim to respond to support requests within 1-2 weeks, and that messages posted here are only visible to CAST and anyone who has been given it's unique link. If you have additional questions please contact <%= mail_to 'hello@wearecast.org.uk', 'hello@wearecast.org.uk'%>.
3535
</div>
3636

3737
<%= simple_form_for @comment, url: new_comment_path(@commentable) do |f| %>

app/views/comments_mailer/new_comment.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@
1010

1111
<h3><%= link_to 'Reply here', new_comment_url(@comment.commentable) %></h3>
1212

13+
<div style="margin-bottom: 20px;">
14+
<p style="margin: 5px 0 20px; display: inline;">Was this helpful?</p>
15+
<%= link_to 'Yes', set_helpful_url(@comment.commentable, @comment.id, Comment::HELPFUL['Yes']) %> |
16+
<%= link_to 'No', set_helpful_url(@comment.commentable, @comment.id, Comment::HELPFUL['No']) %> |
17+
<%= link_to 'Kind of', set_helpful_url(@comment.commentable, @comment.id, Comment::HELPFUL['Kind of']) %>
18+
</div>
19+
1320
<small>This is an automated email for your records.</small>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010

1111
post '/webhook/new-response/:id', to: 'webhook#new_response', as: 'webhooks_new_response'
1212

13+
get '/support/:commentable_id/comments/:comment/:help', to: 'comments#set_helpful', as: 'set_helpful'
14+
1315
root to: 'playbacks#index'
1416
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddHelpfulToComments < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :comments, :helpful, :integer
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_02_06_170022) do
13+
ActiveRecord::Schema.define(version: 2019_02_18_144231) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -23,6 +23,7 @@
2323
t.datetime "created_at", null: false
2424
t.datetime "updated_at", null: false
2525
t.string "commentable_type"
26+
t.integer "helpful"
2627
t.index ["commentable_id"], name: "index_comments_on_commentable_id"
2728
end
2829

test/factories/comments.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
FactoryBot.define do
22
factory :comment do
3+
id { 1 }
34
commentable { Section.new(id: 1, name: 'State your problem') }
45
author { 'John Doe' }
56
body { 'What should I work on next?' }

test/factories/playbacks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
FactoryBot.define do
22
factory :playback do
3+
id { 1 }
34
organisation_name { 'ACME' }
45
email { 'test@wearecast.co.uk' }
56
end

test/factories/sections.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
FactoryBot.define do
22
factory :section do
33
name { 'Discover' }
4+
playback_id { 1 }
45
end
56
end

test/mailers/previews/comments_mailer_preview.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
class CommentsMailerPreview < ActionMailer::Preview
33
def new_comment
44
comment = Comment.new(
5+
id: 1,
56
author: 'John Doe',
67
body: '<div>Is <a href="#">this</a> a good resource</div>',
78
commentable: Step.new(id: 1, name: 'State your problem')

test/system/comments_test.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ class CommentsTest < ApplicationSystemTestCase
44

55
setup do
66
@user = create(:user)
7-
7+
@playback = create(:playback)
8+
@section = create(:section)
89
end
910

10-
test 'send email when comment created' do
11+
test 'send email when comment created if signed in' do
1112
visit new_playback_path
1213
sign_in
1314

1415
fill_in(:playback_organisation_name, with: 'ACME')
16+
fill_in(:playback_email, with: 'test@wearecast.co.uk')
1517

1618
click_link('Add Section')
1719
fill_in('Section name', with: 'Discover')
@@ -29,5 +31,18 @@ class CommentsTest < ApplicationSystemTestCase
2931

3032
end
3133

34+
test 'no email when comment created if not signed in' do
35+
visit playback_path(@playback)
36+
37+
click_on 'Ask for help'
38+
39+
fill_in('Author', with: 'Author')
40+
find('trix-editor').click.set('Test')
41+
42+
click_on 'Comment'
43+
44+
assert(ActionMailer::Base.deliveries.count == 0)
45+
end
46+
3247

3348
end

test/system/create_playback_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CreatePlaybackTest < ApplicationSystemTestCase
1010
sign_in
1111

1212
fill_in(:playback_organisation_name, with: 'ACME')
13+
fill_in(:playback_email, with: 'test@wearecast.co.uk')
1314

1415
click_link('Add Section')
1516
fill_in('Section name', with: 'Discover')

0 commit comments

Comments
 (0)