Skip to content

Commit e2acfea

Browse files
committed
Add creation of complaints
1 parent 8a7c30d commit e2acfea

6 files changed

Lines changed: 102 additions & 30 deletions

File tree

app/controllers/complaints_controller.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,36 @@ def index
66
def report
77
@report_types = AppConfig.safety_center['report_types'].select { |_k, t| t['enabled'] }
88
@content_types = AppConfig.safety_center['content_types']
9+
@complaint = Complaint.new
10+
@errors = []
911
render layout: 'without_sidebar'
1012
end
1113

12-
def create; end
14+
def create
15+
@report_types = AppConfig.safety_center['report_types'].select { |_k, t| t['enabled'] }
16+
@content_types = AppConfig.safety_center['content_types']
17+
18+
complaint_params = params.permit(:report_type, :reported_url, :content_type, :user_wants_updates)
19+
comment_params = params.permit(:content)
20+
if user_signed_in?
21+
complaint_params.merge!(user: current_user, email: current_user.email)
22+
comment_params.merge!(user: current_user)
23+
else
24+
complaint_params.merge!(email: params[:email])
25+
end
26+
27+
@complaint = Complaint.new(complaint_params)
28+
if @complaint.save
29+
@comment = ComplaintComment.new(comment_params.merge(complaint: @complaint, internal: false))
30+
if @comment.save
31+
redirect_to safety_center_path # report path
32+
else
33+
@errors = @comment.errors.full_messages + @complaint.errors.full_messages
34+
render :report, status: :bad_request, layout: 'without_sidebar'
35+
end
36+
else
37+
@errors = @complaint.errors.full_messages
38+
render :report, status: :bad_request, layout: 'without_sidebar'
39+
end
40+
end
1341
end

app/models/complaint.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ class Complaint < ApplicationRecord
44

55
after_create :generate_access_token
66

7+
validates :email, presence: -> { !user.present? }
8+
validates :report_type, presence: true
9+
validates :reported_url, presence: true
10+
711
private
812

913
def generate_access_token

app/views/complaints/report.html.erb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@
1717
</p>
1818
</div>
1919

20+
<% if @errors.any? %>
21+
<div class="notice is-danger is-filled">
22+
<p>Your report could not be saved because:</p>
23+
<ul>
24+
<% @errors.each do |msg| %>
25+
<li><%= msg %></li>
26+
<% end %>
27+
</ul>
28+
</div>
29+
<% end %>
30+
2031
<div class="widget">
2132
<%= form_tag create_complaint_path, class: 'widget--body complaint-form' do %>
2233
<div class="form-group">
2334
<%= label_tag :report_type, 'What are you reporting?' %>
2435
<% @report_types.each do |key, type| %>
2536
<div class="form-radio-group">
26-
<%= radio_button_tag :report_type, key, id: "report_type_#{key}", class: 'form-radio-element' %>
37+
<%= radio_button_tag :report_type, key, id: "report_type_#{key}", class: 'form-radio-element',
38+
required: true %>
2739
<%= label_tag "report_type_#{key}", type['description'] %>
2840
</div>
2941
<% end %>
@@ -35,7 +47,7 @@
3547
Enter a URL or link to where we can find this content on our network. You can use the Copy Link button under
3648
posts to get a direct link to a post.
3749
</div>
38-
<%= text_field_tag :reported_url, nil, class: 'form-element' %>
50+
<%= text_field_tag :reported_url, nil, class: 'form-element', required: true %>
3951
</div>
4052

4153
<div class="form-group hidden js-content-type" data-report-type="illegal">
@@ -63,7 +75,7 @@
6375
</div>
6476

6577
<div class="form-group">
66-
<%= label_tag :comment, 'Tell us more' %>
78+
<%= label_tag :content, 'Tell us more' %>
6779
<div class="form-caption" data-report-type="none">
6880
Tell us any additional information you have about this report.
6981
</div>
@@ -83,18 +95,18 @@
8395
Tell us any additional information you have about this report. Is there any additional content we removed that
8496
you would like to include? Provide detailed reasoning explaining why you disagree with our classification.
8597
</div>
86-
<%= text_area_tag :comment, nil, class: 'form-element', rows: 10 %>
98+
<%= text_area_tag :content, nil, class: 'form-element', required: true, rows: 10 %>
8799
</div>
88100

