set ajax for workflow img

This commit is contained in:
zmagod 2016-08-19 15:36:12 +02:00
parent 58930f3456
commit cac5033eba
10 changed files with 103 additions and 6 deletions

View file

@ -79,4 +79,4 @@ function setupAssetsLoading() {
window.clearInterval(intervalId);
}
}, DELAY);
}
}

View file

@ -0,0 +1,47 @@
(function(){
function initProjectExperiment(){
var url = $("[data-lupdated-url]").data("lupdated-url");
var updated = "[data-id=" + $("[data-lupdated-id]").data('lupdated-id') +"]";
var el = $(updated).find("img");
el.hide();
var timestamp = el.data("timestamp");
var img_url = $(updated).find(".workflowimg-container").data('updated-img');
animateSpinner($(updated).find(".workflowimg-container"), true);
$.ajax({
url: url,
type: "GET",
data: { "timestamp": timestamp },
dataType: "json",
success: function (data) {
getNewWorkforwImg(el, img_url, updated);
animateSpinner($(updated).find(".workflowimg-container"), false);
},
error: function (ev) {
if (ev.status == 404) {
setTimeout(initProjectExperiment(), 200);
}
animateSpinner($(updated).find(".workflowimg-container"), false);
}
});
}
function getNewWorkforwImg(el, url, updated){
$.ajax({
url: url,
type: "GET",
dataType: "json",
success: function (data) {
el.html(data.workflowimg);
animateSpinner($(updated).find(".workflowimg-container"), false);
},
error: function (ev) {
animateSpinner($(updated).find(".workflowimg-container"), false);
}
});
}
// init
initProjectExperiment();
})();

View file

@ -217,6 +217,37 @@ class ExperimentsController < ApplicationController
end
end
def updated_img
respond_to do |format|
format.json do
if @experiment.workflowimg_updated_at.to_i !=
params[:timestamp].to_time.to_i
render json: {}, status: 200
else
render json: {}, status: 404
end
end
end
end
def get_workflow_img
respond_to do |format|
format.json do
render json: {
workflowimg: (link_to image_tag(experiment
.workflowimg
.expiring_url(30),
class: 'img-responsive center-block',
data: {
timestamp: experiment
.workflowimg_updated_at
}),
canvas_experiment_path(experiment)).to_s
}
end
end
end
private
def set_experiment

View file

@ -11,4 +11,11 @@ module ProjectsHelper
end
conns.to_s[1..-2]
end
def last_experiment_updated(project)
project
.active_experiments(updated_at: :desc)
.pluck(:id)
.first.to_s
end
end

View file

@ -179,6 +179,9 @@ class Experiment < ActiveRecord::Base
# Finally, update module groups
update_module_groups(updated_module_groups, current_user)
# update Experiment timestamp
touch
end
rescue ActiveRecord::ActiveRecordError, ArgumentError, ActiveRecord::RecordNotSaved
return false

View file

@ -105,8 +105,8 @@ class Project < ActiveRecord::Base
return (self.user_projects.select { |up| up.user == user }).first.role
end
def active_experiments
experiments.is_archived(false).order('created_at DESC')
def active_experiments(ordered = 'created_at DESC')
experiments.is_archived(false).order(ordered)
end
def archived_experiments

View file

@ -23,7 +23,9 @@
</div>
</div>
</div>
<div class="row">
<div class="row"
data-lupdated-id="<%= last_experiment_updated(@project) %>"
data-lupdated-url="<%= updated_img_experiment_url(last_experiment_updated(@project))%>">
<% @project.active_experiments.each_with_index do |experiment, index| %>
<%= render partial: 'projects/show/experiment',
locals: { experiment: experiment } %>
@ -32,3 +34,4 @@
<% end %>
</div>
<%= javascript_include_tag("experiments/dropdown_actions") %>
<%= javascript_include_tag("projects/show") %>

View file

@ -22,9 +22,12 @@
</div>
<div class="panel-body">
<% if experiment.workflowimg? %>
<div class="workflowimg-container">
<div class="workflowimg-container"
data-updated-img="<%= get_workflow_img_experiment_url(experiment) %>"
>
<%= link_to image_tag(experiment.workflowimg.expiring_url(30),
class: 'img-responsive center-block'),
class: 'img-responsive center-block',
data: { timestamp: experiment.workflowimg_updated_at }),
canvas_experiment_path(experiment) %>
</div>
<% end %>

View file

@ -56,3 +56,4 @@ Rails.application.config.assets.precompile += %w( Sortable.min.js )
Rails.application.config.assets.precompile += %w( reports_pdf.css )
Rails.application.config.assets.precompile += %w( jszip.min.js )
Rails.application.config.assets.precompile += %w( assets.js )
Rails.application.config.assets.precompile += %w( projects/show.js )

View file

@ -128,6 +128,8 @@ Rails.application.routes.draw do
get 'move_modal' # return modal with move options
post 'move' # move experiment
get 'samples' # Samples for single project
get 'updated_img' # Checks if the workflow image is updated
get 'get_workflow_img' # Get udated workflow img
# Renders sample datatable for single project (ajax action)
post 'samples_index'
post :delete_samples,