Merge pull request #660 from Ducz0r/lm-sci-1270

Copy repository functionality [SCI-1270]
This commit is contained in:
Luka Murn 2017-06-08 11:45:57 +02:00 committed by GitHub
commit 2ab31aba2f
8 changed files with 150 additions and 7 deletions

View file

@ -3,5 +3,6 @@
$('.delete-repo-option').initializeModal('#delete-repo-modal');
$('.rename-repo-option').initializeModal('#rename-repo-modal');
$('.copy-repo-option').initializeModal('#copy-repo-modal');
$('.create-repository').initializeModal('#create-repo-modal');
})();

View file

@ -3,6 +3,8 @@ class RepositoriesController < ApplicationController
before_action :check_view_all_permissions, only: :index
before_action :check_edit_and_destroy_permissions, only:
%(destroy destroy_modal rename_modal update)
before_action :check_copy_permissions, only:
%(copy_modal copy)
before_action :check_create_permissions, only:
%(create_new_modal create)
@ -99,6 +101,58 @@ class RepositoriesController < ApplicationController
end
end
def copy_modal
@repository = Repository.find(params[:repository_id])
@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
@repository = Repository.find(params[:repository_id])
@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
# AJAX actions
def repository_table_index
@repository = Repository.find_by_id(params[:repository_id])
@ -137,6 +191,10 @@ class RepositoriesController < ApplicationController
render_403 unless can_edit_and_destroy_repository(@repository)
end
def check_copy_permissions
render_403 unless can_copy_repository(@repository)
end
def repository_params
params.require(:repository).permit(:name)
end

View file

@ -1070,6 +1070,10 @@ module PermissionHelper
is_admin_of_team(repository.team)
end
def can_copy_repository(repository)
can_create_repository(repository.team)
end
def can_create_columns_in_repository(repository)
is_normal_user_or_admin_of_team(repository.team)
end

View file

@ -14,4 +14,31 @@ class Repository < ActiveRecord::Base
length: { maximum: Constants::NAME_MAX_LENGTH }
validates :team, presence: true
validates :created_by, presence: true
def copy(created_by, name)
new_repo = nil
begin
Repository.transaction do
# Clone the repository object
new_repo = dup
new_repo.created_by = created_by
new_repo.name = name
new_repo.save!
# Clone columns (only if new_repo was saved)
repository_columns.find_each do |col|
new_col = col.dup
new_col.repository = new_repo
new_col.created_by = created_by
new_col.save!
end
end
rescue ActiveRecord::RecordInvalid
return false
end
# If everything is okay, return new_repo
new_repo
end
end

View file

@ -0,0 +1,32 @@
<div class="modal fade" id="copy-repo-modal" tabindex="-1" role="dialog">
<%= bootstrap_form_for @tmp_repository,
url: team_repository_copy_path(id: @repository, format: :json),
remote: true do |f| %>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title"><%= t("repositories.index.modal_copy.title_html", name: @repository.name ) %></h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="form-group">
<%= f.text_field :name,
label: t("repositories.index.modal_copy.name"),
autofocus: true,
placeholder: t("repositories.index.modal_copy.name_placeholder") %>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<%= f.submit t("repositories.index.modal_copy.copy"), class: "btn btn-primary" %>
<button type="button" class="btn btn-default" data-dismiss="modal"><%=t "general.cancel" %></button>
</div>
</div>
</div>
<% end %>
</div>

View file

@ -51,17 +51,27 @@
<span class="glyphicon glyphicon-cog"></span>
<span class="caret"></span>
</div>
<% if can_edit_and_destroy_repository repo %>
<ul class="dropdown-menu pull-right">
<li class="dropdown-header">
<%= t("repositories.index.options_dropdown.header") %>
</li>
<ul class="dropdown-menu pull-right">
<li class="dropdown-header">
<%= t("repositories.index.options_dropdown.header") %>
</li>
<% if can_edit_and_destroy_repository(repo) %>
<li>
<%= link_to t('repositories.index.options_dropdown.rename'),
team_repository_rename_modal_path(repository_id: repo),
class: "rename-repo-option",
remote: true %>
</li>
<% end %>
<% if can_copy_repository(repo) %>
<li>
<%= link_to t('repositories.index.options_dropdown.copy'),
team_repository_copy_modal_path(repository_id: repo),
class: "copy-repo-option",
remote: true %>
</li>
<% end %>
<% if can_edit_and_destroy_repository(repo) %>
<li role="separator" class="divider"></li>
<li>
<%= link_to t('repositories.index.modal_delete.delete'),
@ -69,8 +79,8 @@
class: "delete-repo-option",
remote: true %>
</li>
</ul>
<% end %>
<% end %>
</ul>
</div>
</div>

View file

@ -846,12 +846,14 @@ en:
add_new_repository_tab: "Create new repository"
delete_flash: "\"%{name}\" repository was successfully deleted!"
rename_flash: "\"%{old_name}\" repository was successfully renamed to \"%{new_name}\"!"
copy_flash: "\"%{new}\" repository was successfully copied from \"%{old}\"!"
no_teams:
title: "Your dashboard is empty!"
text: "It seems you're not a member of any team. See team management to sort it out."
options_dropdown:
header: "Edit repository"
rename: "Rename"
copy: "Copy"
delete: "Delete"
modal_delete:
title_html: "Delete repository <em>%{name}</em>"
@ -865,6 +867,11 @@ en:
name: "New repository name"
name_placeholder: "My repository"
rename: "Rename repository"
modal_copy:
title_html: "Copy repository <em>%{name}</em>"
name: "New repository name"
name_placeholder: "My repository"
copy: "Copy repository"
modal_create:
title: "Create new repository"
name_label: "Repository name"

View file

@ -132,6 +132,10 @@ Rails.application.routes.draw do
defaults: { format: 'json' }
get 'rename_modal', to: 'repositories#rename_modal',
defaults: { format: 'json' }
get 'copy_modal', to: 'repositories#copy_modal',
defaults: { format: 'json' }
post 'copy', to: 'repositories#copy',
defaults: { format: 'json' }
end
resources :samples, only: [:new, :create]
resources :sample_types, except: [:show, :new] do