rosa-build/app/controllers/projects/collaborators_controller.rb

80 lines
2.3 KiB
Ruby
Raw Normal View History

class Projects::CollaboratorsController < Projects::BaseController
respond_to :html, :json
2015-03-04 23:19:19 +00:00
before_action :authenticate_user!
load_resource :project
2015-03-04 23:19:19 +00:00
before_action :authorize_collaborators
2011-10-30 22:59:03 +00:00
2015-03-04 23:19:19 +00:00
before_action :find_users
before_action :find_groups
2011-10-30 22:59:03 +00:00
def index
@collaborators = Collaborator.find_by_project(@project)
respond_with @collaborators
2011-10-30 22:59:03 +00:00
end
def find
users = User.not_member_of(@project)
groups = Group.not_member_of(@project)
if params[:term].present?
users = users.search(params[:term]).first(5)
groups = groups.search(params[:term]).first(5)
end
2014-01-21 04:51:49 +00:00
@collaborators = (users | groups).map{|act| Collaborator.new(actor: act, project: @project)}
respond_with @collaborators
2011-10-30 22:59:03 +00:00
end
def create
@collaborator = Collaborator.new(params[:collaborator])
@collaborator.project = @project
respond_to do |format|
if @collaborator.save
2015-04-03 16:08:17 +01:00
format.json { render partial: 'collaborator', locals: {collaborator: @collaborator, success: true} }
else
2015-04-03 16:08:17 +01:00
format.json { render json: {message:t('flash.collaborators.error_in_adding')}, status: 422 }
end
2011-10-30 22:59:03 +00:00
end
end
def update
2015-04-03 16:08:17 +01:00
cb = Collaborator.find(params[:id])
respond_to do |format|
if cb.update_attributes(params[:collaborator])
format.json { render json: {message:t('flash.collaborators.successfully_updated', uname: cb.actor.uname)} }
else
format.json { render json: {message:t('flash.collaborators.error_in_updating')}, status: 422 }
end
end
2011-10-30 22:59:03 +00:00
end
def destroy
2015-04-03 16:08:17 +01:00
cb = Collaborator.find(params[:id])
respond_to do |format|
if cb.present? && cb.destroy
format.json { render json: {message:t('flash.collaborators.successfully_removed', uname: cb.actor.uname)} }
else
format.json {
render json: {message:t('flash.collaborators.error_in_removing', uname: cb.try(:actor).try(:uname))},
status: 422
}
end
end
end
2011-10-30 22:59:03 +00:00
protected
def find_users
@users = @project.collaborators.order('uname')#User.all
@users = @users.without(@project.owner_id) if @project.owner_type == 'User'
end
2011-10-30 22:59:03 +00:00
def find_groups
@groups = @project.groups.order('uname')#Group.all
@groups = @groups.without(@project.owner_id) if @project.owner_type == 'Group'
end
def authorize_collaborators
authorize! :update, @project
end
2011-10-30 22:59:03 +00:00
end