commit
						d3886a366c
					
				| @ -0,0 +1,2 @@ | ||||
| // Place all the behaviors and hooks related to the matching controller here.
 | ||||
| // All this logic will automatically be available in application.js.
 | ||||
| @ -0,0 +1,3 @@ | ||||
| // Place all the styles related to the course_stages controller here. | ||||
| // They will automatically be included in application.css. | ||||
| // You can use Sass (SCSS) here: http://sass-lang.com/ | ||||
| @ -0,0 +1,105 @@ | ||||
| class CourseStagesController < ApplicationController | ||||
|   before_action :require_login | ||||
|   before_action :find_course, only: [:create] | ||||
|   before_action :find_course_stage, only: [:update, :destroy, :edit, :up_position, :down_position] | ||||
|   before_action :user_course_identity, :teacher_allowed | ||||
| 
 | ||||
|   def create | ||||
|     ActiveRecord::Base.transaction do | ||||
|       begin | ||||
|         @stage = CourseStage.new(stage_params) | ||||
|         @stage.course_id = @course.id | ||||
|         @stage.position = @course.course_stages.count + 1 | ||||
|         @stage.save! | ||||
|         unless params[:shixun_id].blank? | ||||
|           shixuns = Shixun.where(id: params[:shixun_id]).order("field(id, #{params[:shixun_id].join(",")})") | ||||
|           shixuns.each do |shixun| | ||||
|             CourseStageShixun.create!(course_stage_id: @stage.id, course_id: @course.id, shixun_id: shixun.id, position: @stage.course_stage_shixuns.count + 1) | ||||
|           end | ||||
|         end | ||||
|         normal_status("创建成功") | ||||
|       rescue Exception => e | ||||
|         uid_logger_error(e.message) | ||||
|         tip_exception(e.message) | ||||
|         raise ActiveRecord::Rollback | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def edit | ||||
| 
 | ||||
|   end | ||||
| 
 | ||||
|   def update | ||||
|     ActiveRecord::Base.transaction do | ||||
|       begin | ||||
|         @stage.update_attributes!(stage_params) | ||||
|         @stage.course_stage_shixuns.destroy_all | ||||
|         unless params[:shixun_id].blank? | ||||
|           params[:shixun_id].each do |shixun_id| | ||||
|             shixun = Shixun.where(id: shixun_id).first | ||||
|             @stage.course_stage_shixuns.create!(course_id: @course.id, shixun_id: shixun.id, position: @stage.course_stage_shixuns.count + 1) if shixun.present? | ||||
|           end | ||||
|         end | ||||
|         normal_status("更新成功") | ||||
|       rescue Exception => e | ||||
|         uid_logger_error(e.message) | ||||
|         tip_exception(e.message) | ||||
|         raise ActiveRecord::Rollback | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def destroy | ||||
|     ActiveRecord::Base.transaction do | ||||
|       @course.course_stages.where("position > ?", @stage.position).update_all("position = position - 1") | ||||
|       @stage.destroy! | ||||
|       normal_status("删除成功") | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def up_position | ||||
|     ActiveRecord::Base.transaction do | ||||
|       begin | ||||
|         position = @stage.position | ||||
|         tip_exception("第一章不能向上移动") if @stage.position == 1 | ||||
|         pre_stage = @course.course_stages.where(position: position - 1).first | ||||
|         pre_stage.update_attributes(position: position) | ||||
|         @stage.update_attributes(position: position - 1) | ||||
|         normal_status("更新成功") | ||||
|       rescue Exception => e | ||||
|         uid_logger("stage up failed: #{e.message}") | ||||
|         raise ActiveRecord::Rollback | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def down_position | ||||
|     ActiveRecord::Base.transaction do | ||||
|       begin | ||||
|         position = @stage.position | ||||
|         rails "最后一章不能向下移动" if @stage.position == @course.course_stages.count | ||||
|         next_stage = @course.course_stages.where(position: position + 1).first | ||||
|         next_stage.update_attributes(position: position) | ||||
|         @stage.update_attributes(position: position + 1) | ||||
|         normal_status("更新成功") | ||||
|       rescue Exception => e | ||||
|         uid_logger("stage up failed: #{e.message}") | ||||
|         raise ActiveRecord::Rollback | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   private | ||||
| 
 | ||||
