From ebf861544e13e39b4a1e10539081bcf5d724cca2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Tue, 15 Feb 2022 16:12:28 +0100
Subject: [PATCH] rename rich texts

---
 app/models/communication/website/home.rb      |  5 +--
 app/models/communication/website/page.rb      |  5 +--
 app/models/communication/website/post.rb      |  5 +--
 app/models/concerns/with_inheritance.rb       | 20 ++--------
 app/models/education/program.rb               | 26 ++++++------
 app/models/research/journal/article.rb        |  5 +--
 app/models/research/laboratory/axis.rb        |  5 +--
 app/models/university/person.rb               |  5 +--
 app/models/university/person/administrator.rb |  2 +-
 app/models/university/person/author.rb        |  2 +-
 app/models/university/person/researcher.rb    |  2 +-
 app/models/university/person/teacher.rb       |  2 +-
 .../communication/website/homes/edit.html.erb |  2 +-
 .../website/pages/_form.html.erb              |  2 +-
 .../website/posts/_form.html.erb              |  2 +-
 .../forms/_input_with_inheritance.html.erb    |  4 +-
 .../research/journal/articles/_form.html.erb  |  2 +-
 .../research/laboratory/axes/_form.html.erb   |  2 +-
 .../admin/university/people/_form.html.erb    |  2 +-
 db/migrate/20220215142845_rename_new_texts.rb | 23 +++++++++++
 db/schema.rb                                  | 40 +++++++++----------
 21 files changed, 84 insertions(+), 79 deletions(-)
 create mode 100644 db/migrate/20220215142845_rename_new_texts.rb

diff --git a/app/models/communication/website/home.rb b/app/models/communication/website/home.rb
index 10ea33883..3b55fc80c 100644
--- a/app/models/communication/website/home.rb
+++ b/app/models/communication/website/home.rb
@@ -6,7 +6,7 @@
 #  description              :text
 #  featured_image_alt       :string
 #  github_path              :text
-#  text_new                 :text
+#  text                     :text
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
 #  communication_website_id :uuid             not null, indexed
@@ -30,8 +30,7 @@ class Communication::Website::Home < ApplicationRecord
   belongs_to :university
   belongs_to :website, foreign_key: :communication_website_id
 
-  has_rich_text :text
-  has_summernote :text_new
+  has_summernote :text
 
   def to_s
     website.to_s
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 51d29b9f1..19787f39c 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -12,7 +12,7 @@
 #  position                 :integer          default(0), not null
 #  published                :boolean          default(FALSE)
 #  slug                     :string
-#  text_new                 :text
+#  text                     :text
 #  title                    :string
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
@@ -48,8 +48,7 @@ class Communication::Website::Page < ApplicationRecord
   include WithPosition
   include WithBlocks
 
-  has_rich_text :text
-  has_summernote :text_new
+  has_summernote :text
 
   belongs_to :university
   belongs_to :website,
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 9a9033fbe..21ab6e532 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -11,7 +11,7 @@
 #  published                :boolean          default(FALSE)
 #  published_at             :datetime
 #  slug                     :text
-#  text_new                 :text
+#  text                     :text
 #  title                    :string
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
@@ -38,8 +38,7 @@ class Communication::Website::Post < ApplicationRecord
   include WithMenuItemTarget
   include WithSlug # We override slug_unavailable? method
 
-  has_rich_text :text
-  has_summernote :text_new
+  has_summernote :text
 
   has_one :imported_post,
           class_name: 'Communication::Website::Imported::Post',
diff --git a/app/models/concerns/with_inheritance.rb b/app/models/concerns/with_inheritance.rb
index 279fe3389..7f7dd625c 100644
--- a/app/models/concerns/with_inheritance.rb
+++ b/app/models/concerns/with_inheritance.rb
@@ -4,8 +4,7 @@ module WithInheritance
   included do
     def self.rich_text_areas_with_inheritance(*properties)
       properties.each do |property|
