From 916d99be82516cc1aaa86c59f281457fa4a9ca84 Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 8 Dec 2015 16:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E5=88=A0=E9=99=A4=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 11 +++++++++++ app/models/course.rb | 7 ++++--- app/views/admin/courses.html.erb | 3 +++ db/migrate/20151208015409_add_is_delete_to_courses.rb | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index ffe22a29d..5109e15f4 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -775,7 +775,18 @@ class CoursesController < ApplicationController #删除课程 #删除课程只是将课程的is_deleted状态改为false,is_deleted为false状态的课程只有管理员可以看到 def destroy + @course.update_attributes(:is_delete => true) + @course = nil + redirect_to user_url(User.current) + end + # 恢复已删除的课程 + def recovery + if User.current.admin? + @course.update_attributes(:is_delete => false) + else + return 403 + end end private diff --git a/app/models/course.rb b/app/models/course.rb index 1c1bedb3b..cef05de73 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -20,7 +20,7 @@ class Course < ActiveRecord::Base end end - attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student, :enterprise_name + attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student, :enterprise_name, :is_delete #belongs_to :project, :class_name => 'Course', :foreign_key => :extra, primary_key: :identifier belongs_to :teacher, :class_name => 'User', :foreign_key => :tea_id # 定义一个方法teacher,该方法通过tea_id来调用User表 belongs_to :school, :class_name => 'School', :foreign_key => :school_id #定义一个方法school,该方法通过school_id来调用School表 @@ -86,7 +86,8 @@ class Course < ActiveRecord::Base 'is_public', 'description', 'class_period', - 'open_student' + 'open_student', + 'is_delete' acts_as_customizable @@ -94,7 +95,7 @@ class Course < ActiveRecord::Base scope :active, lambda { where(:status => STATUS_ACTIVE) } scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } scope :all_public, lambda { where(:is_public => true) } - scope :visible, lambda {|*args| where(Course.visible_condition(args.shift || User.current, *args)) } + scope :visible, lambda {|*args| where(Course.where("is_delete =?", 0).visible_condition(args.shift || User.current, *args)) } scope :allowed_to, lambda {|*args| user = User.current permission = nil diff --git a/app/views/admin/courses.html.erb b/app/views/admin/courses.html.erb index 1615bccbc..0bd4fb509 100644 --- a/app/views/admin/courses.html.erb +++ b/app/views/admin/courses.html.erb @@ -67,6 +67,9 @@ <%= format_date(course.created_at) %> + + <%= link_to(l(:button_delete), course_path(course), :method => :delete, :class => 'icon icon-del', :onClick=>"delcfm()" ) %> + <% end %> diff --git a/db/migrate/20151208015409_add_is_delete_to_courses.rb b/db/migrate/20151208015409_add_is_delete_to_courses.rb index d974fb4d9..cf037e010 100644 --- a/db/migrate/20151208015409_add_is_delete_to_courses.rb +++ b/db/migrate/20151208015409_add_is_delete_to_courses.rb @@ -1,5 +1,5 @@ class AddIsDeleteToCourses < ActiveRecord::Migration def change - add_column :courses, :is_delete, :integer + add_column :courses, :is_delete, :integer, :default => 0 end end