|   def find_course_stage | ||||
|     @stage = CourseStage.find_by!(id: params[:id]) | ||||
|     @course = @stage.course | ||||
|   end | ||||
| 
 | ||||
|   def stage_params | ||||
|     tip_exception("章节名称不能为空") if params[:name].blank? | ||||
|     params.permit(:name, :description) | ||||
|   end | ||||
| 
 | ||||
| end | ||||
| @ -0,0 +1,2 @@ | ||||
| module CourseStagesHelper | ||||
| end | ||||
| @ -0,0 +1,9 @@ | ||||
| class CourseStage < ApplicationRecord | ||||
|   belongs_to :course | ||||
| 
 | ||||
|   has_many :course_stage_shixuns, -> { order("course_stage_shixuns.position ASC") }, dependent: :destroy | ||||
|   has_many :shixuns, :through => :course_stage_shixuns | ||||
| 
 | ||||
|   validates :name, length: { maximum: 60 } | ||||
|   validates :description, length: { maximum: 300 } | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| class CourseStageShixun < ApplicationRecord | ||||
|   belongs_to :course | ||||
|   belongs_to :course_stage, counter_cache: :shixuns_count | ||||
|   belongs_to :shixun | ||||
| end | ||||
| @ -0,0 +1,11 @@ | ||||
| json.stage_id @stage.id | ||||
| json.stage_name @stage.name | ||||
| json.stage_description @stage.description | ||||
| json.delete_path "/course_stages/#{@stage.id}" | ||||
| json.shixuns_list do | ||||
|   json.array! @stage.shixuns.each do |shixun| | ||||
|     json.shixun_identifier shixun.identifier | ||||
|     json.shixun_name shixun.name | ||||
|     json.shixun_id shixun.id | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,13 @@ | ||||
| class CreateCourseStages < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     create_table :course_stages do |t| | ||||
|       t.references :course, index: true | ||||
|       t.string :name | ||||
|       t.text :description | ||||
|       t.integer :position | ||||
|       t.integer :shixuns_count | ||||
| 
 | ||||
|       t.timestamps | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,12 @@ | ||||
| class CreateCourseStageShixuns < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     create_table :course_stage_shixuns do |t| | ||||
|       t.references :course, index: true | ||||
|       t.references :course_stage, index: true | ||||
|       t.references :shixun, index: true | ||||
|       t.integer :position | ||||
| 
 | ||||
|       t.timestamps | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,14 @@ | ||||
| class MigrateCourseStages < ActiveRecord::Migration[5.2] | ||||
|   def change | ||||
|     Course.where(excellent: 1).each do |course| | ||||
|       if course.subject | ||||
|         course.subject.stages.each do |stage| | ||||
|           new_stage = CourseStage.create!(course_id: course.id, name: stage.name, description: stage.description, position: stage.position) | ||||
|           stage.stage_shixuns.each do |stage_shixun| | ||||
|             CourseStageShixun.create!(course_id: course.id, course_stage_id: new_stage.id, shixun_id: stage_shixun.shixun_id, position: stage_shixun.position) | ||||
|           end | ||||
|         end | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe CourseStagesController, type: :controller do | ||||
| 
 | ||||
| end | ||||
| @ -0,0 +1,15 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| # Specs in this file have access to a helper object that includes | ||||
| # the CourseStagesHelper. For example: | ||||
| # | ||||
| # describe CourseStagesHelper do | ||||
| #   describe "string concat" do | ||||
| #     it "concats two strings with spaces" do | ||||
| #       expect(helper.concat_strings("this","that")).to eq("this that") | ||||
| #     end | ||||
| #   end | ||||
| # end | ||||
| RSpec.describe CourseStagesHelper, type: :helper do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe CourseStageShixun, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
| @ -0,0 +1,5 @@ | ||||
| require 'rails_helper' | ||||
| 
 | ||||
| RSpec.describe CourseStage, type: :model do | ||||
|   pending "add some examples to (or delete) #{__FILE__}" | ||||
| end | ||||
					Loading…
					
					
				
		Reference in new issue