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
18 changes: 11 additions & 7 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,22 @@ def pending_reasons
end

# rubocop:disable Rails/SkipsModelValidations
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tangential to this PR since it was here already. Does this mean validations are enabled or disabled, and is that the way we want it?

def activate
def activate(reason_deactivated: nil)
confirm_that_profile_can_be_activated!

now = Time.zone.now
is_reproof = Profile.find_by(user_id: user_id, active: true)

attrs = {
active: true,
activated_at: now,
}

attrs[:verified_at] = now unless reason_deactivated == :password_reset

transaction do
Profile.where(user_id: user_id).update_all(active: false)
update!(
active: true,
activated_at: now,
verified_at: now,
)
update!(attrs)
end
send_push_notifications if is_reproof
end
Expand Down Expand Up @@ -97,7 +101,7 @@ def activate_after_password_reset
update!(
deactivation_reason: nil,
)
activate
activate(reason_deactivated: :password_reset)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this pattern! Then we can coalesce the behavior for different reasons inside activate.

end
end

Expand Down
18 changes: 18 additions & 0 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@

expect(profile.active).to eq true
expect(profile.deactivation_reason).to eq nil
expect(profile.verified_at).to eq nil
end

it 'activates a previously verified profile after password reset' do
verified_at = Time.zone.now - 1.year
profile = create(
:profile,
user: user,
active: false,
deactivation_reason: :password_reset,
verified_at: verified_at,
)

profile.activate_after_password_reset

expect(profile.active).to eq true
expect(profile.deactivation_reason).to eq nil
expect(profile.verified_at).to eq verified_at
end

it 'does not activate a profile if it has a pending reason' do
Expand Down