Skip to content
Merged
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
10 changes: 5 additions & 5 deletions app/controllers/concerns/member_concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def set_member
@member = current_user
end

def how_you_found_us_selections_valid?
how_found_present = member_params[:how_you_found_us].present?
other_reason_present = member_params[:how_you_found_us_other_reason].present?
return false if member_params[:how_you_found_us] == 'other' && !other_reason_present
return true if member_params[:how_you_found_us] == 'other' && other_reason_present
def how_you_found_us_selections_valid?(attrs)
how_found_present = attrs[:how_you_found_us].present?
other_reason_present = attrs[:how_you_found_us_other_reason].present?
return false if attrs[:how_you_found_us] == 'other' && !other_reason_present
return true if attrs[:how_you_found_us] == 'other' && other_reason_present

how_found_present != other_reason_present
end
Expand Down
11 changes: 3 additions & 8 deletions app/controllers/member/details_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,17 @@ def edit

def update
attrs = member_params
attrs[:how_you_found_us_other_reason] = nil if attrs[:how_you_found_us] != 'other'

unless how_you_found_us_selections_valid?
unless how_you_found_us_selections_valid?(attrs)
@member.errors.add(:how_you_found_us, 'You must select one option')
return render :edit
end
attrs[:how_you_found_us] = params[:member][:how_you_found_us] if params[:member][:how_you_found_us].present?

if params[:member][:how_you_found_us_other_reason].present? && attrs[:how_you_found_us] == 'other'
attrs[:how_you_found_us_other_reason] =
params[:member][:how_you_found_us_other_reason]
end
attrs[:how_you_found_us_other_reason] = nil if attrs[:how_you_found_us] != 'other'

return render :edit unless @member.update(attrs)

member_params[:newsletter] ? subscribe_to_newsletter(@member) : unsubscribe_from_newsletter(@member)
attrs[:newsletter] ? subscribe_to_newsletter(@member) : unsubscribe_from_newsletter(@member)
redirect_to step2_member_path
end
end
26 changes: 26 additions & 0 deletions spec/controllers/member/details_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@

expect(response.body).to include('You must select one option')
end

it 'validates efficiently by calling member_params only once' do
# This test verifies the fix for the performance issue where member_params
# was called multiple times (previously 6 times!):
# - Old: Line 16 called member_params, line 19 validation called it again, line 32 called it again
# - New: Line 16 calls member_params once, passes result to validation

# Mock to track how many times member_params is called
call_count = 0
allow(controller).to receive(:member_params).and_wrap_original do |method|
call_count += 1
method.call
end

patch :update, params: {
id: member.id,
member: {
how_you_found_us: 'social_media',
how_you_found_us_other_reason: '', # Empty is OK
newsletter: 'true'
}
}

# After fix: member_params is called only once
expect(call_count).to eq(1)
end
end
end
end