Set clone actions/routes/view

DRY code for showing AJAX on project#show page
This commit is contained in:
Jure Grabnar 2016-08-11 09:46:15 +02:00
parent 1ca34a7986
commit 7f21ae138a
6 changed files with 80 additions and 36 deletions

View file

@ -3,15 +3,16 @@
(function(){
// Initialize new experiment form
function initializeNewExperimentModal(){
$("#new-experiment")
// Create ajax hook on given 'element', which should return modal with 'id' =>
// show that modal
function initializeModal(element, id){
$(element)
.on("ajax:beforeSend", function(){
animateSpinner();
})
.on("ajax:success", function(e, data){
$('body').append($.parseHTML(data.html));
$('#new-experiment-modal').modal('show',{
$(id).modal('show',{
backdrop: true,
keyboard: false,
});
@ -25,33 +26,30 @@
});
}
// Initialize edit experiment form
function initializeEditExperimentsModal(){
// Initialize dropdown actions on experiment:
// - edit
// - clone
function initializeDropdownActions(){
// { buttonClass: modalName } mappings
// click on buttonClass summons modalName dialog
modals = {
'.edit-experiment': '#edit-experiment-modal-',
'.clone-experiment': '#clone-experiment-modal-'
}
$.each($(".experiment-panel"), function(){
var id = '#edit-experiment-modal-' + $(this).data('id');
$(this)
.on("ajax:beforeSend", function(){
animateSpinner();
})
.on("ajax:success", function(e, data){
console.log("request success");
$('body').append($.parseHTML(data.html));
$(id).modal('show',{
backdrop: true,
keyboard: false,
});
})
.on("ajax:error", function() {
animateSpinner(null, false);
// TODO
})
.on("ajax:complete", function(){
animateSpinner(null, false);
var $expPanel = $(this);
$.each(modals, function(buttonClass, modalName) {
var id = modalName + $expPanel.data('id');
initializeModal($expPanel.find(buttonClass), id);
});
});
}
// init modals
initializeNewExperimentModal();
initializeEditExperimentsModal();
// Bind modal to new-experiment action
initializeModal($("#new-experiment"), '#new-experiment-modal');
// Bind modal to all actions listed on dropdown accesible from experiment
// panel
initializeDropdownActions();
})();

View file

@ -9,6 +9,8 @@ class ExperimentsController < ApplicationController
only: [:canvas, :module_archive]
before_action :check_module_archive_permissions,
only: [:module_archive]
before_action :check_experiment_clone_permissions,
only: [:clone_modal, :clone]
# except parameter could be used but it is not working.
layout :choose_layout
@ -86,6 +88,24 @@ class ExperimentsController < ApplicationController
end
end
# GET: clone_modal_experiment_path(id)
def clone_modal
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'clone_modal.html.erb'
)
}
end
end
end
# POST: clone_experiment(id)
def clone
redirect_to project_path(@experiment.project)
end
def module_archive
end
@ -134,6 +154,10 @@ class ExperimentsController < ApplicationController
render_403 unless can_view_experiment_archive(@experiment)
end
def check_experiment_clone_permissions
render_403 unless can_clone_experiment(@experiment)
end
def choose_layout
action_name.in?(%w(index archive)) ? 'main' : 'fluid'
end

View file

@ -355,6 +355,14 @@ module PermissionHelper
def can_view_experiment_samples(experiment)
can_view_samples(experiment.project.organization)
end
def can_clone_experiment(experiment)
is_user_or_higher_of_project(experiment.project)
end
def can_move_experiment(experiment)
is_user_or_higher_of_project(experiment.project)
end
# ---- WORKFLOW PERMISSIONS ----
def can_edit_canvas(experiment)

View file

@ -16,16 +16,24 @@
<ul class="dropdown-menu"
aria-labelledby="exActionsMenu-<%= experiment.id %>">
<% if can_create_experiment(@project) %>
<li><%= link_to t('experiments.edit.panel_label'),
edit_project_experiment_url(@project, experiment),
remote: true,
type: 'button' %></li>
<li><%= link_to t('experiments.edit.panel_label'),
edit_project_experiment_url(@project, experiment),
remote: true,
type: 'button',
class: 'edit-experiment' %></li>
<% end %>
<% if can_clone_experiment(experiment) %>
<li><%= link_to t('experiments.clone.label_title'),
clone_modal_experiment_url(experiment),
remote: true,
type: 'button',
class: 'clone-experiment' %></li>
<% end %>
<% if can_archive_experiment(experiment) %>
<li><%= link_to t('experiments.archive.label_title'),
archive_experiment_url(experiment),
type: 'button',
data: { confirm: t('experiments.canvas.archive_confirm') } %></li>
<li><%= link_to t('experiments.archive.label_title'),
archive_experiment_url(experiment),
type: 'button',
data: { confirm: t('experiments.canvas.archive_confirm') } %></li>
<% end %>
</ul>
</div>

View file

@ -615,6 +615,10 @@ en:
success_flash: "Successfully archived experiment %{experiment}"
error_flash: 'Could not archive the experiment.'
label_title: 'Archive'
clone:
modal_title: 'Clone experiment %{experiment}'
label_title: 'Clone'
modal_submit: 'Clone'
canvas:
archive_confirm: "Are you sure to archive this experiment?"
actions: 'Actions'

View file

@ -123,6 +123,8 @@ Rails.application.routes.draw do
post 'canvas', to: 'canvas#update' # Save updated canvas action
get 'module_archive' # Module archive for single experiment
get 'archive' # archive experiment
get 'clone_modal' # return modal with clone options
post 'clone' # clone experiment
get 'samples' # Samples for single project
# Renders sample datatable for single project (ajax action)
post 'samples_index'