From fa2a21ece49fddb582b6732cf89bb92604907f08 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Mon, 20 Dec 2021 18:03:46 +0100 Subject: [PATCH] limit inheritance to rich texts --- app/models/concerns/with_inheritance.rb | 26 +++++-------------- app/models/education/program.rb | 2 -- .../admin/education/programs/_form.html.erb | 2 +- .../forms/_input_with_inheritance.html.erb | 6 ++--- 4 files changed, 10 insertions(+), 26 deletions(-) diff --git a/app/models/concerns/with_inheritance.rb b/app/models/concerns/with_inheritance.rb index 106b7e324..7257f9c53 100644 --- a/app/models/concerns/with_inheritance.rb +++ b/app/models/concerns/with_inheritance.rb @@ -5,22 +5,12 @@ module WithInheritance 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) + define_method :"best_#{property}" do + best(property) + end + define_method :"best_#{property}_source" do + best_source(property) + end end end end @@ -35,8 +25,6 @@ module WithInheritance 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 } + best(property)&.record end end diff --git a/app/models/education/program.rb b/app/models/education/program.rb index 0924ffff2..75f1e2574 100644 --- a/app/models/education/program.rb +++ b/app/models/education/program.rb @@ -47,8 +47,6 @@ class Education::Program < ApplicationRecord :pricing, :registration - values_with_inheritance :description - attr_accessor :skip_websites_categories_callback has_one_attached_deletable :featured_image diff --git a/app/views/admin/education/programs/_form.html.erb b/app/views/admin/education/programs/_form.html.erb index 11fe6ccbf..9c430556d 100644 --- a/app/views/admin/education/programs/_form.html.erb +++ b/app/views/admin/education/programs/_form.html.erb @@ -48,7 +48,7 @@ <h5 class="card-title mb-0"><%= t('education.program.useful_informations') %></h5> </div> <div class="card-body"> - <%= render 'admin/education/programs/forms/input_with_inheritance', f: f, property: :description %> + <%= f.input :description %> <%= render 'admin/education/programs/forms/input_with_inheritance', f: f, property: :registration %> <%= render 'admin/education/programs/forms/input_with_inheritance', f: f, property: :pricing %> <%= render 'admin/education/programs/forms/input_with_inheritance', f: f, property: :duration %> diff --git a/app/views/admin/education/programs/forms/_input_with_inheritance.html.erb b/app/views/admin/education/programs/forms/_input_with_inheritance.html.erb index 1553ecf87..50abd4dd0 100644 --- a/app/views/admin/education/programs/forms/_input_with_inheritance.html.erb +++ b/app/views/admin/education/programs/forms/_input_with_inheritance.html.erb @@ -2,8 +2,6 @@ program = f.object best_prop_value = program.public_send("best_#{property}") best_prop_source = program.public_send("best_#{property}_source") -is_rich_text = best_prop_value.is_a?(ActionText::RichText) -as = is_rich_text ? :rich_text_area : :text id = "#{property}Collapse" %> <% if best_prop_source %> @@ -19,7 +17,7 @@ id = "#{property}Collapse" </a> </div> <div class="collapse" id="<%= id %>"> - <%= f.input property, as: as, label: false %> + <%= f.input property, as: :rich_text_area, label: false %> <div class="bg-light p-2 mt-n2"> <b><%= t 'admin.inheritance.sentence_html', link: link_to(best_prop_source, [:admin, best_prop_source]) %></b><br> <%= best_prop_value %> @@ -27,5 +25,5 @@ id = "#{property}Collapse" </div> </div> <% else %> - <%= f.input property, as: as %> + <%= f.input property, as: :rich_text_area %> <% end %> -- GitLab