89101
<div class="form-group">
90-
<%= label_tag :user_email, 'Your email address' %>
102+
<%= label_tag :email, 'Your email address' %>
91103
<div class="form-caption">
92104
This is used for abuse prevention purposes, and to contact you if we need additional information or to update
93105
you on the status of your report. If you do not wish to receive further updates about your report, please
94106
opt out below.
95107
</div>
96-
<%= text_field_tag :user_email, user_signed_in? ? current_user.email : nil, class: 'form-element',
97-
disabled: user_signed_in? %>
108+
<%= text_field_tag :email, user_signed_in? ? current_user.email : nil, class: 'form-element',
109+
disabled: user_signed_in?, required: true %>
98110
</div>
99111

100112
<div class="form-group">

config/locales/en.yml

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
# Files in the config/locales directory are used for internationalization
2-
# and are automatically loaded by Rails. If you want to use locales other
3-
# than English, add the necessary files in this directory.
4-
#
5-
# To use the locales, use `I18n.t`:
6-
#
7-
# I18n.t 'hello'
8-
#
9-
# In views, this is aliased to just `t`:
10-
#
11-
# <%= t('hello') %>
12-
#
13-
# To use a different locale, set it with `I18n.locale`:
14-
#
15-
# I18n.locale = :es
16-
#
17-
# This would use the information in config/locales/es.yml.
18-
#
19-
# To learn more, please read the Rails Internationalization guide
20-
# available at http://guides.rubyonrails.org/i18n.html.
21-
221
en:
23-
hello: "Hello world"
2+
activerecord:
3+
attributes:
4+
complaint:
5+
email: 'Your email address'
6+
reported_url: 'Reported URL'
7+
errors:
8+
models:
9+
complaint:
10+
attributes:
11+
email:
12+
blank: 'is required'
13+
reported_url:
14+
blank: 'is required (please answer "Where is this content?")'
15+
report_type:
16+
blank: 'is required (please answer "What are you reporting?")'

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@
378378
root to: 'complaints#index', as: :safety_center
379379
get 'report', to: 'complaints#report', as: :new_complaint
380380
post 'report', to: 'complaints#create', as: :create_complaint
381+
get 'report/:token', to: 'complaints#show', as: :complaint
381382
end
382383

383384
get '403', to: 'errors#forbidden'

test/controllers/complaints_controller_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'test_helper'
22

33
class ComplaintsControllerTest < ActionDispatch::IntegrationTest
4+
include Devise::Test::IntegrationHelpers
5+
46
test 'should get safety center' do
57
get safety_center_path
68
assert_response(:success)
@@ -34,4 +36,36 @@ class ComplaintsControllerTest < ActionDispatch::IntegrationTest
3436
get new_complaint_path
3537
assert_response(:success)
3638
end
39+
40+
test 'should use signed in user email if available' do
41+
sign_in users(:basic_user)
42+
try_create_report report_type: 'illegal', reported_url: 'https://example.com', content_type: 'fraud',
43+
content: 'test', email: 'something@else.com', user_wants_updates: true
44+
assert_response :found
45+
assert_not_nil assigns(:complaint)
46+
#assert_redirected_to complaint_path(@complaint.access_token)
47+
assert_equal users(:basic_user).email, assigns(:complaint).email
48+
end
49+
50+
test 'should create report for anonymous user and use email provided' do
51+
try_create_report report_type: 'illegal', reported_url: 'https://example.com', content_type: 'fraud',
52+
content: 'test', email: 'something@else.com', user_wants_updates: true
53+
assert_response :found
54+
assert_not_nil assigns(:complaint)
55+
#assert_redirected_to complaint_path(@complaint.access_token)
56+
assert_equal 'something@else.com', assigns(:complaint).email
57+
end
58+
59+
test 'should correctly validate fields for report' do
60+
try_create_report user_wants_updates: true
61+
assert_response :bad_request
62+
assert_not_nil assigns(:complaint)
63+
assert_equal 3, assigns(:errors).size
64+
end
65+
66+
private
67+
68+
def try_create_report(**params)
69+
post create_complaint_path, params: params
70+
end
3771
end

0 commit comments

Comments
 (0)