2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-03-11 16:15:06 +00:00
|
|
|
class RepositoriesController < ApplicationController
|
2011-03-11 16:08:41 +00:00
|
|
|
before_filter :authenticate_user!
|
2011-11-24 21:46:19 +00:00
|
|
|
before_filter :find_repository, :except => [:index, :new, :create]
|
|
|
|
before_filter :find_platform, :only => [:show, :destroy, :add_project, :remove_project]
|
2011-10-19 14:16:29 +01:00
|
|
|
before_filter :get_paths, :only => [:show, :new, :create, :add_project, :remove_project]
|
2011-10-17 15:20:35 +01:00
|
|
|
before_filter :find_platforms, :only => [:new, :create]
|
2011-12-05 09:29:17 +00:00
|
|
|
before_filter :build_repository_stub, :only => [:new, :create]
|
2011-11-19 11:41:11 +00:00
|
|
|
|
2011-11-24 21:46:19 +00:00
|
|
|
load_and_authorize_resource :platform
|
|
|
|
load_and_authorize_resource :repository, :through => :platform, :shallow => true
|
2011-10-17 15:20:35 +01:00
|
|
|
|
|
|
|
def index
|
2011-11-01 16:27:34 +00:00
|
|
|
if params[:platform_id]
|
|
|
|
@repositories = Platform.find(params[:platform_id]).repositories.paginate(:page => params[:repository_page])
|
|
|
|
else
|
|
|
|
@repositories = Repository.paginate(:page => params[:repository_page])
|
|
|
|
end
|
2011-10-17 15:20:35 +01:00
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
|
|
|
|
def show
|
2011-04-28 12:01:58 +01:00
|
|
|
if params[:query]
|
2012-01-28 01:04:11 +00:00
|
|
|
@projects = @repository.projects.recent.by_name("%#{params[:query]}%").paginate :page => params[:project_page], :per_page => 30
|
2011-04-28 12:01:58 +01:00
|
|
|
else
|
2011-10-19 14:16:29 +01:00
|
|
|
@projects = @repository.projects.recent.paginate :page => params[:project_page], :per_page => 30
|
2011-04-28 12:01:58 +01:00
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2011-10-17 15:20:35 +01:00
|
|
|
@repository = Repository.new
|
2011-10-27 23:48:39 +01:00
|
|
|
@platform_id = params[:platform_id]
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2011-03-31 01:58:27 +01:00
|
|
|
@repository.destroy
|
2011-10-28 00:16:22 +01:00
|
|
|
platform_id = @repository.platform_id
|
2011-03-31 01:58:27 +01:00
|
|
|
|
2011-03-31 03:00:21 +01:00
|
|
|
flash[:notice] = t("flash.repository.destroyed")
|
2011-10-28 00:16:22 +01:00
|
|
|
redirect_to platform_path(platform_id)
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2011-10-17 15:20:35 +01:00
|
|
|
@repository = Repository.new(params[:repository])
|
2011-11-24 21:46:19 +00:00
|
|
|
@repository.platform_id = params[:platform_id]
|
2011-10-26 21:57:51 +01:00
|
|
|
@repository.owner = get_owner
|
2011-03-11 16:08:41 +00:00
|
|
|
if @repository.save
|
2011-03-31 01:59:12 +01:00
|
|
|
flash[:notice] = t('flash.repository.saved')
|
2011-10-17 15:20:35 +01:00
|
|
|
redirect_to @repositories_path
|
2011-03-11 16:08:41 +00:00
|
|
|
else
|
2011-03-31 01:59:12 +01:00
|
|
|
flash[:error] = t('flash.repository.save_error')
|
2011-03-11 16:08:41 +00:00
|
|
|
render :action => :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-19 14:16:29 +01:00
|
|
|
def add_project
|
|
|
|
if params[:project_id]
|
|
|
|
@project = Project.find(params[:project_id])
|
2011-11-28 15:41:42 +00:00
|
|
|
unless @repository.projects.find_by_name(@project.name)
|
2011-10-19 14:16:29 +01:00
|
|
|
@repository.projects << @project
|
2011-11-02 21:37:00 +00:00
|
|
|
flash[:notice] = t('flash.repository.project_added')
|
2011-10-19 14:16:29 +01:00
|
|
|
else
|
|
|
|
flash[:error] = t('flash.repository.project_not_added')
|
|
|
|
end
|
2011-11-02 21:37:00 +00:00
|
|
|
redirect_to repository_path(@repository)
|
2011-10-19 14:16:29 +01:00
|
|
|
else
|
2012-01-11 21:12:56 +00:00
|
|
|
render :projects_list
|
2011-10-19 14:16:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-30 22:42:58 +00:00
|
|
|
def projects_list
|
2012-01-12 16:37:50 +00:00
|
|
|
owner_subquery = "
|
|
|
|
INNER JOIN (
|
|
|
|
SELECT id, 'User' AS type, uname
|
|
|
|
FROM users
|
|
|
|
UNION
|
|
|
|
SELECT id, 'Group' AS type, uname
|
|
|
|
FROM groups
|
|
|
|
) AS owner
|
|
|
|
ON projects.owner_id = owner.id AND projects.owner_type = owner.type"
|
2011-12-30 22:42:58 +00:00
|
|
|
colName = ['owner.uname', 'projects.name']
|
|
|
|
sort_col = params[:iSortCol_0] || 0
|
|
|
|
sort_dir = params[:sSortDir_0]=="asc" ? 'asc' : 'desc'
|
|
|
|
order = "#{colName[sort_col.to_i]} #{sort_dir}"
|
|
|
|
|
2012-01-12 16:37:50 +00:00
|
|
|
@projects = Project.joins(owner_subquery).addable_to_repository(@repository.id)
|
|
|
|
@projects = @projects.paginate(:page => (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i).to_i + 1, :per_page => params[:iDisplayLength])
|
2011-12-30 22:42:58 +00:00
|
|
|
@projects = @projects.by_visibilities(['open']) if @repository.platform.platform_type == 'main'
|
|
|
|
|
|
|
|
@total_projects = @projects.count
|
|
|
|
@projects = @projects.where(['projects.name LIKE ?', "#{params[:sSearch]}%"]) if params[:sSearch] and !params[:sSearch].empty?
|
|
|
|
@total_project = @projects.count
|
|
|
|
@projects = @projects.order(order)#.includes(:owner) #WTF????
|
|
|
|
|
|
|
|
render :partial => 'proj_ajax', :layout => false
|
|
|
|
end
|
|
|
|
|
2011-10-19 14:16:29 +01:00
|
|
|
def remove_project
|
2011-11-02 21:37:00 +00:00
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
ProjectToRepository.where(:project_id => @project.id, :repository_id => @repository.id).destroy_all
|
|
|
|
redirect_to repository_path(@repository), :notice => t('flash.repository.project_removed')
|
2011-10-19 14:16:29 +01:00
|
|
|
end
|
|
|
|
|
2011-03-11 16:08:41 +00:00
|
|
|
protected
|
|
|
|
|
2011-10-17 15:20:35 +01:00
|
|
|
def get_paths
|
|
|
|
if params[:user_id]
|
|
|
|
@user = User.find params[:user_id]
|
|
|
|
@repositories_path = user_repositories_path @user
|
|
|
|
@new_repository_path = new_user_repository_path @user
|
|
|
|
elsif params[:group_id]
|
|
|
|
@group = Group.find params[:group_id]
|
|
|
|
@repositories_path = group_repositories_path @group
|
|
|
|
@new_repository_path = new_group_repository_path @group
|
|
|
|
elsif params[:platform_id]
|
|
|
|
@platform = Platform.find params[:platform_id]
|
|
|
|
@repositories_path = platform_repositories_path @platform
|
|
|
|
@new_repository_path = new_platform_repository_path @platform
|
|
|
|
else
|
|
|
|
@repositories_path = repositories_path
|
|
|
|
@new_repository_path = new_repository_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-19 14:16:29 +01:00
|
|
|
def find_platform
|
|
|
|
@platform = @repository.platform
|
|
|
|
end
|
|
|
|
|
2011-10-17 15:20:35 +01:00
|
|
|
def find_platforms
|
|
|
|
@platforms = Platform.all
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|
2011-03-31 01:58:27 +01:00
|
|
|
|
|
|
|
def find_repository
|
2011-10-17 15:20:35 +01:00
|
|
|
@repository = Repository.find(params[:id])
|
2011-03-31 01:58:27 +01:00
|
|
|
end
|
2011-12-05 09:29:17 +00:00
|
|
|
|
|
|
|
def build_repository_stub
|
|
|
|
@repository = Repository.build_stub(Platform.find(params[:platform_id]))
|
|
|
|
end
|
2011-03-11 16:08:41 +00:00
|
|
|
end
|