-        has_rich_text property
-        has_summernote :"#{property}_new"
+        has_summernote property
 
         class_eval <<-CODE, __FILE__, __LINE__ + 1
           def best_#{property}
@@ -15,17 +14,6 @@ module WithInheritance
           def best_#{property}_source
             best_source("#{property}")
           end
-
-          def best_#{property}_new
-            value = send("#{property}_new")
-            value.blank? ? parent&.best_#{property}_new : value
-          end
-
-          def best_#{property}_new_source(is_ancestor: false)
-            value = send("#{property}_new")
-            return (is_ancestor ? self : nil) if value.present?
-            parent&.best_#{property}_new_source(is_ancestor: true)
-          end
         CODE
       end
     end
@@ -38,9 +26,9 @@ module WithInheritance
     value.blank? ? parent&.send("best_#{property}") : value
   end
 
-  def best_source(property)
+  def best_source(property, is_ancestor: false)
     value = send(property)
-    return nil if !value.blank? # There is a value, no inheritance needed
-    best(property)&.record
+    return (is_ancestor ? self : nil) if value.present?
+    parent&.send(:best_source, property, is_ancestor: true)
   end
 end
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index a30ad88fa..cec653a4a 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -3,29 +3,29 @@
 # Table name: education_programs
 #
 #  id                 :uuid             not null, primary key
-#  accessibility_new  :text
+#  accessibility      :text
 #  capacity           :integer
-#  contacts_new       :text
-#  content_new        :text
+#  contacts           :text
+#  content            :text
 #  continuing         :boolean
 #  description        :text
-#  duration_new       :text
+#  duration           :text
 #  ects               :integer
-#  evaluation_new     :text
+#  evaluation         :text
 #  featured_image_alt :string
 #  level              :integer
 #  name               :string
-#  objectives_new     :text
-#  opportunities_new  :text
-#  other_new          :text
+#  objectives         :text
+#  opportunities      :text
+#  other              :text
 #  path               :string
-#  pedagogy_new       :text
+#  pedagogy           :text
 #  position           :integer          default(0)
-#  prerequisites_new  :text
-#  pricing_new        :text
+#  prerequisites      :text
+#  pricing            :text
 #  published          :boolean          default(FALSE)
-#  registration_new   :text
-#  results_new        :text
+#  registration       :text
+#  results            :text
 #  slug               :string
 #  created_at         :datetime         not null
 #  updated_at         :datetime         not null
diff --git a/app/models/research/journal/article.rb b/app/models/research/journal/article.rb
index cb9bf3f7f..18e2717ae 100644
--- a/app/models/research/journal/article.rb
+++ b/app/models/research/journal/article.rb
@@ -10,7 +10,7 @@
 #  published_at               :datetime
 #  references                 :text
 #  slug                       :string
-#  text_new                   :text
+#  text                       :text
 #  title                      :string
 #  created_at                 :datetime         not null
 #  updated_at                 :datetime         not null
@@ -38,8 +38,7 @@ class Research::Journal::Article < ApplicationRecord
   include WithBlobs
   include WithPosition
 
-  has_rich_text :text
-  has_summernote :text_new
+  has_summernote :text
   has_one_attached :pdf
 
   belongs_to :university
diff --git a/app/models/research/laboratory/axis.rb b/app/models/research/laboratory/axis.rb
index 414cef823..11e2f3d15 100644
--- a/app/models/research/laboratory/axis.rb
+++ b/app/models/research/laboratory/axis.rb
@@ -7,7 +7,7 @@
 #  name                   :string
 #  position               :integer
 #  short_name             :string
-#  text_new               :text
+#  text                   :text
 #  created_at             :datetime         not null
 #  updated_at             :datetime         not null
 #  research_laboratory_id :uuid             not null, indexed
