Refactor Experiment workflow images [SCI-3539]

This commit is contained in:
Oleksii Kriuchykhin 2019-07-04 09:33:12 +02:00
parent 4ea6b56582
commit c0c8e0e1a3
8 changed files with 14 additions and 31 deletions

View file

@ -238,14 +238,13 @@ class ExperimentsController < ApplicationController
end
def updated_img
if @experiment.workflowimg.present? && !@experiment.workflowimg.exists?
@experiment.workflowimg = nil
@experiment.save
if @experiment.workflowimg.attached? && !@experiment.workflowimg_exists?
@experiment.workflowimg.purge
@experiment.generate_workflow_img
end
respond_to do |format|
format.json do
if @experiment.workflowimg.present?
if @experiment.workflowimg.attached?
render json: {}, status: 200
else
render json: {}, status: 404

View file

@ -212,14 +212,14 @@ class Asset < ApplicationRecord
def medium_preview
return file.variant(resize: Constants::MEDIUM_PIC_FORMAT) if previewable_image?
'medium/processing.gif'
'/images/medium/processing.gif'
# file.preview(resize: Constants::MEDIUM_PIC_FORMAT)
end
def large_preview
return file.variant(resize: Constants::LARGE_PIC_FORMAT) if previewable_image?
'large/processing.gif'
'/images/large/processing.gif'
# file.preview(resize: Constants::LARGE_PIC_FORMAT)
end

View file

@ -28,10 +28,7 @@ class Experiment < ApplicationRecord
# Associations for old activity type
has_many :activities, inverse_of: :experiment
has_attached_file :workflowimg
validates_attachment :workflowimg,
content_type: { content_type: ['image/png'] },
if: :workflowimg_check
has_one_attached :workflowimg
auto_strip_attributes :name, :description, nullify: false
validates :name,
@ -220,13 +217,14 @@ class Experiment < ApplicationRecord
end
def generate_workflow_img
if workflowimg.present?
self.workflowimg = nil
save
end
workflowimg.purge if workflowimg.attached?
Experiments::GenerateWorkflowImageService.delay.call(experiment_id: id)
end
def workflowimg_exists?
workflowimg.service.exist?(workflowimg.blob.key)
end
# Get projects where user is either owner or user in the same team
# as this experiment
def projects_with_role_above_user(current_user)
@ -613,12 +611,6 @@ class Experiment < ApplicationRecord
true
end
def workflowimg_check
workflowimg_content_type
rescue
false
end
def log_activity(type_of, current_user, my_module)
Activities::CreateActivityService
.call(activity_type: type_of,

View file

@ -76,9 +76,8 @@ module Experiments
def save_file
file = Tempfile.open(%w(wimg .png), Rails.root.join('tmp'))
@graph.output(png: file.path)
@exp.workflowimg = file
@exp.workflowimg.attach(io: file, filename: File.basename(file.path))
file.close
@exp.save
@exp.touch(:workflowimg_updated_at)
end
end

View file

@ -29,8 +29,6 @@ module Experiments
end
raise ActiveRecord::Rollback unless @exp.save
# To pass the ExperimentsController#updated_img check
@exp.update(workflowimg_updated_at: @exp.updated_at)
end
@errors.merge!(@exp.errors.to_hash) unless @exp.valid?

View file

@ -1,4 +1 @@
<%= image_tag(@experiment.workflowimg.expiring_url(
Constants::URL_SHORT_EXPIRE_TIME
),
class: 'img-responsive center-block') %>
<%= image_tag url_for(@experiment.workflowimg), class: 'img-responsive center-block' %>

View file

@ -22,5 +22,5 @@
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>

View file

@ -579,8 +579,6 @@ Rails.application.routes.draw do
# We cannot use 'resources :assets' because assets is a reserved route
# in Rails (assets pipeline) and causes funky behavior
get 'files/:id/present', to: 'assets#file_present', as: 'file_present_asset'
get 'files/:id/present_in_step', to: 'assets#step_file_present', as: 'step_file_present_asset'
get 'files/:id/preview',
to: 'assets#file_preview',
as: 'asset_file_preview'