实训课程的教学团队新增删除、上移、下移的功能

dev_haigong
cxt 6 years ago
parent ff98ef9463
commit e7c04e8c58

@ -242,14 +242,56 @@ class SubjectsController < ApplicationController
memberships = params[:membership][:user_ids] memberships = params[:membership][:user_ids]
memberships.each do |member| memberships.each do |member|
user = User.find(member) user = User.find(member)
SubjectMember.create!(:user_id => member, :subject_id => @subject.id, :role => 2) SubjectMember.create!(:user_id => member, :subject_id => @subject.id, :role => 2,
:position => @subject.subject_members.size + 1)
end end
end end
end end
def delete_member def delete_member
member = @subject.subject_members.where(:user_id => params[:mem_id]).first if User.current.admin?
member.destroy if member ActiveRecord::Base.transaction do
member = @subject.subject_members.where(:id => params[:mem_id]).first
if member.present? && member.role != 1
@subject.subject_members.where("position > #{member.position}").update_all("position = position - 1")
member.destroy
end
end
else
render_403
end
end
def up_member_position
if User.current.member_of_subject?(@subject)
ActiveRecord::Base.transaction do
member = @subject.subject_members.where(:id => params[:mem_id]).first
raise "不能再上移了" if member.position == 1
up_member = @subject.subject_members.where(:position => member.position - 1).first
up_member.update_attribute(:position, member.position)
member.update_attribute(:position, member.position - 1)
end
else
render_403
end
end
def down_member_position
if User.current.member_of_subject?(@subject)
ActiveRecord::Base.transaction do
begin
member = @subject.subject_members.where(:id => params[:mem_id]).first
raise "不能再下移了" if member.position == @subject.subject_members.count
down_member = @subject.subject_members.where(:position => member.position + 1).first
down_member.update_attribute(:position, member.position)
member.update_attribute(:position, member.position + 1)
rescue Exception => e
raise ActiveRecord::Rollback
end
end
else
render_403
end
end end
def statistics def statistics

@ -2,7 +2,7 @@ class Subject < ActiveRecord::Base
#status :0 编辑中 1 审核中 2 发布 #status :0 编辑中 1 审核中 2 发布
# belongs_to :user # belongs_to :user
has_many :users, :through => :subject_members has_many :users, :through => :subject_members
has_many :subject_members, :dependent => :destroy, :order => "subject_members.id DESC" has_many :subject_members, :dependent => :destroy, :order => "subject_members.position asc"
has_many :career_stages, :through => :career_stage_subjects has_many :career_stages, :through => :career_stage_subjects
has_many :career_stage_subjects, :dependent => :destroy has_many :career_stage_subjects, :dependent => :destroy

@ -14,23 +14,23 @@
<span class="fr"> <span class="fr">
<a class="fl mr20 mt4" data-tip-down="删除" onclick="delete_confirm_box_2('确定要删除该阶段吗?');"> <% if User.current.admin? && member.role != 1 %>
<img src="/images/educoder/icon/del.svg" class="fl mt4 ml4"> <a class="fl mr20 mt4" data-tip-down="删除" onclick="delete_confirm_box_2('<%= delete_member_subject_path(@subject, :mem_id => member.id) %>', '确定要删除该成员吗?');">
</a> <img src="/images/educoder/icon/del.svg" class="fl mt4 ml4">
</a>
<% end %>
<a data-method="POST" class="fl ring-op-green mr25 mt7" data-remote="true"> <% if User.current.member_of_subject?(@subject) && member.position > 1 %>
<a data-method="POST" class="fl ring-op-green mr25 mt7" data-remote="true" href="<%= up_member_position_subject_path(@subject, :mem_id => member.id) %>">
<img src="/images/educoder/icon/moveup.svg" data-tip-down="向上移动" class="fl mt2 ml4"> <img src="/images/educoder/icon/moveup.svg" data-tip-down="向上移动" class="fl mt2 ml4">
</a> </a>
<% end %>
<% if User.current.member_of_subject?(@subject) && member.position < @subject.subject_members.size %>
<a data-method="POST" class="fl ring-op-green mr25 mt7" data-remote="true"> <a data-method="POST" class="fl ring-op-green mr25 mt7" data-remote="true" href="<%= down_member_position_subject_path(@subject, :mem_id => member.id) %>">
<img src="/images/educoder/icon/movedown.svg" data-tip-down="向下移动" class="fl mt2 ml4"> <img src="/images/educoder/icon/movedown.svg" data-tip-down="向下移动" class="fl mt2 ml4">
</a> </a>
<% end %>
<a data-tip-down="编辑" class="ring-green fl mt7">
<img src="/images/educoder/icon/edit.svg" class="fl mt3 ml2">
</a>
</span> </span>

@ -0,0 +1 @@
$("#subject_members").html("<%= j(render :partial => 'subject_members') %>");

@ -0,0 +1 @@
$("#subject_members").html("<%= j(render :partial => 'subject_members') %>");

@ -339,6 +339,8 @@ RedmineApp::Application.routes.draw do ## oauth相关
match 'add_collaborators', :via => [:get, :post] match 'add_collaborators', :via => [:get, :post]
post 'add_subject_members' post 'add_subject_members'
delete 'delete_member' delete 'delete_member'
post 'up_member_position'
post 'down_member_position'
end end
collection do collection do

@ -0,0 +1,11 @@
class AddPositionToSubjectMembers < ActiveRecord::Migration
def change
add_column :subject_members, :position, :integer, :default => 1
Subject.all.each do |subject|
subject.subject_members.reorder("id asc").each_with_index do |member, index|
member.update_column('position', index + 1)
end
end
end
end
Loading…
Cancel
Save