diff --git a/app/models/course.rb b/app/models/course.rb index 45830c719..04b0535bd 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -35,7 +35,7 @@ class Course < ActiveRecord::Base :delete_permission => :manage_files validates_presence_of :password, :term,:name - validates_format_of :class_period, :with =>/^[1-9]\d*$/ + validates_format_of :class_period, :with =>/^\d*$/ #validates_format_of :name,:with =>/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/ before_save :self_validate diff --git a/lib/legacy.rb b/lib/legacy.rb new file mode 100644 index 000000000..5eeceabbc --- /dev/null +++ b/lib/legacy.rb @@ -0,0 +1,26 @@ +class Legacy + def self.clear_period + begin + count = 0 + puts '---------------------' + + Course.transaction do + Course.all.each do |course| + if course.class_period =~ /^\D*$/ + count += 1 + puts "[#{course.id}]: #{course.name}" + puts "period #{course.class_period} ===> 0" + puts '---------------------' + course.class_period = '0' + course.save + end + end # each end + end # transaction end + + puts "clean period count: #{count}" + rescue Exception => ex + puts "*** transaction abored!" + puts "*** errors: #{ex.message}" + end + end +end diff --git a/lib/tasks/modify_courses_attr_type.rake b/lib/tasks/modify_courses_attr_type.rake new file mode 100644 index 000000000..052c7f772 --- /dev/null +++ b/lib/tasks/modify_courses_attr_type.rake @@ -0,0 +1,8 @@ +desc "migrate old data." + +namespace :legacy do + desc "migrate Course class period data" + task :clear_period => :environment do + Legacy.clear_period + end +end