Skip to content
Snippets Groups Projects
Commit 65af49cc authored by Arnaud Levy's avatar Arnaud Levy
Browse files

inheritance refactored

parent dbf453d7
No related branches found
No related tags found
No related merge requests found
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
......@@ -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)
......
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
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment