From 39e0aa25e0f41bc52eca8ec61065f18857bdccf9 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 15 Nov 2019 15:11:38 +0800 Subject: [PATCH] laboratory: library data && data migration --- app/controllers/libraries_controller.rb | 2 +- app/models/laboratory.rb | 1 + app/models/library.rb | 8 ++++++++ .../20191115070508_add_laboratory_id_to_libraries.rb | 5 +++++ db/migrate/20191115070841_transfer_laboratory_data.rb | 7 +++++++ 5 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20191115070508_add_laboratory_id_to_libraries.rb create mode 100644 db/migrate/20191115070841_transfer_laboratory_data.rb diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index a2e70d95a..883f8a3aa 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -7,7 +7,7 @@ class LibrariesController < ApplicationController helper_method :current_library, :library_manageable? def index - libraries = Library.all + libraries = current_laboratory.libraries libraries = if User.current&.logged? && params[:type] == 'mine' diff --git a/app/models/laboratory.rb b/app/models/laboratory.rb index 45ef0c76e..466e6d4d8 100644 --- a/app/models/laboratory.rb +++ b/app/models/laboratory.rb @@ -16,6 +16,7 @@ class Laboratory < ApplicationRecord has_many :courses, dependent: :destroy has_many :competitions, dependent: :destroy + has_many :libraries, dependent: :destroy validates :identifier, uniqueness: { case_sensitive: false }, allow_nil: true diff --git a/app/models/library.rb b/app/models/library.rb index e2bc20989..743959d30 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -3,6 +3,7 @@ class Library < ApplicationRecord belongs_to :user belongs_to :cover, class_name: 'Attachment', foreign_key: :cover_id, optional: true + belongs_to :laboratory, optional: true has_many :library_applies, dependent: :delete_all has_many :library_library_tags, dependent: :delete_all @@ -34,6 +35,13 @@ class Library < ApplicationRecord end end + before_save :set_laboratory + private def set_laboratory + return unless new_record? + + self.laboratory = Laboratory.current if laboratory_id.blank? + end + def increment_visited_count!(num = 1) increment_column!(:visited_count, num) end diff --git a/db/migrate/20191115070508_add_laboratory_id_to_libraries.rb b/db/migrate/20191115070508_add_laboratory_id_to_libraries.rb new file mode 100644 index 000000000..f067908fc --- /dev/null +++ b/db/migrate/20191115070508_add_laboratory_id_to_libraries.rb @@ -0,0 +1,5 @@ +class AddLaboratoryIdToLibraries < ActiveRecord::Migration[5.2] + def change + add_reference :libraries, :laboratory + end +end diff --git a/db/migrate/20191115070841_transfer_laboratory_data.rb b/db/migrate/20191115070841_transfer_laboratory_data.rb new file mode 100644 index 000000000..4812ade30 --- /dev/null +++ b/db/migrate/20191115070841_transfer_laboratory_data.rb @@ -0,0 +1,7 @@ +class TransferLaboratoryData < ActiveRecord::Migration[5.2] + def change + Course.where(laboratory_id: nil).update_all(laboratory_id: 1) + Competition.where(laboratory_id: nil).update_all(laboratory_id: 1) + Library.where(laboratory_id: nil).update_all(laboratory_id: 1) + end +end