diff --git a/app/models/attachment.rb b/app/models/attachment.rb index e4e37a8f3..e9fbad11d 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -28,6 +28,7 @@ class Attachment < ActiveRecord::Base belongs_to :softapplication, foreign_key: 'container_id', conditions: "attachments.container_type = 'Softapplication'" belongs_to :author, :class_name => "User", :foreign_key => "author_id" belongs_to :attachmentstype, :foreign_key => "attachtype",:primary_key => "id" + belongs_to :resource_bank # 被ForgeActivity虚拟关联 has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy has_many :forwards, :as => :from, :dependent => :destroy diff --git a/app/models/resource_bank.rb b/app/models/resource_bank.rb new file mode 100644 index 000000000..b2d9b29c6 --- /dev/null +++ b/app/models/resource_bank.rb @@ -0,0 +1,5 @@ +class ResourceBank < ActiveRecord::Base + belongs_to :course + belongs_to :user + attr_accessible :course_id, :user_id, :applicable_syllabus, :copy_from, :description, :digest, :discipline_category_id, :disk_directory, :disk_filename, :downloads, :filename, :filesize, :first_level_discipline_id, :is_public, :major_level, :quotes, :attachment_id +end diff --git a/db/migrate/20170220065632_homework_bank_migration.rb b/db/migrate/20170220065632_homework_bank_migration.rb index 6f159b631..6e47288ae 100644 --- a/db/migrate/20170220065632_homework_bank_migration.rb +++ b/db/migrate/20170220065632_homework_bank_migration.rb @@ -1,43 +1,43 @@ class HomeworkBankMigration < ActiveRecord::Migration def up - # add_column :homework_commons, :homework_bank_id, :integer - # add_column :homework_commons, :is_update, :boolean, :default => false - # - # count =Course.all.count / 30 + 2 - # transaction do - # for i in 1 ... count do i - # Course.page(i).per(30).each do |course| - # if course.is_delete == 0 - # course.homework_commons.each do |homework| - # unless homework.user.homework_banks.select("name").to_s.include?(homework.name) - # homework_bank = HomeworkBank.create(:user_id => homework.user_id, :name => homework.name, :description => homework.description, :homework_type => homework.homework_type, - # :quotes => 1, :is_public => course.is_public, :applicable_syllabus => course.syllabus.title, :homework_common_id => homework.id, :created_at => homework.created_at) - # if homework.homework_type == 2 && homework.homework_detail_programing - # homework_bank.update_attributes(:language => homework.homework_detail_programing.language, :standard_code => homework.homework_detail_programing.standard_code) - # homework.homework_tests.each do |test| - # homework_bank.homework_bank_tests << HomeworkBankTest.create( - # input: test.input, - # output: test.output, - # test_type: true - # ) - # end - # homework.homework_samples.each do |test| - # homework_bank.homework_bank_tests << HomeworkBankTest.create( - # input: test.input, - # output: test.output, - # test_type: false - # ) - # end - # elsif homework.homework_type == 3 && homework.homework_detail_group - # homework_bank.update_attributes(:min_num => homework.homework_detail_group.min_num, :max_num => homework.homework_detail_group.max_num, :base_on_project => homework.homework_detail_group.base_on_project) - # end - # homework.update_column('homework_bank_id', homework_bank.id) - # end - # end - # end - # end - # end - # end + add_column :homework_commons, :homework_bank_id, :integer + add_column :homework_commons, :is_update, :boolean, :default => false + + count =Course.all.count / 30 + 2 + transaction do + for i in 1 ... count do i + Course.page(i).per(30).each do |course| + if course.is_delete == 0 + course.homework_commons.each do |homework| + unless homework.user.homework_banks.select("name").to_s.include?(homework.name) + homework_bank = HomeworkBank.create(:user_id => homework.user_id, :name => homework.name, :description => homework.description, :homework_type => homework.homework_type, + :quotes => 1, :is_public => course.is_public, :applicable_syllabus => course.syllabus.title, :homework_common_id => homework.id, :created_at => homework.created_at) + if homework.homework_type == 2 && homework.homework_detail_programing + homework_bank.update_attributes(:language => homework.homework_detail_programing.language, :standard_code => homework.homework_detail_programing.standard_code) + homework.homework_tests.each do |test| + homework_bank.homework_bank_tests << HomeworkBankTest.create( + input: test.input, + output: test.output, + test_type: true + ) + end + homework.homework_samples.each do |test| + homework_bank.homework_bank_tests << HomeworkBankTest.create( + input: test.input, + output: test.output, + test_type: false + ) + end + elsif homework.homework_type == 3 && homework.homework_detail_group + homework_bank.update_attributes(:min_num => homework.homework_detail_group.min_num, :max_num => homework.homework_detail_group.max_num, :base_on_project => homework.homework_detail_group.base_on_project) + end + homework.update_column('homework_bank_id', homework_bank.id) + end + end + end + end + end + end end def down diff --git a/db/migrate/20170414084212_create_resource_banks.rb b/db/migrate/20170414084212_create_resource_banks.rb new file mode 100644 index 000000000..baf20915b --- /dev/null +++ b/db/migrate/20170414084212_create_resource_banks.rb @@ -0,0 +1,27 @@ +class CreateResourceBanks < ActiveRecord::Migration + def change + create_table :resource_banks do |t| + t.references :course + t.integer :attachment_id + t.string :filename + t.string :disk_filename + t.integer :filesize + t.string :digest + t.integer :downloads + t.references :user + t.text :description + t.string :disk_directory + t.boolean :is_public + t.integer :copy_from + t.integer :quotes + t.string :applicable_syllabus + t.integer :major_level + t.integer :discipline_category_id + t.integer :first_level_discipline_id + + t.timestamps + end + add_index :resource_banks, :course_id + add_index :resource_banks, :user_id + end +end diff --git a/db/migrate/20170417081351_resource_bank_migration.rb b/db/migrate/20170417081351_resource_bank_migration.rb new file mode 100644 index 000000000..d76f68289 --- /dev/null +++ b/db/migrate/20170417081351_resource_bank_migration.rb @@ -0,0 +1,24 @@ +class ResourceBankMigration < ActiveRecord::Migration + def up + #add_column :attachments, :resource_bank_id, :integer + + count =Attachment.where(:container_type => 'Course').count / 30 + 2 + transaction do + for i in 1 ... count do i + Attachment.where(:container_type => 'Course').page(i).per(30).each do |atta| + if atta.course && atta.course.syllabus + course = atta.course + syllabus = course.syllabus + resource_bank = ResourceBank.create(:course_id => course.id, :attachment_id => atta.id, :filename => atta.filename, :disk_filename => atta.disk_filename, :filesize => atta.filesize, :digest => atta.digest, :downloads => 0, + :user_id => atta.author_id, :description => atta.description, :disk_directory => atta.disk_directory, :is_public => atta.is_public, :copy_from => atta.copy_from, + :quotes => 1, :applicable_syllabus => syllabus.title, :major_level => syllabus.major_level, :discipline_category_id => syllabus.discipline_category_id, :first_level_discipline_id => syllabus.first_level_discipline_id, :created_at => atta.created_on) + atta.update_column('resource_bank_id', resource_bank.id) + end + end + end + end + end + + def down + end +end diff --git a/spec/factories/resource_banks.rb b/spec/factories/resource_banks.rb new file mode 100644 index 000000000..68981d9e3 --- /dev/null +++ b/spec/factories/resource_banks.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :resource_bank do + + end + +end diff --git a/spec/models/resource_bank_spec.rb b/spec/models/resource_bank_spec.rb new file mode 100644 index 000000000..ff8fd11a4 --- /dev/null +++ b/spec/models/resource_bank_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ResourceBank, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end