2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-05-02 10:18:07 +01:00
|
|
|
class Projects::ProjectsController < Projects::BaseController
|
2012-09-24 19:04:53 +01:00
|
|
|
include ProjectsHelper
|
2012-03-07 21:34:49 +00:00
|
|
|
before_filter :authenticate_user!
|
2012-07-17 09:02:56 +01:00
|
|
|
load_and_authorize_resource :id_param => :project_name # to force member actions load
|
2011-03-10 13:38:50 +00:00
|
|
|
|
2011-10-26 21:57:51 +01:00
|
|
|
def index
|
2012-03-27 22:28:50 +01:00
|
|
|
@projects = Project.accessible_by(current_ability, :membered)
|
2012-03-27 16:50:00 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
2012-09-24 19:04:53 +01:00
|
|
|
format.html {
|
|
|
|
@all_projects = @projects
|
|
|
|
@groups = current_user.groups
|
2012-09-25 10:36:50 +01:00
|
|
|
@owners = User.where(:id => @projects.where(:owner_type => 'User').uniq.pluck(:owner_id))
|
2012-09-24 19:04:53 +01:00
|
|
|
@projects = @projects.recent.paginate(:page => params[:page], :per_page => 25)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
selected_groups = params[:groups] || []
|
2012-09-24 19:07:53 +01:00
|
|
|
selected_owners = params[:users] || []
|
2012-09-24 19:04:53 +01:00
|
|
|
@projects = prepare_list(@projects, selected_groups, selected_owners)
|
|
|
|
}
|
2012-03-27 16:50:00 +01:00
|
|
|
end
|
2011-03-10 14:20:09 +00:00
|
|
|
end
|
|
|
|
|
2011-10-26 21:57:51 +01:00
|
|
|
def new
|
|
|
|
@project = Project.new
|
2012-02-29 14:45:04 +00:00
|
|
|
@who_owns = :me
|
2011-10-26 21:57:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@project = Project.new params[:project]
|
2012-02-27 09:03:28 +00:00
|
|
|
@project.owner = choose_owner
|
2012-02-29 14:45:04 +00:00
|
|
|
@who_owns = (@project.owner_type == 'User' ? :me : :group)
|
2013-05-08 15:41:15 +01:00
|
|
|
authorize! :write, @project.owner if @project.owner.class == Group
|
2011-10-26 21:57:51 +01:00
|
|
|
|
2011-11-30 12:58:14 +00:00
|
|
|
if @project.save
|
2012-04-10 10:40:38 +01:00
|
|
|
flash[:notice] = t('flash.project.saved')
|
2011-10-27 23:43:44 +01:00
|
|
|
redirect_to @project
|
2011-10-26 21:57:51 +01:00
|
|
|
else
|
|
|
|
flash[:error] = t('flash.project.save_error')
|
2012-03-03 01:05:19 +00:00
|
|
|
flash[:warning] = @project.errors.full_messages.join('. ')
|
2011-10-26 21:57:51 +01:00
|
|
|
render :action => :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-08-31 18:47:52 +01:00
|
|
|
params[:project].delete(:maintainer_id) if params[:project][:maintainer_id].blank?
|
2011-10-26 21:57:51 +01:00
|
|
|
if @project.update_attributes(params[:project])
|
|
|
|
flash[:notice] = t('flash.project.saved')
|
2011-10-27 23:43:44 +01:00
|
|
|
redirect_to @project
|
2011-10-26 21:57:51 +01:00
|
|
|
else
|
2011-11-30 12:58:14 +00:00
|
|
|
@project.save
|
2011-10-26 21:57:51 +01:00
|
|
|
flash[:error] = t('flash.project.save_error')
|
2012-04-01 16:19:54 +01:00
|
|
|
flash[:warning] = @project.errors.full_messages.join('. ')
|
2011-10-26 21:57:51 +01:00
|
|
|
render :action => :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@project.destroy
|
|
|
|
flash[:notice] = t("flash.project.destroyed")
|
2011-10-27 13:49:21 +01:00
|
|
|
redirect_to @project.owner
|
2011-10-26 21:57:51 +01:00
|
|
|
end
|
|
|
|
|
2011-11-23 15:52:33 +00:00
|
|
|
def fork
|
2012-04-09 18:56:03 +01:00
|
|
|
owner = (Group.find params[:group] if params[:group].present?) || current_user
|
2013-05-07 15:56:13 +01:00
|
|
|
authorize! :write, owner if owner.class == Group
|
2012-04-09 18:56:03 +01:00
|
|
|
if forked = @project.fork(owner) and forked.valid?
|
2011-11-29 21:42:58 +00:00
|
|
|
redirect_to forked, :notice => t("flash.project.forked")
|
|
|
|
else
|
|
|
|
flash[:warning] = t("flash.project.fork_error")
|
2013-04-08 15:27:50 +01:00
|
|
|
flash[:error] = forked.errors.full_messages.join("\n")
|
2011-11-29 21:42:58 +00:00
|
|
|
redirect_to @project
|
|
|
|
end
|
2011-11-23 15:52:33 +00:00
|
|
|
end
|
|
|
|
|
2012-02-28 07:59:38 +00:00
|
|
|
def sections
|
|
|
|
if request.post?
|
|
|
|
if @project.update_attributes(params[:project])
|
|
|
|
flash[:notice] = t('flash.project.saved')
|
2012-04-19 20:45:50 +01:00
|
|
|
redirect_to sections_project_path(@project)
|
2012-02-28 07:59:38 +00:00
|
|
|
else
|
|
|
|
@project.save
|
|
|
|
flash[:error] = t('flash.project.save_error')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-07 21:34:49 +00:00
|
|
|
def remove_user
|
2012-07-27 05:12:09 +01:00
|
|
|
@project.relations.by_actor(current_user).destroy_all
|
2012-03-07 21:34:49 +00:00
|
|
|
flash[:notice] = t("flash.project.user_removed")
|
|
|
|
redirect_to projects_path
|
|
|
|
end
|
2011-10-17 15:21:29 +01:00
|
|
|
|
2012-08-24 16:19:26 +01:00
|
|
|
def autocomplete_maintainers
|
2012-08-30 17:19:12 +01:00
|
|
|
term, limit = params[:term], params[:limit] || 10
|
2012-08-24 16:19:26 +01:00
|
|
|
items = User.member_of_project(@project)
|
|
|
|
.where("users.name ILIKE ? OR users.uname ILIKE ?", "%#{term}%", "%#{term}%")
|
|
|
|
.limit(limit).map { |u| {:value => u.fullname, :label => u.fullname, :id => u.id} }
|
|
|
|
render :json => items
|
|
|
|
end
|
|
|
|
|
2012-08-29 15:58:58 +01:00
|
|
|
def preview
|
2012-10-04 19:40:12 +01:00
|
|
|
render :inline => view_context.markdown(params[:text] || ''), :layout => false
|
2012-08-29 15:58:58 +01:00
|
|
|
end
|
|
|
|
|
2012-10-01 17:03:08 +01:00
|
|
|
def refs_list
|
|
|
|
refs = @project.repo.branches_and_tags.map(&:name)
|
|
|
|
@selected = (refs.include? params[:selected]) ? params[:selected] : @project.default_branch
|
|
|
|
render :layout => false
|
|
|
|
end
|
2012-08-29 15:58:58 +01:00
|
|
|
|
2012-03-07 21:34:49 +00:00
|
|
|
protected
|
2012-02-27 09:03:28 +00:00
|
|
|
|
2012-09-24 19:04:53 +01:00
|
|
|
def prepare_list(projects, groups, owners)
|
2012-03-27 16:50:00 +01:00
|
|
|
res = {}
|
|
|
|
|
|
|
|
colName = ['name']
|
|
|
|
sort_col = params[:iSortCol_0] || 0
|
|
|
|
sort_dir = params[:sSortDir_0] == "desc" ? 'desc' : 'asc'
|
|
|
|
order = "#{colName[sort_col.to_i]} #{sort_dir}"
|
|
|
|
|
|
|
|
res[:total_count] = projects.count
|
2012-09-21 18:57:22 +01:00
|
|
|
|
2012-09-24 19:04:53 +01:00
|
|
|
if groups.present? || owners.present?
|
|
|
|
projects = projects.by_owners(groups, owners)
|
2012-09-21 18:57:22 +01:00
|
|
|
end
|
2012-10-04 19:40:12 +01:00
|
|
|
|
2013-07-01 11:50:35 +01:00
|
|
|
projects = projects.search(params[:sSearch])
|
2012-09-21 18:57:22 +01:00
|
|
|
|
2012-03-27 16:50:00 +01:00
|
|
|
res[:filtered_count] = projects.count
|
|
|
|
|
|
|
|
projects = projects.order(order)
|
|
|
|
res[:projects] = if params[:iDisplayLength].present?
|
|
|
|
start = params[:iDisplayStart].present? ? params[:iDisplayStart].to_i : 0
|
|
|
|
length = params[:iDisplayLength].to_i
|
|
|
|
page = start/length + 1
|
|
|
|
|
|
|
|
projects.paginate(:page => page, :per_page => length)
|
|
|
|
else
|
|
|
|
projects
|
|
|
|
end
|
|
|
|
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2012-03-07 21:34:49 +00:00
|
|
|
def choose_owner
|
|
|
|
if params[:who_owns] == 'group'
|
|
|
|
Group.find(params[:owner_id])
|
|
|
|
else
|
|
|
|
current_user
|
2012-02-27 09:03:28 +00:00
|
|
|
end
|
2012-03-07 21:34:49 +00:00
|
|
|
end
|
2011-03-10 11:35:46 +00:00
|
|
|
end
|