Skip to content

Commit

Permalink
Attach Mefs to SchoolYears (#1075)
Browse files Browse the repository at this point in the history
Closes #1064 

- [x] New relationship
- [x] Migration + new ref
  • Loading branch information
pskl authored Aug 16, 2024
1 parent 6dcfc34 commit df08a8a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/mef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Mef < ApplicationRecord
enum :ministry, %i[menj masa armee mer]

belongs_to :school_year

scope :with_wages, -> { joins("JOIN wages ON wages.mef_codes ? mefs.code") }

validates :label, :code, :short, :mefstat11, :ministry, presence: true
Expand Down
2 changes: 1 addition & 1 deletion db/mef_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def self.seed
end
end

attributes
attributes.merge("school_year_id" => SchoolYear.current.id)
end

Mef.upsert_all(mefs, unique_by: :code) # rubocop:disable Rails/SkipsModelValidations
Expand Down
20 changes: 20 additions & 0 deletions db/migrate/20240814133039_add_school_year_to_mef.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AddSchoolYearToMef < ActiveRecord::Migration[7.2]
def up
add_reference :mefs, :school_year, foreign_key: true

current_school_year = SchoolYear.current

if current_school_year
execute <<-SQL
UPDATE mefs
SET school_year_id = #{current_school_year.id}
SQL
else
puts "Warning: No current SchoolYear found. Mef records were not updated."
end
end

def down
remove_reference :mefs, :school_year
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/factories/mefs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
short { "1ERE COLLAGE DE GOMETTES" }
mefstat11 { Faker::Number.number(digits: 11) }
ministry { Mef.ministries[:menj] }
school_year { SchoolYear.current }

transient do
daily_rate { 1 }
Expand Down

0 comments on commit df08a8a

Please sign in to comment.