-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0134db6
commit aae7c5a
Showing
3 changed files
with
75 additions
and
59 deletions.
There are no files selected for viewing
23 changes: 0 additions & 23 deletions
23
db/migrate/20230915105810_replace_correspondance_language_with_core_language.rb
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
db/migrate/20230915105810_replace_correspondence_language_with_core_language.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class ReplaceCorrespondenceLanguageWithCoreLanguage < ActiveRecord::Migration[6.1] | ||
CANTONS_LANGUAGE = { | ||
# de: [:ag, :ai, :ar, :be, :bl, :bs, :gl, :gr, :lu, :nw, :ow, :sg, :sh, :so, :sz, :tg, :ur, :zg, :zh], | ||
fr: [:fr, :ge, :ju, :ne, :vd, :vs], | ||
it: [:ti] | ||
}.freeze | ||
|
||
def up | ||
Person.where.not(correspondence_language: nil).update_all("language = correspondence_language") | ||
Person.where(correspondence_language: nil).find_each do |person| | ||
person.update!(language: infer_person_language(person)) | ||
end | ||
|
||
remove_column :people, :correspondence_language, :string, limit: 5 | ||
end | ||
|
||
def down | ||
add_column :people, :correspondence_language, :string, limit: 5 | ||
Person.update_all("correspondence_language = language") | ||
end | ||
|
||
def infer_person_language(person) | ||
return person.language if person.language.present? | ||
return person.correspondence_language if person.try(:correspondence_language).present? | ||
|
||
kv = person.send(:find_kantonalverband)&.becomes(Group::Kantonalverband) | ||
canton = kv&.kantonalverband_cantons&.first&.to_s&.downcase&.to_sym | ||
CANTONS_LANGUAGE.find { |language, cantons| cantons.include?(canton) }&.first || :de | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters