diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index c13b0e29f2e73bbeafc0a0594f266a3014acab5c..20f0e94f9a8d48192e6dbbcf5496a463222f4d18 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -69,7 +69,7 @@ class Communication::Website::Page < ApplicationRecord validates :slug, uniqueness: { scope: :communication_website_id } before_validation :make_path - after_save :update_children_paths if :saved_change_to_path? + after_save :update_children_paths, if: :saved_change_to_path? scope :ordered, -> { order(:position) } scope :recent, -> { order(updated_at: :desc).limit(5) } @@ -94,7 +94,7 @@ class Communication::Website::Page < ApplicationRecord end def make_path - self.path = "#{parent&.path}/#{slug}".gsub('//', '/') + self.path = "#{parent&.path}/#{slug}".gsub(/\/+/, '/') end def update_children_paths diff --git a/app/models/education/program.rb b/app/models/education/program.rb index 9d46a22aebbab5271b4648972f8468b00529f141..f0ae123ab941a38f0e054e06dbe9043b6763dc19 100644 --- a/app/models/education/program.rb +++ b/app/models/education/program.rb @@ -8,6 +8,7 @@ # ects :integer # level :integer # name :string +# path :string # position :integer default(0) # slug :string # created_at :datetime not null @@ -74,6 +75,8 @@ class Education::Program < ApplicationRecord validates_presence_of :name + before_validation :make_path + after_save :update_children_paths, if: :saved_change_to_path? after_save_commit :set_websites_categories, unless: :skip_websites_categories_callback scope :ordered, -> { order(:position) } @@ -82,6 +85,15 @@ class Education::Program < ApplicationRecord "#{name}" end + # Override from WithGithubFiles + def github_path_generated + "_programs/#{path}/index.html".gsub(/\/+/, '/') + end + + def make_path + self.path = "#{parent&.path}/#{slug}".gsub(/\/+/, '/') + end + def list_of_other_programs university.list_of_programs.reject! { |p| p[:id] == id } end diff --git a/db/migrate/20211210112555_add_path_to_education_programs.rb b/db/migrate/20211210112555_add_path_to_education_programs.rb new file mode 100644 index 0000000000000000000000000000000000000000..b2e7bb34e56e9e02c8be044110347892a064f46e --- /dev/null +++ b/db/migrate/20211210112555_add_path_to_education_programs.rb @@ -0,0 +1,5 @@ +class AddPathToEducationPrograms < ActiveRecord::Migration[6.1] + def change + add_column :education_programs, :path, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 27a6a46e36c55048519fd18a8bed19d286520535..98bd292dd3d7f73f20dcb93dc2dcc21e28984fa0 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: 2021_12_09_144737) do +ActiveRecord::Schema.define(version: 2021_12_10_112555) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -365,6 +365,7 @@ ActiveRecord::Schema.define(version: 2021_12_09_144737) do t.uuid "parent_id" t.integer "position", default: 0 t.string "slug" + t.string "path" t.index ["parent_id"], name: "index_education_programs_on_parent_id" t.index ["university_id"], name: "index_education_programs_on_university_id" end diff --git a/lib/tasks/app.rake b/lib/tasks/app.rake index 402a331c29dbdf9ddfc3b6ea91a2ed1eef9df6e9..d5d196178625107ab6b480803aba6efcd9f03540 100644 --- a/lib/tasks/app.rake +++ b/lib/tasks/app.rake @@ -52,6 +52,10 @@ namespace :app do end end end + + 10.times do + Education::Program.find_each { |p| p.update_column :path, "#{p.parent&.path}/#{p.slug}".gsub(/\/+/, '/') } + end end namespace :db do