class SyllabusMemberController < ApplicationController def syl_member_autocomplete @syllabus = Syllabus.find(params[:syllabus]) @flag = params[:flag] || false respond_to do |format| format.js end end def create @syllabus = Syllabus.find(params[:syllabus]) if params[:membership].nil? @fail_hint = l(:label_blank_user_lists_for_org) else member_ids = params[:membership][:user_ids] last_rank = @syllabus.syllabus_members.order("rank asc").last.rank user_ids = @syllabus.syllabus_members.map{|sy| sy.user_id} member_ids.each_with_index do |user_id, i| unless user_ids.include?(user_id.to_i) member = SyllabusMember.create(:user_id => user_id, :rank => last_rank + 1 + i) @syllabus.syllabus_members << member end end @members = @syllabus.syllabus_members.order("rank asc") end respond_to do |format| format.js end end def destroy member = SyllabusMember.find(params[:id]) @syllabus = member.syllabus after_syl_members = @syllabus.syllabus_members.where("rank > #{member.rank}") after_syl_members.update_all("rank = rank - 1") member.destroy @members = @syllabus.syllabus_members.order("rank asc") respond_to do |format| format.js end end def update_rank member = SyllabusMember.find(params[:id]) @syllabus = member.syllabus members = @syllabus.syllabus_members if params[:opr] == 'up' && member.rank > 1 before_mem = members.where("rank = #{member.rank - 1}").first if before_mem && member.update_attribute('rank', member.rank - 1) before_mem.update_attribute('rank', before_mem.rank + 1) end elsif params[:opr] == 'down' && member.rank < members.count after_mem = members.where("rank = #{member.rank + 1}").first if after_mem && member.update_attribute('rank', member.rank + 1) after_mem.update_attribute('rank', after_mem.rank - 1) end end @members = @syllabus.syllabus_members.order("rank asc") respond_to do |format| format.js end end end