diff --git a/app/models/concerns/with_inheritance.rb b/app/models/concerns/with_inheritance.rb new file mode 100644 index 0000000000000000000000000000000000000000..106b7e324ae2f6770d2b7c108454ddac7946e638 --- /dev/null +++ b/app/models/concerns/with_inheritance.rb @@ -0,0 +1,42 @@ +module WithInheritance + extend ActiveSupport::Concern + + included do + def self.rich_text_areas_with_inheritance(*properties) + properties.each do |property| + has_rich_text property + has_inheritance_methods property + end + end + + def self.values_with_inheritance(*properties) + properties.each do |property| + has_inheritance_methods property + end + end + + def self.has_inheritance_methods(property) + define_method :"best_#{property}" do + best(property) + end + define_method :"best_#{property}_source" do + best_source(property) + end + end + end + + protected + + def best(property) + value = send(property) + value.blank? ? parent&.send("best_#{property}") : value + end + + def best_source(property) + value = send(property) + return nil if !value.blank? # There is a value, no inheritance needed + best_value = best(property) + best_value.is_a?(ActionText::RichText) ? best_value.record + : ancestors.detect { |ancestor| ancestor.public_send(property) == best_value } + end +end diff --git a/app/models/education/program.rb b/app/models/education/program.rb index 207a259a24c7a884aea9cf517699c17a670d7ca9..0924ffff2042bc7c8e73d8924102c8abb53ce7a1 100644 --- a/app/models/education/program.rb +++ b/app/models/education/program.rb @@ -32,9 +32,23 @@ class Education::Program < ApplicationRecord include WithGithubFiles include WithMenuItemTarget include WithTree - include WithRichTexts + include WithInheritance include Communication::Website::WithMedia + rich_text_areas_with_inheritance :accessibility, + :contacts, + :duration, + :evaluation, + :objectives, + :opportunities, + :other, + :pedagogy, + :prerequisites, + :pricing, + :registration + + values_with_inheritance :description + attr_accessor :skip_websites_categories_callback has_one_attached_deletable :featured_image @@ -87,10 +101,6 @@ class Education::Program < ApplicationRecord "#{name}" end - def best_description - description.blank? ? parent&.best_description : description - end - def best_featured_image(fallback: true) return featured_image if featured_image.attached? best_image = parent&.best_featured_image(fallback: false) diff --git a/app/models/education/program/with_rich_texts.rb b/app/models/education/program/with_rich_texts.rb deleted file mode 100644 index dc0ad2bb3cf657b1537bbe37b78de83c910c1ff4..0000000000000000000000000000000000000000 --- a/app/models/education/program/with_rich_texts.rb +++ /dev/null @@ -1,62 +0,0 @@ -module Education::Program::WithRichTexts - extend ActiveSupport::Concern - - included do - has_rich_text :accessibility - has_rich_text :contacts - has_rich_text :duration - has_rich_text :evaluation - has_rich_text :objectives - has_rich_text :opportunities - has_rich_text :other - has_rich_text :pedagogy - has_rich_text :prerequisites - has_rich_text :pricing - has_rich_text :registration - - def best_accessibility - accessibility.blank? ? parent&.best_accessibility : accessibility - end - - def best_contacts - contacts.blank? ? parent&.best_contacts : contacts - end - - def best_duration - duration.blank? ? parent&.best_duration : duration - end - - def best_evaluation - evaluation.blank? ? parent&.best_evaluation : evaluation - end - - def best_objectives - objectives.blank? ? parent&.best_objectives : objectives - end - - def best_opportunities - opportunities.blank? ? parent&.best_opportunities : opportunities - end - - def best_other - other.blank? ? parent&.best_other : other - end - - def best_pedagogy - pedagogy.blank? ? parent&.best_pedagogy : pedagogy - end - - def best_prerequisites - prerequisites.blank? ? parent&.best_prerequisites : prerequisites - end - - def best_pricing - pricing.blank? ? parent&.best_pricing : pricing - end - - def best_registration - registration.blank? ? parent&.best_registration : registration - end - - end -end diff --git a/app/views/admin/education/programs/show.html.erb b/app/views/admin/education/programs/show.html.erb index b3c016f7e06d71d726e8278b42f16be7ced3de8c..7c2086946282398942f0d6e4044f704bad5b54a8 100644 --- a/app/views/admin/education/programs/show.html.erb +++ b/app/views/admin/education/programs/show.html.erb @@ -45,28 +45,22 @@ <h5 class="card-title mb-0"><%= t('education.program.useful_informations') %></h5> </div> <div class="card-body"> - <% i = 0 %> - <% ['description', 'registration', 'pricing', 'duration', 'contacts', 'accessibility', 'other'].each do |prop| %> + <% ['description', 'registration', 'pricing', 'duration', 'contacts', 'accessibility', 'other'].each_with_index do |prop, index| %> <% best_prop_value = @program.public_send("best_#{prop}") - prop_value = @program.public_send(prop) + best_prop_source = @program.public_send("best_#{prop}_source") + # prop_value = @program.public_send(prop) %> - <% next if best_prop_value.blank? %> - <h3 class="h5 <%= 'mt-4' if i > 0 %>"><%= Education::Program.human_attribute_name(prop) %></h3> - <% if prop_value != best_prop_value %> - <% - if best_prop_value.is_a?(ActionText::RichText) - best_prop_source = best_prop_value.record - else - best_prop_source = @program.ancestors.detect { |ancestor| ancestor.public_send(prop) == best_prop_value } - end - %> - <h4 class="small text-muted"> - Par héritage (<%= link_to best_prop_source, [:admin, best_prop_source] %>) - </h4> - <% end %> + <% next if best_prop_value.blank? # No value at all%> + <h3 class="h5<%= ' mt-4' if index > 0 %>"> + <%= Education::Program.human_attribute_name(prop) %> + <% if best_prop_source %> + <span class="small text-muted"> + (valeur héritée de <%= link_to best_prop_source, [:admin, best_prop_source] %>) + </span> + <% end %> + </h3> <%= best_prop_value %> - <% i += 1 %> <% end %> </div> </div>