Skip to content

Commit

Permalink
add validation and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
baldarn committed Aug 28, 2024
1 parent f8f680d commit 1f89c8c
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 5 deletions.
18 changes: 17 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-08-20 08:53:30 UTC using RuboCop version 1.64.1.
# on 2024-08-24 12:41:44 UTC using RuboCop version 1.65.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 1
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Exclude:
- 'app/controllers/members_controller.rb'

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 11

# Offense count: 2
# Configuration parameters: Include.
# Include: app/controllers/**/*.rb, app/mailers/**/*.rb
Expand Down
45 changes: 45 additions & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class Member < ApplicationRecord
:address,
:postal_code,
:email,
:tax_code,
:municipality, presence: true

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :groups, length: { minimum: 1 }

validate :at_least_a_parent?, if: -> { born_at && born_at >= (Time.zone.today - 18.years) }

scope :with_expiring_medical_certificate,
-> { where(medical_certificate_expires_at: Time.zone.today.beginning_of_day..2.months.from_now) }

Expand Down Expand Up @@ -76,4 +79,46 @@ def payments_status

'ok'
end

private

def at_least_a_parent?
return true if first_parent_present? || second_parent_present?

errors.add(:at_least_a_parent, 'add at least a parent')
end

def first_parent_present?
if first_parent_first_name.present? &&
first_parent_last_name.present? &&
first_parent_born_at.present? &&
first_parent_born_in.present? &&
first_parent_citizenship.present? &&
first_parent_address.present? &&
first_parent_postal_code.present? &&
first_parent_email.present? &&
first_parent_tax_code.present? &&
first_parent_municipality.present?
return true
end

false
end

def second_parent_present?
if second_parent_first_name.present? &&
second_parent_last_name.present? &&
second_parent_born_at.present? &&
second_parent_born_in.present? &&
second_parent_citizenship.present? &&
second_parent_address.present? &&
second_parent_postal_code.present? &&
second_parent_email.present? &&
second_parent_tax_code.present? &&
second_parent_municipality.present?
return true
end

false
end
end
5 changes: 5 additions & 0 deletions test/fixtures/clubs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

club:
name: the club
email: [email protected]
4 changes: 4 additions & 0 deletions test/fixtures/groups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

group:
name: the group
76 changes: 73 additions & 3 deletions test/fixtures/members.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,83 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

gino:
adult:
club: club
groups: [group]
first_name: gino
last_name: ginetti
email: [email protected]
born_at: 2000-01-02
born_at: <%= Time.zone.today - 20.years %>
born_in: comunino
citizenship: italiano
address: via delle vie 123 R
postal_code: 12345
tax_code: TAXCODE
province: provinciona
municipality: comunone
municipality: comunone

minor_with_no_parents:
club: club
groups: [group]
first_name: gino
last_name: ginetti
email: [email protected]
born_at: <%= Time.zone.today - 17.years %>
born_in: comunino
citizenship: italiano
address: via delle vie 123 R
postal_code: 12345
tax_code: TAXCODE
province: provinciona
municipality: comunone

minor_with_first_parent:
club: club
groups: [group]
first_name: gino
last_name: ginetti
email: [email protected]
born_at: <%= Time.zone.today - 17.years %>
born_in: comunino
citizenship: italiano
address: via delle vie 123 R
postal_code: 12345
tax_code: TAXCODE
province: provinciona
municipality: comunone
first_parent_first_name: gino
first_parent_last_name: ginetti
first_parent_email: [email protected]
first_parent_born_at: <%= Time.zone.today - 30.years %>
first_parent_born_in: comunino
first_parent_tax_code: TAX_CODE
first_parent_citizenship: italiano
first_parent_address: via delle vie 123 R
first_parent_postal_code: 12345
first_parent_province: provinciona
first_parent_municipality: comunone

minor_with_second_parent:
club: club
groups: [group]
first_name: gino
last_name: ginetti
email: [email protected]
born_at: <%= Time.zone.today - 17.years %>
born_in: comunino
citizenship: italiano
address: via delle vie 123 R
postal_code: 12345
province: provinciona
tax_code: TAXCODE
municipality: comunone
second_parent_first_name: gino
second_parent_last_name: ginetti
second_parent_email: [email protected]
second_parent_born_at: <%= Time.zone.today - 30.years %>
second_parent_born_in: comunino
second_parent_tax_code: TAX_CODE
second_parent_citizenship: italiano
second_parent_address: via delle vie 123 R
second_parent_postal_code: 12345
second_parent_province: provinciona
second_parent_municipality: comunone
2 changes: 1 addition & 1 deletion test/mailers/medical_certificate_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class MedicalCertificateMailerTest < ActionMailer::TestCase
test 'expiring_email' do
member = members(:gino)
member = members(:adult)
email = MedicalCertificateMailer.with(member:).expiring_email

assert_emails 1 do
Expand Down
7 changes: 7 additions & 0 deletions test/models/member_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ class MemberTest < ActiveSupport::TestCase
assert(member.errors.key?('address'))
assert(member.errors.key?('postal_code'))
end

test 'has at least a parent if minor' do
assert_not(members(:minor_with_no_parents).valid?)

assert(members(:minor_with_first_parent).valid?)
assert(members(:minor_with_second_parent).valid?)
end
end

0 comments on commit 1f89c8c

Please sign in to comment.