-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Les formations héritent certains champs des diplômes (#2346)
- Loading branch information
1 parent
e3d7c4e
commit e3aad9c
Showing
17 changed files
with
295 additions
and
168 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
64 changes: 64 additions & 0 deletions
64
app/models/education/program/localization/with_inheritance.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,64 @@ | ||
module Education::Program::Localization::WithInheritance | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def self.rich_text_areas_with_inheritance(*properties) | ||
properties.each do |property| | ||
has_summernote property | ||
|
||
class_eval <<-CODE, __FILE__, __LINE__ + 1 | ||
def best_#{property} | ||
best("#{property}") | ||
end | ||
def best_#{property}_source | ||
best_source("#{property}") | ||
end | ||
CODE | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def best(property) | ||
value = public_send(property) | ||
Static.blank?(value.to_s) ? search_above(property) : value | ||
end | ||
|
||
def search_above(property) | ||
if parent.present? | ||
parent.public_send("best_#{property}") | ||
elsif diploma.present? && diploma.respond_to?(property) | ||
diploma.public_send(property) | ||
end | ||
end | ||
|
||
def best_source(property, is_ancestor: false) | ||
value = public_send(property) | ||
if Static.has_content?(value.to_s) | ||
# Le contenu vient de cette formation | ||
# Si c'est un ancêtre (via appel récursif), c'est bien la meilleure source | ||
# Si ce n'est pas un ancêtre, c'est que la formation a sa propre donnée, et il n'y a pas de meilleure source | ||
is_ancestor ? self : nil | ||
else | ||
search_source_above(property) | ||
end | ||
end | ||
|
||
def search_source_above(property) | ||
if parent.present? | ||
# Appel récursif, on n'a pas trouvé, on remonte la parentèle | ||
parent&.send(:best_source, property, is_ancestor: true) | ||
elsif is_diploma_source?(property) | ||
diploma | ||
end | ||
end | ||
|
||
def is_diploma_source?(property) | ||
return false if diploma.nil? | ||
return false unless diploma.respond_to?(property) | ||
value = diploma.public_send(property) | ||
Static.has_content?(value.to_s) | ||
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
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
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
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
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
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
Oops, something went wrong.