Skip to content

Commit 22910d6

Browse files
authored
Return boolean from AccountTypes#student_account? (#91)
Previously this method returned either `0` or `nil`. This works fine in Ruby, because `0` is truth-y, but it causes problems if you want to persist the value in a Postgres boolean type, because `0` is cast to `FALSE`. The new implementation always returns either `true` or `false`. This will mean we can remove [this workaround][1] in `experience-cs`. [1]: https://github.com/RaspberryPiFoundation/experience-cs/commit/130592b833509a52aac1511bb17320a4d7eadeb4
2 parents d8559a2 + 55fea6b commit 22910d6

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Fixed
1515

16+
- Return boolean from `AccountTypes#student_account?` (#91)
17+
1618
### Removed
1719

1820
## [v4.2.1]

lib/rpi_auth/models/account_types.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ module AccountTypes
1010
include Authenticatable
1111

1212
def student_account?
13-
user_id =~ /^#{STUDENT_PREFIX}/o
13+
return false if user_id.blank?
14+
15+
user_id.start_with?(STUDENT_PREFIX)
1416
end
1517
end
1618
end

spec/rpi_auth/models/account_types_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@
1616
context "when user_id has the 'student:' prefix" do
1717
let(:user_id) { RpiAuth::Models::AccountTypes::STUDENT_PREFIX + SecureRandom.uuid }
1818

19-
it 'returns truthy' do
20-
expect(user).to be_student_account
19+
it 'returns true' do
20+
expect(user.student_account?).to be(true)
2121
end
2222
end
2323

2424
context "when user_id does not have the 'student:' prefix" do
2525
let(:user_id) { SecureRandom.uuid }
2626

27-
it 'returns falsey' do
28-
expect(user).not_to be_student_account
27+
it 'returns false' do
28+
expect(user.student_account?).to be(false)
29+
end
30+
end
31+
32+
context 'when user_id is not set' do
33+
let(:user_id) { nil }
34+
35+
it 'returns false' do
36+
expect(user.student_account?).to be(false)
2937
end
3038
end
3139
end

0 commit comments

Comments
 (0)