Skip to content

Commit f7f277d

Browse files
committed
chore: fix merge conflict in schema.rb
1 parent 121d1a9 commit f7f277d

File tree

11 files changed

+137
-5
lines changed

11 files changed

+137
-5
lines changed

app/controllers/concerns/member_concerns.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ module InstanceMethods
1010

1111
def member_params
1212
params.require(:member).permit(
13-
:pronouns, :name, :surname, :email, :mobile, :about_you, :skill_list, :newsletter, :other_dietary_restrictions,
14-
dietary_restrictions: [],
13+
:pronouns, :name, :surname, :email, :mobile, :about_you, :skill_list, :newsletter, :other_dietary_restrictions, :other_reason, :how_you_found_us_other_reason, how_you_found_us: [], dietary_restrictions: []
1514
).tap do |params|
1615
# We want to keep Rails' hidden blank field in the form so that all dietary restrictions for a member can be
1716
# removed by submitting the form with all check boxes unticked. However, we want to remove the blank value

app/controllers/member/details_controller.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,29 @@ class Member::DetailsController < ApplicationController
88

99
def edit
1010
accept_terms
11-
1211
flash[notice] = I18n.t('notifications.signing_up')
1312
@member.newsletter ||= true
1413
end
1514

1615
def update
17-
return render :edit unless @member.update(member_params)
16+
how_found = Array(params.dig(:member, :how_you_found_us)).reject(&:blank?)
17+
other_reason = params.dig(:member, :how_you_found_us_other_reason)
18+
19+
how_found << other_reason if other_reason.present?
20+
how_found.uniq!
21+
22+
if how_found.blank?
23+
@member.assign_attributes(member_params.to_h.except(:how_you_found_us_other_reason))
24+
@member.errors.add(:how_you_found_us, 'You must select at least one option')
25+
return render :edit
26+
end
27+
28+
attrs = member_params.to_h.except(:how_you_found_us_other_reason)
29+
attrs[:how_you_found_us] = how_found
30+
31+
return render :edit unless @member.update(attrs)
1832

1933
member_params[:newsletter] ? subscribe_to_newsletter(@member) : unsubscribe_from_newsletter(@member)
2034
redirect_to step2_member_path
2135
end
22-
end
36+
end

app/controllers/members_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class MembersController < ApplicationController
99

1010
def new
1111
@page_title = 'Sign up'
12+
1213
end
1314

1415
def edit; end

app/views/member/details/edit.html.haml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@
1919
label_method: ->(r) { r.humanize.upcase_first }
2020
= f.input :other_dietary_restrictions, placeholder: 'Other dietary restrictions',
2121
wrapper_html: { class: class_names('mt-n3', 'd-none': !@member.other_dietary_restrictions?) }, label_html: { class: 'sr-only' }
22+
- if @member.errors.any?
23+
#error_explanation
24+
%h2= "#{pluralize(@member.errors.count, 'error')} prohibited this member from being saved:"
25+
%ul
26+
- @member.errors.full_messages.each do |msg|
27+
%li= msg
28+
- if @member.errors[:how_you_found_us].any?
29+
%span.text-danger= @member.errors[:how_you_found_us].first
30+
.col-12.mb-3
31+
%label{ for: 'how_you_found_us' }= t('member.details.edit.how_you_found_us')
32+
%span *
33+
- options = ['From a friend', 'Search engine (Google etc.)', 'Social Media', "One of Codebar's hosts or partners"]
34+
- options.each do |option|
35+
.form-check
36+
= check_box_tag 'member[how_you_found_us][]', option, false, id: "how_you_found_us_#{option.parameterize}", class: 'form-check-input'
37+
= label_tag "how_you_found_us_#{option.parameterize}", option, class: 'form-check-label', style: 'margin-left: 8px;'
38+
= label_tag :how_you_found_us_other_reason, t('member.details.edit.how_you_found_us_other_reason'), class: 'my-1'
39+
= text_field_tag 'member[how_you_found_us_other_reason]', nil, placeholder: "Please specify how you heard about us", class: 'form-control w-100'
2240
= f.input :newsletter, as: :boolean, checked_value: true, unchecked_value: false
2341
.text-right.mb-4
2442
= hidden_field_tag :next_page, step2_member_path(member_type: @type)

