diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e798b14..c9be8b5 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 diff --git a/app/models/member.rb b/app/models/member.rb index 75690f1..03eb70e 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -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) } @@ -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 diff --git a/test/fixtures/clubs.yml b/test/fixtures/clubs.yml new file mode 100644 index 0000000..f492b17 --- /dev/null +++ b/test/fixtures/clubs.yml @@ -0,0 +1,5 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +club: + name: the club + email: club@email.com diff --git a/test/fixtures/groups.yml b/test/fixtures/groups.yml new file mode 100644 index 0000000..2c3fbdc --- /dev/null +++ b/test/fixtures/groups.yml @@ -0,0 +1,4 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +group: + name: the group diff --git a/test/fixtures/members.yml b/test/fixtures/members.yml index e251a8d..4462183 100644 --- a/test/fixtures/members.yml +++ b/test/fixtures/members.yml @@ -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: gino.ginetti@email.com - 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 \ No newline at end of file + municipality: comunone + +minor_with_no_parents: + club: club + groups: [group] + first_name: gino + last_name: ginetti + email: gino.ginetti@email.com + 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: gino.ginetti@email.com + 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: gino.ginetti@email.com + 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: gino.ginetti@email.com + 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: gino.ginetti@email.com + 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 \ No newline at end of file diff --git a/test/mailers/medical_certificate_mailer_test.rb b/test/mailers/medical_certificate_mailer_test.rb index 40dda1e..648ddab 100644 --- a/test/mailers/medical_certificate_mailer_test.rb +++ b/test/mailers/medical_certificate_mailer_test.rb @@ -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 diff --git a/test/models/member_test.rb b/test/models/member_test.rb index 913d52f..141c455 100644 --- a/test/models/member_test.rb +++ b/test/models/member_test.rb @@ -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