Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/concerns/member_concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def member_params
if permitted_params[:dietary_restrictions]
permitted_params[:dietary_restrictions] = permitted_params[:dietary_restrictions].reject(&:blank?)
end
# The checkbox submits the raw string "true"/"false". Coerce to a real
# boolean so the re-rendered checkbox reflects the user's choice and the
# subscribe/unsubscribe decision evaluates correctly ("false" is truthy).
permitted_params[:newsletter] = ActiveModel::Type::Boolean.new.cast(permitted_params[:newsletter])
end
end

Expand Down
48 changes: 48 additions & 0 deletions spec/controllers/member/details_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,54 @@
expect(member.how_you_found_us_other_reason).to eq('From a colleague')
expect(response).to redirect_to(step2_member_path)
end

it 'subscribes to the newsletter when checked' do
patch :update, params: {
id: member.id,
member: {
how_you_found_us: 'social_media',
newsletter: 'true'
}
}

expect(mailing_list).to have_received(:subscribe)
expect(mailing_list).not_to have_received(:unsubscribe)
end

it 'unsubscribes from the newsletter when unchecked' do
patch :update, params: {
id: member.id,
member: {
how_you_found_us: 'social_media',
newsletter: 'false'
}
}

expect(mailing_list).to have_received(:unsubscribe)
expect(mailing_list).not_to have_received(:subscribe)
end
end

context 'with a validation failure' do
it 'keeps the newsletter checkbox checked when it was checked' do
patch :update, params: {
id: member.id,
member: { newsletter: 'true' }
}

expect(response.body).to include('You must select one option')
expect(response.body).to have_css('input#member_newsletter[checked]')
end

it 'keeps the newsletter checkbox unchecked when it was unchecked' do
patch :update, params: {
id: member.id,
member: { newsletter: 'false' }
}

expect(response.body).to include('You must select one option')
expect(response.body).to have_no_css('input#member_newsletter[checked]')
end
end

context 'when update fails (invalid data)' do
Expand Down