@@ -26,8 +26,7 @@
 class Research::Laboratory::Axis < ApplicationRecord
   include WithPosition
 
-  has_rich_text :text
-  has_summernote :text_new
+  has_summernote :text
 
   belongs_to :university
   belongs_to :laboratory, foreign_key: :research_laboratory_id
diff --git a/app/models/university/person.rb b/app/models/university/person.rb
index 5964bd039..32ff3e587 100644
--- a/app/models/university/person.rb
+++ b/app/models/university/person.rb
@@ -3,7 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
-#  biography_new     :text
+#  biography         :text
 #  description       :text
 #  email             :string
 #  first_name        :string
@@ -37,8 +37,7 @@ class University::Person < ApplicationRecord
   include WithPicture
   include WithEducation
 
-  has_rich_text :biography
-  has_summernote :biography_new
+  has_summernote :biography
 
   belongs_to :university
   belongs_to :user, optional: true
diff --git a/app/models/university/person/administrator.rb b/app/models/university/person/administrator.rb
index 45c0cf4c7..b7bca4442 100644
--- a/app/models/university/person/administrator.rb
+++ b/app/models/university/person/administrator.rb
@@ -3,7 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
-#  biography_new     :text
+#  biography         :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/author.rb b/app/models/university/person/author.rb
index 1bb44c1eb..5bf2ee2c4 100644
--- a/app/models/university/person/author.rb
+++ b/app/models/university/person/author.rb
@@ -3,7 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
-#  biography_new     :text
+#  biography         :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/researcher.rb b/app/models/university/person/researcher.rb
index 207a0919a..9ca522425 100644
--- a/app/models/university/person/researcher.rb
+++ b/app/models/university/person/researcher.rb
@@ -3,7 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
-#  biography_new     :text
+#  biography         :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/models/university/person/teacher.rb b/app/models/university/person/teacher.rb
index 18518ba92..2f75bd1ab 100644
--- a/app/models/university/person/teacher.rb
+++ b/app/models/university/person/teacher.rb
@@ -3,7 +3,7 @@
 # Table name: university_people
 #
 #  id                :uuid             not null, primary key
-#  biography_new     :text
+#  biography         :text
 #  description       :text
 #  email             :string
 #  first_name        :string
diff --git a/app/views/admin/communication/website/homes/edit.html.erb b/app/views/admin/communication/website/homes/edit.html.erb
index ca117a77c..5f6ed054c 100644
--- a/app/views/admin/communication/website/homes/edit.html.erb
+++ b/app/views/admin/communication/website/homes/edit.html.erb
@@ -10,7 +10,7 @@
           </div>
           <div class="card-body">
             <%= f.input :description %>
-            <%= f.input :text, as: :rich_text_area %>
+            <%= f.input :text, as: :summernote %>
           </div>
         </div>
       </div>
diff --git a/app/views/admin/communication/website/pages/_form.html.erb b/app/views/admin/communication/website/pages/_form.html.erb
index b7b890ce6..2781e1729 100644
--- a/app/views/admin/communication/website/pages/_form.html.erb
+++ b/app/views/admin/communication/website/pages/_form.html.erb
@@ -8,7 +8,7 @@
         <div class="card-body">
           <%= f.input :title %>
           <%= f.input :description %>
-          <%= f.input :text, as: :rich_text_area %>
+          <%= f.input :text, as: :summernote %>
           <%= f.association :related_category,
                             collection: collection_tree(@website.categories),
                             label_method: ->(p) { sanitize p[:label] },
diff --git a/app/views/admin/communication/website/posts/_form.html.erb b/app/views/admin/communication/website/posts/_form.html.erb
index 99427bb40..c82cdb4b2 100644
--- a/app/views/admin/communication/website/posts/_form.html.erb
+++ b/app/views/admin/communication/website/posts/_form.html.erb
@@ -8,7 +8,7 @@
         <div class="card-body">
           <%= f.input :title %>
           <%= f.input :description %>