config/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ en:
442442
edit:
443443
title: Almost there...
444444
summary: We need some more details from you to finish creating your account. We use these to help run our events.
445+
how_you_found_us: "How did you find out about us?"
446+
how_you_found_us_other_reason: "Please specify (if Other)"
445447
coach:
446448
about_you: What experience do you have? What languages do you like to use? Tell us a little bit about yourself!
447449
student:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddHowYouFoundUsOptions < ActiveRecord::Migration[7.0]
2+
def change
3+
add_column :members, :how_you_found_us, :text, array: true, default: []
4+
end
5+
end

db/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@
397397
t.datetime "opt_in_newsletter_at", precision: nil
398398
t.enum "dietary_restrictions", default: [], array: true, enum_type: "dietary_restriction_enum"
399399
t.string "other_dietary_restrictions"
400+
t.text "how_you_found_us", default: [], array: true
400401
t.index ["email"], name: "index_members_on_email", unique: true
401402
end
402403

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
RSpec.describe Member::DetailsController, type: :controller do
2+
render_views
3+
let(:member) { Fabricate(:member) }
4+
5+
before do
6+
allow(controller).to receive(:current_user).and_return(member)
7+
end
8+
9+
describe 'PATCH #update' do
10+
context 'with valid params' do
11+
it 'updates how_you_found_us with checkbox options' do
12+
patch :update, params: {
13+
id: member.id,
14+
member: {
15+
how_you_found_us: ['Social Media', 'From a friend'],
16+
newsletter: 'true'
17+
}
18+
}
19+
20+
member.reload
21+
expect(member.how_you_found_us).to contain_exactly('Social Media', 'From a friend')
22+
expect(response).to redirect_to(step2_member_path)
23+
end
24+
25+
it 'adds other_reason to how_you_found_us when provided' do
26+
patch :update, params: {
27+
id: member.id,
28+
member: {
29+
how_you_found_us: ['Search engine (Google etc.)'],
30+
how_you_found_us_other_reason: 'Saw a pamphlet',
31+
newsletter: 'false'
32+
},
33+
}
34+
35+
member.reload
36+
expect(member.how_you_found_us).to contain_exactly('Search engine (Google etc.)', 'Saw a pamphlet')
37+
expect(response).to redirect_to(step2_member_path)
38+
end
39+
40+
it 'updates how_you_found_us with only other_reason' do
41+
patch :update, params: {
42+
id: member.id,
43+
member: {
44+
how_you_found_us: [],
45+
how_you_found_us_other_reason: 'At a meetup',
46+
newsletter: 'true'
47+
},
48+
}
49+
50+
member.reload
51+
expect(member.how_you_found_us).to eq(['At a meetup'])
52+
expect(response).to redirect_to(step2_member_path)
53+
end
54+
55+
it 'removes duplicates and blank entries' do
56+
patch :update, params: {
57+
id: member.id,
58+
member: {
59+
how_you_found_us: ['From a friend', '', 'From a friend'],
60+
how_you_found_us_other_reason: 'From a friend',
61+
newsletter: 'true'
62+
},
63+
}
64+
65+
member.reload
66+
expect(member.how_you_found_us).to eq(['From a friend'])
67+
expect(response).to redirect_to(step2_member_path)
68+
end
69+
end
70+
71+
context 'when update fails (invalid data)' do
72+
it 'renders the edit template' do
73+
patch :update, params: {
74+
id: member.id,
75+
member: {
76+
how_you_found_us: []
77+
}
78+
}
79+
80+
expect(response.body).to include('You must select at least one option')
81+
end
82+
end
83+
end
84+
end

spec/fabricators/member_fabricator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
surname { Faker::Name.last_name }
55
email { Faker::Internet.email }
66
about_you { Faker::Lorem.sentence }
7+
how_you_found_us { ['From a friend'] }
78
auth_services(count: 1) { Fabricate(:auth_service) }
89
accepted_toc_at { Time.zone.now }
910
end

spec/features/member_joining_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
expect(page).to have_content "Surname can't be blank"
2828
expect(page).to have_content "Email address can't be blank"
2929
expect(page).to have_content "About you can't be blank"
30+
expect(page).to have_content "You must select at least one option"
3031
end
3132

3233
scenario 'A new member details are successfully captured' do
@@ -43,6 +44,8 @@
4344
check 'Vegan'
4445
check 'Other'
4546
fill_in 'Other dietary restrictions', with: 'peanut allergy'
47+
find('#how_you_found_us_from-a-friend').click
48+
fill_in 'member_how_you_found_us_other_reason', with: 'found on a poster', id: true
4649
click_on 'Next'
4750

4851
expect(page).to have_current_path(step2_member_path)

0 commit comments

Comments
 (0)