2017-05-22 23:54:30 +08:00
|
|
|
class RepositoriesController < ApplicationController
|
2017-06-19 20:05:37 +08:00
|
|
|
before_action :load_vars,
|
2017-06-21 21:56:07 +08:00
|
|
|
except: %i(index create create_modal parse_sheet import_records)
|
2017-06-12 23:49:42 +08:00
|
|
|
before_action :load_parent_vars, except:
|
2017-06-21 21:56:07 +08:00
|
|
|
%i(repository_table_index export_repository parse_sheet import_records)
|
2017-06-19 20:05:37 +08:00
|
|
|
before_action :check_team, only: %i(parse_sheet import_records)
|
2017-05-22 23:54:30 +08:00
|
|
|
before_action :check_view_all_permissions, only: :index
|
2017-06-12 23:49:42 +08:00
|
|
|
before_action :check_view_permissions, only: :export_repository
|
2017-06-05 16:02:16 +08:00
|
|
|
before_action :check_edit_and_destroy_permissions, only:
|
2017-06-12 23:49:42 +08:00
|
|
|
%i(destroy destroy_modal rename_modal update)
|
2017-06-08 01:11:25 +08:00
|
|
|
before_action :check_copy_permissions, only:
|
2017-06-12 23:49:42 +08:00
|
|
|
%i(copy_modal copy)
|
2017-06-06 17:10:55 +08:00
|
|
|
before_action :check_create_permissions, only:
|
2017-06-12 23:49:42 +08:00
|
|
|
%i(create_new_modal create)
|
2017-05-22 23:54:30 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
render('repositories/index')
|
|
|
|
end
|
|
|
|
|
2017-06-08 17:44:24 +08:00
|
|
|
def show_tab
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2017-06-08 21:13:00 +08:00
|
|
|
partial: 'repositories/repository.html.erb',
|
|
|
|
locals: { repository: @repository }
|
2017-06-08 17:44:24 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 21:50:43 +08:00
|
|
|
def create_modal
|
|
|
|
@repository = Repository.new
|
2017-05-24 15:29:44 +08:00
|
|
|
respond_to do |format|
|
2017-06-06 19:33:57 +08:00
|
|
|
format.json do
|
2017-05-24 15:29:44 +08:00
|
|
|
render json: {
|
2017-06-06 19:33:57 +08:00
|
|
|
html: render_to_string(
|
2017-06-06 21:50:43 +08:00
|
|
|
partial: 'create_repository_modal.html.erb'
|
2017-06-06 19:33:57 +08:00
|
|
|
)
|
2017-05-24 15:29:44 +08:00
|
|
|
}
|
2017-06-06 19:33:57 +08:00
|
|
|
end
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@repository = Repository.new(
|
|
|
|
team: @team,
|
|
|
|
created_by: current_user
|
|
|
|
)
|
|
|
|
@repository.assign_attributes(repository_params)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2017-06-06 19:33:57 +08:00
|
|
|
format.json do
|
|
|
|
if @repository.save
|
|
|
|
flash[:success] = t('repositories.index.modal_create.success_flash',
|
|
|
|
name: @repository.name)
|
2017-06-06 17:10:55 +08:00
|
|
|
render json: { url: team_repositories_path(repository: @repository) },
|
2017-05-24 15:29:44 +08:00
|
|
|
status: :ok
|
2017-06-06 19:33:57 +08:00
|
|
|
else
|
2017-05-24 15:29:44 +08:00
|
|
|
render json: @repository.errors,
|
|
|
|
status: :unprocessable_entity
|
2017-06-06 19:33:57 +08:00
|
|
|
end
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 21:11:28 +08:00
|
|
|
def destroy_modal
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'delete_repository_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 21:09:09 +08:00
|
|
|
def destroy
|
2017-06-02 23:38:46 +08:00
|
|
|
flash[:success] = t('repositories.index.delete_flash',
|
|
|
|
name: @repository.name)
|
|
|
|
@repository.destroy
|
2017-06-05 23:56:09 +08:00
|
|
|
redirect_to team_repositories_path
|
2017-06-01 01:24:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def rename_modal
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'rename_repository_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-06-02 23:38:46 +08:00
|
|
|
old_name = @repository.name
|
|
|
|
@repository.update_attributes(repository_params)
|
2017-05-30 21:09:09 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
2017-06-01 01:24:38 +08:00
|
|
|
format.json do
|
2017-06-02 23:38:46 +08:00
|
|
|
if @repository.save
|
2017-06-01 01:24:38 +08:00
|
|
|
flash[:success] = t('repositories.index.rename_flash',
|
2017-06-02 23:38:46 +08:00
|
|
|
old_name: old_name, new_name: @repository.name)
|
2017-06-03 01:08:51 +08:00
|
|
|
render json: {
|
2017-06-05 23:56:09 +08:00
|
|
|
url: team_repositories_path(repository: @repository)
|
2017-06-03 01:08:51 +08:00
|
|
|
}, status: :ok
|
2017-06-01 01:24:38 +08:00
|
|
|
else
|
2017-06-02 23:38:46 +08:00
|
|
|
render json: @repository.errors, status: :unprocessable_entity
|
2017-06-01 01:24:38 +08:00
|
|
|
end
|
|
|
|
end
|
2017-05-30 21:09:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-08 01:11:25 +08:00
|
|
|
def copy_modal
|
|
|
|
@tmp_repository = Repository.new(
|
|
|
|
team: @team,
|
|
|
|
created_by: current_user,
|
|
|
|
name: @repository.name
|
|
|
|
)
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'copy_repository_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy
|
|
|
|
@tmp_repository = Repository.new(
|
|
|
|
team: @team,
|
|
|
|
created_by: current_user
|
|
|
|
)
|
|
|
|
@tmp_repository.assign_attributes(repository_params)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
if !@tmp_repository.valid?
|
|
|
|
render json: @tmp_repository.errors, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
copied_repository =
|
|
|
|
@repository.copy(current_user, @tmp_repository.name)
|
|
|
|
|
|
|
|
if !copied_repository
|
|
|
|
render json: { 'name': ['Server error'] },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
flash[:success] = t(
|
|
|
|
'repositories.index.copy_flash',
|
|
|
|
old: @repository.name,
|
|
|
|
new: copied_repository.name
|
|
|
|
)
|
|
|
|
render json: {
|
|
|
|
url: team_repositories_path(repository: copied_repository)
|
|
|
|
}, status: :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
# AJAX actions
|
|
|
|
def repository_table_index
|
|
|
|
if @repository.nil? || !can_view_repository(@repository)
|
|
|
|
render_403
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
render json: ::RepositoryDatatable.new(view_context,
|
|
|
|
@repository,
|
|
|
|
nil,
|
|
|
|
current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-13 14:10:10 +08:00
|
|
|
def parse_sheet
|
2017-06-14 15:11:20 +08:00
|
|
|
repository = current_team.repositories.find_by_id(params[:id])
|
2017-06-19 20:05:37 +08:00
|
|
|
parsed_file = ImportRepository::ParseRepository.new(
|
|
|
|
file: params[:file],
|
|
|
|
repository: repository,
|
|
|
|
session: session
|
|
|
|
)
|
2017-06-14 15:11:20 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
unless params[:file]
|
2017-06-21 21:01:55 +08:00
|
|
|
repository_response(t('teams.parse_sheet.errors.no_file_selected'))
|
2017-06-19 20:05:37 +08:00
|
|
|
return
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
|
|
|
begin
|
2017-06-19 20:05:37 +08:00
|
|
|
if parsed_file.too_large?
|
2017-06-14 15:11:20 +08:00
|
|
|
repository_response(t('general.file.size_exceeded',
|
|
|
|
file_size: Constants::FILE_MAX_SIZE_MB))
|
2017-06-21 23:39:20 +08:00
|
|
|
elsif parsed_file.empty?
|
2017-06-14 15:11:20 +08:00
|
|
|
flash[:notice] = t('teams.parse_sheet.errors.empty_file')
|
2017-06-21 23:39:20 +08:00
|
|
|
redirect_to back and return
|
|
|
|
else
|
2017-06-19 20:05:37 +08:00
|
|
|
@import_data = parsed_file.data
|
|
|
|
if parsed_file.generated_temp_file?
|
2017-06-14 15:11:20 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repositories/parse_records_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
repository_response(t('teams.parse_sheet.errors.temp_file_failure'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ArgumentError, CSV::MalformedCSVError
|
|
|
|
repository_response(t('teams.parse_sheet.errors.invalid_file',
|
|
|
|
encoding: ''.encoding))
|
|
|
|
rescue TypeError
|
|
|
|
repository_response(t('teams.parse_sheet.errors.invalid_extension'))
|
|
|
|
end
|
|
|
|
end
|
2017-06-13 14:10:10 +08:00
|
|
|
end
|
|
|
|
|
2017-06-19 20:05:37 +08:00
|
|
|
def import_records
|
2017-06-21 20:45:50 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-06-26 22:50:40 +08:00
|
|
|
# Check if there exist mapping for repository record (it's mandatory)
|
|
|
|
if params[:mappings].value?('-1')
|
|
|
|
import_records = repostiory_import_actions
|
|
|
|
status = import_records.import!
|
|
|
|
|
|
|
|
if status[:status] == :ok
|
|
|
|
flash[:success] = t('repositories.import_records.success_flash',
|
|
|
|
number_of_rows: status[:nr_of_added])
|
|
|
|
render json: {}, status: :ok
|
|
|
|
else
|
|
|
|
flash[:alert] = t('repositories.import_records.error_flash',
|
|
|
|
message: status[:errors])
|
|
|
|
render json: {}, status: :unprocessable_entity
|
|
|
|
end
|
2017-06-21 20:45:50 +08:00
|
|
|
else
|
2017-06-26 22:50:40 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'shared/flash_errors.html.erb',
|
|
|
|
locals: { error_title: t('repositories.import_records'\
|
|
|
|
'.error_message.errors_list_title'),
|
|
|
|
error: t('repositories.import_records.error_message'\
|
|
|
|
'.no_repository_name') }
|
|
|
|
)
|
|
|
|
},
|
|
|
|
status: :unprocessable_entity
|
2017-06-21 20:45:50 +08:00
|
|
|
end
|
|
|
|
end
|
2017-06-21 15:31:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 22:12:34 +08:00
|
|
|
def export_repository
|
|
|
|
if params[:row_ids] && params[:header_ids]
|
|
|
|
generate_zip
|
|
|
|
else
|
|
|
|
flash[:alert] = t('zip_export.export_error')
|
|
|
|
end
|
|
|
|
redirect_to :back
|
|
|
|
end
|
|
|
|
|
2017-06-21 15:31:39 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def repostiory_import_actions
|
|
|
|
ImportRepository::ImportRecords.new(
|
2017-06-19 20:05:37 +08:00
|
|
|
temp_file: TempFile.find_by_id(params[:file_id]),
|
|
|
|
repository: current_team.repositories.find_by_id(params[:id]),
|
|
|
|
mappings: params[:mappings],
|
|
|
|
session: session,
|
|
|
|
user: current_user
|
|
|
|
)
|
2017-06-13 14:10:10 +08:00
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def load_vars
|
2017-06-12 23:49:42 +08:00
|
|
|
repository_id = params[:id] || params[:repository_id]
|
|
|
|
@repository = Repository.find_by_id(repository_id)
|
|
|
|
render_404 unless @repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_parent_vars
|
2017-05-22 23:54:30 +08:00
|
|
|
@team = Team.find_by_id(params[:team_id])
|
|
|
|
render_404 unless @team
|
2017-05-25 16:06:48 +08:00
|
|
|
@repositories = @team.repositories.order(created_at: :asc)
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
|
|
|
|
2017-06-19 20:05:37 +08:00
|
|
|
def check_team
|
|
|
|
render_404 unless params[:team_id].to_i == current_team.id
|
|
|
|
end
|
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def check_view_all_permissions
|
|
|
|
render_403 unless can_view_team_repositories(@team)
|
|
|
|
end
|
2017-06-01 01:24:38 +08:00
|
|
|
|
2017-06-12 23:49:42 +08:00
|
|
|
def check_view_permissions
|
|
|
|
render_403 unless can_view_repository(@repository)
|
|
|
|
end
|
|
|
|
|
2017-05-24 15:29:44 +08:00
|
|
|
def check_create_permissions
|
2017-06-06 21:50:43 +08:00
|
|
|
render_403 unless can_create_repository(@team)
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2017-06-05 16:02:16 +08:00
|
|
|
def check_edit_and_destroy_permissions
|
2017-06-05 23:25:05 +08:00
|
|
|
render_403 unless can_edit_and_destroy_repository(@repository)
|
2017-06-01 02:56:05 +08:00
|
|
|
end
|
|
|
|
|
2017-06-08 01:11:25 +08:00
|
|
|
def check_copy_permissions
|
|
|
|
render_403 unless can_copy_repository(@repository)
|
|
|
|
end
|
|
|
|
|
2017-06-01 01:24:38 +08:00
|
|
|
def repository_params
|
|
|
|
params.require(:repository).permit(:name)
|
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
|
|
|
|
def repository_response(message)
|
|
|
|
format.html do
|
|
|
|
flash[:alert] = message
|
|
|
|
redirect_to :back
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: { message: message },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2017-06-21 21:56:07 +08:00
|
|
|
|
2017-06-06 22:12:34 +08:00
|
|
|
def generate_zip
|
2017-06-10 00:40:15 +08:00
|
|
|
# Fetch rows in the same order as in the currently viewed datatable
|
|
|
|
ordered_row_ids = params[:row_ids]
|
2017-06-12 23:49:42 +08:00
|
|
|
id_row_map = RepositoryRow.where(id: ordered_row_ids,
|
|
|
|
repository: @repository)
|
|
|
|
.index_by(&:id)
|
2017-06-10 00:40:15 +08:00
|
|
|
ordered_rows = ordered_row_ids.collect { |id| id_row_map[id.to_i] }
|
|
|
|
|
2017-06-06 22:12:34 +08:00
|
|
|
zip = ZipExport.create(user: current_user)
|
|
|
|
zip.generate_exportable_zip(
|
|
|
|
current_user,
|
2017-06-10 00:40:15 +08:00
|
|
|
to_csv(ordered_rows, params[:header_ids]),
|
2017-06-06 22:12:34 +08:00
|
|
|
:repositories
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-06-10 00:40:15 +08:00
|
|
|
def to_csv(rows, column_ids)
|
2017-06-06 22:12:34 +08:00
|
|
|
require 'csv'
|
2017-06-21 16:49:00 +08:00
|
|
|
|
2017-06-10 00:40:15 +08:00
|
|
|
# Parse column names
|
|
|
|
csv_header = []
|
|
|
|
column_ids.each do |c_id|
|
|
|
|
csv_header << case c_id.to_i
|
2017-06-21 00:40:41 +08:00
|
|
|
when -1, -2
|
2017-06-10 00:40:15 +08:00
|
|
|
next
|
|
|
|
when -3
|
2017-06-21 00:40:41 +08:00
|
|
|
I18n.t('repositories.table.row_name')
|
2017-06-10 00:40:15 +08:00
|
|
|
when -4
|
2017-06-21 00:40:41 +08:00
|
|
|
I18n.t('repositories.table.added_by')
|
|
|
|
when -5
|
2017-06-10 00:40:15 +08:00
|
|
|
I18n.t('repositories.table.added_on')
|
|
|
|
else
|
|
|
|
column = RepositoryColumn.find_by_id(c_id)
|
|
|
|
column ? column.name : nil
|
|
|
|
end
|
2017-06-06 22:12:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
CSV.generate do |csv|
|
2017-06-10 00:40:15 +08:00
|
|
|
csv << csv_header
|
2017-06-06 22:12:34 +08:00
|
|
|
rows.each do |row|
|
2017-06-09 23:11:41 +08:00
|
|
|
csv_row = []
|
2017-06-10 00:40:15 +08:00
|
|
|
column_ids.each do |c_id|
|
|
|
|
csv_row << case c_id.to_i
|
2017-06-21 00:40:41 +08:00
|
|
|
when -1, -2
|
2017-06-10 00:40:15 +08:00
|
|
|
next
|
|
|
|
when -3
|
2017-06-21 00:40:41 +08:00
|
|
|
row.name
|
2017-06-10 00:40:15 +08:00
|
|
|
when -4
|
2017-06-21 00:40:41 +08:00
|
|
|
row.created_by.full_name
|
|
|
|
when -5
|
2017-06-10 00:40:15 +08:00
|
|
|
I18n.l(row.created_at, format: :full)
|
|
|
|
else
|
|
|
|
cell = row.repository_cells
|
2017-06-12 23:49:42 +08:00
|
|
|
.find_by(repository_column_id: c_id)
|
2017-06-10 00:40:15 +08:00
|
|
|
cell ? cell.value.data : nil
|
|
|
|
end
|
2017-06-06 22:12:34 +08:00
|
|
|
end
|
2017-06-09 23:11:41 +08:00
|
|
|
csv << csv_row
|
2017-06-06 22:12:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|