-          <%= f.input :text, as: :rich_text_area %>
+          <%= f.input :text, as: :summernote %>
         </div>
       </div>
       <div class="card flex-fill w-100">
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 50abd4dd0..2fd2b5fcd 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
@@ -17,7 +17,7 @@ id = "#{property}Collapse"
       </a>
     </div>
     <div class="collapse" id="<%= id %>">
-      <%= f.input property, as: :rich_text_area, label: false %>
+      <%= f.input property, as: :summernote, 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 %>
@@ -25,5 +25,5 @@ id = "#{property}Collapse"
     </div>
   </div>
 <% else %>
-  <%= f.input property, as: :rich_text_area %>
+  <%= f.input property, as: :summernote %>
 <% end %>
diff --git a/app/views/admin/research/journal/articles/_form.html.erb b/app/views/admin/research/journal/articles/_form.html.erb
index 46bf97002..dfb583c60 100644
--- a/app/views/admin/research/journal/articles/_form.html.erb
+++ b/app/views/admin/research/journal/articles/_form.html.erb
@@ -15,7 +15,7 @@
                       } %>
           <%= f.input :abstract, as: :text, input_html: { rows: 8 } %>
           <%= f.input :pdf %>
-          <%= f.input :text, as: :rich_text_area %>
+          <%= f.input :text, as: :summernote %>
           <%= f.input :references, as: :text, input_html: { rows: 10 } %>
         </div>
       </div>
diff --git a/app/views/admin/research/laboratory/axes/_form.html.erb b/app/views/admin/research/laboratory/axes/_form.html.erb
index 1528df4da..f0476a152 100644
--- a/app/views/admin/research/laboratory/axes/_form.html.erb
+++ b/app/views/admin/research/laboratory/axes/_form.html.erb
@@ -15,7 +15,7 @@
             </div>
           </div>
           <%= f.input :description, as: :text, input_html: { rows: 2 } %>
-          <%= f.input :text, as: :rich_text_area %>
+          <%= f.input :text, as: :summernote %>
         </div>
       </div>
     </div>
diff --git a/app/views/admin/university/people/_form.html.erb b/app/views/admin/university/people/_form.html.erb
index e5781875d..57edd82ed 100644
--- a/app/views/admin/university/people/_form.html.erb
+++ b/app/views/admin/university/people/_form.html.erb
@@ -23,7 +23,7 @@
             </div>
           </div>
           <%= f.input :description %>
-          <%= f.input :biography, as: :rich_text_area %>
+          <%= f.input :biography, as: :summernote %>
         </div>
       </div>
       <div class="card flex-fill w-100">
diff --git a/db/migrate/20220215142845_rename_new_texts.rb b/db/migrate/20220215142845_rename_new_texts.rb
new file mode 100644
index 000000000..165437ba0
--- /dev/null
+++ b/db/migrate/20220215142845_rename_new_texts.rb
@@ -0,0 +1,23 @@
+class RenameNewTexts < ActiveRecord::Migration[6.1]
+  def change
+    rename_column :communication_website_homes, :text_new, :text
+    rename_column :communication_website_pages, :text_new, :text
+    rename_column :communication_website_posts, :text_new, :text
+    rename_column :research_journal_articles, :text_new, :text
+    rename_column :research_laboratory_axes, :text_new, :text
+    rename_column :university_people, :biography_new, :biography
+    rename_column :education_programs, :accessibility_new, :accessibility
+    rename_column :education_programs, :contacts_new, :contacts
+    rename_column :education_programs, :duration_new, :duration
+    rename_column :education_programs, :evaluation_new, :evaluation
+    rename_column :education_programs, :objectives_new, :objectives
+    rename_column :education_programs, :opportunities_new, :opportunities
+    rename_column :education_programs, :other_new, :other
+    rename_column :education_programs, :pedagogy_new, :pedagogy
+    rename_column :education_programs, :prerequisites_new, :prerequisites
+    rename_column :education_programs, :pricing_new, :pricing
+    rename_column :education_programs, :registration_new, :registration
+    rename_column :education_programs, :content_new, :content
+    rename_column :education_programs, :results_new, :results
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 11b72faae..6afe5bdb9 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2022_02_15_094808) do
+ActiveRecord::Schema.define(version: 2022_02_15_142845) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -141,7 +141,7 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.text "github_path"
     t.string "featured_image_alt"
     t.text "description"
