Skip to content

Commit 57bc6e0

Browse files
committed
fix: validation and styling
Move validation from model to controller so validation doesn't block new user creation and is only valid for the member details page.
1 parent 9ed57f6 commit 57bc6e0

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

app/controllers/member/details_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ def edit
1515
def update
1616
attrs = member_params.to_h
1717

18+
if attrs[:how_you_found_us].blank?
19+
@member.assign_attributes(attrs)
20+
@member.errors.add(:how_you_found_us, 'You must select at least one option')
21+
return render :edit
22+
end
23+
1824
how_found = params.dig(:member, :how_you_found_us)
1925
attrs[:how_you_found_us] =
2026
if how_found.is_a?(Array)
@@ -37,4 +43,4 @@ def update
3743
member_params[:newsletter] ? subscribe_to_newsletter(@member) : unsubscribe_from_newsletter(@member)
3844
redirect_to step2_member_path
3945
end
40-
end
46+
end

app/controllers/members_controller.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ def new
1212

1313
end
1414

15-
def edit
16-
binding.pry
17-
end
15+
def edit; end
1816

1917
def step2
2018
@type = cookies[:member_type]

app/models/member.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class Member < ApplicationRecord
1919
validates :name, :surname, :email, :about_you, presence: true, if: :can_log_in?
2020
validates :email, uniqueness: true
2121
validates :about_you, length: { maximum: 255 }
22-
validate :how_you_found_us_must_have_at_least_one_option
2322

2423
scope :accepted_toc, -> { where.not(accepted_toc_at: nil) }
2524
scope :order_by_email, -> { order(:email) }
@@ -126,12 +125,6 @@ def self.find_members_by_name(name)
126125

127126
private
128127

129-
def how_you_found_us_must_have_at_least_one_option
130-
if how_you_found_us.blank? || how_you_found_us.reject(&:blank?).empty?
131-
errors.add(:how_you_found_us, 'You must select at least one option')
132-
end
133-
end
134-
135128
def invitations_on(date)
136129
workshop_invitations
137130
.joins(:workshop)

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@
1515
= f.input :about_you, as: :text, label: t('member.details.edit.coach.about_you'), input_html: { rows: 3 }, required: true
1616
- else
1717
= f.input :about_you, as: :text, label: t('member.details.edit.student.about_you'), input_html: { rows: 3 }, required: true
18-
- if @member.errors.any?
19-
#error_explanation
20-
%h2= "#{pluralize(@member.errors.count, 'error')} prohibited this member from being saved:"
21-
%ul
22-
- @member.errors.full_messages.each do |msg|
23-
%li= msg
18+
- if @member.errors[:how_you_found_us].any?
19+
%span.text-danger= @member.errors[:how_you_found_us].first
2420
.col-12.mb-3
2521
%label{ for: 'how_you_found_us' }= t('member.details.edit.how_you_found_us')
22+
%span *
2623
- options = ['From a friend', 'Search engine (Google etc.)', 'Social Media', "One of Codebar's hosts or partners"]
2724
- options.each do |option|
2825
.form-check
2926
= check_box_tag 'member[how_you_found_us][]', option, false, id: "how_you_found_us_#{option.parameterize}", class: 'form-check-input'
3027
= label_tag "how_you_found_us_#{option.parameterize}", option, class: 'form-check-label', style: 'margin-left: 8px;'
3128
= label_tag :other_reason, t('member.details.edit.other_reason'), class: 'my-1'
32-
= text_field_tag :other_reason, nil, placeholder: "Please specify", style: 'width: 500px;'
29+
= text_field_tag :other_reason, nil, placeholder: "Please specify how you heard about us", class: 'form-control w-100;'
3330
= f.input :newsletter, as: :boolean, checked_value: true, unchecked_value: false
3431
.text-right.mb-4
3532
= hidden_field_tag :next_page, step2_member_path(member_type: @type)

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
@@ -40,6 +41,8 @@
4041
fill_in 'member_surname', with: 'Doe'
4142
fill_in 'member_email', with: 'jane@codebar.io'
4243
fill_in 'member_about_you', with: Faker::Lorem.paragraph
44+
find('#how_you_found_us_from-a-friend').click
45+
fill_in 'other_reason', with: 'found on a poster', id: true
4346
click_on 'Next'
4447

4548
expect(page).to have_current_path(step2_member_path)

0 commit comments

Comments
 (0)