-    t.text "text_new"
+    t.text "text"
     t.index ["communication_website_id"], name: "idx_comm_website_homes_on_communication_website_id"
     t.index ["university_id"], name: "index_communication_website_homes_on_university_id"
   end
@@ -304,7 +304,7 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.text "github_path"
     t.uuid "related_category_id"
     t.string "featured_image_alt"
-    t.text "text_new"
+    t.text "text"
     t.index ["about_type", "about_id"], name: "index_communication_website_pages_on_about"
     t.index ["communication_website_id"], name: "index_communication_website_pages_on_communication_website_id"
     t.index ["parent_id"], name: "index_communication_website_pages_on_parent_id"
@@ -327,7 +327,7 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.uuid "author_id"
     t.boolean "pinned", default: false
     t.string "featured_image_alt"
-    t.text "text_new"
+    t.text "text"
     t.index ["author_id"], name: "index_communication_website_posts_on_author_id"
     t.index ["communication_website_id"], name: "index_communication_website_posts_on_communication_website_id"
     t.index ["university_id"], name: "index_communication_website_posts_on_university_id"
@@ -424,19 +424,19 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.text "description"
     t.boolean "published", default: false
     t.string "featured_image_alt"
-    t.text "accessibility_new"
-    t.text "contacts_new"
-    t.text "duration_new"
-    t.text "evaluation_new"
-    t.text "objectives_new"
-    t.text "opportunities_new"
-    t.text "other_new"
-    t.text "pedagogy_new"
-    t.text "prerequisites_new"
-    t.text "pricing_new"
-    t.text "registration_new"
-    t.text "content_new"
-    t.text "results_new"
+    t.text "accessibility"
+    t.text "contacts"
+    t.text "duration"
+    t.text "evaluation"
+    t.text "objectives"
+    t.text "opportunities"
+    t.text "other"
+    t.text "pedagogy"
+    t.text "prerequisites"
+    t.text "pricing"
+    t.text "registration"
+    t.text "content"
+    t.text "results"
     t.index ["parent_id"], name: "index_education_programs_on_parent_id"
     t.index ["university_id"], name: "index_education_programs_on_university_id"
   end
@@ -491,7 +491,7 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.string "slug"
     t.boolean "published", default: false
     t.integer "position"
-    t.text "text_new"
+    t.text "text"
     t.index ["research_journal_id"], name: "index_research_journal_articles_on_research_journal_id"
     t.index ["research_journal_volume_id"], name: "index_research_journal_articles_on_research_journal_volume_id"
     t.index ["university_id"], name: "index_research_journal_articles_on_university_id"
@@ -555,7 +555,7 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
     t.string "short_name"
-    t.text "text_new"
+    t.text "text"
     t.index ["research_laboratory_id"], name: "index_research_laboratory_axes_on_research_laboratory_id"
     t.index ["university_id"], name: "index_research_laboratory_axes_on_university_id"
   end
@@ -609,7 +609,7 @@ ActiveRecord::Schema.define(version: 2022_02_15_094808) do
     t.text "description"
     t.boolean "habilitation", default: false
     t.boolean "tenure", default: false
-    t.text "biography_new"
+    t.text "biography"
     t.index ["university_id"], name: "index_university_people_on_university_id"
     t.index ["user_id"], name: "index_university_people_on_user_id"
   end
-- 
GitLab